private static long TablePerTypeInsert <T>(IDbConnection connection, T entityToInsert, IDbTransaction transaction, int?commandTimeout) where T : class { var type = typeof(T); var parentType = type.BaseType; ISqlAdapter adapter = GetFormatter(connection); #region Insert the parent class var parentName = GetTableName(parentType); var parentProperties = TypePropertiesCache(parentType); var parentKeyProperties = KeyPropertiesCache(parentType); var parentComputedProperties = ComputedPropertiesCache(parentType); var parentAllPropertiesExceptKeyAndComputed = parentProperties.Except(parentKeyProperties.Union(parentComputedProperties)).ToArray(); var parentColumnList = GenerateColumnList <T>(parentAllPropertiesExceptKeyAndComputed, adapter); var parentParameterList = GenerateParameterList <T>(parentAllPropertiesExceptKeyAndComputed); #if DEBUG foreach (var key in parentKeyProperties) { var value = key.GetValue(entityToInsert); if (value is int && (int)value != 0) { throw new Exception("Trying to Insert but the [Id] is not 0"); } else if (value is long && (long)value != 0L) { throw new Exception("Trying to Insert but the [Id] is not 0"); } } #endif long id = adapter.Insert(connection, transaction, commandTimeout, parentName, parentColumnList, parentParameterList, parentKeyProperties, entityToInsert); #endregion #region Insert the child class var name = GetTableName(type); var allProperties = TypePropertiesCache(type); //Child class shouldn't have a Key directly, it is inherited from the parent and needs to be inserted here var computedProperties = ComputedPropertiesCache(type); var allPropertiesIncludingKeyExceptComputed = allProperties.Except(computedProperties).ToArray(); var columnList = GenerateColumnList <T>(allPropertiesIncludingKeyExceptComputed, adapter); var parameterList = GenerateParameterList <T>(allPropertiesIncludingKeyExceptComputed); adapter.Insert(connection, transaction, commandTimeout, name, columnList, parameterList, Enumerable.Empty <PropertyInfo>(), entityToInsert); #endregion return(id); }
/// <summary> /// Inserts an entity into table "Ts" and returns identity id. /// </summary> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToInsert">Entity to insert</param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns>Identity of inserted entity</returns> public static long Insert <T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { Type type = typeof(T); string name = GetTableName(type); StringBuilder sbColumnList = new StringBuilder(null); IEnumerable <PropertyInfo> allProperties = TypePropertiesCache(type); IEnumerable <PropertyInfo> keyProperties = KeyPropertiesCache(type); PropertyInfo[] propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray(); IEnumerable <PropertyInfo> allPropertiesExceptKey = allProperties.Except(propertyInfos); PropertyInfo[] propertiesExceptKey = allPropertiesExceptKey as PropertyInfo[] ?? allPropertiesExceptKey.ToArray(); for (int i = 0; i < propertiesExceptKey.Count(); i++) { PropertyInfo property = propertiesExceptKey.ElementAt(i); sbColumnList.AppendFormat("[{0}]", property.Name); if (i < propertiesExceptKey.Count() - 1) { sbColumnList.Append(", "); } } StringBuilder sbParameterList = new StringBuilder(null); for (int i = 0; i < propertiesExceptKey.Count(); i++) { PropertyInfo property = propertiesExceptKey.ElementAt(i); sbParameterList.AppendFormat("@{0}", property.Name); if (i < propertiesExceptKey.Count() - 1) { sbParameterList.Append(", "); } } ISqlAdapter adapter = GetFormatter(connection); long id = 0; if (transaction == null) { id = adapter.Insert(connection, commandTimeout, name, sbColumnList.ToString(), sbParameterList.ToString(), propertyInfos, entityToInsert); } else { id = adapter.Insert(transaction, commandTimeout, name, sbColumnList.ToString(), sbParameterList.ToString(), propertyInfos, entityToInsert); } return(id); }
public string SqlString() { string sql = string.Empty; SqlEntity entity = GetSqlEntity(); switch (this._type) { case SqlType.Query: sql = _adapter.Query(entity); break; case SqlType.Insert: sql = _adapter.Insert(_userKey, entity); break; case SqlType.Update: sql = _adapter.Update(entity); break; case SqlType.Delete: sql = _adapter.Delete(entity); break; default: break; } return(sql); }
/// <summary> /// Inserts an entity into table "Ts" and returns identity id. /// </summary> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToInsert">Entity to insert</param> /// <returns>Identity of inserted entity</returns> public static long Insert<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class { var type = typeof(T); var name = GetTableName(type); var sbColumnList = new StringBuilder(null); var allProperties = TypePropertiesCache(type); var keyProperties = KeyPropertiesCache(type); var computedProperties = ComputedPropertiesCache(type); var allPropertiesExceptKeyAndComputed = allProperties.Except(keyProperties.Union(computedProperties)); for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count(); i++) { var property = allPropertiesExceptKeyAndComputed.ElementAt(i); sbColumnList.AppendFormat("[{0}]", property.Name); if (i < allPropertiesExceptKeyAndComputed.Count() - 1) sbColumnList.Append(", "); } var sbParameterList = new StringBuilder(null); for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count(); i++) { var property = allPropertiesExceptKeyAndComputed.ElementAt(i); sbParameterList.AppendFormat("@{0}", property.Name); if (i < allPropertiesExceptKeyAndComputed.Count() - 1) sbParameterList.Append(", "); } ISqlAdapter adapter = GetFormatter(connection); int id = adapter.Insert(connection, transaction, commandTimeout, name, sbColumnList.ToString(), sbParameterList.ToString(), keyProperties, entityToInsert); return id; }
/// <summary> /// Inserts an entity into table "Ts" and returns identity id. /// </summary> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToInsert">Entity to insert</param> /// <param name="transaction">The transaction scope</param> /// <param name="commandTimeout">A command timeout</param> /// <returns>Identity of inserted entity</returns> public static long Insert <T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { Type type = typeof(T); StringBuilder columnList = new StringBuilder(); List <PropertyInfo> allProperties = TypePropertiesCache(type).ToList(); PropertyInfo key = allProperties.First(prop => prop.Name.ToLowerInvariant() == "id"); List <PropertyInfo> allPropertiesExceptKey = allProperties.Except(new[] { key }).ToList(); for (int i = 0; i < allPropertiesExceptKey.Count; i++) { PropertyInfo property = allPropertiesExceptKey.ElementAt(i); columnList.Append(property.Name); if (i < allPropertiesExceptKey.Count - 1) { columnList.Append(", "); } } StringBuilder parameterList = new StringBuilder(); for (int i = 0; i < allPropertiesExceptKey.Count; i++) { PropertyInfo property = allPropertiesExceptKey.ElementAt(i); parameterList.AppendFormat("@{0}", property.Name); if (i < allPropertiesExceptKey.Count - 1) { parameterList.Append(", "); } } ISqlAdapter adapter = GetFormatter(connection); int id = adapter.Insert(connection, transaction, commandTimeout, type.Name, columnList.ToString(), parameterList.ToString(), key, entityToInsert); return(id); }
/// <summary> /// Inserts an entity into table "T". /// </summary> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToInsert">Entity to insert</param> public static void Insert <T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { // Argument Validation if (connection == null) { throw new ArgumentNullException("connection", "connection cannot be null"); } if (entityToInsert == null) { throw new ArgumentNullException("entityToInsert", "entityToInsert cannot be null"); } InitializeCacheSweeper(); var type = typeof(T); // Get TableName var name = GetTableName(type); // Build base sql var sbColumnList = new StringBuilder(null); // Get type details var allProperties = TypePropertiesCache(type); var keyProperties = KeyPropertiesCache(type); var computedProperties = ComputedPropertiesCache(type); var allPropertiesExceptComputed = allProperties.Except(computedProperties); // Build column list for (var i = 0; i < allPropertiesExceptComputed.Count(); i++) { var property = allPropertiesExceptComputed.ElementAt(i); sbColumnList.AppendFormat("[{0}]", property.Name); if (i < allPropertiesExceptComputed.Count() - 1) { sbColumnList.Append(", "); } } // Build parameter list var sbParameterList = new StringBuilder(null); for (var i = 0; i < allPropertiesExceptComputed.Count(); i++) { var property = allPropertiesExceptComputed.ElementAt(i); sbParameterList.AppendFormat("@{0}", property.Name); if (i < allPropertiesExceptComputed.Count() - 1) { sbParameterList.Append(", "); } } // Execute ISqlAdapter adapter = GetFormatter(connection); adapter.Insert(connection, transaction, commandTimeout, name, sbColumnList.ToString(), sbParameterList.ToString(), keyProperties, entityToInsert); }
/// <summary> /// Inserts an entity into table "Ts" and returns identity id or number if inserted rows if inserting a list. /// </summary> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToInsert">Entity to insert, can be list of entities</param> /// <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, ISqlAdapter sqlAdapter = null) where T : class { var isList = false; var type = typeof(T); if (type.IsArray || type.IsGenericType) { isList = true; type = type.GetGenericArguments()[0]; } var name = GetTableName(type); var sbColumnList = new StringBuilder(null); var allProperties = TypePropertiesCache(type); var keyProperties = KeyPropertiesCache(type); var computedProperties = ComputedPropertiesCache(type); var allPropertiesExceptKeyAndComputed = allProperties.Except(keyProperties.Union(computedProperties)).ToList(); for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count(); i++) { var property = allPropertiesExceptKeyAndComputed.ElementAt(i); sbColumnList.AppendFormat("[{0}]", property.Name); if (i < allPropertiesExceptKeyAndComputed.Count() - 1) { sbColumnList.Append(", "); } } var sbParameterList = new StringBuilder(null); for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count(); i++) { var property = allPropertiesExceptKeyAndComputed.ElementAt(i); sbParameterList.AppendFormat("@{0}", property.Name); if (i < allPropertiesExceptKeyAndComputed.Count() - 1) { sbParameterList.Append(", "); } } if (!isList) //single entity { if (sqlAdapter == null) { sqlAdapter = GetFormatter(connection); } return(sqlAdapter.Insert(connection, transaction, commandTimeout, name, sbColumnList.ToString(), sbParameterList.ToString(), keyProperties, entityToInsert)); } //insert list of entities var cmd = String.Format("insert into {0} ({1}) values ({2})", name, sbColumnList, sbParameterList); return(connection.Execute(cmd, entityToInsert, transaction, commandTimeout)); }
/// <summary> /// Inserts an entity into table "Ts" and returns identity id. /// </summary> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToInsert">Entity to insert</param> /// <returns>Identity of inserted entity</returns> public static long Insert <T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { var type = typeof(T); #if DEBUG if (type.IsAbstract) { throw new Exception("It's really weird to insert an abstract class"); } #endif if (TypeIsTablePerType(type)) { return(TablePerTypeInsert(connection, entityToInsert, transaction, commandTimeout)); } ISqlAdapter adapter = GetFormatter(connection); var name = GetTableName(type); var allProperties = TypePropertiesCache(type); var keyProperties = KeyPropertiesCache(type); var computedProperties = ComputedPropertiesCache(type); var allPropertiesExceptKeyAndComputed = allProperties.Except(keyProperties.Union(computedProperties)).ToArray(); var columnList = GenerateColumnList <T>(allPropertiesExceptKeyAndComputed, adapter); var parameterList = GenerateParameterList <T>(allPropertiesExceptKeyAndComputed); #if DEBUG foreach (var key in keyProperties) { var value = key.GetValue(entityToInsert); if (value is int && (int)value != 0) { throw new Exception("Trying to Insert but the [Id] is not 0"); } else if (value is long && (long)value != 0L) { throw new Exception("Trying to Insert but the [Id] is not 0"); } } #endif int id = adapter.Insert(connection, transaction, commandTimeout, name, columnList, parameterList, keyProperties, entityToInsert); return(id); }
/// <summary> /// Inserts an entity into table "Ts" and returns identity id. /// </summary> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToInsert">Entity to insert</param> /// <returns>Identity of inserted entity</returns> public static long Insert <T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { var type = typeof(T); var name = GetTableName(type); int value = findUpperLetter(name); if (value != -1) { var temp = name.Substring(0, value); temp = temp + "_" + name.Substring(value, name.Length - value); name = temp; } var sbColumnList = new StringBuilder(null); var allProperties = TypePropertiesCache(type); var keyProperties = KeyPropertiesCache(type); var allPropertiesExceptKey = allProperties.Except(keyProperties); for (var i = 0; i < allPropertiesExceptKey.Count(); i++) { var property = allPropertiesExceptKey.ElementAt(i); sbColumnList.Append(property.Name); if (i < allPropertiesExceptKey.Count() - 1) { sbColumnList.Append(", "); } } var sbParameterList = new StringBuilder(null); for (var i = 0; i < allPropertiesExceptKey.Count(); i++) { var property = allPropertiesExceptKey.ElementAt(i); sbParameterList.AppendFormat("@{0}", property.Name); if (i < allPropertiesExceptKey.Count() - 1) { sbParameterList.Append(", "); } } ISqlAdapter adapter = GetFormatter(connection); int id = adapter.Insert(connection, transaction, commandTimeout, name.ToUpper(new CultureInfo("en-US", false)), sbColumnList.ToString(), sbParameterList.ToString(), keyProperties, entityToInsert); return(id); }
/// <summary> /// Inserts entities into table "Ts". /// </summary> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToInsert">Entity to insert</param> /// <returns>Identity of inserted entity</returns> public static void Insert <T>(this IDbConnection connection, IEnumerable <T> entities, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { var type = typeof(T); var name = GetTableName(type); var sbColumnList = new StringBuilder(null); var allProperties = TypePropertiesCache(type); var keyProperties = KeyPropertiesCache(type); var allPropertiesExceptKey = allProperties.Except(keyProperties); for (var i = 0; i < allPropertiesExceptKey.Count(); i++) { var property = allPropertiesExceptKey.ElementAt(i); sbColumnList.AppendFormat("{0}", property.Name); if (i < allPropertiesExceptKey.Count() - 1) { sbColumnList.Append(", "); } } var sbParameterList = new StringBuilder(null); for (var i = 0; i < allPropertiesExceptKey.Count(); i++) { var property = allPropertiesExceptKey.ElementAt(i); sbParameterList.AppendFormat("@{0}", property.Name); if (i < allPropertiesExceptKey.Count() - 1) { sbParameterList.Append(", "); } } ISqlAdapter adapter = GetFormatter(connection); foreach (var entityToInsert in entities) { adapter.Insert(connection, transaction, commandTimeout, name, sbColumnList.ToString(), sbParameterList.ToString(), keyProperties, entityToInsert); } }
/// <summary> /// Inserts an entity into table "Ts" and returns identity id. /// </summary> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToInsert">Entity to insert</param> /// <returns>Identity of inserted entity</returns> public static long InsertWithKey <T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { var type = typeof(T); var name = GetTableName(type); var sbColumnList = new StringBuilder(null); var allProperties = TypePropertiesCache(type); for (var i = 0; i < allProperties.Count(); i++) { var property = allProperties.ElementAt(i); sbColumnList.AppendFormat("{0}", property.Name); if (i < allProperties.Count() - 1) { sbColumnList.Append(", "); } } var sbParameterList = new StringBuilder(null); for (var i = 0; i < allProperties.Count(); i++) { var property = allProperties.ElementAt(i); sbParameterList.AppendFormat("@{0}", property.Name); if (i < allProperties.Count() - 1) { sbParameterList.Append(", "); } } ISqlAdapter adapter = GetFormatter(connection); int id = adapter.Insert(connection, transaction, commandTimeout, name, sbColumnList.ToString(), sbParameterList.ToString(), new List <PropertyInfo>(), entityToInsert); return(id); }
/// <summary> /// Inserts an entity into table "Ts" and returns identity id. /// </summary> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToInsert">Entity to insert</param> /// <returns>Identity of inserted entity</returns> public static long Insert <T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { var type = typeof(T); ISqlAdapter adapter = GetFormatter(connection); var name = GetTableName(type); var sbColumnList = new StringBuilder(null); var allProperties = TypePropertiesCache(type); var keyProperties = KeyPropertiesCache(type).ToList(); for (var i = keyProperties.Count - 1; i >= 0; i--) { if (null != keyProperties.ElementAt(i).GetValue(entityToInsert, null)) { keyProperties.RemoveAt(i); } } var allPropertiesExceptKey = allProperties.Except(keyProperties); for (var i = 0; i < allPropertiesExceptKey.Count(); i++) { var property = allPropertiesExceptKey.ElementAt(i); //edit by leo 为空的属性不赋值 if (null == property.GetValue(entityToInsert, null)) { continue; } //sbColumnList.AppendFormat("[{0}]", property.Name); //by liuliqiang 为解决数据库兼容性 sbColumnList.AppendFormat("{0}, ", property.Name); } sbColumnList.Remove(sbColumnList.Length - 2, 2); var sbParameterList = new StringBuilder(null); for (var i = 0; i < allPropertiesExceptKey.Count(); i++) { var property = allPropertiesExceptKey.ElementAt(i); //edit by leo 为空的属性不赋值 if (null == property.GetValue(entityToInsert, null)) { continue; } sbParameterList.AppendFormat("{1}{0}, ", property.Name, adapter.PreParamName); } sbParameterList.Remove(sbParameterList.Length - 2, 2); bool wasClosed = connection.State == ConnectionState.Closed; try { if (wasClosed) { connection.Open(); } int id = adapter.Insert(connection, transaction, commandTimeout, name, sbColumnList.ToString(), sbParameterList.ToString(), keyProperties, entityToInsert); return(id); } finally { if (wasClosed) { connection.Close(); } } }
public static long Insert <T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int?commandTimeout = default(int?)) where T : class { bool flag = false; Type type = typeof(T); if (type.IsArray) { flag = true; type = type.GetElementType(); } else if (type.IsGenericType()) { flag = true; type = type.GetGenericArguments()[0]; } string tableName = GetTableName(type); StringBuilder stringBuilder = new StringBuilder(null); List <PropertyInfo> first = TypePropertiesCache(type); List <PropertyInfo> list = KeyPropertiesCache(type); List <PropertyInfo> second = ComputedPropertiesCache(type); List <PropertyInfo> list2 = first.Except(list.Union(second)).ToList(); ISqlAdapter formatter = GetFormatter(connection); for (int i = 0; i < list2.Count; i++) { PropertyInfo propertyInfo = list2.ElementAt(i); formatter.AppendColumnName(stringBuilder, propertyInfo.Name); if (i < list2.Count - 1) { stringBuilder.Append(", "); } } StringBuilder stringBuilder2 = new StringBuilder(null); for (int j = 0; j < list2.Count; j++) { PropertyInfo propertyInfo2 = list2.ElementAt(j); stringBuilder2.AppendFormat("@{0}", propertyInfo2.Name); if (j < list2.Count - 1) { stringBuilder2.Append(", "); } } bool num = connection.State == ConnectionState.Closed; if (num) { connection.Open(); } int num2; if (!flag) { num2 = formatter.Insert(connection, transaction, commandTimeout, tableName, stringBuilder.ToString(), stringBuilder2.ToString(), list, entityToInsert); } else { string text = string.Format("insert into {0} ({1}) values ({2})", tableName, stringBuilder, stringBuilder2); num2 = SqlMapper.Execute(connection, text, (object)entityToInsert, transaction, commandTimeout, (CommandType?)null); } if (num) { connection.Close(); } return(num2); }