Commit ad0a2622 authored by Nick Craver's avatar Nick Craver

Contrib, Rainbow, EntityFramework & SqlBuilder: code cleanup

parent bc8468f4
...@@ -16,8 +16,6 @@ ...@@ -16,8 +16,6 @@
using System.Data; using System.Data;
#endif #endif
#pragma warning disable 1573, 1591 // xml comments
namespace Dapper.Contrib.Extensions namespace Dapper.Contrib.Extensions
{ {
...@@ -32,6 +30,8 @@ public static partial class SqlMapperExtensions ...@@ -32,6 +30,8 @@ public static partial class SqlMapperExtensions
/// <typeparam name="T">Interface type to create and populate</typeparam> /// <typeparam name="T">Interface type to create and populate</typeparam>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="id">Id of the entity to get, must be marked with [Key] attribute</param> /// <param name="id">Id of the entity to get, must be marked with [Key] attribute</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>Entity of T</returns> /// <returns>Entity of T</returns>
public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
...@@ -41,7 +41,7 @@ public static partial class SqlMapperExtensions ...@@ -41,7 +41,7 @@ public static partial class SqlMapperExtensions
{ {
var keys = KeyPropertiesCache(type); var keys = KeyPropertiesCache(type);
var explicitKeys = ExplicitKeyPropertiesCache(type); var explicitKeys = ExplicitKeyPropertiesCache(type);
if (keys.Count() > 1 || explicitKeys.Count() > 1) if (keys.Count > 1 || explicitKeys.Count > 1)
throw new DataException("Get<T> only supports an entity with a single [Key] or [ExplicitKey] property"); throw new DataException("Get<T> only supports an entity with a single [Key] or [ExplicitKey] property");
if (!keys.Any() && !explicitKeys.Any()) if (!keys.Any() && !explicitKeys.Any())
throw new DataException("Get<T> only supports an entity with a [Key] or an [ExplicitKey] property"); throw new DataException("Get<T> only supports an entity with a [Key] or an [ExplicitKey] property");
...@@ -88,6 +88,8 @@ public static partial class SqlMapperExtensions ...@@ -88,6 +88,8 @@ public static partial class SqlMapperExtensions
/// </summary> /// </summary>
/// <typeparam name="T">Interface or type to create and populate</typeparam> /// <typeparam name="T">Interface or type to create and populate</typeparam>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>Entity of T</returns> /// <returns>Entity of T</returns>
public static async Task<IEnumerable<T>> GetAllAsync<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static async Task<IEnumerable<T>> GetAllAsync<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
...@@ -98,7 +100,7 @@ public static partial class SqlMapperExtensions ...@@ -98,7 +100,7 @@ public static partial class SqlMapperExtensions
if (!GetQueries.TryGetValue(cacheType.TypeHandle, out sql)) if (!GetQueries.TryGetValue(cacheType.TypeHandle, out sql))
{ {
var keys = KeyPropertiesCache(type); var keys = KeyPropertiesCache(type);
if (keys.Count() > 1) if (keys.Count > 1)
throw new DataException("Get<T> only supports an entity with a single [Key] property"); throw new DataException("Get<T> only supports an entity with a single [Key] property");
if (!keys.Any()) if (!keys.Any())
throw new DataException("Get<T> only supports en entity with a [Key] property"); throw new DataException("Get<T> only supports en entity with a [Key] property");
...@@ -112,7 +114,7 @@ public static partial class SqlMapperExtensions ...@@ -112,7 +114,7 @@ public static partial class SqlMapperExtensions
if (!type.IsInterface()) if (!type.IsInterface())
{ {
return await connection.QueryAsync<T>(sql, null, transaction, commandTimeout: commandTimeout); return await connection.QueryAsync<T>(sql, null, transaction, commandTimeout);
} }
var result = await connection.QueryAsync(sql); var result = await connection.QueryAsync(sql);
...@@ -137,6 +139,9 @@ public static partial class SqlMapperExtensions ...@@ -137,6 +139,9 @@ public static partial class SqlMapperExtensions
/// </summary> /// </summary>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="entityToInsert">Entity to insert</param> /// <param name="entityToInsert">Entity to insert</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <param name="sqlAdapter">The specific ISqlAdapter to use, auto-detected based on connection if null</param>
/// <returns>Identity of inserted entity</returns> /// <returns>Identity of inserted entity</returns>
public static async Task<int> InsertAsync<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, public static async Task<int> InsertAsync<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null,
int? commandTimeout = null, ISqlAdapter sqlAdapter = null) where T : class int? commandTimeout = null, ISqlAdapter sqlAdapter = null) where T : class
...@@ -158,20 +163,20 @@ public static partial class SqlMapperExtensions ...@@ -158,20 +163,20 @@ public static partial class SqlMapperExtensions
var computedProperties = ComputedPropertiesCache(type); var computedProperties = ComputedPropertiesCache(type);
var allPropertiesExceptKeyAndComputed = allProperties.Except(keyProperties.Union(computedProperties)).ToList(); var allPropertiesExceptKeyAndComputed = allProperties.Except(keyProperties.Union(computedProperties)).ToList();
for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count(); i++) for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++)
{ {
var property = allPropertiesExceptKeyAndComputed.ElementAt(i); var property = allPropertiesExceptKeyAndComputed.ElementAt(i);
sbColumnList.AppendFormat("[{0}]", property.Name); sbColumnList.AppendFormat("[{0}]", property.Name);
if (i < allPropertiesExceptKeyAndComputed.Count() - 1) if (i < allPropertiesExceptKeyAndComputed.Count - 1)
sbColumnList.Append(", "); sbColumnList.Append(", ");
} }
var sbParameterList = new StringBuilder(null); var sbParameterList = new StringBuilder(null);
for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count(); i++) for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++)
{ {
var property = allPropertiesExceptKeyAndComputed.ElementAt(i); var property = allPropertiesExceptKeyAndComputed.ElementAt(i);
sbParameterList.AppendFormat("@{0}", property.Name); sbParameterList.AppendFormat("@{0}", property.Name);
if (i < allPropertiesExceptKeyAndComputed.Count() - 1) if (i < allPropertiesExceptKeyAndComputed.Count - 1)
sbParameterList.Append(", "); sbParameterList.Append(", ");
} }
...@@ -194,6 +199,8 @@ public static partial class SqlMapperExtensions ...@@ -194,6 +199,8 @@ public static partial class SqlMapperExtensions
/// <typeparam name="T">Type to be updated</typeparam> /// <typeparam name="T">Type to be updated</typeparam>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="entityToUpdate">Entity to be updated</param> /// <param name="entityToUpdate">Entity to be updated</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>true if updated, false if not found or not modified (tracked entities)</returns> /// <returns>true if updated, false if not found or not modified (tracked entities)</returns>
public static async Task<bool> UpdateAsync<T>(this IDbConnection connection, T entityToUpdate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static async Task<bool> UpdateAsync<T>(this IDbConnection connection, T entityToUpdate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
...@@ -223,19 +230,19 @@ public static partial class SqlMapperExtensions ...@@ -223,19 +230,19 @@ public static partial class SqlMapperExtensions
var computedProperties = ComputedPropertiesCache(type); var computedProperties = ComputedPropertiesCache(type);
var nonIdProps = allProperties.Except(keyProperties.Union(computedProperties)).ToList(); var nonIdProps = allProperties.Except(keyProperties.Union(computedProperties)).ToList();
for (var i = 0; i < nonIdProps.Count(); i++) for (var i = 0; i < nonIdProps.Count; i++)
{ {
var property = nonIdProps.ElementAt(i); var property = nonIdProps.ElementAt(i);
sb.AppendFormat("{0} = @{1}", property.Name, property.Name); sb.AppendFormat("{0} = @{1}", property.Name, property.Name);
if (i < nonIdProps.Count() - 1) if (i < nonIdProps.Count - 1)
sb.AppendFormat(", "); sb.AppendFormat(", ");
} }
sb.Append(" where "); sb.Append(" where ");
for (var i = 0; i < keyProperties.Count(); i++) for (var i = 0; i < keyProperties.Count; i++)
{ {
var property = keyProperties.ElementAt(i); var property = keyProperties.ElementAt(i);
sb.AppendFormat("{0} = @{1}", property.Name, property.Name); sb.AppendFormat("{0} = @{1}", property.Name, property.Name);
if (i < keyProperties.Count() - 1) if (i < keyProperties.Count - 1)
sb.AppendFormat(" and "); sb.AppendFormat(" and ");
} }
var updated = await connection.ExecuteAsync(sb.ToString(), entityToUpdate, commandTimeout: commandTimeout, transaction: transaction).ConfigureAwait(false); var updated = await connection.ExecuteAsync(sb.ToString(), entityToUpdate, commandTimeout: commandTimeout, transaction: transaction).ConfigureAwait(false);
...@@ -248,11 +255,13 @@ public static partial class SqlMapperExtensions ...@@ -248,11 +255,13 @@ public static partial class SqlMapperExtensions
/// <typeparam name="T">Type of entity</typeparam> /// <typeparam name="T">Type of entity</typeparam>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="entityToDelete">Entity to delete</param> /// <param name="entityToDelete">Entity to delete</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>true if deleted, false if not found</returns> /// <returns>true if deleted, false if not found</returns>
public static async Task<bool> DeleteAsync<T>(this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static async Task<bool> DeleteAsync<T>(this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
if (entityToDelete == null) if (entityToDelete == null)
throw new ArgumentException("Cannot Delete null Object", "entityToDelete"); throw new ArgumentException("Cannot Delete null Object", nameof(entityToDelete));
var type = typeof(T); var type = typeof(T);
...@@ -268,14 +277,14 @@ public static partial class SqlMapperExtensions ...@@ -268,14 +277,14 @@ public static partial class SqlMapperExtensions
keyProperties.AddRange(explicitKeyProperties); keyProperties.AddRange(explicitKeyProperties);
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendFormat("delete from {0} where ", name); sb.AppendFormat("DELETE FROM {0} WHERE ", name);
for (var i = 0; i < keyProperties.Count(); i++) for (var i = 0; i < keyProperties.Count; i++)
{ {
var property = keyProperties.ElementAt(i); var property = keyProperties.ElementAt(i);
sb.AppendFormat("{0} = @{1}", property.Name, property.Name); sb.AppendFormat("{0} = @{1}", property.Name, property.Name);
if (i < keyProperties.Count() - 1) if (i < keyProperties.Count - 1)
sb.AppendFormat(" and "); sb.AppendFormat(" AND ");
} }
var deleted = await connection.ExecuteAsync(sb.ToString(), entityToDelete, transaction, commandTimeout).ConfigureAwait(false); var deleted = await connection.ExecuteAsync(sb.ToString(), entityToDelete, transaction, commandTimeout).ConfigureAwait(false);
return deleted > 0; return deleted > 0;
...@@ -286,12 +295,14 @@ public static partial class SqlMapperExtensions ...@@ -286,12 +295,14 @@ public static partial class SqlMapperExtensions
/// </summary> /// </summary>
/// <typeparam name="T">Type of entity</typeparam> /// <typeparam name="T">Type of entity</typeparam>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>true if deleted, false if none found</returns> /// <returns>true if deleted, false if none found</returns>
public static async Task<bool> DeleteAllAsync<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static async Task<bool> DeleteAllAsync<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
var type = typeof(T); var type = typeof(T);
var name = GetTableName(type); var name = GetTableName(type);
var statement = String.Format("delete from {0}", name); var statement = $"DELETE FROM {name}";
var deleted = await connection.ExecuteAsync(statement, null, transaction, commandTimeout).ConfigureAwait(false); var deleted = await connection.ExecuteAsync(statement, null, transaction, commandTimeout).ConfigureAwait(false);
return deleted > 0; return deleted > 0;
} }
...@@ -328,9 +339,9 @@ public partial class SqlCeServerAdapter ...@@ -328,9 +339,9 @@ public partial class SqlCeServerAdapter
{ {
public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert) public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert)
{ {
var cmd = $"INSERT INTO {tableName} ({columnList}) values ({parameterList})"; var cmd = $"INSERT INTO {tableName} ({columnList}) VALUES ({parameterList})";
await connection.ExecuteAsync(cmd, entityToInsert, transaction, commandTimeout).ConfigureAwait(false); await connection.ExecuteAsync(cmd, entityToInsert, transaction, commandTimeout).ConfigureAwait(false);
var r = (await connection.QueryAsync<dynamic>("select @@IDENTITY id", transaction: transaction, commandTimeout: commandTimeout).ConfigureAwait(false)).ToList(); var r = (await connection.QueryAsync<dynamic>("SELECT @@IDENTITY id", transaction: transaction, commandTimeout: commandTimeout).ConfigureAwait(false)).ToList();
if (r.First() == null || r.First().id == null) return 0; if (r.First() == null || r.First().id == null) return 0;
var id = (int)r.First().id; var id = (int)r.First().id;
...@@ -352,7 +363,7 @@ public partial class MySqlAdapter ...@@ -352,7 +363,7 @@ public partial class MySqlAdapter
{ {
var cmd = $"INSERT INTO {tableName} ({columnList}) VALUES ({parameterList})"; var cmd = $"INSERT INTO {tableName} ({columnList}) VALUES ({parameterList})";
await connection.ExecuteAsync(cmd, entityToInsert, transaction, commandTimeout).ConfigureAwait(false); await connection.ExecuteAsync(cmd, entityToInsert, transaction, commandTimeout).ConfigureAwait(false);
var r = await connection.QueryAsync<dynamic>("SELECT LAST_INSERT_ID()", transaction: transaction, commandTimeout: commandTimeout).ConfigureAwait(false); var r = await connection.QueryAsync<dynamic>("SELECT LAST_INSERT_ID() id", transaction: transaction, commandTimeout: commandTimeout).ConfigureAwait(false);
var id = r.First().id; var id = r.First().id;
if (id == null) return 0; if (id == null) return 0;
...@@ -362,13 +373,12 @@ public partial class MySqlAdapter ...@@ -362,13 +373,12 @@ public partial class MySqlAdapter
var idp = pi.First(); var idp = pi.First();
idp.SetValue(entityToInsert, Convert.ChangeType(id, idp.PropertyType), null); idp.SetValue(entityToInsert, Convert.ChangeType(id, idp.PropertyType), null);
return id; return (int)id;
} }
} }
public partial class PostgresAdapter public partial class PostgresAdapter
{ {
public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert) public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert)
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
...@@ -391,13 +401,14 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran ...@@ -391,13 +401,14 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran
} }
} }
var results = await connection.QueryAsync<dynamic>(sb.ToString(), entityToInsert, transaction, commandTimeout).ConfigureAwait(false); var results = await connection.QueryAsync(sb.ToString(), entityToInsert, transaction, commandTimeout).ConfigureAwait(false);
// Return the key by assinging the corresponding property in the object - by product is that it supports compound primary keys // Return the key by assinging the corresponding property in the object - by product is that it supports compound primary keys
var id = 0; var id = 0;
var values = results.First();
foreach (var p in propertyInfos) foreach (var p in propertyInfos)
{ {
var value = ((IDictionary<string, object>)results.First())[p.Name.ToLower()]; var value = values[p.Name.ToLower()];
p.SetValue(entityToInsert, value, null); p.SetValue(entityToInsert, value, null);
if (id == 0) if (id == 0)
id = Convert.ToInt32(value); id = Convert.ToInt32(value);
......
...@@ -17,11 +17,8 @@ ...@@ -17,11 +17,8 @@
using System.Threading; using System.Threading;
#endif #endif
#pragma warning disable 1573, 1591 // xml comments
namespace Dapper.Contrib.Extensions namespace Dapper.Contrib.Extensions
{ {
public static partial class SqlMapperExtensions public static partial class SqlMapperExtensions
{ {
// ReSharper disable once MemberCanBePrivate.Global // ReSharper disable once MemberCanBePrivate.Global
...@@ -45,13 +42,15 @@ public interface ITableNameMapper ...@@ -45,13 +42,15 @@ public interface ITableNameMapper
private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> GetQueries = new ConcurrentDictionary<RuntimeTypeHandle, string>(); private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> GetQueries = new ConcurrentDictionary<RuntimeTypeHandle, string>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> TypeTableName = new ConcurrentDictionary<RuntimeTypeHandle, string>(); private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> TypeTableName = new ConcurrentDictionary<RuntimeTypeHandle, string>();
private static readonly Dictionary<string, ISqlAdapter> AdapterDictionary = new Dictionary<string, ISqlAdapter> { private static readonly Dictionary<string, ISqlAdapter> AdapterDictionary
{"sqlconnection", new SqlServerAdapter()}, = new Dictionary<string, ISqlAdapter>
{"sqlceconnection", new SqlCeServerAdapter()}, {
{"npgsqlconnection", new PostgresAdapter()}, {"sqlconnection", new SqlServerAdapter()},
{"sqliteconnection", new SQLiteAdapter()}, {"sqlceconnection", new SqlCeServerAdapter()},
{"mysqlconnection", new MySqlAdapter()}, {"npgsqlconnection", new PostgresAdapter()},
}; {"sqliteconnection", new SQLiteAdapter()},
{"mysqlconnection", new MySqlAdapter()},
};
private static List<PropertyInfo> ComputedPropertiesCache(Type type) private static List<PropertyInfo> ComputedPropertiesCache(Type type)
{ {
...@@ -137,6 +136,8 @@ private static bool IsWriteable(PropertyInfo pi) ...@@ -137,6 +136,8 @@ private static bool IsWriteable(PropertyInfo pi)
/// <typeparam name="T">Interface or type to create and populate</typeparam> /// <typeparam name="T">Interface or type to create and populate</typeparam>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="id">Id of the entity to get, must be marked with [Key] attribute</param> /// <param name="id">Id of the entity to get, must be marked with [Key] attribute</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>Entity of T</returns> /// <returns>Entity of T</returns>
public static T Get<T>(this IDbConnection connection, dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static T Get<T>(this IDbConnection connection, dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
...@@ -198,6 +199,8 @@ private static bool IsWriteable(PropertyInfo pi) ...@@ -198,6 +199,8 @@ private static bool IsWriteable(PropertyInfo pi)
/// </summary> /// </summary>
/// <typeparam name="T">Interface or type to create and populate</typeparam> /// <typeparam name="T">Interface or type to create and populate</typeparam>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>Entity of T</returns> /// <returns>Entity of T</returns>
public static IEnumerable<T> GetAll<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static IEnumerable<T> GetAll<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
...@@ -282,6 +285,8 @@ private static string GetTableName(Type type) ...@@ -282,6 +285,8 @@ private static string GetTableName(Type type)
/// </summary> /// </summary>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="entityToInsert">Entity to insert, can be list of entities</param> /// <param name="entityToInsert">Entity to insert, can be list of entities</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>Identity of inserted entity, or number of inserted rows if inserting a list</returns> /// <returns>Identity of inserted entity, or number of inserted rows if inserting a list</returns>
public static long Insert<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static long Insert<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
...@@ -346,6 +351,8 @@ private static string GetTableName(Type type) ...@@ -346,6 +351,8 @@ private static string GetTableName(Type type)
/// <typeparam name="T">Type to be updated</typeparam> /// <typeparam name="T">Type to be updated</typeparam>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="entityToUpdate">Entity to be updated</param> /// <param name="entityToUpdate">Entity to be updated</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>true if updated, false if not found or not modified (tracked entities)</returns> /// <returns>true if updated, false if not found or not modified (tracked entities)</returns>
public static bool Update<T>(this IDbConnection connection, T entityToUpdate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static bool Update<T>(this IDbConnection connection, T entityToUpdate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
...@@ -402,6 +409,8 @@ private static string GetTableName(Type type) ...@@ -402,6 +409,8 @@ private static string GetTableName(Type type)
/// <typeparam name="T">Type of entity</typeparam> /// <typeparam name="T">Type of entity</typeparam>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="entityToDelete">Entity to delete</param> /// <param name="entityToDelete">Entity to delete</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>true if deleted, false if not found</returns> /// <returns>true if deleted, false if not found</returns>
public static bool Delete<T>(this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static bool Delete<T>(this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
...@@ -442,6 +451,8 @@ private static string GetTableName(Type type) ...@@ -442,6 +451,8 @@ private static string GetTableName(Type type)
/// </summary> /// </summary>
/// <typeparam name="T">Type of entity</typeparam> /// <typeparam name="T">Type of entity</typeparam>
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="transaction">The transaction to run under, null (the defualt) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>true if deleted, false if none found</returns> /// <returns>true if deleted, false if none found</returns>
public static bool DeleteAll<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static bool DeleteAll<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
...@@ -666,7 +677,7 @@ public class ComputedAttribute : Attribute ...@@ -666,7 +677,7 @@ public class ComputedAttribute : Attribute
public partial interface ISqlAdapter public partial interface ISqlAdapter
{ {
int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert); int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert);
//new methods for issue #336 //new methods for issue #336
void AppendColumnName(StringBuilder sb, string columnName); void AppendColumnName(StringBuilder sb, string columnName);
...@@ -675,7 +686,7 @@ public partial interface ISqlAdapter ...@@ -675,7 +686,7 @@ public partial interface ISqlAdapter
public partial class SqlServerAdapter : ISqlAdapter public partial class SqlServerAdapter : ISqlAdapter
{ {
public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert) public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert)
{ {
var cmd = $"insert into {tableName} ({columnList}) values ({parameterList});select SCOPE_IDENTITY() id"; var cmd = $"insert into {tableName} ({columnList}) values ({parameterList});select SCOPE_IDENTITY() id";
var multi = connection.QueryMultiple(cmd, entityToInsert, transaction, commandTimeout); var multi = connection.QueryMultiple(cmd, entityToInsert, transaction, commandTimeout);
...@@ -706,7 +717,7 @@ public void AppendColumnNameEqualsValue(StringBuilder sb, string columnName) ...@@ -706,7 +717,7 @@ public void AppendColumnNameEqualsValue(StringBuilder sb, string columnName)
public partial class SqlCeServerAdapter : ISqlAdapter public partial class SqlCeServerAdapter : ISqlAdapter
{ {
public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert) public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert)
{ {
var cmd = $"insert into {tableName} ({columnList}) values ({parameterList})"; var cmd = $"insert into {tableName} ({columnList}) values ({parameterList})";
connection.Execute(cmd, entityToInsert, transaction, commandTimeout); connection.Execute(cmd, entityToInsert, transaction, commandTimeout);
...@@ -737,7 +748,7 @@ public void AppendColumnNameEqualsValue(StringBuilder sb, string columnName) ...@@ -737,7 +748,7 @@ public void AppendColumnNameEqualsValue(StringBuilder sb, string columnName)
public partial class MySqlAdapter : ISqlAdapter public partial class MySqlAdapter : ISqlAdapter
{ {
public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert) public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert)
{ {
var cmd = $"insert into {tableName} ({columnList}) values ({parameterList})"; var cmd = $"insert into {tableName} ({columnList}) values ({parameterList})";
connection.Execute(cmd, entityToInsert, transaction, commandTimeout); connection.Execute(cmd, entityToInsert, transaction, commandTimeout);
...@@ -768,7 +779,7 @@ public void AppendColumnNameEqualsValue(StringBuilder sb, string columnName) ...@@ -768,7 +779,7 @@ public void AppendColumnNameEqualsValue(StringBuilder sb, string columnName)
public partial class PostgresAdapter : ISqlAdapter public partial class PostgresAdapter : ISqlAdapter
{ {
public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert) public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert)
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendFormat("insert into {0} ({1}) values ({2})", tableName, columnList, parameterList); sb.AppendFormat("insert into {0} ({1}) values ({2})", tableName, columnList, parameterList);
......
...@@ -33,9 +33,10 @@ public override void SetValue(IDbDataParameter parameter, DbGeography value) ...@@ -33,9 +33,10 @@ public override void SetValue(IDbDataParameter parameter, DbGeography value)
parsed = SqlGeography.STGeomFromWKB(new SqlBytes(value.AsBinary()), value.CoordinateSystemId); parsed = SqlGeography.STGeomFromWKB(new SqlBytes(value.AsBinary()), value.CoordinateSystemId);
} }
parameter.Value = parsed ?? DBNull.Value; parameter.Value = parsed ?? DBNull.Value;
if (parameter is SqlParameter) var sqlParameter = parameter as SqlParameter;
if (sqlParameter != null)
{ {
((SqlParameter)parameter).UdtTypeName = "geography"; sqlParameter.UdtTypeName = "geography";
} }
} }
/// <summary> /// <summary>
......
...@@ -10,7 +10,7 @@ namespace Dapper.EntityFramework ...@@ -10,7 +10,7 @@ namespace Dapper.EntityFramework
/// <summary> /// <summary>
/// Type-handler for the DbGeometry spatial type /// Type-handler for the DbGeometry spatial type
/// </summary> /// </summary>
public class DbGeometryHandler : Dapper.SqlMapper.TypeHandler<DbGeometry> public class DbGeometryHandler : SqlMapper.TypeHandler<DbGeometry>
{ {
/// <summary> /// <summary>
/// Create a new handler instance /// Create a new handler instance
......
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace SqlServerTypes
{
/// <summary>
/// Utility methods related to CLR Types for SQL Server
/// </summary>
internal class Utilities
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr LoadLibrary(string libname);
/// <summary>
/// Loads the required native assemblies for the current architecture (x86 or x64)
/// </summary>
/// <param name="rootApplicationPath">
/// Root path of the current application. Use Server.MapPath(".") for ASP.NET applications
/// and AppDomain.CurrentDomain.BaseDirectory for desktop applications.
/// </param>
public static void LoadNativeAssemblies(string rootApplicationPath)
{
var nativeBinaryPath = IntPtr.Size > 4
? Path.Combine(rootApplicationPath, @"SqlServerTypes\x64\")
: Path.Combine(rootApplicationPath, @"SqlServerTypes\x86\");
LoadNativeAssembly(nativeBinaryPath, "msvcr100.dll");
LoadNativeAssembly(nativeBinaryPath, "SqlServerSpatial110.dll");
}
private static void LoadNativeAssembly(string nativeBinaryPath, string assemblyName)
{
var path = Path.Combine(nativeBinaryPath, assemblyName);
var ptr = LoadLibrary(path);
if (ptr == IntPtr.Zero)
{
throw new Exception(string.Format(
"Error loading {0} (ErrorCode: {1})",
assemblyName,
Marshal.GetLastWin32Error()));
}
}
}
}
\ No newline at end of file
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>Microsoft.SqlServer.Types</title>
<style>
body {
background: #fff;
color: #505050;
margin: 20px;
}
#main {
background: #efefef;
padding: 5px 30px;
}
</style>
</head>
<body>
<div id="main">
<h1>Action required to load native assemblies</h1>
<p>
To deploy an application that uses spatial data types to a machine that does not have 'System CLR Types for SQL Server' installed you also need to deploy the native assembly SqlServerSpatial110.dll. Both x86 (32 bit) and x64 (64 bit) versions of this assembly have been added to your project under the SqlServerTypes\x86 and SqlServerTypes\x64 subdirectories. The native assembly msvcr100.dll is also included in case the C++ runtime is not installed.
</p>
<p>
You need to add code to load the correct one of these assemblies at runtime (depending on the current architecture).
</p>
<h2>ASP.NET applications</h2>
<p>
For ASP.NET applications, add the following line of code to the Application_Start method in Global.asax.cs:
<pre> SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));</pre>
</p>
<h2>Desktop applications</h2>
<p>
For desktop applications, add the following line of code to run before any spatial operations are performed:
<pre> SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);</pre>
</p>
</div>
</body>
</html>
\ No newline at end of file
...@@ -161,8 +161,8 @@ public static TDatabase Init(DbConnection connection, int commandTimeout) ...@@ -161,8 +161,8 @@ public static TDatabase Init(DbConnection connection, int commandTimeout)
internal void InitDatabase(DbConnection connection, int commandTimeout) internal void InitDatabase(DbConnection connection, int commandTimeout)
{ {
this._connection = connection; _connection = connection;
this._commandTimeout = commandTimeout; _commandTimeout = commandTimeout;
if (tableConstructor == null) if (tableConstructor == null)
{ {
tableConstructor = CreateTableConstructorForTable(); tableConstructor = CreateTableConstructorForTable();
...@@ -204,13 +204,7 @@ protected Action<TDatabase> CreateTableConstructor(params Type[] tableTypes) ...@@ -204,13 +204,7 @@ protected Action<TDatabase> CreateTableConstructor(params Type[] tableTypes)
var il = dm.GetILGenerator(); var il = dm.GetILGenerator();
var setters = GetType().GetProperties() var setters = GetType().GetProperties()
.Where( .Where(p => p.PropertyType.IsGenericType() && tableTypes.Contains(p.PropertyType.GetGenericTypeDefinition()))
#if COREFX
p => p.PropertyType.GetTypeInfo().IsGenericType && tableTypes.Contains(p.PropertyType.GetGenericTypeDefinition())
#else
p => p.PropertyType.IsGenericType && tableTypes.Contains(p.PropertyType.GetGenericTypeDefinition())
#endif
)
.Select(p => Tuple.Create( .Select(p => Tuple.Create(
p.GetSetMethod(true), p.GetSetMethod(true),
p.PropertyType.GetConstructor(new[] { typeof(TDatabase), typeof(string) }), p.PropertyType.GetConstructor(new[] { typeof(TDatabase), typeof(string) }),
...@@ -286,48 +280,48 @@ private bool TableExists(string name) ...@@ -286,48 +280,48 @@ private bool TableExists(string name)
} }
var builder = new StringBuilder("select 1 from INFORMATION_SCHEMA.TABLES where "); var builder = new StringBuilder("select 1 from INFORMATION_SCHEMA.TABLES where ");
if (!String.IsNullOrEmpty(schemaName)) builder.Append("TABLE_SCHEMA = @schemaName AND "); if (!string.IsNullOrEmpty(schemaName)) builder.Append("TABLE_SCHEMA = @schemaName AND ");
builder.Append("TABLE_NAME = @name"); builder.Append("TABLE_NAME = @name");
return _connection.Query(builder.ToString(), new { schemaName, name }, transaction: _transaction).Count() == 1; return _connection.Query(builder.ToString(), new { schemaName, name }, _transaction).Count() == 1;
} }
public int Execute(string sql, dynamic param = null) public int Execute(string sql, dynamic param = null)
{ {
return SqlMapper.Execute(_connection, sql, param as object, _transaction, commandTimeout: this._commandTimeout); return _connection.Execute(sql, param as object, _transaction, _commandTimeout);
} }
public IEnumerable<T> Query<T>(string sql, dynamic param = null, bool buffered = true) public IEnumerable<T> Query<T>(string sql, dynamic param = null, bool buffered = true)
{ {
return SqlMapper.Query<T>(_connection, sql, param as object, _transaction, buffered, _commandTimeout); return _connection.Query<T>(sql, param as object, _transaction, buffered, _commandTimeout);
} }
public IEnumerable<TReturn> Query<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) public IEnumerable<TReturn> Query<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
{ {
return SqlMapper.Query(_connection, sql, map, param as object, transaction, buffered, splitOn); return _connection.Query(sql, map, param as object, transaction, buffered, splitOn);
} }
public IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TReturn>(string sql, Func<TFirst, TSecond, TThird, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) public IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TReturn>(string sql, Func<TFirst, TSecond, TThird, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
{ {
return SqlMapper.Query(_connection, sql, map, param as object, transaction, buffered, splitOn); return _connection.Query(sql, map, param as object, transaction, buffered, splitOn);
} }
public IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TReturn>(string sql, Func<TFirst, TSecond, TThird, TFourth, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) public IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TReturn>(string sql, Func<TFirst, TSecond, TThird, TFourth, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
{ {
return SqlMapper.Query(_connection, sql, map, param as object, transaction, buffered, splitOn); return _connection.Query(sql, map, param as object, transaction, buffered, splitOn);
} }
public IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(string sql, Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) public IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(string sql, Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
{ {
return SqlMapper.Query(_connection, sql, map, param as object, transaction, buffered, splitOn); return _connection.Query(sql, map, param as object, transaction, buffered, splitOn);
} }
public IEnumerable<dynamic> Query(string sql, dynamic param = null, bool buffered = true) public IEnumerable<dynamic> Query(string sql, dynamic param = null, bool buffered = true)
{ {
return SqlMapper.Query(_connection, sql, param as object, _transaction, buffered); return _connection.Query(sql, param as object, _transaction, buffered);
} }
public Dapper.SqlMapper.GridReader QueryMultiple(string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) public SqlMapper.GridReader QueryMultiple(string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
{ {
return SqlMapper.QueryMultiple(_connection, sql, param, transaction, commandTimeout, commandType); return SqlMapper.QueryMultiple(_connection, sql, param, transaction, commandTimeout, commandType);
} }
......
...@@ -68,17 +68,20 @@ static List<PropertyInfo> RelevantProperties() ...@@ -68,17 +68,20 @@ static List<PropertyInfo> RelevantProperties()
).ToList(); ).ToList();
} }
// This is used by IL, ReSharper is wrong.
// ReSharper disable UnusedMember.Local
private static bool AreEqual<U>(U first, U second) private static bool AreEqual<U>(U first, U second)
{ {
if (first == null && second == null) return true; if (first == null && second == null) return true;
if (first == null) return false; if (first == null) return false;
return first.Equals(second); return first.Equals(second);
} }
// ReSharper restore UnusedMember.Local
private static Func<T, T, List<Change>> GenerateDiffer() private static Func<T, T, List<Change>> GenerateDiffer()
{ {
var dm = new DynamicMethod("DoDiff", typeof(List<Change>), new Type[] { typeof(T), typeof(T) }, true); var dm = new DynamicMethod("DoDiff", typeof(List<Change>), new[] { typeof(T), typeof(T) }, true);
var il = dm.GetILGenerator(); var il = dm.GetILGenerator();
// change list // change list
...@@ -198,4 +201,3 @@ private static bool AreEqual<U>(U first, U second) ...@@ -198,4 +201,3 @@ private static bool AreEqual<U>(U first, U second)
} }
} }
} }
...@@ -6,8 +6,8 @@ namespace Dapper ...@@ -6,8 +6,8 @@ namespace Dapper
{ {
public class SqlBuilder public class SqlBuilder
{ {
Dictionary<string, Clauses> data = new Dictionary<string, Clauses>(); private readonly Dictionary<string, Clauses> _data = new Dictionary<string, Clauses>();
int seq; private int _seq;
class Clause class Clause
{ {
...@@ -18,15 +18,15 @@ class Clause ...@@ -18,15 +18,15 @@ class Clause
class Clauses : List<Clause> class Clauses : List<Clause>
{ {
string joiner; private readonly string _joiner;
string prefix; private readonly string _prefix;
string postfix; private readonly string _postfix;
public Clauses(string joiner, string prefix = "", string postfix = "") public Clauses(string joiner, string prefix = "", string postfix = "")
{ {
this.joiner = joiner; _joiner = joiner;
this.prefix = prefix; _prefix = prefix;
this.postfix = postfix; _postfix = postfix;
} }
public string ResolveClauses(DynamicParameters p) public string ResolveClauses(DynamicParameters p)
...@@ -36,8 +36,8 @@ public string ResolveClauses(DynamicParameters p) ...@@ -36,8 +36,8 @@ public string ResolveClauses(DynamicParameters p)
p.AddDynamicParams(item.Parameters); p.AddDynamicParams(item.Parameters);
} }
return this.Any(a => a.IsInclusive) return this.Any(a => a.IsInclusive)
? prefix + ? _prefix +
string.Join(joiner, string.Join(_joiner,
this.Where(a => !a.IsInclusive) this.Where(a => !a.IsInclusive)
.Select(c => c.Sql) .Select(c => c.Sql)
.Union(new[] .Union(new[]
...@@ -45,46 +45,45 @@ public string ResolveClauses(DynamicParameters p) ...@@ -45,46 +45,45 @@ public string ResolveClauses(DynamicParameters p)
" ( " + " ( " +
string.Join(" OR ", this.Where(a => a.IsInclusive).Select(c => c.Sql).ToArray()) + string.Join(" OR ", this.Where(a => a.IsInclusive).Select(c => c.Sql).ToArray()) +
" ) " " ) "
}).ToArray()) + postfix }).ToArray()) + _postfix
: prefix + string.Join(joiner, this.Select(c => c.Sql).ToArray()) + postfix; : _prefix + string.Join(_joiner, this.Select(c => c.Sql).ToArray()) + _postfix;
} }
} }
public class Template public class Template
{ {
readonly string sql; private readonly string _sql;
readonly SqlBuilder builder; private readonly SqlBuilder _builder;
readonly object initParams; private readonly object _initParams;
int dataSeq = -1; // Unresolved private int _dataSeq = -1; // Unresolved
public Template(SqlBuilder builder, string sql, dynamic parameters) public Template(SqlBuilder builder, string sql, dynamic parameters)
{ {
this.initParams = parameters; _initParams = parameters;
this.sql = sql; _sql = sql;
this.builder = builder; _builder = builder;
} }
static Regex regex = private static readonly Regex _regex = new Regex(@"\/\*\*.+\*\*\/", RegexOptions.Compiled | RegexOptions.Multiline);
new System.Text.RegularExpressions.Regex(@"\/\*\*.+\*\*\/", System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.Multiline);
void ResolveSql() void ResolveSql()
{ {
if (dataSeq != builder.seq) if (_dataSeq != _builder._seq)
{ {
DynamicParameters p = new DynamicParameters(initParams); DynamicParameters p = new DynamicParameters(_initParams);
rawSql = sql; rawSql = _sql;
foreach (var pair in builder.data) foreach (var pair in _builder._data)
{ {
rawSql = rawSql.Replace("/**" + pair.Key + "**/", pair.Value.ResolveClauses(p)); rawSql = rawSql.Replace("/**" + pair.Key + "**/", pair.Value.ResolveClauses(p));
} }
parameters = p; parameters = p;
// replace all that is left with empty // replace all that is left with empty
rawSql = regex.Replace(rawSql, ""); rawSql = _regex.Replace(rawSql, "");
dataSeq = builder.seq; _dataSeq = _builder._seq;
} }
} }
...@@ -95,8 +94,6 @@ void ResolveSql() ...@@ -95,8 +94,6 @@ void ResolveSql()
public object Parameters { get { ResolveSql(); return parameters; } } public object Parameters { get { ResolveSql(); return parameters; } }
} }
public SqlBuilder() { }
public Template AddTemplate(string sql, dynamic parameters = null) public Template AddTemplate(string sql, dynamic parameters = null)
{ {
return new Template(this, sql, parameters); return new Template(this, sql, parameters);
...@@ -105,13 +102,13 @@ public Template AddTemplate(string sql, dynamic parameters = null) ...@@ -105,13 +102,13 @@ public Template AddTemplate(string sql, dynamic parameters = null)
protected void AddClause(string name, string sql, object parameters, string joiner, string prefix = "", string postfix = "", bool isInclusive = false) protected void AddClause(string name, string sql, object parameters, string joiner, string prefix = "", string postfix = "", bool isInclusive = false)
{ {
Clauses clauses; Clauses clauses;
if (!data.TryGetValue(name, out clauses)) if (!_data.TryGetValue(name, out clauses))
{ {
clauses = new Clauses(joiner, prefix, postfix); clauses = new Clauses(joiner, prefix, postfix);
data[name] = clauses; _data[name] = clauses;
} }
clauses.Add(new Clause { Sql = sql, Parameters = parameters, IsInclusive = isInclusive }); clauses.Add(new Clause { Sql = sql, Parameters = parameters, IsInclusive = isInclusive });
seq++; _seq++;
} }
public SqlBuilder Intersect(string sql, dynamic parameters = null) public SqlBuilder Intersect(string sql, dynamic parameters = null)
......
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CE/@EntryIndexedValue">CE</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CE/@EntryIndexedValue">CE</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SQ/@EntryIndexedValue">SQ</s:String></wpf:ResourceDictionary> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SQ/@EntryIndexedValue">SQ</s:String>
\ No newline at end of file <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SQL/@EntryIndexedValue">SQL</s:String></wpf:ResourceDictionary>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment