diff --git a/QueryBuilder/Compilers/Compiler.cs b/QueryBuilder/Compilers/Compiler.cs
index f670bdb8..07cbda7c 100644
--- a/QueryBuilder/Compilers/Compiler.cs
+++ b/QueryBuilder/Compilers/Compiler.cs
@@ -8,13 +8,21 @@ namespace SqlKata.Compilers
public partial class Compiler
{
private readonly ConditionsCompilerProvider _compileConditionMethodsProvider;
+ ///
protected virtual string parameterPlaceholder { get; set; } = "?";
+ ///
protected virtual string parameterPrefix { get; set; } = "@p";
+ /// Character used to identify the start of a keyword ( such as a column or table name )
protected virtual string OpeningIdentifier { get; set; } = "\"";
+ /// Character used to identify the end of a keyword ( such as a column or table name )
protected virtual string ClosingIdentifier { get; set; } = "\"";
+ /// Keyword used to indicate a column alias
protected virtual string ColumnAsKeyword { get; set; } = "AS ";
+ /// Keyword used to indicate a table alias
protected virtual string TableAsKeyword { get; set; } = "AS ";
+ ///
protected virtual string LastId { get; set; } = "";
+ /// Character used to escape special characters to allow interpreting them as their literal value
protected virtual string EscapeCharacter { get; set; } = "\\";
protected Compiler()
@@ -41,6 +49,10 @@ protected Compiler()
"similar to", "not similar to"
};
+ ///
+ /// A list of white-listed operators specific to this compiler
+ ///
+ ///
protected HashSet userOperators = new HashSet
{
@@ -170,11 +182,9 @@ public virtual SqlResult Compile(IEnumerable queries)
combinedBindings.AddRange(cb);
}
- var ctx = new SqlResult
- {
- RawSql = compiled.Select(r => r.RawSql).Aggregate((a, b) => a + ";\n" + b),
- Bindings = combinedBindings,
- };
+ var ctx = GetNewSqlResult();
+ ctx.RawSql = compiled.Select(r => r.RawSql).Aggregate((a, b) => a + ";\n" + b);
+ ctx.Bindings = combinedBindings;
ctx = PrepareResult(ctx);
@@ -183,10 +193,7 @@ public virtual SqlResult Compile(IEnumerable queries)
protected virtual SqlResult CompileSelectQuery(Query query)
{
- var ctx = new SqlResult
- {
- Query = query.Clone(),
- };
+ var ctx = GetNewSqlResult(query.Clone());
var results = new[] {
this.CompileColumns(ctx),
@@ -212,7 +219,7 @@ protected virtual SqlResult CompileSelectQuery(Query query)
protected virtual SqlResult CompileAdHocQuery(AdHocTableFromClause adHoc)
{
- var ctx = new SqlResult();
+ var ctx = GetNewSqlResult();
var row = "SELECT " + string.Join(", ", adHoc.Columns.Select(col => $"? AS {Wrap(col)}"));
@@ -233,10 +240,7 @@ protected virtual SqlResult CompileAdHocQuery(AdHocTableFromClause adHoc)
protected virtual SqlResult CompileDeleteQuery(Query query)
{
- var ctx = new SqlResult
- {
- Query = query
- };
+ var ctx = GetNewSqlResult(query);
if (!ctx.Query.HasComponent("from", EngineCode))
{
@@ -277,10 +281,7 @@ protected virtual SqlResult CompileDeleteQuery(Query query)
protected virtual SqlResult CompileUpdateQuery(Query query)
{
- var ctx = new SqlResult
- {
- Query = query
- };
+ var ctx = GetNewSqlResult(query);
if (!ctx.Query.HasComponent("from", EngineCode))
{
@@ -355,10 +356,7 @@ protected virtual SqlResult CompileUpdateQuery(Query query)
protected virtual SqlResult CompileInsertQuery(Query query)
{
- var ctx = new SqlResult
- {
- Query = query
- };
+ var ctx = GetNewSqlResult(query);
if (!ctx.Query.HasComponent("from", EngineCode))
{
@@ -501,7 +499,7 @@ public virtual string CompileColumn(SqlResult ctx, AbstractColumn column)
public virtual SqlResult CompileCte(AbstractFrom cte)
{
- var ctx = new SqlResult();
+ var ctx = GetNewSqlResult();
if (null == cte)
{
@@ -940,6 +938,27 @@ public virtual string Parameter(SqlResult ctx, object parameter)
return "?";
}
+ ///
+ /// Create a new object
+ ///
+ ///
+ public virtual SqlResult GetNewSqlResult()
+ {
+ return new SqlResult();
+ }
+
+ ///
+ /// Create a new object via , then assign the to it
+ ///
+ /// The query to assign to the object
+ ///
+ public SqlResult GetNewSqlResult(Query query)
+ {
+ SqlResult ctx = GetNewSqlResult();
+ ctx.Query = query;
+ return ctx;
+ }
+
///
/// Create query parameter place-holders for an array.
///
@@ -961,6 +980,16 @@ public virtual List WrapArray(List values)
return values.Select(x => Wrap(x)).ToList();
}
+ ///
+ /// Replaces opening/closing braces and brackets if the character is not preceeded by the
+ ///
{ [ --> Replaced by
+ ///
} ] --> Replaced by
+ ///
+ /// string to wrap with and
+ ///
+ /// {text} --> + "text" +
+ ///
[text] --> + "text" +
+ ///
public virtual string WrapIdentifiers(string input)
{
return input
diff --git a/QueryBuilder/Compilers/SqlServerCompiler.cs b/QueryBuilder/Compilers/SqlServerCompiler.cs
index f9aee65e..274e4abd 100644
--- a/QueryBuilder/Compilers/SqlServerCompiler.cs
+++ b/QueryBuilder/Compilers/SqlServerCompiler.cs
@@ -23,10 +23,7 @@ protected override SqlResult CompileSelectQuery(Query query)
query = query.Clone();
- var ctx = new SqlResult
- {
- Query = query,
- };
+ var ctx = GetNewSqlResult(query);
var limit = query.GetLimit(EngineCode);
var offset = query.GetOffset(EngineCode);
@@ -173,7 +170,7 @@ protected override string CompileBasicDateCondition(SqlResult ctx, BasicDateCond
protected override SqlResult CompileAdHocQuery(AdHocTableFromClause adHoc)
{
- var ctx = new SqlResult();
+ var ctx = GetNewSqlResult();
var colNames = string.Join(", ", adHoc.Columns.Select(Wrap));
diff --git a/QueryBuilder/Helper.cs b/QueryBuilder/Helper.cs
index 218a95e3..9417c2ca 100644
--- a/QueryBuilder/Helper.cs
+++ b/QueryBuilder/Helper.cs
@@ -161,6 +161,23 @@ public static IEnumerable Repeat(this string str, int count)
return Enumerable.Repeat(str, count);
}
+ ///
+ /// Replace instances of the within the string with the , unless the was escaped via the
+ ///
+ /// input string to modify
+ /// escape character to search for within the string
+ /// string to search for and replace with
+ /// string that will replace instances of that have not been escaped
+ ///
+ ///Example ( Not Escaped ) :
+ ///
Input = [ Test ] , = '\', = '[', = '{'
+ ///
Result: { Test ]
+ ///
+ ///Example ( Escaped ) :
+ ///
Input = \[ Test ] , = '\', = '[', = '{'
+ ///
Result: [ Test ]
+ ///
+ ///
public static string ReplaceIdentifierUnlessEscaped(this string input, string escapeCharacter, string identifier, string newIdentifier)
{
//Replace standard, non-escaped identifiers first
diff --git a/QueryBuilder/SqlResult.cs b/QueryBuilder/SqlResult.cs
index 58a7b722..679fc7d4 100644
--- a/QueryBuilder/SqlResult.cs
+++ b/QueryBuilder/SqlResult.cs
@@ -8,6 +8,8 @@ namespace SqlKata
{
public class SqlResult
{
+ public SqlResult() { }
+
public Query Query { get; set; }
public string RawSql { get; set; } = "";
public List