From d7964651b7239842356b6ae7977c9849d996e963 Mon Sep 17 00:00:00 2001 From: jED Date: Wed, 20 Nov 2024 11:53:55 +0100 Subject: [PATCH] Fixed SQLQueryCompiler extensions. --- Lib/CSQLQueryExpress/CSQLQueryExpress.csproj | 2 +- .../Compiler/SQLQueryCompiler.cs | 11 ++++++----- .../Compiler/SQLQueryExtensions.cs | 7 ++++--- .../Compiler/SQLQueryParametersBuilder.cs | 18 +++++++++--------- .../Compiler/SQLQueryTableNameResolver.cs | 12 ++++++------ 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/Lib/CSQLQueryExpress/CSQLQueryExpress.csproj b/Lib/CSQLQueryExpress/CSQLQueryExpress.csproj index 2c46706..da6e695 100644 --- a/Lib/CSQLQueryExpress/CSQLQueryExpress.csproj +++ b/Lib/CSQLQueryExpress/CSQLQueryExpress.csproj @@ -3,7 +3,7 @@ netstandard2.0 CSQLQueryExpress - 1.3.7 + 1.3.8 A simple c# library to compile TSQL queries README.md diff --git a/Lib/CSQLQueryExpress/Compiler/SQLQueryCompiler.cs b/Lib/CSQLQueryExpress/Compiler/SQLQueryCompiler.cs index d4bf084..9a0890a 100644 --- a/Lib/CSQLQueryExpress/Compiler/SQLQueryCompiler.cs +++ b/Lib/CSQLQueryExpress/Compiler/SQLQueryCompiler.cs @@ -13,13 +13,14 @@ public static class SQLQueryCompiler /// /// Compile a instance in a . /// - /// SQL query expression built with , + /// SQL query expression built with + /// [OPTIONAL] SQL query compiler setting /// A compiled expression . - public static SQLQueryCompiled Compile(ISQLQuery query) + public static SQLQueryCompiled Compile(ISQLQuery query, SQLQueryCompilerSettings settings = null) { - var settings = new SQLQueryCompilerSettings(); - var parameterBuilder = new SQLQueryParametersBuilder(settings); - var tableNameResolver = new SQLQueryTableNameResolver(settings); + var compilerSettings = settings ?? new SQLQueryCompilerSettings(); + var parameterBuilder = new SQLQueryParametersBuilder(compilerSettings); + var tableNameResolver = new SQLQueryTableNameResolver(compilerSettings); return Compile(query, parameterBuilder, tableNameResolver); } diff --git a/Lib/CSQLQueryExpress/Compiler/SQLQueryExtensions.cs b/Lib/CSQLQueryExpress/Compiler/SQLQueryExtensions.cs index 4e1c183..f7f2f67 100644 --- a/Lib/CSQLQueryExpress/Compiler/SQLQueryExtensions.cs +++ b/Lib/CSQLQueryExpress/Compiler/SQLQueryExtensions.cs @@ -5,11 +5,12 @@ public static class SQLQueryExtensions /// /// Compile a instance in a . /// - /// SQL query expression built with , + /// SQL query expression built with + /// [OPTIONAL] SQL query compiler setting /// A compiled expression . - public static SQLQueryCompiled Compile(this ISQLQuery query) + public static SQLQueryCompiled Compile(this ISQLQuery query, SQLQueryCompilerSettings settings = null) { - return SQLQueryCompiler.Compile(query); + return SQLQueryCompiler.Compile(query, settings); } /// diff --git a/Lib/CSQLQueryExpress/Compiler/SQLQueryParametersBuilder.cs b/Lib/CSQLQueryExpress/Compiler/SQLQueryParametersBuilder.cs index 4b7d90a..eb91f6d 100644 --- a/Lib/CSQLQueryExpress/Compiler/SQLQueryParametersBuilder.cs +++ b/Lib/CSQLQueryExpress/Compiler/SQLQueryParametersBuilder.cs @@ -3,9 +3,9 @@ namespace CSQLQueryExpress { - internal class SQLQueryParametersBuilder : ISQLQueryParametersBuilder + public sealed class SQLQueryParametersBuilder : ISQLQueryParametersBuilder { - public IDictionary Parameters { get; } = new Dictionary(); + IDictionary ISQLQueryParametersBuilder.Parameters { get; } = new Dictionary(); private readonly SQLQueryCompilerSettings _settings; @@ -14,22 +14,22 @@ public SQLQueryParametersBuilder(SQLQueryCompilerSettings settings) _settings = settings; } - public void Initialize() + void ISQLQueryParametersBuilder.Initialize() { - Parameters.Clear(); + ((ISQLQueryParametersBuilder)this).Parameters.Clear(); } - public string AddParameter(object value) + string ISQLQueryParametersBuilder.AddParameter(object value) { - var parameterName = $"{_settings.QueryParameterPrefix}{Parameters.Count}"; - Parameters.Add(parameterName, new SQLQueryParameter(parameterName, value)); + var parameterName = $"{_settings.QueryParameterPrefix}{((ISQLQueryParametersBuilder)this).Parameters.Count}"; + ((ISQLQueryParametersBuilder)this).Parameters.Add(parameterName, new SQLQueryParameter(parameterName, value)); return parameterName; } - public string AddStoredProcedureParameter(string name, object value, SQLQueryParameterDirection direction) + string ISQLQueryParametersBuilder.AddStoredProcedureParameter(string name, object value, SQLQueryParameterDirection direction) { var parameterName = $"{_settings.StoredProcedureParameterPrefix}{name}"; - Parameters.Add(parameterName, new SQLQueryParameter(parameterName, value, direction)); + ((ISQLQueryParametersBuilder)this).Parameters.Add(parameterName, new SQLQueryParameter(parameterName, value, direction)); return parameterName; } } diff --git a/Lib/CSQLQueryExpress/Compiler/SQLQueryTableNameResolver.cs b/Lib/CSQLQueryExpress/Compiler/SQLQueryTableNameResolver.cs index 263ae45..c462326 100644 --- a/Lib/CSQLQueryExpress/Compiler/SQLQueryTableNameResolver.cs +++ b/Lib/CSQLQueryExpress/Compiler/SQLQueryTableNameResolver.cs @@ -6,7 +6,7 @@ namespace CSQLQueryExpress { - internal class SQLQueryTableNameResolver : ISQLQueryTableNameResolver + public sealed class SQLQueryTableNameResolver : ISQLQueryTableNameResolver { private readonly SQLQueryCompilerSettings _settings; @@ -19,13 +19,13 @@ public SQLQueryTableNameResolver(SQLQueryCompilerSettings settings) _settings = settings; } - public void Initialize() + void ISQLQueryTableNameResolver.Initialize() { Alias.Clear(); MembersTypes.Clear(); } - public SQLQueryTableName ResolveTableName(Type objType) + SQLQueryTableName ISQLQueryTableNameResolver.ResolveTableName(Type objType) { if (!Alias.TryGetValue(objType, out SQLQueryTableName alias)) { @@ -59,14 +59,14 @@ public SQLQueryTableName ResolveTableName(Type objType) return alias; } - public string ResolveTableNameAsAlias(Type objType) + string ISQLQueryTableNameResolver.ResolveTableNameAsAlias(Type objType) { - var tableAlias = ResolveTableName(objType); + var tableAlias = ((ISQLQueryTableNameResolver)this).ResolveTableName(objType); return $"{tableAlias.TableName} AS {tableAlias.TableAlias}"; } - public string ResolveColumnName(Type objType, MemberInfo member) + string ISQLQueryTableNameResolver.ResolveColumnName(Type objType, MemberInfo member) { if (!MembersTypes.TryGetValue(member.DeclaringType, out IDictionary members)) {