diff --git a/src/SimpleStack.Orm.Logging.MicrosoftExtensions/SimpleStack.Orm.Logging.MicrosoftExtensions.csproj b/src/SimpleStack.Orm.Logging.MicrosoftExtensions/SimpleStack.Orm.Logging.MicrosoftExtensions.csproj
index e42b897..e4d4164 100644
--- a/src/SimpleStack.Orm.Logging.MicrosoftExtensions/SimpleStack.Orm.Logging.MicrosoftExtensions.csproj
+++ b/src/SimpleStack.Orm.Logging.MicrosoftExtensions/SimpleStack.Orm.Logging.MicrosoftExtensions.csproj
@@ -1,13 +1,15 @@
- netstandard2.0
- 1.2.5
+ 1.3.0
+ net6.0;net8.0
+ latestmajor
+ 1.3.0
+ 1.3.0
-
-
+
diff --git a/src/SimpleStack.Orm.MySQL/MySqlDialectProvider.cs b/src/SimpleStack.Orm.MySQL/MySqlDialectProvider.cs
index 85f7ca3..454b99d 100644
--- a/src/SimpleStack.Orm.MySQL/MySqlDialectProvider.cs
+++ b/src/SimpleStack.Orm.MySQL/MySqlDialectProvider.cs
@@ -4,6 +4,7 @@
using System.Data.Common;
using System.Linq;
using System.Text;
+using System.Threading;
using Dapper;
using MySql.Data.MySqlClient;
using SimpleStack.Orm.Expressions.Statements;
@@ -49,23 +50,7 @@ public override string GetQuotedName(string name)
{
return string.Format("`{0}`", name);
}
-
- /// Query if 'dbCmd' does table exist.
- /// The database command.
- /// Name of the table.
- /// true if it succeeds, false if it fails.
- public override bool DoesTableExist(IDbConnection connection, string tableName, string schemaName = null)
- {
- //Same as SQL Server apparently?
- var sql = $@"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
- WHERE TABLE_NAME = '{tableName}'
- AND TABLE_SCHEMA = '{schemaName ?? connection.Database}'";
-
- var result = connection.ExecuteScalar(sql);
-
- return result > 0;
- }
-
+
public override string GetCreateSchemaStatement(string schema, bool ignoreIfExists)
{
return $"CREATE SCHEMA {(ignoreIfExists ? "IF NOT EXISTS" : string.Empty)} {schema}";
@@ -108,7 +93,7 @@ public override IEnumerable GetTableColumnDefinitions(IDbConn
FROM INFORMATION_SCHEMA.STATISTICS
WHERE INDEX_NAME NOT IN (SELECT INDEX_NAME FROM INFORMATION_SCHEMA.STATISTICS WHERE SEQ_IN_INDEX = 2 AND "+ whereClause + ") AND "+whereClause;
- var columnQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE " + whereClause;
+ var columnQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE " + whereClause + " ORDER BY ordinal_position";
var indexedColumns = connection.Query(indexQuery, new {TableName = tableName, SchemaName = schemaName})
.ToDictionary(x => (string)x.COLUMN_NAME, x => (bool) (x.NON_UNIQUE == 0));
@@ -192,7 +177,7 @@ protected virtual DbType GetDbType(string dataType, int? length, string columnTy
}
}
- public override CommandDefinition ToInsertStatement(InsertStatement insertStatement, CommandFlags flags)
+ public override CommandDefinition ToInsertStatement(InsertStatement insertStatement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken())
{
var query = new StringBuilder("INSERT INTO ");
query.Append(insertStatement.TableName);
@@ -206,13 +191,13 @@ public override CommandDefinition ToInsertStatement(InsertStatement insertStatem
if (insertStatement.Parameters.Any())
{
- query.Append(insertStatement.Parameters.Select(x => x.Key).Aggregate((x, y) => x + ", " + y));
+ query.Append(insertStatement.Parameters.Select(x => x.ParameterName).Aggregate((x, y) => x + ", " + y));
}
query.Append(");");
query.Append(insertStatement.HasIdentity ? SelectIdentitySql : "SELECT 0");
- return new CommandDefinition(query.ToString(), insertStatement.Parameters, flags: flags);
+ return new CommandDefinition(query.ToString(), insertStatement.Parameters.ToDynamicParameters(), flags: flags, cancellationToken: cancellationToken);
}
}
}
\ No newline at end of file
diff --git a/src/SimpleStack.Orm.MySQL/SimpleStack.Orm.MySQL.csproj b/src/SimpleStack.Orm.MySQL/SimpleStack.Orm.MySQL.csproj
index eb95aef..d0df1fe 100644
--- a/src/SimpleStack.Orm.MySQL/SimpleStack.Orm.MySQL.csproj
+++ b/src/SimpleStack.Orm.MySQL/SimpleStack.Orm.MySQL.csproj
@@ -4,7 +4,7 @@
MySQL Provider for SimpleStack.Orm
1.0.2
SimpleStack Team;NServiceKit;ServiceStack
- netstandard2.0
+ net6.0;net8.0
SimpleStack.Orm.MySQL
SimpleStack.Orm.MySQL
MySQL;SimpleStack;Orm
@@ -14,12 +14,13 @@
false
false
1.2.0-beta95
- 1.2.0.0
- 1.2.0.0
- 1.2.5
+ 1.3.0
+ 1.3.0
+ 1.3.0
https://simplestack.org/
https://github.com/SimpleStack/simplestack.orm
git
+ latestmajor
@@ -27,7 +28,7 @@
-
+
diff --git a/src/SimpleStack.Orm.MySQLConnector/MySqlConnectorDialectProvider.cs b/src/SimpleStack.Orm.MySQLConnector/MySqlConnectorDialectProvider.cs
index 60cf733..b25ee29 100644
--- a/src/SimpleStack.Orm.MySQLConnector/MySqlConnectorDialectProvider.cs
+++ b/src/SimpleStack.Orm.MySQLConnector/MySqlConnectorDialectProvider.cs
@@ -4,6 +4,7 @@
using System.Data.Common;
using System.Linq;
using System.Text;
+using System.Threading;
using Dapper;
using MySqlConnector;
using SimpleStack.Orm.Expressions.Statements;
@@ -52,22 +53,6 @@ public override string GetQuotedName(string name)
return string.Format("`{0}`", name);
}
- /// Query if 'dbCmd' does table exist.
- /// The database command.
- /// Name of the table.
- /// true if it succeeds, false if it fails.
- public override bool DoesTableExist(IDbConnection connection, string tableName, string schemaName = null)
- {
- //Same as SQL Server apparently?
- var sql = $@"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
- WHERE TABLE_NAME = '{tableName}'
- AND TABLE_SCHEMA = '{schemaName ?? connection.Database}'";
-
- var result = connection.ExecuteScalar(sql);
-
- return result > 0;
- }
-
public override string GetCreateSchemaStatement(string schema, bool ignoreIfExists)
{
return $"CREATE SCHEMA {(ignoreIfExists ? "IF NOT EXISTS" : string.Empty)} {schema}";
@@ -110,7 +95,7 @@ public override IEnumerable GetTableColumnDefinitions(IDbConn
FROM INFORMATION_SCHEMA.STATISTICS
WHERE INDEX_NAME NOT IN (SELECT INDEX_NAME FROM INFORMATION_SCHEMA.STATISTICS WHERE SEQ_IN_INDEX = 2 AND "+ whereClause + ") AND "+whereClause;
- var columnQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE " + whereClause;
+ var columnQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE " + whereClause + " ORDER BY ordinal_position";
var indexedColumns = connection.Query(indexQuery, new {TableName = tableName, SchemaName = schemaName})
.ToDictionary(x => (string)x.COLUMN_NAME, x => (bool) (x.NON_UNIQUE == 0));
@@ -195,7 +180,7 @@ protected virtual DbType GetDbType(string dataType, int? length, string columnTy
}
}
- public override CommandDefinition ToInsertStatement(InsertStatement insertStatement, CommandFlags flags)
+ public override CommandDefinition ToInsertStatement(InsertStatement insertStatement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken())
{
var query = new StringBuilder("INSERT INTO ");
query.Append(insertStatement.TableName);
@@ -209,13 +194,13 @@ public override CommandDefinition ToInsertStatement(InsertStatement insertStatem
if (insertStatement.Parameters.Any())
{
- query.Append(insertStatement.Parameters.Select(x => x.Key).Aggregate((x, y) => x + ", " + y));
+ query.Append(insertStatement.Parameters.Select(x => x.ParameterName).Aggregate((x, y) => x + ", " + y));
}
query.Append(");");
query.Append(insertStatement.HasIdentity ? SelectIdentitySql : "SELECT 0");
- return new CommandDefinition(query.ToString(), insertStatement.Parameters, flags: flags);
+ return new CommandDefinition(query.ToString(), insertStatement.Parameters.ToDynamicParameters(), flags: flags, cancellationToken: cancellationToken);
}
}
}
\ No newline at end of file
diff --git a/src/SimpleStack.Orm.MySQLConnector/SimpleStack.Orm.MySQLConnector.csproj b/src/SimpleStack.Orm.MySQLConnector/SimpleStack.Orm.MySQLConnector.csproj
index 41d01e6..04cf2e1 100644
--- a/src/SimpleStack.Orm.MySQLConnector/SimpleStack.Orm.MySQLConnector.csproj
+++ b/src/SimpleStack.Orm.MySQLConnector/SimpleStack.Orm.MySQLConnector.csproj
@@ -12,12 +12,13 @@
false
false
1.2.0-beta95
- 1.2.0.0
- 1.2.0.0
- 1.2.5
- netstandard2.0
+ 1.3.0
+ 1.3.0
+ 1.3.0
https://simplestack.org/
https://github.com/SimpleStack/simplestack.orm
+ net6.0;net8.0
+ latestmajor
@@ -25,7 +26,7 @@
-
+
diff --git a/src/SimpleStack.Orm.PostgreSQL/PostgreSQLDialectProvider.cs b/src/SimpleStack.Orm.PostgreSQL/PostgreSQLDialectProvider.cs
index e3e5255..319f6b4 100644
--- a/src/SimpleStack.Orm.PostgreSQL/PostgreSQLDialectProvider.cs
+++ b/src/SimpleStack.Orm.PostgreSQL/PostgreSQLDialectProvider.cs
@@ -5,6 +5,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Text;
+using System.Threading;
using Dapper;
using Npgsql;
@@ -95,27 +96,30 @@ public override string GetColumnDefinition(
return sql.ToString();
}
-
- /// Query if 'dbCmd' does table exist.
- /// The database command.
- /// Name of the table.
- /// true if it succeeds, false if it fails.
- public override bool DoesTableExist(IDbConnection connection, string tableName, string schemaName = null)
+
+ public override CommandDefinition ToTableExistStatement(string tableName, string schemaName,
+ CancellationToken cancellationToken = new CancellationToken())
{
- var query = @"SELECT COUNT(*) FROM pg_class
+ var sql = $@"SELECT COUNT(*) FROM pg_class
LEFT JOIN pg_namespace n ON n.oid = pg_class.relnamespace
- WHERE relname = :table AND nspname = ";
-
+ WHERE relname = {GetParameterName(0)}";
+
+ var parameters = new Dictionary
+ {
+ {GetParameterName(0), NamingStrategy.GetTableName(tableName)},
+ };
+
if (string.IsNullOrEmpty(schemaName))
- query += "current_schema()";
+ {
+ sql += " AND nspname = current_schema()";
+ }
else
{
- query += $"'{NamingStrategy.GetTableName(schemaName)}'";
+ sql += $" AND nspname = {GetParameterName(1)}";
+ parameters.Add(GetParameterName(1),NamingStrategy.GetTableName(schemaName));
}
- var result = connection.ExecuteScalar(query, new {table = NamingStrategy.GetTableName(tableName)});
-
- return result > 0;
+ return new CommandDefinition(sql, parameters, cancellationToken: cancellationToken);
}
// /// Gets quoted table name.
@@ -180,6 +184,8 @@ public override IEnumerable GetTableColumnDefinitions(IDbConn
sqlQuery += " AND table_schema = @SchemaName";
}
+ sqlQuery += " ORDER BY ordinal_position";
+
var table = string.IsNullOrEmpty(schemaName) ? tableName : $"{schemaName}.{tableName}";
var pks = connection.Query($@"SELECT a.attname
diff --git a/src/SimpleStack.Orm.PostgreSQL/SimpleStack.Orm.PostgreSQL.csproj b/src/SimpleStack.Orm.PostgreSQL/SimpleStack.Orm.PostgreSQL.csproj
index da14033..cf1ba10 100644
--- a/src/SimpleStack.Orm.PostgreSQL/SimpleStack.Orm.PostgreSQL.csproj
+++ b/src/SimpleStack.Orm.PostgreSQL/SimpleStack.Orm.PostgreSQL.csproj
@@ -4,7 +4,7 @@
PostgreSQL Provider for SimpleStack.Orm
1.0.2
SimpleStack Team;NServiceKit;ServiceStack
- netstandard2.0
+ net6.0;net8.0
SimpleStack.Orm.PostgreSQL
SimpleStack.Orm.PostgreSQL
npgsql;SimpleStack;PostgreSQL
@@ -13,11 +13,12 @@
false
false
1.2.0-beta95
- 1.2.1.0
- 1.2.1.0
- 1.2.5
+ 1.3.0
+ 1.3.0
+ 1.3.0
https://simplestack.org/
https://github.com/SimpleStack/simplestack.orm
+ latestmajor
@@ -25,7 +26,7 @@
-
+
diff --git a/src/SimpleStack.Orm.SDSQLite/SimpleStack.Orm.SDSQLite.csproj b/src/SimpleStack.Orm.SDSQLite/SimpleStack.Orm.SDSQLite.csproj
index e537be3..347c335 100644
--- a/src/SimpleStack.Orm.SDSQLite/SimpleStack.Orm.SDSQLite.csproj
+++ b/src/SimpleStack.Orm.SDSQLite/SimpleStack.Orm.SDSQLite.csproj
@@ -4,7 +4,7 @@
SQLite Provider for SimpleStack.Orm based on System.Data.Sqlite
1.0.2
SimpleStack Team;NServiceKit;ServiceStack
- netstandard2.0
+ netstandard2.1
SimpleStack.Orm.SDSQLite
SimpleStack.Orm.SDSQLite
System.Data.Sqlite;SimpleStack;Sqlite
@@ -13,16 +13,13 @@
false
false
1.2.0-beta95
- 1.2.0.0
- 1.2.0.0
- 1.2.5
+ 1.3.0
+ 1.3.0
+ 1.3.0
SimpleStack.Orm.SDSQlite
https://simplestack.org/
https://github.com/SimpleStack/simplestack.orm
-
-
-
- netstandard2.0
+ latestmajor
@@ -30,9 +27,9 @@
-
-
-
+
+
+
diff --git a/src/SimpleStack.Orm.SDSQLite/SqliteDialectProvider.cs b/src/SimpleStack.Orm.SDSQLite/SqliteDialectProvider.cs
index d9f2340..f429c89 100644
--- a/src/SimpleStack.Orm.SDSQLite/SqliteDialectProvider.cs
+++ b/src/SimpleStack.Orm.SDSQLite/SqliteDialectProvider.cs
@@ -4,6 +4,7 @@
using System.Data.Common;
using System.Data.SQLite;
using System.Text.RegularExpressions;
+using System.Threading;
using System.Threading.Tasks;
using Dapper;
@@ -42,21 +43,16 @@ public override string GetQuotedTableName(string tableName, string schemaName =
? $"{EscapeChar}{NamingStrategy.GetTableName(tableName)}{EscapeChar}"
: $"{EscapeChar}{NamingStrategy.GetTableName(schemaName)}_{NamingStrategy.GetTableName(tableName)}{EscapeChar}";
}
-
- /// Query if 'dbCmd' does table exist.
- /// The database command.
- /// Name of the table.
- /// true if it succeeds, false if it fails.
- public override bool DoesTableExist(IDbConnection connection, string tableName, string schemaName = null)
+
+ public override CommandDefinition ToTableExistStatement(string tableName, string schemaName,
+ CancellationToken cancellationToken = new CancellationToken())
{
var name = string.IsNullOrEmpty(schemaName) ? tableName : $"{schemaName}_{tableName}";
-
- var sql = string.Format("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name = '{0}'"
- , name);
-
- var result = connection.ExecuteScalar(sql);
-
- return result > 0;
+
+ var sql = $"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name = {GetParameterName(0)}";
+ var parameters = new Dictionary {{GetParameterName(0), name}};
+
+ return new CommandDefinition(sql, parameters, cancellationToken: cancellationToken);
}
/// Gets column definition.
diff --git a/src/SimpleStack.Orm.SQLServer/SimpleStack.Orm.SQLServer.csproj b/src/SimpleStack.Orm.SQLServer/SimpleStack.Orm.SQLServer.csproj
index ed918e3..9c44737 100644
--- a/src/SimpleStack.Orm.SQLServer/SimpleStack.Orm.SQLServer.csproj
+++ b/src/SimpleStack.Orm.SQLServer/SimpleStack.Orm.SQLServer.csproj
@@ -12,12 +12,13 @@
false
false
1.2.0-beta95
- 1.2.0.0
- 1.2.0.0
- 1.2.5
- netstandard2.0
+ 1.3.0
+ 1.3.0
+ 1.3.0
https://simplestack.org/
https://github.com/SimpleStack/simplestack.orm
+ net6.0;net8.0
+ latestmajor
@@ -25,7 +26,7 @@
-
+
diff --git a/src/SimpleStack.Orm.SQLServer/SqlServerOrmLiteDialectProvider.cs b/src/SimpleStack.Orm.SQLServer/SqlServerOrmLiteDialectProvider.cs
index c901a06..9fd20e3 100644
--- a/src/SimpleStack.Orm.SQLServer/SqlServerOrmLiteDialectProvider.cs
+++ b/src/SimpleStack.Orm.SQLServer/SqlServerOrmLiteDialectProvider.cs
@@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Text;
+using System.Threading;
using Dapper;
using Microsoft.Data.SqlClient;
using SimpleStack.Orm.Expressions.Statements;
@@ -51,29 +52,12 @@ public override DbConnection CreateIDbConnection(string connectionString)
return new SqlConnection(connectionString);
}
-
- /// Query if 'dbCmd' does table exist.
- /// The database command.
- /// Name of the table.
- /// true if it succeeds, false if it fails.
- public override bool DoesTableExist(IDbConnection connection, string tableName, string schemaName = null)
- {
- var sql = String.Format("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{0}'"
- ,tableName);
-
- if (!string.IsNullOrEmpty(schemaName))
- sql += $" AND TABLE_SCHEMA = '{schemaName}'";
- var result = connection.ExecuteScalar(sql);
-
- return result > 0;
- }
-
public override string GetCreateSchemaStatement(string schema, bool ignoreIfExists)
{
return ignoreIfExists ?
$@"IF NOT EXISTS ( SELECT * FROM sys.schemas WHERE name = N'{schema}' )
- EXEC('CREATE SCHEMA [app]')" :
+ EXEC('CREATE SCHEMA [{schema}]')" :
$"CREATE SCHEMA {schema}";
}
@@ -170,11 +154,11 @@ public override string ToAlterColumnStatement(Type modelType, FieldDefinition fi
column);
}
- public override CommandDefinition ToSelectStatement(SelectStatement statement, CommandFlags flags)
+ public override CommandDefinition ToSelectStatement(SelectStatement statement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken())
{
if (statement.Offset == null && (statement.MaxRows == null || statement.MaxRows == int.MaxValue))
{
- return base.ToSelectStatement(statement, flags);
+ return base.ToSelectStatement(statement, flags, cancellationToken);
}
//Ensure we have an OrderBy clause, this is required by SQLServer paging (and makes more sense anyway)
@@ -184,7 +168,7 @@ public override CommandDefinition ToSelectStatement(SelectStatement statement, C
statement.OrderByExpression.Append(1);
}
- return base.ToSelectStatement(statement, flags);
+ return base.ToSelectStatement(statement, flags, cancellationToken);
}
public override string GetLimitExpression(int? skip, int? rows)
@@ -213,7 +197,7 @@ public override string GetDatePartFunction(string name, string quotedColName)
return $"DATEPART({name.ToLower()},{quotedColName})";
}
- public override string GetStringFunction(string functionName, string quotedColumnName, IDictionary parameters,
+ public override string GetStringFunction(string functionName, string quotedColumnName, StatementParameters parameters,
params string[] availableParameters)
{
switch (functionName.ToLower())
@@ -224,8 +208,8 @@ public override string GetStringFunction(string functionName, string quotedColum
return $"ltrim(rtrim({quotedColumnName}))";
case "substring":
//Ensure Offset is start at 1 instead of 0
- int offset = ((int) parameters[availableParameters[0]]) + 1;
- parameters[availableParameters[0]] = offset;
+ int offset = ((int) parameters[availableParameters[0]].Value) + 1;
+ parameters[availableParameters[0]].Value = offset;
if (parameters.Count == 2)
{
@@ -252,7 +236,8 @@ FROM sys.columns c
LEFT JOIN sys.computed_columns cl ON cl.column_id = c.column_id AND c.object_id = cl.object_id
LEFT JOIN sys.default_constraints dc ON dc.object_id = c.default_object_id
INNER JOIN sys.types t ON t.user_type_id = c.user_type_id
- where c.object_id = OBJECT_ID(@TableName);";
+ where c.object_id = OBJECT_ID(@TableName)
+ order by c.column_id;";
string uniqueQuery = @"SELECT COL_NAME(ic.object_id,ic.column_id) AS COLUMN_NAME , is_unique IS_UNIQUE, is_primary_key IS_PRIMARYKEY
FROM sys.indexes AS i
diff --git a/src/SimpleStack.Orm.SQLite/SimpleStack.Orm.SQLite.csproj b/src/SimpleStack.Orm.SQLite/SimpleStack.Orm.SQLite.csproj
index f68754a..8bdb189 100644
--- a/src/SimpleStack.Orm.SQLite/SimpleStack.Orm.SQLite.csproj
+++ b/src/SimpleStack.Orm.SQLite/SimpleStack.Orm.SQLite.csproj
@@ -4,7 +4,7 @@
SQLite Provider for SimpleStack.Orm based on Microsoft.Data.Sqlite
1.0.2
SimpleStack Team;NServiceKit;ServiceStack
- netstandard2.0
+ net6.0;net8.0
SimpleStack.Orm.SQLite
SimpleStack.Orm.SQLite
Microsoft.Data.Sqlite;SimpleStack;Sqlite
@@ -13,11 +13,12 @@
false
false
1.2.0-beta95
- 1.2.0.0
- 1.2.0.0
- 1.2.5
+ 1.3.0
+ 1.3.0
+ 1.3.0
https://simplestack.org/
https://github.com/SimpleStack/simplestack.orm
+ latestmajor
@@ -25,9 +26,9 @@
-
-
-
+
+
+
diff --git a/src/SimpleStack.Orm.SQLite/SqliteDialectProvider.cs b/src/SimpleStack.Orm.SQLite/SqliteDialectProvider.cs
index 1fbc1d8..449b3ce 100644
--- a/src/SimpleStack.Orm.SQLite/SqliteDialectProvider.cs
+++ b/src/SimpleStack.Orm.SQLite/SqliteDialectProvider.cs
@@ -3,6 +3,7 @@
using System.Data;
using System.Data.Common;
using System.Text.RegularExpressions;
+using System.Threading;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Data.Sqlite;
@@ -13,6 +14,8 @@ namespace SimpleStack.Orm.Sqlite
/// A sqlite ORM lite dialect provider base.
public class SqliteDialectProvider : DialectProviderBase
{
+
+
private static readonly Regex PrecisionAndScaleRegex = new Regex(@"\w+\((?\d+),?(?\d+)?\)",RegexOptions.Compiled);
///
@@ -43,21 +46,16 @@ public override string GetQuotedTableName(string tableName, string schemaName =
? $"{EscapeChar}{NamingStrategy.GetTableName(tableName)}{EscapeChar}"
: $"{EscapeChar}{NamingStrategy.GetTableName(schemaName)}_{NamingStrategy.GetTableName(tableName)}{EscapeChar}";
}
-
- /// Query if 'dbCmd' does table exist.
- /// The database command.
- /// Name of the table.
- /// true if it succeeds, false if it fails.
- public override bool DoesTableExist(IDbConnection connection, string tableName, string schemaName = null)
+
+ public override CommandDefinition ToTableExistStatement(string tableName, string schemaName,
+ CancellationToken cancellationToken = new CancellationToken())
{
var name = string.IsNullOrEmpty(schemaName) ? tableName : $"{schemaName}_{tableName}";
-
- var sql = string.Format("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name = '{0}'"
- , name);
-
- var result = connection.ExecuteScalar(sql);
-
- return result > 0;
+
+ var sql = $"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name = {GetParameterName(0)}";
+ var parameters = new Dictionary {{GetParameterName(0), name}};
+
+ return new CommandDefinition(sql, parameters, cancellationToken: cancellationToken);
}
/// Gets column definition.
diff --git a/src/SimpleStack.Orm/DialectProviderBase.cs b/src/SimpleStack.Orm/DialectProviderBase.cs
index 4dd088e..5439734 100644
--- a/src/SimpleStack.Orm/DialectProviderBase.cs
+++ b/src/SimpleStack.Orm/DialectProviderBase.cs
@@ -16,6 +16,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
using Dapper;
using SimpleStack.Orm.Attributes;
@@ -103,7 +104,7 @@ public virtual string GetQuotedColumnName(string columnName)
}
///
- public virtual CommandDefinition ToSelectStatement(SelectStatement statement, CommandFlags flags)
+ public virtual CommandDefinition ToSelectStatement(SelectStatement statement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken())
{
var sql = new StringBuilder("SELECT ");
@@ -149,10 +150,10 @@ public virtual CommandDefinition ToSelectStatement(SelectStatement statement, Co
}
- return new CommandDefinition(sql.ToString(), statement.Parameters, flags: flags);
+ return new CommandDefinition(sql.ToString(), statement.Parameters.ToDynamicParameters(), flags: flags, cancellationToken:cancellationToken);
}
- public virtual CommandDefinition ToCountStatement(CountStatement statement, CommandFlags flags)
+ public virtual CommandDefinition ToCountStatement(CountStatement statement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken())
{
var sql = new StringBuilder("SELECT COUNT(");
@@ -183,10 +184,10 @@ public virtual CommandDefinition ToCountStatement(CountStatement statement, Comm
sql.Append(statement.HavingExpression);
}
- return new CommandDefinition(sql.ToString(), statement.Parameters, flags: flags);
+ return new CommandDefinition(sql.ToString(), statement.Parameters.ToDynamicParameters(), flags: flags, cancellationToken:cancellationToken);
}
- public virtual CommandDefinition ToDeleteStatement(DeleteStatement statement)
+ public virtual CommandDefinition ToDeleteStatement(DeleteStatement statement, CancellationToken cancellationToken = new CancellationToken())
{
var query = new StringBuilder("DELETE FROM ");
query.Append(statement.TableName);
@@ -196,10 +197,10 @@ public virtual CommandDefinition ToDeleteStatement(DeleteStatement statement)
query.Append(statement.WhereExpression);
}
- return new CommandDefinition(query.ToString(), statement.Parameters);
+ return new CommandDefinition(query.ToString(), statement.Parameters.ToDynamicParameters(), cancellationToken:cancellationToken);
}
- public virtual CommandDefinition ToInsertStatement(InsertStatement insertStatement, CommandFlags flags)
+ public virtual CommandDefinition ToInsertStatement(InsertStatement insertStatement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken())
{
var query = new StringBuilder("INSERT INTO ");
query.Append(insertStatement.TableName);
@@ -208,7 +209,7 @@ public virtual CommandDefinition ToInsertStatement(InsertStatement insertStateme
query.Append(" (");
query.Append(insertStatement.InsertFields.Aggregate((x, y) => x + ", " + y));
query.Append(" ) VALUES (");
- query.Append(insertStatement.Parameters.Select(x => x.Key).Aggregate((x, y) => x + ", " + y));
+ query.Append(insertStatement.Parameters.Select(x => x.ParameterName).Aggregate((x, y) => x + ", " + y));
query.Append(");");
}
else
@@ -218,11 +219,10 @@ public virtual CommandDefinition ToInsertStatement(InsertStatement insertStateme
query.Append(insertStatement.HasIdentity ? SelectIdentitySql : "SELECT 0");
- return new CommandDefinition(query.ToString(), insertStatement.Parameters, flags: flags);
+ return new CommandDefinition(query.ToString(), insertStatement.Parameters.ToDynamicParameters(), flags: flags, cancellationToken:cancellationToken);
}
-
- public virtual CommandDefinition ToUpdateStatement(UpdateStatement statement, CommandFlags flags)
+ public virtual CommandDefinition ToUpdateStatement(UpdateStatement statement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken())
{
var query = new StringBuilder("UPDATE ");
query.Append(statement.TableName);
@@ -248,13 +248,13 @@ public virtual CommandDefinition ToUpdateStatement(UpdateStatement statement, Co
query.Append(statement.WhereExpression);
}
- return new CommandDefinition(query.ToString(), statement.Parameters, flags: flags);
+ return new CommandDefinition(query.ToString(), statement.Parameters.ToDynamicParameters(), flags: flags, cancellationToken:cancellationToken);
}
/// Converts a tableType to a create table statement.
/// Model Definition.
/// tableType as a string.
- public virtual string ToCreateTableStatement(ModelDefinition modelDef)
+ public virtual CommandDefinition ToCreateTableStatement(ModelDefinition modelDef, CancellationToken cancellationToken = new CancellationToken())
{
var sbColumns = new StringBuilder();
var sbConstraints = new StringBuilder();
@@ -306,8 +306,8 @@ public virtual string ToCreateTableStatement(ModelDefinition modelDef)
sbPrimaryKeys.Append(")");
}
- return
- $"CREATE TABLE {GetQuotedTableName(modelDef)} \n(\n {sbColumns}{sbPrimaryKeys}{sbConstraints} \n); \n";
+ return new CommandDefinition($"CREATE TABLE {GetQuotedTableName(modelDef)} \n(\n {sbColumns}{sbPrimaryKeys}{sbConstraints} \n); \n",
+ cancellationToken:cancellationToken);
}
/// Converts a tableType to a create index statements.
@@ -343,22 +343,19 @@ public virtual List ToCreateIndexStatements(ModelDefinition modelDef)
return sqlIndexes;
}
- /// Query if 'dbCmd' does table exist.
- /// The database.
- /// Name of the table.
- /// true if it succeeds, false if it fails.
- public virtual bool DoesTableExist(IDbConnection connection, string tableName, string schemaName = null)
+ public virtual CommandDefinition ToTableExistStatement(string tableName, string schemaName,
+ CancellationToken cancellationToken = new CancellationToken())
{
- return false;
- }
+ var sql = $"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = {GetParameterName(0)}";
+ var parameters = new Dictionary {{GetParameterName(0), NamingStrategy.GetTableName(tableName)}};
- /// Query if 'dbCmd' does table exist.
- /// The database.
- /// Name of the table.
- /// true if it succeeds, false if it fails.
- public virtual bool DoesSchemaExist(IDbConnection connection, string schemaName)
- {
- return false;
+ if (!string.IsNullOrEmpty(schemaName))
+ {
+ sql += $" AND TABLE_SCHEMA = {GetParameterName(1)}";
+ parameters.Add(GetParameterName(1), schemaName);
+ }
+
+ return new CommandDefinition(sql, parameters, cancellationToken: cancellationToken);
}
/// Query if 'dbCmd' does sequence exist.
@@ -542,7 +539,7 @@ public virtual string BindOperand(ExpressionType e, bool isIntegral)
}
public virtual string GetStringFunction(string functionName, string quotedColumnName,
- IDictionary parameters, params string[] availableParameters)
+ StatementParameters parameters, params string[] availableParameters)
{
switch (functionName.ToLower())
{
@@ -559,19 +556,18 @@ public virtual string GetStringFunction(string functionName, string quotedColumn
case "tolower":
return $"lower({quotedColumnName})";
case "startswith":
- parameters[availableParameters[0]] = parameters[availableParameters[0]].ToString().ToUpper() + "%";
+ parameters[availableParameters[0]].Value = parameters[availableParameters[0]].Value.ToString().ToUpper() + "%";
return $"upper({quotedColumnName}) LIKE {availableParameters[0]} ";
case "endswith":
- parameters[availableParameters[0]] = "%" + parameters[availableParameters[0]].ToString().ToUpper();
+ parameters[availableParameters[0]].Value = "%" + parameters[availableParameters[0]].Value.ToString().ToUpper();
return $"upper({quotedColumnName}) LIKE {availableParameters[0]} ";
case "contains":
- parameters[availableParameters[0]] =
- "%" + parameters[availableParameters[0]].ToString().ToUpper() + "%";
+ parameters[availableParameters[0]].Value = "%" + parameters[availableParameters[0]].Value.ToString().ToUpper() + "%";
return $"upper({quotedColumnName}) LIKE {availableParameters[0]} ";
case "substring":
//Ensure Offset is start at 1 instead of 0
- var offset = (int) parameters[availableParameters[0]] + 1;
- parameters[availableParameters[0]] = offset;
+ var offset = (int) parameters[availableParameters[0]].Value + 1;
+ parameters[availableParameters[0]].Value = offset;
if (parameters.Count == 2)
{
@@ -648,8 +644,12 @@ protected virtual string GetDefaultValueDefinition(object defaultValue)
/// The column type definition.
public virtual string GetColumnTypeDefinition(Type fieldType, string fieldName, int? fieldLength)
{
+ // DateTime has been removed from Dapper DBTypes
+ if(fieldType == typeof(DateTime))
+ return TypesMapper.GetFieldDefinition(DbType.DateTime2, fieldLength);
+
#pragma warning disable 618
- var dbType = SqlMapper.LookupDbType(fieldType, fieldName, false, out var typeHandler);
+ var dbType = SqlMapper.LookupDbType(fieldType, fieldName, true, out var typeHandler);
#pragma warning restore 618
if (typeHandler is ITypeHandlerColumnType typeHandlerColumnType)
@@ -658,7 +658,7 @@ public virtual string GetColumnTypeDefinition(Type fieldType, string fieldName,
fieldLength = typeHandlerColumnType.Length;
}
- return TypesMapper.GetFieldDefinition(dbType, fieldLength);
+ return TypesMapper.GetFieldDefinition(dbType.GetValueOrDefault(DbType.Object), fieldLength);
}
/// Gets foreign key on delete clause.
diff --git a/src/SimpleStack.Orm/Expressions/ColumnWhereExpresionVisitor.cs b/src/SimpleStack.Orm/Expressions/ColumnWhereExpressionVisitor.cs
similarity index 96%
rename from src/SimpleStack.Orm/Expressions/ColumnWhereExpresionVisitor.cs
rename to src/SimpleStack.Orm/Expressions/ColumnWhereExpressionVisitor.cs
index c0752b2..33c66e0 100644
--- a/src/SimpleStack.Orm/Expressions/ColumnWhereExpresionVisitor.cs
+++ b/src/SimpleStack.Orm/Expressions/ColumnWhereExpressionVisitor.cs
@@ -2,15 +2,16 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
+using SimpleStack.Orm.Expressions.Statements;
namespace SimpleStack.Orm.Expressions
{
- internal class ColumnWhereExpresionVisitor : ExpressionVisitor
+ internal class ColumnWhereExpressionVisitor : ExpressionVisitor
{
private readonly string _statement;
- public ColumnWhereExpresionVisitor(IDialectProvider dialectProvider,
- IDictionary parameters,
+ public ColumnWhereExpressionVisitor(IDialectProvider dialectProvider,
+ StatementParameters parameters,
string statement)
: base(dialectProvider, parameters)
{
diff --git a/src/SimpleStack.Orm/Expressions/ExpressionVisitor.cs b/src/SimpleStack.Orm/Expressions/ExpressionVisitor.cs
index 87e0629..3b1a6ec 100644
--- a/src/SimpleStack.Orm/Expressions/ExpressionVisitor.cs
+++ b/src/SimpleStack.Orm/Expressions/ExpressionVisitor.cs
@@ -5,16 +5,17 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
+using SimpleStack.Orm.Expressions.Statements;
namespace SimpleStack.Orm.Expressions
{
internal abstract class ExpressionVisitor
{
protected readonly IDialectProvider DialectProvider;
- protected readonly IDictionary Parameters;
+ protected readonly StatementParameters Parameters;
protected ExpressionVisitor(IDialectProvider dialectProvider,
- IDictionary parameters)
+ StatementParameters parameters)
{
DialectProvider = dialectProvider;
Parameters = parameters;
@@ -31,7 +32,7 @@ public virtual string VisitExpression(Expression exp)
if (statement is ParameterPart pp)
{
- return (bool) Parameters[pp.Text] ? "1=1" : "1=0";
+ return (bool) Parameters[pp.Text].Value ? "1=1" : "1=0";
}
if (statement is ColumnAccessPart cp)
@@ -236,7 +237,7 @@ protected virtual StatementPart VisitUnary(UnaryExpression unaryExpression)
if (o is ParameterPart op && op.ParameterType == typeof(bool))
{
- Parameters[op.Text] = !(bool) Parameters[op.Text];
+ Parameters[op.Text].Value = !(bool) Parameters[op.Text].Value;
return o;
}
@@ -310,7 +311,7 @@ protected virtual StatementPart VisitBinary(BinaryExpression binaryExpression)
if (leftp?.ParameterType == typeof(bool) || rightp?.ParameterType == typeof(bool))
{
- var boolValue = (bool) (leftp != null ? Parameters[leftp.Text] : Parameters[rightp.Text]);
+ var boolValue = (bool) (leftp != null ? Parameters[leftp.Text].Value : Parameters[rightp.Text].Value);
if (operand == "AND")
{
if (boolValue)
@@ -336,12 +337,12 @@ protected virtual StatementPart VisitBinary(BinaryExpression binaryExpression)
{
if (leftca != null && leftca.ColumnType.IsEnum() && rightp != null)
{
- Parameters[rightp.Text] = Enum.ToObject(leftca.ColumnType, Parameters[rightp.Text]);
+ Parameters[rightp.Text].Value = Enum.ToObject(leftca.ColumnType, Parameters[rightp.Text].Value);
}
if (rightca != null && rightca.ColumnType.IsEnum() && leftp != null)
{
- Parameters[leftp.Text] = Enum.ToObject(rightca.ColumnType, Parameters[leftp.Text]);
+ Parameters[leftp.Text].Value = Enum.ToObject(rightca.ColumnType, Parameters[leftp.Text].Value);
}
}
@@ -432,9 +433,12 @@ protected virtual StatementPart VisitDateTimeMemberAccess(MemberExpression m)
///
protected ParameterPart AddParameter(object param)
{
- var paramName = DialectProvider.GetParameterName(Parameters.Count);
- Parameters.Add(paramName, param);
- return new ParameterPart(paramName, param.GetType());
+ var statementParameter = new StatementParameter(
+ DialectProvider.GetParameterName(Parameters.Count),
+ param.GetType(),
+ param);
+ Parameters.Add(statementParameter);
+ return new ParameterPart(statementParameter.ParameterName, statementParameter.Type);
}
protected class StatementPart
diff --git a/src/SimpleStack.Orm/Expressions/Statements/Dynamic/DynamicCountStatement.cs b/src/SimpleStack.Orm/Expressions/Statements/Dynamic/DynamicCountStatement.cs
index 6bb05a0..2f308db 100644
--- a/src/SimpleStack.Orm/Expressions/Statements/Dynamic/DynamicCountStatement.cs
+++ b/src/SimpleStack.Orm/Expressions/Statements/Dynamic/DynamicCountStatement.cs
@@ -65,7 +65,7 @@ public DynamicCountStatement Having(string columnName, Expression(_dialectProvider, Statement.Parameters,
+ new ColumnWhereExpressionVisitor(_dialectProvider, Statement.Parameters,
_dialectProvider.GetQuotedColumnName(columnName))
.VisitExpression(predicate));
}
@@ -87,9 +87,9 @@ public DynamicCountStatement OrRaw(string condition)
return Where(condition, "OR");
}
- internal ColumnWhereExpresionVisitor GetExpressionVisitor(string columnOrExpression)
+ internal ColumnWhereExpressionVisitor GetExpressionVisitor(string columnOrExpression)
{
- return new ColumnWhereExpresionVisitor(_dialectProvider, Statement.Parameters, columnOrExpression);
+ return new ColumnWhereExpressionVisitor(_dialectProvider, Statement.Parameters, columnOrExpression);
}
internal DynamicCountStatement Where(ExpressionVisitor visitor, Expression> func,
diff --git a/src/SimpleStack.Orm/Expressions/Statements/Statement.cs b/src/SimpleStack.Orm/Expressions/Statements/Statement.cs
index 80e6917..87e80ea 100644
--- a/src/SimpleStack.Orm/Expressions/Statements/Statement.cs
+++ b/src/SimpleStack.Orm/Expressions/Statements/Statement.cs
@@ -1,10 +1,49 @@
+using System;
+using Dapper;
using System.Collections.Generic;
+using System.Collections.ObjectModel;
namespace SimpleStack.Orm.Expressions.Statements
{
+ public class StatementParameters : KeyedCollection
+ {
+ protected override string GetKeyForItem(StatementParameter item)
+ {
+ return item.ParameterName;
+ }
+
+ public DynamicParameters ToDynamicParameters()
+ {
+ var result = new DynamicParameters();
+ foreach (var pair in this)
+ {
+#pragma warning disable CS0618 // Type or member is obsolete
+ result.Add(pair.ParameterName, pair.Value,
+ SqlMapper.LookupDbType(pair.Type, pair.ParameterName, demand: true, out _));
+#pragma warning restore CS0618 // Type or member is obsolete
+ }
+
+ return result;
+ }
+ }
+
+ public class StatementParameter
+ {
+ public string ParameterName { get; }
+ public Type Type { get; }
+ public object Value { get; set; }
+
+ public StatementParameter(string parameterName, Type type, object value)
+ {
+ ParameterName = parameterName;
+ Type = type;
+ Value = value;
+ }
+ }
+
public abstract class Statement
{
public string TableName { get; set; }
- public IDictionary Parameters { get; } = new Dictionary();
+ public StatementParameters Parameters { get; } = new StatementParameters();
}
}
\ No newline at end of file
diff --git a/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedDeleteStatement.cs b/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedDeleteStatement.cs
index 4f2a959..bc6d36c 100644
--- a/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedDeleteStatement.cs
+++ b/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedDeleteStatement.cs
@@ -36,7 +36,7 @@ public TypedDeleteStatement AddPrimaryKeyWhereCondition(T values)
Statement.WhereExpression.Append(_dialectProvider.GetQuotedColumnName(pk.FieldName));
Statement.WhereExpression.Append("=");
Statement.WhereExpression.Append(pname);
- Statement.Parameters.Add(pname, pk.GetValue(values));
+ Statement.Parameters.Add(new StatementParameter(pname,pk.FieldType, pk.GetValue(values)));
}
return this;
diff --git a/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedInsertStatement.cs b/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedInsertStatement.cs
index ab5359d..ea56d87 100644
--- a/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedInsertStatement.cs
+++ b/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedInsertStatement.cs
@@ -54,7 +54,7 @@ public TypedInsertStatement Values(T values, IEnumerable onlyFields)
if (v != null)
{
var pname = _dialectProvider.GetParameterName(Statement.Parameters.Count);
- Statement.Parameters.Add(pname, v);
+ Statement.Parameters.Add(new StatementParameter(pname,fieldDef.FieldType,v));
Statement.InsertFields.Add(_dialectProvider.GetQuotedColumnName(fieldDef.FieldName));
}
}
diff --git a/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedUpdateStatement.cs b/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedUpdateStatement.cs
index ca212da..52607f4 100644
--- a/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedUpdateStatement.cs
+++ b/src/SimpleStack.Orm/Expressions/Statements/Typed/TypedUpdateStatement.cs
@@ -85,7 +85,7 @@ private TypedUpdateStatement Values(object values, IEnumerable onlyFi
{
var pname = _dialectProvider.GetParameterName(Statement.Parameters.Count);
Statement.UpdateFields.Add(_dialectProvider.GetQuotedColumnName(fieldDef.FieldName), pname);
- Statement.Parameters.Add(pname, fieldDef.GetValue(values));
+ Statement.Parameters.Add(new StatementParameter(pname,fieldDef.FieldType, fieldDef.GetValue(values)));
}
//Add Primarykey filter if required
@@ -103,7 +103,7 @@ private TypedUpdateStatement Values(object values, IEnumerable onlyFi
Statement.WhereExpression.Append(_dialectProvider.GetQuotedColumnName(pk.FieldName));
Statement.WhereExpression.Append("=");
Statement.WhereExpression.Append(pname);
- Statement.Parameters.Add(pname, pk.GetValue(values));
+ Statement.Parameters.Add(new StatementParameter(pname,pk.FieldType, pk.GetValue(values)));
}
}
diff --git a/src/SimpleStack.Orm/Expressions/TableFieldsExpresionVisitor.cs b/src/SimpleStack.Orm/Expressions/TableFieldsExpresionVisitor.cs
index 5491754..94ca3f9 100644
--- a/src/SimpleStack.Orm/Expressions/TableFieldsExpresionVisitor.cs
+++ b/src/SimpleStack.Orm/Expressions/TableFieldsExpresionVisitor.cs
@@ -4,6 +4,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
+using SimpleStack.Orm.Expressions.Statements;
namespace SimpleStack.Orm.Expressions
{
@@ -13,7 +14,7 @@ internal sealed class TableWFieldsExpresionVisitor : ExpressionVisitor
private readonly ModelDefinition _modelDefinition;
public TableWFieldsExpresionVisitor(IDialectProvider dialectProvider,
- IDictionary parameters,
+ StatementParameters parameters,
ModelDefinition modelDefinition,
bool addAliasSpecification)
: base(dialectProvider, parameters)
diff --git a/src/SimpleStack.Orm/Expressions/TableWhereExpresionVisitor.cs b/src/SimpleStack.Orm/Expressions/TableWhereExpresionVisitor.cs
index fca5486..59467c8 100644
--- a/src/SimpleStack.Orm/Expressions/TableWhereExpresionVisitor.cs
+++ b/src/SimpleStack.Orm/Expressions/TableWhereExpresionVisitor.cs
@@ -3,6 +3,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
+using SimpleStack.Orm.Expressions.Statements;
namespace SimpleStack.Orm.Expressions
{
@@ -11,7 +12,7 @@ internal class TableWhereExpresionVisitor : ExpressionVisitor
private readonly ModelDefinition _modelDefinition;
public TableWhereExpresionVisitor(IDialectProvider dialectProvider,
- IDictionary parameters,
+ StatementParameters parameters,
ModelDefinition modelDefinition)
: base(dialectProvider, parameters)
{
diff --git a/src/SimpleStack.Orm/IDialectProvider.cs b/src/SimpleStack.Orm/IDialectProvider.cs
index f4097a0..75d9d01 100644
--- a/src/SimpleStack.Orm/IDialectProvider.cs
+++ b/src/SimpleStack.Orm/IDialectProvider.cs
@@ -13,6 +13,7 @@
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;
+using System.Threading;
using System.Threading.Tasks;
using Dapper;
using SimpleStack.Orm.Expressions.Statements;
@@ -73,20 +74,22 @@ public interface IDialectProvider
string GetQuotedColumnName(string columnName);
- CommandDefinition ToInsertStatement(InsertStatement insertStatement, CommandFlags flags);
+ CommandDefinition ToInsertStatement(InsertStatement insertStatement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken());
- CommandDefinition ToSelectStatement(SelectStatement statement, CommandFlags flags);
+ CommandDefinition ToSelectStatement(SelectStatement statement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken());
- CommandDefinition ToUpdateStatement(UpdateStatement statement, CommandFlags flags);
+ CommandDefinition ToUpdateStatement(UpdateStatement statement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken());
- CommandDefinition ToCountStatement(CountStatement statement, CommandFlags flags);
+ CommandDefinition ToCountStatement(CountStatement statement, CommandFlags flags, CancellationToken cancellationToken = new CancellationToken());
- CommandDefinition ToDeleteStatement(DeleteStatement deleteStatement);
+ CommandDefinition ToDeleteStatement(DeleteStatement deleteStatement, CancellationToken cancellationToken = new CancellationToken());
+
+ CommandDefinition ToTableExistStatement(string tableName, string schemaName, CancellationToken cancellationToken = new CancellationToken());
/// Converts a tableType to a create table statement.
/// Model Definition.
/// tableType as a string.
- string ToCreateTableStatement(ModelDefinition modelDefinition);
+ CommandDefinition ToCreateTableStatement(ModelDefinition modelDefinition, CancellationToken cancellationToken = new CancellationToken());
/// Converts a tableType to a create index statements.
/// Model Definition.
@@ -109,18 +112,6 @@ public interface IDialectProvider
/// A List<string>
List SequenceList(ModelDefinition modelDefinition);
- /// Query if table exist.
- /// The database.
- /// Name of the table.
- /// true if it exists, false otherwise.
- bool DoesTableExist(IDbConnection connection, string tableName, string schemaName = null);
-
- /// Query if schema exist.
- /// The database.
- /// Name of the table.
- /// true if it exists, false otherwise.
- bool DoesSchemaExist(IDbConnection connection, string schemaName);
-
/// Query if 'dbCmd' does sequence exist.
/// The database command.
/// Name of the sequenc.
@@ -210,7 +201,7 @@ Task> GetTableDefinitions(IDbConnection connection
string BindOperand(ExpressionType e, bool isIntegral);
- string GetStringFunction(string functionName, string quotedColumnName, IDictionary parameters,
+ string GetStringFunction(string functionName, string quotedColumnName, StatementParameters parameters,
params string[] availableParameters);
}
}
\ No newline at end of file
diff --git a/src/SimpleStack.Orm/OrmConnectionAsync.cs b/src/SimpleStack.Orm/OrmConnectionAsync.cs
index 3d21b24..8ea53e2 100644
--- a/src/SimpleStack.Orm/OrmConnectionAsync.cs
+++ b/src/SimpleStack.Orm/OrmConnectionAsync.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
+using System.Threading;
using System.Threading.Tasks;
using Dapper;
using SimpleStack.Orm.Attributes;
@@ -20,9 +21,9 @@ public partial class OrmConnection
/// the command flags
/// IEnumerable of results as dynamix objects
public Task> SelectAsync(string tableName,
- Action selectStatement, CommandFlags flags = CommandFlags.Buffered)
+ Action selectStatement, CommandFlags flags = CommandFlags.Buffered, CancellationToken cancellationToken = new CancellationToken())
{
- return SelectAsync(tableName, null, selectStatement, flags);
+ return SelectAsync(tableName, null, selectStatement, flags,cancellationToken);
}
///
@@ -34,13 +35,13 @@ public Task> SelectAsync(string tableName,
/// the command flags
/// IEnumerable of results as dynamix objects
public Task> SelectAsync(string tableName, string schemaName,
- Action selectStatement, CommandFlags flags = CommandFlags.Buffered)
+ Action selectStatement, CommandFlags flags = CommandFlags.Buffered, CancellationToken cancellationToken = new CancellationToken())
{
var statement = new DynamicSelectStatement(DialectProvider);
selectStatement(statement.From(tableName, schemaName));
- var cmd = DialectProvider.ToSelectStatement(statement.Statement, flags);
+ var cmd = DialectProvider.ToSelectStatement(statement.Statement, flags, cancellationToken);
return this.QueryAsync(cmd.CommandText, cmd.Parameters, cmd.Transaction, cmd.CommandTimeout,
cmd.CommandType);
}
@@ -53,11 +54,11 @@ public Task> SelectAsync(string tableName, string schemaNam
///
///
public Task> SelectAsync(Expression> predicate,
- CommandFlags flags = CommandFlags.Buffered)
+ CommandFlags flags = CommandFlags.Buffered, CancellationToken cancellationToken = new CancellationToken())
{
var select = new TypedSelectStatement(DialectProvider);
select.Where(predicate);
- return this.QueryAsync(DialectProvider.ToSelectStatement(select.Statement, flags));
+ return this.QueryAsync(DialectProvider.ToSelectStatement(select.Statement, flags,cancellationToken));
}
///
@@ -68,11 +69,11 @@ public Task> SelectAsync(Expression> predicate,
///
///
public Task> SelectAsync(Action> expression,
- CommandFlags flags = CommandFlags.Buffered)
+ CommandFlags flags = CommandFlags.Buffered, CancellationToken cancellationToken = new CancellationToken())
{
var select = new TypedSelectStatement(DialectProvider);
expression(select);
- return this.QueryAsync(DialectProvider.ToSelectStatement(select.Statement, flags));
+ return this.QueryAsync(DialectProvider.ToSelectStatement(select.Statement, flags, cancellationToken));
}
///
@@ -81,10 +82,10 @@ public Task> SelectAsync(Action> expre
/// Command flags
///
///
- public Task> SelectAsync(CommandFlags flags = CommandFlags.Buffered)
+ public Task> SelectAsync(CommandFlags flags = CommandFlags.Buffered, CancellationToken cancellationToken = new CancellationToken())
{
return this.QueryAsync(
- DialectProvider.ToSelectStatement(new TypedSelectStatement(DialectProvider).Statement, flags));
+ DialectProvider.ToSelectStatement(new TypedSelectStatement(DialectProvider).Statement, flags, cancellationToken));
}
///
@@ -94,13 +95,13 @@ public Task> SelectAsync(CommandFlags flags = CommandFlags.Buf
///
///
/// If the query does not returns any element
- public async Task FirstAsync(Expression> predicate)
+ public async Task FirstAsync(Expression> predicate, CancellationToken cancellationToken = new CancellationToken())
{
var r = await SelectAsync(x =>
{
x.Where(predicate);
x.Limit(1);
- }).ConfigureAwait(false);
+ }, cancellationToken: cancellationToken).ConfigureAwait(false);
return r.First();
}
@@ -111,13 +112,13 @@ public async Task FirstAsync(Expression> predicate)
///
///
/// If the query does not returns any element
- public async Task FirstAsync(Action> expression)
+ public async Task FirstAsync(Action> expression, CancellationToken cancellationToken = new CancellationToken())
{
var r = await SelectAsync(x =>
{
expression(x);
x.Limit(1);
- }).ConfigureAwait(false);
+ }, cancellationToken: cancellationToken).ConfigureAwait(false);
return r.First();
}
@@ -127,13 +128,13 @@ public async Task FirstAsync(Action> expression)
/// Where clause
///
///
- public async Task FirstOrDefaultAsync(Expression> predicate)
+ public async Task FirstOrDefaultAsync(Expression> predicate, CancellationToken cancellationToken = new CancellationToken())
{
var r = await SelectAsync(x =>
{
x.Where(predicate);
x.Limit(1);
- }).ConfigureAwait(false);
+ }, cancellationToken: cancellationToken).ConfigureAwait(false);
return r.FirstOrDefault();
}
@@ -143,13 +144,13 @@ public async Task FirstOrDefaultAsync(Expression> predicate)
///
///
///
- public async Task FirstOrDefaultAsync(Action> expression)
+ public async Task FirstOrDefaultAsync(Action> expression, CancellationToken cancellationToken = new CancellationToken())
{
var r = await SelectAsync(x =>
{
expression(x);
x.Limit(1);
- }).ConfigureAwait(false);
+ }, cancellationToken: cancellationToken).ConfigureAwait(false);
return r.FirstOrDefault();
}
@@ -163,12 +164,12 @@ public async Task FirstOrDefaultAsync(Action> expr
/// The Type specifying the target table
/// The Type of the result
///
- public Task GetScalarAsync(Expression> field)
+ public Task GetScalarAsync(Expression> field, CancellationToken cancellationToken = new CancellationToken())
{
var select = new TypedSelectStatement(DialectProvider);
select.Select(field);
return this.ExecuteScalarAsync(
- DialectProvider.ToSelectStatement(select.Statement, CommandFlags.None));
+ DialectProvider.ToSelectStatement(select.Statement, CommandFlags.None, cancellationToken));
}
///
@@ -183,13 +184,13 @@ public Task GetScalarAsync(Expression> field)
/// The type of the result
///
public Task GetScalarAsync(Expression> field,
- Expression> predicate)
+ Expression> predicate, CancellationToken cancellationToken = new CancellationToken())
{
var select = new TypedSelectStatement(DialectProvider);
select.Select(field);
select.Where(predicate);
return this.ExecuteScalarAsync(
- DialectProvider.ToSelectStatement(select.Statement, CommandFlags.None));
+ DialectProvider.ToSelectStatement(select.Statement, CommandFlags.None, cancellationToken));
}
///
@@ -198,13 +199,13 @@ public Task GetScalarAsync(Expression> field,
/// The Select Statement action
///
///
- public Task CountAsync(Action> expression)
+ public Task CountAsync(Action> expression, CancellationToken cancellationToken = new CancellationToken())
{
var select = new TypedSelectStatement(DialectProvider);
expression(select);
return this.ExecuteScalarAsync(
- DialectProvider.ToCountStatement(select.Statement, CommandFlags.None));
+ DialectProvider.ToCountStatement(select.Statement, CommandFlags.None, cancellationToken));
}
///
@@ -213,9 +214,9 @@ public Task CountAsync(Action> expression)
/// the where clause expression
///
///
- public Task CountAsync(Expression> expression)
+ public Task CountAsync(Expression> expression, CancellationToken cancellationToken = new CancellationToken())
{
- return CountAsync(e => e.Where(expression));
+ return CountAsync(e => e.Where(expression), cancellationToken);
}
///
@@ -224,9 +225,9 @@ public Task CountAsync(Expression> expression)
/// The table name
/// The Select Statement action
///
- public Task CountAsync(string tableName, Action expression)
+ public Task CountAsync(string tableName, Action expression, CancellationToken cancellationToken = new CancellationToken())
{
- return CountAsync(tableName, null, expression);
+ return CountAsync(tableName, null, expression, cancellationToken);
}
///
@@ -237,13 +238,13 @@ public Task CountAsync(string tableName, Action ex
/// The Select Statement action
///
public Task CountAsync(string tableName, string schemaName,
- Action expression)
+ Action expression, CancellationToken cancellationToken = new CancellationToken())
{
var select = new DynamicSelectStatement(DialectProvider);
expression(select.From(tableName, schemaName));
return this.ExecuteScalarAsync(
- DialectProvider.ToCountStatement(select.Statement, CommandFlags.None));
+ DialectProvider.ToCountStatement(select.Statement, CommandFlags.None, cancellationToken));
}
///
@@ -251,9 +252,9 @@ public Task CountAsync(string tableName, string schemaName,
///
///
///
- public Task CountAsync()
+ public Task CountAsync(CancellationToken cancellationToken = new CancellationToken())
{
- return CountAsync(e => { });
+ return CountAsync(e => { }, cancellationToken);
}
///
@@ -262,11 +263,11 @@ public Task CountAsync()
/// the object to update
///
///
- public Task UpdateAsync(T model)
+ public Task UpdateAsync(T model, CancellationToken cancellationToken = new CancellationToken())
{
var s = new TypedUpdateStatement(DialectProvider);
s.ValuesOnly(model);
- var cmd = DialectProvider.ToUpdateStatement(s.Statement, CommandFlags.None);
+ var cmd = DialectProvider.ToUpdateStatement(s.Statement, CommandFlags.None, cancellationToken);
return this.ExecuteScalarAsync(cmd);
}
@@ -278,11 +279,11 @@ public Task UpdateAsync(T model)
///
///
///
- public Task UpdateAsync(T model, Expression> onlyFields)
+ public Task UpdateAsync(T model, Expression> onlyFields, CancellationToken cancellationToken = new CancellationToken())
{
var s = new TypedUpdateStatement(DialectProvider);
s.ValuesOnly(model, onlyFields);
- var cmd = DialectProvider.ToUpdateStatement(s.Statement, CommandFlags.None);
+ var cmd = DialectProvider.ToUpdateStatement(s.Statement, CommandFlags.None, cancellationToken);
return this.ExecuteScalarAsync(cmd);
}
@@ -295,8 +296,8 @@ public Task UpdateAsync(T model, Expression> onlyFie
///
///
///
- public Task UpdateAllAsync(object obj, Expression> onlyField,
- Expression> where = null)
+ public async Task UpdateAllAsync(object obj, Expression> onlyField,
+ Expression> where = null, CancellationToken cancellationToken = new CancellationToken())
{
var s = new TypedUpdateStatement(DialectProvider);
if (where != null)
@@ -306,8 +307,8 @@ public Task UpdateAllAsync(object obj, Expression> o
s.Values(obj, onlyField);
- var cmd = DialectProvider.ToUpdateStatement(s.Statement, CommandFlags.None);
- return this.ExecuteScalarAsync(cmd);
+ var cmd = DialectProvider.ToUpdateStatement(s.Statement, CommandFlags.None, cancellationToken);
+ return await this.ExecuteScalarAsync(cmd);
}
///
@@ -317,9 +318,9 @@ public Task UpdateAllAsync(object obj, Expression> o
///
///
///
- public Task InsertAsync(T obj)
+ public Task InsertAsync(T obj, CancellationToken cancellationToken = new CancellationToken())
{
- return InsertAsync(obj);
+ return InsertAsync(obj, cancellationToken);
}
///
@@ -329,14 +330,14 @@ public Task InsertAsync(T obj)
///
///
///
- public Task InsertAsync(T obj)
+ public async Task InsertAsync(T obj, CancellationToken cancellationToken = new CancellationToken())
{
try
{
var insertStatement = new TypedInsertStatement(DialectProvider);
insertStatement.Values(obj, Array.Empty());
- var commandDefinition = DialectProvider.ToInsertStatement(insertStatement.Statement, CommandFlags.None);
- return this.ExecuteScalarAsync(commandDefinition);
+ var commandDefinition = DialectProvider.ToInsertStatement(insertStatement.Statement, CommandFlags.None, cancellationToken);
+ return await this.ExecuteScalarAsync(commandDefinition);
}
catch (Exception e)
{
@@ -350,18 +351,7 @@ public Task InsertAsync(T obj)
/// The objects to insert
///
///
- public Task> InsertAsync