public bool Update <T>(T entity, string tableName = null, int?commandTimeout = null) where T : class { IClassMapper <T> classMapper = ClassMapperFactory.GetMapper <T>(tableName); classMapper.BeforeSave(entity); KeyConditionResult keyConditionResult = SqlGenerator.GetKeyConditionByEntity(classMapper, entity); if (classMapper.GetVersionMap() != null) { int oldVersion = (int)classMapper.GetVersionMap().PropertyInfo.GetValue(entity, null); keyConditionResult.Sql = string.Format("{0} AND {1}={2}", keyConditionResult.Sql, SqlGenerator.DbProvider.QuoteString(classMapper.GetVersionMap().ColumnName), oldVersion); classMapper.GetVersionMap().PropertyInfo.SetValue(entity, oldVersion + 1, null); } SqlConvertResult sqlConvertResult = SqlGenerator.Update(classMapper, keyConditionResult.Sql, keyConditionResult.Parameters, entity); sqlConvertResult.Parameters.AddDynamicParams(entity); bool flag = DbConnection.Execute(sqlConvertResult.Sql, sqlConvertResult.Parameters, DbTransaction, commandTimeout, CommandType.Text) > 0; if (flag) { classMapper.AfterSave(entity); } return(flag); }
public bool Insert <T>(T entity, string tableName = null, int?commandTimeout = null) where T : class { bool flag; IClassMapper <T> classMapper = ClassMapperFactory.GetMapper <T>(tableName); classMapper.BeforeSave(entity); var identityColumn = classMapper.Properties.SingleOrDefault(p => p.KeyType == KeyType.Identity); IDictionary <string, object> keyValues = new ExpandoObject(); SqlConvertResult sqlConvertResult = SqlGenerator.Insert(classMapper, null); if (identityColumn != null) { IEnumerable <long> result; if (SqlGenerator.SupportsMultipleStatements()) { sqlConvertResult.Sql += SqlGenerator.DbProvider.BatchSeperator + SqlGenerator.IdentitySql(classMapper); result = DbConnection.Query <long>(sqlConvertResult.Sql, entity, DbTransaction, false, commandTimeout, CommandType.Text); } else { DbConnection.Execute(sqlConvertResult.Sql, entity, DbTransaction, commandTimeout, CommandType.Text); sqlConvertResult.Sql = SqlGenerator.IdentitySql(classMapper); result = DbConnection.Query <long>(sqlConvertResult.Sql, entity, DbTransaction, false, commandTimeout, CommandType.Text); } long identityValue = result.First(); int identityInt = Convert.ToInt32(identityValue); keyValues.Add(identityColumn.Name, identityInt); identityColumn.PropertyInfo.SetValue(entity, identityInt, null); flag = identityInt > 0; } else { flag = DbConnection.Execute(sqlConvertResult.Sql, entity, DbTransaction, commandTimeout, CommandType.Text) > 0; } if (flag) { classMapper.AfterSave(entity); } return(flag); }
public bool Insert <T>(IEnumerable <T> entities, string tableName = null, int?commandTimeout = null) where T : class { IClassMapper <T> classMapper = ClassMapperFactory.GetMapper <T>(tableName); if (entities != null) { foreach (T entity in entities) { classMapper.BeforeSave(entity); } } SqlConvertResult sqlConvertResult = SqlGenerator.Insert(classMapper, null); bool flag = DbConnection.Execute(sqlConvertResult.Sql, entities, DbTransaction, commandTimeout, CommandType.Text) > 0; if (flag && entities != null) { foreach (T entity in entities) { classMapper.AfterSave(entity); } } return(flag); }