aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.Loading.Sql/src/DbBuilder.cs
blob: a82fdfa00f22a84803a230c885dd521dd2e801b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (c) 2024 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Extensions.Loading.Sql
* File: DbBuilder.cs 
*
* DbBuilder.cs is part of VNLib.Plugins.Extensions.Loading.Sql which 
* is part of the larger VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Extensions.Loading.Sql is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Extensions.Loading.Sql is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

using VNLib.Plugins.Extensions.Loading.Sql.DatabaseBuilder;

namespace VNLib.Plugins.Extensions.Loading.Sql
{
    internal sealed class DbBuilder : IDbContextBuilder
    {
        private readonly LinkedList<IDbTable> _tables = new();
        ///<inheritdoc/>
        public IDbTableBuilder<T> DefineTable<T>()
        {
            //Use the table attribute to specify the table name
            TableAttribute? tnA = typeof(T).GetCustomAttribute<TableAttribute>();

            return DefineTable<T>(tnA?.Name);
        }

        ///<inheritdoc/>
        public IDbTableBuilder<T> DefineTable<T>(string? tableName)
        {
            Type rtType = typeof(T);

            //Table name is the defined name, or the type name
            DataTable table = new(tableName ?? rtType.Name);

            //Create table with name
            TableBuilder<T> builder = new(table, rtType);

            //Store the new table builder
            _tables.AddLast(builder);

            return builder;
        }

        internal string[] BuildCreateCommand(IDBCommandGenerator cmdBuilder)
        {
            List<string> tableCommands = new();

            foreach (IDbTable table in _tables)
            {
                //Setup a new string builder for this table command
                StringBuilder sb = new();

                table.WriteCommand(sb, cmdBuilder);

                //build the command string and add to the list
                string cmd = sb.ToString();
                tableCommands.Add(cmd);
            }

            return tableCommands.ToArray();
        }

        private class TableBuilder<T>(DataTable Table, Type RuntimeType) : IDbTable, IDbTableBuilder<T>
        {
            ///<inheritdoc/>
            public IDbColumnBuilder<T> WithColumn<TCol>(Expression<Func<T, TCol>> selector)
            {
                KeyValuePair<string, Type> selectorData;

                //recover the expression information to determine the selected property
                if (selector.Body is MemberExpression me)
                {
                    selectorData = new(me.Member.Name, (me.Member as PropertyInfo)!.PropertyType);
                }
                else if(selector.Body is UnaryExpression ue)
                {
                    //We need to get the property name from the operand
                    string name = ((MemberExpression)ue.Operand).Member.Name;

                    //We want to get the operand type if the user wants to cast the type, we want to capture the casted type
                    selectorData = new(name, ue.Type);
                }
                else
                {
                    throw new ArgumentException("The selector expression type is not supported", nameof(selector));
                }

                //try to see if an altername column name is defined on the type
                string? colNameAttr = GetPropertyColumnName(selectorData.Key);

                /*
                 * Create the new column with the name of the column attribute, or fallback to the propearty name
                 * 
                 * NOTE: I am recovering the column type from the expression type, not the model type. This allows
                 * the user to alter the type without having to alter the entity to 'fool' database type conversion
                 */
                DataColumn col = new(colNameAttr ?? selectorData.Key, selectorData.Value);

                //Check for maxLen property
                int? maxLen = GetPropertyMaxLen(col.ColumnName);

                if (maxLen.HasValue)
                {
                    col.MaxLength(maxLen.Value);
                }

                //Store the column
                Table.Columns.Add(col);

                //See if key is found, then add the colum to the primary key table
                if (GetPropertyIsKey(selectorData.Key))
                {
                    col.AddToPrimaryKeys();
                }

                //Set the colum as timestamp
                if (GetPropertyIsRowVersion(selectorData.Key))
                {
                    col.SetTimeStamp();
                }

                //Init new column builder
                return new ColumnBuilder(col, this);
            }

            ///<inheritdoc/>
            public void WriteCommand(StringBuilder sb, IDBCommandGenerator commandBuilder) => commandBuilder.BuildCreateStatment(sb, Table);


            private int? GetPropertyMaxLen(string propertyName) 
                => GetAttributePropertyName<MaxLengthAttribute>(propertyName)?.Length;

            private string? GetPropertyColumnName(string propertyName) 
                => GetAttributePropertyName<ColumnAttribute>(propertyName)?.Name;

            private bool GetPropertyIsKey(string propertyName) 
                => GetAttributePropertyName<KeyAttribute>(propertyName) is not null;

            //Get the properties' timestamp attribute
            private bool GetPropertyIsRowVersion(string propertyName) 
                => GetAttributePropertyName<TimestampAttribute>(propertyName) is not null;

            private TA? GetAttributePropertyName<TA>(string propertyName) where TA : Attribute
            {
                PropertyInfo? property = RuntimeType.GetProperties()
                                                .Where(p => propertyName.Equals(p.Name, StringComparison.OrdinalIgnoreCase))
                                                .FirstOrDefault();

                return property?.GetCustomAttribute<TA>();
            }

            private class ColumnBuilder(DataColumn Column, IDbTableBuilder<T> Table) : IDbColumnBuilder<T>
            {
                public IDbTableBuilder<T> Next() => Table;

                public IDbColumnBuilder<T> ConfigureColumn(Action<DataColumn> columnSetter)
                {
                    columnSetter(Column);
                    return this;
                }

                public IDbColumnBuilder<T> AutoIncrement(int seed = 1, int step = 1)
                {
                    Column.AutoIncrement = true;
                    Column.AutoIncrementSeed = seed;
                    Column.AutoIncrementStep = step;
                    return this;
                }
            }
        }
    }
}