aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.Data/src/SQL/DbExtensions.cs
blob: 2682ed2f8f8e5c129ddf48dcfb63905518d427e2 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Extensions.Data
* File: DbExtensions.cs 
*
* DbExtensions.cs is part of VNLib.Plugins.Extensions.Data which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Extensions.Data 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.Data 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.Reflection;
using System.Data.Common;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using VNLib.Utils;
using VNLib.Utils.Memory.Caching;

namespace VNLib.Plugins.Extensions.Data.SQL
{
    /// <summary>
    /// Provides basic extension methods for ADO.NET abstract classes
    /// for rapid development
    /// </summary>
    public static class DbExtensions
    {
        /*
         * Object rental for propery dictionaries used for custom result objects
         */
        private static ObjectRental<Dictionary<string, PropertyInfo>> DictStore { get; } = ObjectRental.Create<Dictionary<string, PropertyInfo>>(null, static dict => dict.Clear(), 20);
      

        /// <summary>
        /// Creates a new <see cref="DbParameter"/> configured for <see cref="ParameterDirection.Input"/> with the specified value
        /// and adds it to the command.
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="name">The parameter name</param>
        /// <param name="value">The value of the parameter</param>
        /// <param name="type">The <see cref="DbType"/> of the column</param>
        /// <param name="nullable">Are null types allowed in the value parameter</param>
        /// <returns>The created parameter</returns>
        public static DbParameter AddParameter<T>(this DbCommand cmd, string @name, T @value, DbType @type, bool @nullable = false)
        {
            //Create the new parameter from command
            DbParameter param = cmd.CreateParameter();
            //Set parameter variables
            param.ParameterName = name;
            param.Value = value;
            param.DbType = type;
            //Force non null mapping
            param.SourceColumnNullMapping = nullable;
            //Specify input parameter
            param.Direction = ParameterDirection.Input;
            //Add param to list
            cmd.Parameters.Add(param);
            return param;
        }
        /// <summary>
        /// Creates a new <see cref="DbParameter"/> configured for <see cref="ParameterDirection.Input"/> with the specified value
        /// and adds it to the command.
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="name">The parameter name</param>
        /// <param name="value">The value of the parameter</param>
        /// <param name="type">The <see cref="DbType"/> of the column</param>
        /// <param name="size">Size of the data value</param>
        /// <param name="nullable">Are null types allowed in the value parameter</param>
        /// <returns>The created parameter</returns>
        public static DbParameter AddParameter<T>(this DbCommand cmd, string @name, T @value, DbType @type, int @size, bool @nullable = false)
        {
            DbParameter param = AddParameter(cmd, name, value, type, nullable);
            //Set size parameter
            param.Size = size;
            return param;
        }
        /// <summary>
        /// Creates a new <see cref="DbParameter"/> configured for <see cref="ParameterDirection.Output"/> with the specified value
        /// and adds it to the command.
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="name">The parameter name</param>
        /// <param name="value">The value of the parameter</param>
        /// <param name="type">The <see cref="DbType"/> of the column</param>
        /// <param name="nullable">Are null types allowed in the value parameter</param>
        /// <returns>The created parameter</returns>
        public static DbParameter AddOutParameter<T>(this DbCommand cmd, string @name, T @value, DbType @type, bool @nullable = false)
        {
            //Create the new parameter from command
            DbParameter param = AddParameter(cmd, name, value, type, nullable);
            //Specify output parameter
            param.Direction = ParameterDirection.Output;
            return param;
        }
        /// <summary>
        /// Creates a new <see cref="DbParameter"/> configured for <see cref="ParameterDirection.Output"/> with the specified value
        /// and adds it to the command.
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="name">The parameter name</param>
        /// <param name="value">The value of the parameter</param>
        /// <param name="type">The <see cref="DbType"/> of the column</param>
        /// <param name="size">Size of the data value</param>
        /// <param name="nullable">Are null types allowed in the value parameter</param>
        /// <returns>The created parameter</returns>
        public static DbParameter AddOutParameter<T>(this DbCommand cmd, string @name, T @value, DbType @type, int @size, bool @nullable = false)
        {
            DbParameter param = AddOutParameter(cmd, name, value, type, nullable);
            //Set size parameter
            param.Size = size;
            return param;
        }

        /// <summary>
        /// Creates a new <see cref="DbCommand"/> for <see cref="CommandType.Text"/> with the specified command
        /// </summary>
        /// <param name="db"></param>
        /// <param name="cmdText">The command to run against the connection</param>
        /// <returns>The initalized <see cref="DbCommand"/></returns>
        public static DbCommand CreateTextCommand(this DbConnection db, string cmdText)
        {
            //Create the new command
            DbCommand cmd = db.CreateCommand();
            cmd.CommandText = cmdText;
            cmd.CommandType = CommandType.Text;      //Specify text command
            return cmd;
        }
        /// <summary>
        /// Creates a new <see cref="DbCommand"/> for <see cref="CommandType.StoredProcedure"/> with the specified procedure name
        /// </summary>
        /// <param name="db"></param>
        /// <param name="procedureName">The name of the stored proecedure to execute</param>
        /// <returns>The initalized <see cref="DbCommand"/></returns>
        public static DbCommand CreateProcedureCommand(this DbConnection db, string procedureName)
        {
            //Create the new command
            DbCommand cmd = db.CreateCommand();
            cmd.CommandText = procedureName;
            cmd.CommandType = CommandType.StoredProcedure;      //Specify stored procedure
            return cmd;
        }

        /// <summary>
        /// Creates a new <see cref="DbCommand"/> for <see cref="CommandType.Text"/> with the specified command 
        /// on a given transaction
        /// </summary>
        /// <param name="db"></param>
        /// <param name="cmdText">The command to run against the connection</param>
        /// <param name="transaction">The transaction to execute on</param>
        /// <returns>The initalized <see cref="DbCommand"/></returns>
        public static DbCommand CreateTextCommand(this DbConnection db, string cmdText, DbTransaction transaction)
        {
            return CreateCommand(db, transaction, CommandType.Text, cmdText);
        }
        /// <summary>
        /// Shortcut to create a command on a transaction with the specifed command type and command
        /// </summary>
        /// <param name="db"></param>
        /// <param name="transaction">The transaction to complete the operation on</param>
        /// <param name="type">The command type</param>
        /// <param name="command">The command to execute</param>
        /// <returns>The intialized db command</returns>
        public static DbCommand CreateCommand(this DbConnection db, DbTransaction transaction, CommandType type, string command)
        {
            //Create the new command
            DbCommand cmd = db.CreateCommand();
            cmd.Transaction = transaction;
            cmd.CommandText = command;
            cmd.CommandType = type;
            return cmd;
        }
        /// <summary>
        /// Creates a new <see cref="DbCommand"/> for <see cref="CommandType.StoredProcedure"/> with the specified procedure name
        /// </summary>
        /// <param name="db"></param>
        /// <param name="procedureName">The name of the stored proecedure to execute</param>
        /// <param name="transaction">The transaction to execute on</param>
        /// <returns>The initalized <see cref="DbCommand"/></returns>
        public static DbCommand CreateProcedureCommand(this DbConnection db, string procedureName, DbTransaction transaction)
        {
            return CreateCommand(db, transaction, CommandType.StoredProcedure, procedureName);
        }

        /// <summary>
        /// Reads all available rows from the reader, adapts columns to public properties with <see cref="SqlColumnName"/>
        /// attributes, and adds them to the collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <param name="container">The container to write created objects to</param>
        /// <returns>The number of objects created and written to the collection</returns>
        public static int GetAllObjects<T>(this DbDataReader reader, ICollection<T> container) where T : new()
        {
            //make sure its worth collecting object meta
            if (!reader.HasRows)
            {
                return 0;
            }
            Type objectType = typeof(T);
            //Rent a dict of properties that have the column attribute set so we can load the proper results
            Dictionary<string, PropertyInfo> avialbleProps = DictStore.Rent();
            //Itterate through public properties
            foreach (PropertyInfo prop in objectType.GetProperties())
            {
                //try to get the column name attribute of the propery
                SqlColumnNameAttribute? colAtt = prop.GetCustomAttribute<SqlColumnNameAttribute>(true);
                //Attribute is valid and coumn name is not empty
                if (!string.IsNullOrWhiteSpace(colAtt?.ColumnName))
                {
                    //Store the property for later
                    avialbleProps[colAtt.ColumnName] = prop;
                }
            }
            //Get the column schema
            ReadOnlyCollection<DbColumn> columns = reader.GetColumnSchema();
            int count = 0;
            //Read
            while (reader.Read())
            {
                //Create the new object
                T ret = new();
                //Iterate through columns
                foreach (DbColumn col in columns)
                {
                    //Get the propery if its specified by its column-name attribute
                    if (avialbleProps.TryGetValue(col.ColumnName, out PropertyInfo? prop))
                    {
                        //make sure the column has a value
                        if (col.ColumnOrdinal.HasValue)
                        {
                            //Get the object
                            object val = reader.GetValue(col.ColumnOrdinal.Value);
                            //Set check if the row is DB null, if so set it, otherwise set the value
                            prop.SetValue(ret, Convert.IsDBNull(val) ? null : val);
                        }
                    }
                }
                //Add the object to the collection
                container.Add(ret);
                //Increment count
                count++;
            }
            //return dict (if an error occurs, just let the dict go and create a new one next time, no stress setting up a try/finally block)
            DictStore.Return(avialbleProps);
            return count;
        }
        /// <summary>
        /// Reads all available rows from the reader, adapts columns to public properties with <see cref="SqlColumnName"/>
        /// attributes, and adds them to the collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <param name="container">The container to write created objects to</param>
        /// <returns>The number of objects created and written to the collection</returns>
        public static async ValueTask<int> GetAllObjectsAsync<T>(this DbDataReader reader, ICollection<T> container) where T : new()
        {
            //make sure its worth collecting object meta
            if (!reader.HasRows)
            {
                return 0;
            }
            Type objectType = typeof(T);
            //Rent a dict of properties that have the column attribute set so we can load the proper results
            Dictionary<string, PropertyInfo> avialbleProps = DictStore.Rent();
            //Itterate through public properties
            foreach (PropertyInfo prop in objectType.GetProperties())
            {
                //try to get the column name attribute of the propery
                SqlColumnNameAttribute? colAtt = prop.GetCustomAttribute<SqlColumnNameAttribute>(true);
                //Attribute is valid and coumn name is not empty
                if (!string.IsNullOrWhiteSpace(colAtt?.ColumnName))
                {
                    //Store the property for later
                    avialbleProps[colAtt.ColumnName] = prop;
                }
            }
            //Get the column schema
            ReadOnlyCollection<DbColumn> columns = await reader.GetColumnSchemaAsync();
            int count = 0;
            //Read
            while (await reader.ReadAsync())
            {
                //Create the new object
                T ret = new();
                //Iterate through columns
                foreach (DbColumn col in columns)
                {
                    //Get the propery if its specified by its column-name attribute
                    if (avialbleProps.TryGetValue(col.ColumnName, out PropertyInfo? prop))
                    {
                        //make sure the column has a value
                        if (col.ColumnOrdinal.HasValue)
                        {
                            //Get the object
                            object val = reader.GetValue(col.ColumnOrdinal.Value);
                            //Set check if the row is DB null, if so set it, otherwise set the value
                            prop.SetValue(ret, Convert.IsDBNull(val) ? null : val);
                        }
                    }
                }
                //Add the object to the collection
                container.Add(ret);
                //Increment count
                count++;
            }
            //return dict (if an error occurs, just let the dict go and create a new one next time, no stress setting up a try/finally block)
            DictStore.Return(avialbleProps);
            return count;
        }
        /// <summary>
        /// Reads the first available row from the reader, adapts columns to public properties with <see cref="SqlColumnName"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <returns>The created object, or default if no rows are available</returns>
        public static T? GetFirstObject<T>(this DbDataReader reader) where T : new()
        {
            //make sure its worth collecting object meta
            if (!reader.HasRows)
            {
                return default;
            }
            //Get the object type
            Type objectType = typeof(T);
            //Get the column schema
            ReadOnlyCollection<DbColumn> columns = reader.GetColumnSchema();
            //Read
            if (reader.Read())
            {
                //Rent a dict of properties that have the column attribute set so we can load the proper results
                Dictionary<string, PropertyInfo> availbleProps = DictStore.Rent();
                //Itterate through public properties
                foreach (PropertyInfo prop in objectType.GetProperties())
                {
                    //try to get the column name attribute of the propery
                    SqlColumnNameAttribute? colAtt = prop.GetCustomAttribute<SqlColumnNameAttribute>(true);
                    //Attribute is valid and coumn name is not empty
                    if (colAtt != null && !string.IsNullOrWhiteSpace(colAtt.ColumnName))
                    {
                        //Store the property for later
                        availbleProps[colAtt.ColumnName] = prop;
                    }
                }
                //Create the new object
                T ret = new();
                //Iterate through columns
                foreach (DbColumn col in columns)
                {
                    //Get the propery if its specified by its column-name attribute
                    if (availbleProps.TryGetValue(col.ColumnName, out PropertyInfo? prop) && col.ColumnOrdinal.HasValue)
                    {
                        //Get the object
                        object val = reader.GetValue(col.ColumnOrdinal.Value);
                        //Set check if the row is DB null, if so set it, otherwise set the value
                        prop.SetValue(ret, Convert.IsDBNull(val) ? null : val);
                    }
                }
                //Return dict, no stress if error occurs, the goal is lower overhead
                DictStore.Return(availbleProps);
                //Return the new object
                return ret;
            }
            return default;
        }
        /// <summary>
        /// Reads the first available row from the reader, adapts columns to public properties with <see cref="SqlColumnName"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <returns>The created object, or default if no rows are available</returns>
        public static async Task<T?> GetFirstObjectAsync<T>(this DbDataReader reader) where T : new()
        {
            //Read
            if (await reader.ReadAsync())
            {
                //Get the object type
                Type objectType = typeof(T);
                //Get the column schema
                ReadOnlyCollection<DbColumn> columns = await reader.GetColumnSchemaAsync();
                //Rent a dict of properties that have the column attribute set so we can load the proper results
                Dictionary<string, PropertyInfo> availbleProps = DictStore.Rent();
                //Itterate through public properties
                foreach (PropertyInfo prop in objectType.GetProperties())
                {
                    //try to get the column name attribute of the propery
                    SqlColumnNameAttribute? colAtt = prop.GetCustomAttribute<SqlColumnNameAttribute>(true);
                    //Attribute is valid and coumn name is not empty
                    if (colAtt != null && !string.IsNullOrWhiteSpace(colAtt.ColumnName))
                    {
                        //Store the property for later
                        availbleProps[colAtt.ColumnName] = prop;
                    }
                }
                //Create the new object
                T ret = new();
                //Iterate through columns
                foreach (DbColumn col in columns)
                {
                    //Get the propery if its specified by its column-name attribute
                    if (availbleProps.TryGetValue(col.ColumnName, out PropertyInfo? prop) && col.ColumnOrdinal.HasValue)
                    {
                        //Get the object
                        object val = reader.GetValue(col.ColumnOrdinal.Value);
                        //Set check if the row is DB null, if so set it, otherwise set the value
                        prop.SetValue(ret, Convert.IsDBNull(val) ? null : val);
                    }
                }
                //Return dict, no stress if error occurs, the goal is lower overhead
                DictStore.Return(availbleProps);
                //Return the new object
                return ret;
            }
            return default;
        }
        /// <summary>
        /// Executes a nonquery operation with the specified command using the object properties set with the 
        /// <see cref="SqlVariableAttribute"/> attributes
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="obj">The object containing the <see cref="SqlVariableAttribute"/> properties to write to command variables</param>
        /// <returns>The number of rows affected</returns>
        /// <exception cref="TypeLoadException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="AmbiguousMatchException"></exception>
        /// <exception cref="TargetInvocationException"></exception>
        public static ERRNO ExecuteNonQuery<T>(this DbCommand cmd, T obj) where T : notnull
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            //Get the objec type 
            Type objtype = typeof(T);
            //Itterate through public properties
            foreach (PropertyInfo prop in objtype.GetProperties())
            {
                //try to get the variable attribute of the propery
                SqlVariableAttribute varprops = prop.GetCustomAttribute<SqlVariableAttribute>(true);
                //This property is an sql variable, so lets add it
                if (varprops == null)
                {
                    continue;
                }
                //If the command type is text, then make sure the variable is actually in the command, if not, ignore it
                if (cmd.CommandType != CommandType.Text || cmd.CommandText.Contains(varprops.VariableName))
                {
                    //Add the parameter to the command list
                    cmd.AddParameter(varprops.VariableName, prop.GetValue(obj), varprops.DataType, varprops.Size, varprops.IsNullable).Direction = varprops.Direction;
                }
            }
            //Prepare the sql statement
            cmd.Prepare();
            //Exect the query and return the results
            return cmd.ExecuteNonQuery();
        }
        /// <summary>
        /// Executes a nonquery operation with the specified command using the object properties set with the 
        /// <see cref="SqlVariable"/> attributes
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="obj">The object containing the <see cref="SqlVariable"/> properties to write to command variables</param>
        /// <returns>The number of rows affected</returns>
        /// <exception cref="TypeLoadException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="AmbiguousMatchException"></exception>
        /// <exception cref="TargetInvocationException"></exception>
        public static async Task<ERRNO> ExecuteNonQueryAsync<T>(this DbCommand cmd, T obj) where T : notnull
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            //Get the objec type 
            Type objtype = typeof(T);
            //Itterate through public properties
            foreach (PropertyInfo prop in objtype.GetProperties())
            {
                //try to get the variable attribute of the propery
                SqlVariableAttribute? varprops = prop.GetCustomAttribute<SqlVariableAttribute>(true);
                //This property is an sql variable, so lets add it
                if (varprops == null)
                {
                    continue;
                }
                //If the command type is text, then make sure the variable is actually in the command, if not, ignore it
                if (cmd.CommandType != CommandType.Text || cmd.CommandText.Contains(varprops.VariableName))
                {
                    //Add the parameter to the command list
                    cmd.AddParameter(varprops.VariableName, prop.GetValue(obj), varprops.DataType, varprops.Size, varprops.IsNullable).Direction = varprops.Direction;
                }
            }
            //Prepare the sql statement
            await cmd.PrepareAsync();
            //Exect the query and return the results
            return await cmd.ExecuteNonQueryAsync();
        }
    }
}