diff options
author | vman <public@vaughnnugent.com> | 2022-12-09 13:54:16 -0500 |
---|---|---|
committer | vman <public@vaughnnugent.com> | 2022-12-09 13:54:16 -0500 |
commit | 8b5f3eebb9f8d9bd55e922a809ffa3bd52e33401 (patch) | |
tree | 024fcdd2445b2fe37fc96d2870879d0f6aa5626f /VNLib.Plugins.Extensions.Data/SQL | |
parent | c9d9e6d23ad7b6fdf25f30de9b4a84be23885e16 (diff) |
Sql essentials classes moved, secret loading updates
Diffstat (limited to 'VNLib.Plugins.Extensions.Data/SQL')
-rw-r--r-- | VNLib.Plugins.Extensions.Data/SQL/DbExtensions.cs | 41 | ||||
-rw-r--r-- | VNLib.Plugins.Extensions.Data/SQL/SqlColumnNameAttribute.cs (renamed from VNLib.Plugins.Extensions.Data/SQL/SqlColumnName.cs) | 21 | ||||
-rw-r--r-- | VNLib.Plugins.Extensions.Data/SQL/SqlTableNameAttribute.cs | 40 | ||||
-rw-r--r-- | VNLib.Plugins.Extensions.Data/SQL/SqlVariable.cs | 16 | ||||
-rw-r--r-- | VNLib.Plugins.Extensions.Data/SQL/TableManager.cs | 5 |
5 files changed, 74 insertions, 49 deletions
diff --git a/VNLib.Plugins.Extensions.Data/SQL/DbExtensions.cs b/VNLib.Plugins.Extensions.Data/SQL/DbExtensions.cs index e6ee6b1..1f9164e 100644 --- a/VNLib.Plugins.Extensions.Data/SQL/DbExtensions.cs +++ b/VNLib.Plugins.Extensions.Data/SQL/DbExtensions.cs @@ -44,13 +44,8 @@ namespace VNLib.Plugins.Extensions.Data.SQL /* * Object rental for propery dictionaries used for custom result objects */ - private static readonly ObjectRental<Dictionary<string, PropertyInfo>> DictStore; - - static DbExtensions() - { - //Setup dict store - DictStore = ObjectRental.Create<Dictionary<string, PropertyInfo>>(null, static dict => dict.Clear(), 20); - } + 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 @@ -225,7 +220,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL foreach (PropertyInfo prop in objectType.GetProperties()) { //try to get the column name attribute of the propery - SqlColumnName colAtt = prop.GetCustomAttribute<SqlColumnName>(true); + SqlColumnNameAttribute? colAtt = prop.GetCustomAttribute<SqlColumnNameAttribute>(true); //Attribute is valid and coumn name is not empty if (!string.IsNullOrWhiteSpace(colAtt?.ColumnName)) { @@ -245,7 +240,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL foreach (DbColumn col in columns) { //Get the propery if its specified by its column-name attribute - if (avialbleProps.TryGetValue(col.ColumnName, out PropertyInfo prop)) + if (avialbleProps.TryGetValue(col.ColumnName, out PropertyInfo? prop)) { //make sure the column has a value if (col.ColumnOrdinal.HasValue) @@ -288,7 +283,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL foreach (PropertyInfo prop in objectType.GetProperties()) { //try to get the column name attribute of the propery - SqlColumnName colAtt = prop.GetCustomAttribute<SqlColumnName>(true); + SqlColumnNameAttribute? colAtt = prop.GetCustomAttribute<SqlColumnNameAttribute>(true); //Attribute is valid and coumn name is not empty if (!string.IsNullOrWhiteSpace(colAtt?.ColumnName)) { @@ -308,7 +303,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL foreach (DbColumn col in columns) { //Get the propery if its specified by its column-name attribute - if (avialbleProps.TryGetValue(col.ColumnName, out PropertyInfo prop)) + if (avialbleProps.TryGetValue(col.ColumnName, out PropertyInfo? prop)) { //make sure the column has a value if (col.ColumnOrdinal.HasValue) @@ -335,7 +330,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL /// <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() + public static T? GetFirstObject<T>(this DbDataReader reader) where T : new() { //make sure its worth collecting object meta if (!reader.HasRows) @@ -355,7 +350,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL foreach (PropertyInfo prop in objectType.GetProperties()) { //try to get the column name attribute of the propery - SqlColumnName colAtt = prop.GetCustomAttribute<SqlColumnName>(true); + SqlColumnNameAttribute? colAtt = prop.GetCustomAttribute<SqlColumnNameAttribute>(true); //Attribute is valid and coumn name is not empty if (colAtt != null && !string.IsNullOrWhiteSpace(colAtt.ColumnName)) { @@ -369,7 +364,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL 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) + if (availbleProps.TryGetValue(col.ColumnName, out PropertyInfo? prop) && col.ColumnOrdinal.HasValue) { //Get the object object val = reader.GetValue(col.ColumnOrdinal.Value); @@ -390,7 +385,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL /// <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() + public static async Task<T?> GetFirstObjectAsync<T>(this DbDataReader reader) where T : new() { //Read if (await reader.ReadAsync()) @@ -405,7 +400,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL foreach (PropertyInfo prop in objectType.GetProperties()) { //try to get the column name attribute of the propery - SqlColumnName colAtt = prop.GetCustomAttribute<SqlColumnName>(true); + SqlColumnNameAttribute? colAtt = prop.GetCustomAttribute<SqlColumnNameAttribute>(true); //Attribute is valid and coumn name is not empty if (colAtt != null && !string.IsNullOrWhiteSpace(colAtt.ColumnName)) { @@ -419,7 +414,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL 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) + if (availbleProps.TryGetValue(col.ColumnName, out PropertyInfo? prop) && col.ColumnOrdinal.HasValue) { //Get the object object val = reader.GetValue(col.ColumnOrdinal.Value); @@ -436,11 +431,11 @@ namespace VNLib.Plugins.Extensions.Data.SQL } /// <summary> /// Executes a nonquery operation with the specified command using the object properties set with the - /// <see cref="SqlVariable"/> attributes + /// <see cref="SqlVariableAttribute"/> 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> + /// <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> @@ -460,7 +455,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL foreach (PropertyInfo prop in objtype.GetProperties()) { //try to get the variable attribute of the propery - SqlVariable varprops = prop.GetCustomAttribute<SqlVariable>(true); + SqlVariableAttribute varprops = prop.GetCustomAttribute<SqlVariableAttribute>(true); //This property is an sql variable, so lets add it if (varprops == null) { @@ -470,7 +465,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL 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.Nullable).Direction = varprops.Direction; + cmd.AddParameter(varprops.VariableName, prop.GetValue(obj), varprops.DataType, varprops.Size, varprops.IsNullable).Direction = varprops.Direction; } } //Prepare the sql statement @@ -504,7 +499,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL foreach (PropertyInfo prop in objtype.GetProperties()) { //try to get the variable attribute of the propery - SqlVariable varprops = prop.GetCustomAttribute<SqlVariable>(true); + SqlVariableAttribute? varprops = prop.GetCustomAttribute<SqlVariableAttribute>(true); //This property is an sql variable, so lets add it if (varprops == null) { @@ -514,7 +509,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL 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.Nullable).Direction = varprops.Direction; + cmd.AddParameter(varprops.VariableName, prop.GetValue(obj), varprops.DataType, varprops.Size, varprops.IsNullable).Direction = varprops.Direction; } } //Prepare the sql statement diff --git a/VNLib.Plugins.Extensions.Data/SQL/SqlColumnName.cs b/VNLib.Plugins.Extensions.Data/SQL/SqlColumnNameAttribute.cs index 0039fb5..c18dab9 100644 --- a/VNLib.Plugins.Extensions.Data/SQL/SqlColumnName.cs +++ b/VNLib.Plugins.Extensions.Data/SQL/SqlColumnNameAttribute.cs @@ -3,9 +3,9 @@ * * Library: VNLib * Package: VNLib.Plugins.Extensions.Data -* File: SqlColumnName.cs +* File: SqlColumnNameAttribute.cs * -* SqlColumnName.cs is part of VNLib.Plugins.Extensions.Data which is part of the larger +* SqlColumnNameAttribute.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 @@ -30,12 +30,12 @@ namespace VNLib.Plugins.Extensions.Data.SQL /// Property attribute that specifies the property represents an SQL column in the database /// </summary> [AttributeUsage(AttributeTargets.Property)] - public class SqlColumnName : Attribute + public sealed class SqlColumnNameAttribute : Attribute { public bool Nullable { get; } public bool Unique { get; } public bool PrimaryKey { get; } - public string ColumnName { get; init; } + public string ColumnName { get; } /// <summary> /// Specifies the property is an SQL column name /// </summary> @@ -43,7 +43,7 @@ namespace VNLib.Plugins.Extensions.Data.SQL /// <param name="primaryKey"></param> /// <param name="nullable"></param> /// <param name="unique"></param> - public SqlColumnName(string columnName, bool primaryKey = false, bool nullable = true, bool unique = false) + public SqlColumnNameAttribute(string columnName, bool primaryKey = false, bool nullable = true, bool unique = false) { this.ColumnName = columnName; this.PrimaryKey = primaryKey; @@ -51,15 +51,4 @@ namespace VNLib.Plugins.Extensions.Data.SQL this.Unique = unique; } } - - /// <summary> - /// Allows a type to declare itself as a <see cref="System.Data.DataTable"/> with the specified name - /// </summary> - [AttributeUsage(AttributeTargets.Class, AllowMultiple =false, Inherited = true)] - public class SqlTableName : Attribute - { - public string TableName { get; } - - public SqlTableName(string tableName) => TableName = tableName; - } }
\ No newline at end of file diff --git a/VNLib.Plugins.Extensions.Data/SQL/SqlTableNameAttribute.cs b/VNLib.Plugins.Extensions.Data/SQL/SqlTableNameAttribute.cs new file mode 100644 index 0000000..9c870ea --- /dev/null +++ b/VNLib.Plugins.Extensions.Data/SQL/SqlTableNameAttribute.cs @@ -0,0 +1,40 @@ +/* +* Copyright (c) 2022 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Plugins.Extensions.Data +* File: SqlColumnName.cs +* +* SqlColumnName.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 General Public License as published +* by the Free Software Foundation, either version 2 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 +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Plugins.Extensions.Data. If not, see http://www.gnu.org/licenses/. +*/ + +using System; + +namespace VNLib.Plugins.Extensions.Data.SQL +{ + + /// <summary> + /// Allows a type to declare itself as a <see cref="System.Data.DataTable"/> with the specified name + /// </summary> + [AttributeUsage(AttributeTargets.Class, AllowMultiple =false, Inherited = true)] + public sealed class SqlTableNameAttribute : Attribute + { + public string TableName { get; } + + public SqlTableNameAttribute(string tableName) => TableName = tableName; + } +}
\ No newline at end of file diff --git a/VNLib.Plugins.Extensions.Data/SQL/SqlVariable.cs b/VNLib.Plugins.Extensions.Data/SQL/SqlVariable.cs index d33854a..b18d27b 100644 --- a/VNLib.Plugins.Extensions.Data/SQL/SqlVariable.cs +++ b/VNLib.Plugins.Extensions.Data/SQL/SqlVariable.cs @@ -31,13 +31,13 @@ namespace VNLib.Plugins.Extensions.Data.SQL /// Property attribute that specifies the property is to be used for a given command variable /// </summary> [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] - public class SqlVariable : Attribute + public sealed class SqlVariableAttribute : Attribute { - public string VariableName { get; init; } - public DbType DataType { get; init; } - public ParameterDirection Direction { get; init; } - public int Size { get; init; } - public bool Nullable { get; init; } + public string VariableName { get; } + public DbType DataType { get; } + public ParameterDirection Direction { get; } + public int Size { get; } + public bool IsNullable { get; } /// <summary> /// Specifies the property to be used as an SQL variable /// </summary> @@ -46,13 +46,13 @@ namespace VNLib.Plugins.Extensions.Data.SQL /// <param name="direction">Data direction during execution</param> /// <param name="size">Column size</param> /// <param name="isNullable">Is this property allowed to be null</param> - public SqlVariable(string variableName, DbType dataType, ParameterDirection direction, int size, bool isNullable) + public SqlVariableAttribute(string variableName, DbType dataType, ParameterDirection direction, int size, bool isNullable) { this.VariableName = variableName; this.DataType = dataType; this.Direction = direction; this.Size = size; - this.Nullable = isNullable; + this.IsNullable = isNullable; } } } diff --git a/VNLib.Plugins.Extensions.Data/SQL/TableManager.cs b/VNLib.Plugins.Extensions.Data/SQL/TableManager.cs index 14c4e64..a7f7873 100644 --- a/VNLib.Plugins.Extensions.Data/SQL/TableManager.cs +++ b/VNLib.Plugins.Extensions.Data/SQL/TableManager.cs @@ -43,12 +43,13 @@ namespace VNLib.Plugins.Extensions.Data.SQL /// </summary> protected string TableName { get; } - public TableManager(Func<DbConnection> factory, string tableName) + protected TableManager(Func<DbConnection> factory, string tableName) { this.Factory = factory ?? throw new ArgumentNullException(nameof(factory)); this.TableName = !string.IsNullOrWhiteSpace(tableName) ? tableName : throw new ArgumentNullException(nameof(tableName)); } - public TableManager(Func<DbConnection> factory) + + protected TableManager(Func<DbConnection> factory) { this.Factory = factory ?? throw new ArgumentNullException(nameof(factory)); this.TableName = ""; |