public T Get <T>(object id, string tableName = null, int?commandTimeout = null) where T : class { IClassMapper classMapper = ClassMapperFactory.GetMapper <T>(tableName); KeyConditionResult keyConditionResult = SqlGenerator.GetKeyConditionById(classMapper, id); SqlConvertResult sqlConvertResult = SqlGenerator.Select(classMapper, keyConditionResult.Sql, null, keyConditionResult.Parameters, false); T result = DbConnection.Query <T>(sqlConvertResult.Sql, sqlConvertResult.Parameters, DbTransaction, true, commandTimeout, CommandType.Text).SingleOrDefault(); if (result == null) { return(null); } IPropertyMap persistedMap = classMapper.GetPersistedMap(); if (persistedMap != null) { persistedMap.PropertyInfo.SetValue(result, true, null); } IPropertyMap propertyChangedListMap = classMapper.GetPropertyChangedListMap(); if (propertyChangedListMap != null) { IList <string> changedList = propertyChangedListMap.PropertyInfo.GetValue(result, null) as IList <string>; if (changedList != null) { changedList.Clear(); } } return(result); }
public IList <T> List <T>(string tableName, string condition, string orderBy, DynamicParameters dynamicParameters, int firstResult, int maxResults, int?commandTimeout = null) where T : class { IClassMapper classMapper = ClassMapperFactory.GetMapper <T>(tableName); SqlConvertResult sqlConvertResult = SqlGenerator.Select(classMapper, firstResult, maxResults, condition, orderBy, dynamicParameters); return(DbConnection.Query <T>(sqlConvertResult.Sql, sqlConvertResult.Parameters, DbTransaction, true, commandTimeout, CommandType.Text).ToList()); }
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 Delete <T>(object id, string tableName = null, int?commandTimeout = null) where T : class { IClassMapper classMapper = ClassMapperFactory.GetMapper <T>(tableName); KeyConditionResult keyConditionResult = SqlGenerator.GetKeyConditionById(classMapper, id); SqlConvertResult sqlConvertResult = SqlGenerator.Delete(classMapper, keyConditionResult.Sql, keyConditionResult.Parameters); return(DbConnection.Execute(sqlConvertResult.Sql, sqlConvertResult.Parameters, DbTransaction, commandTimeout, CommandType.Text) > 0); }
public bool Save <T>(T entity, string tableName = null, int?commandTimeout = null) where T : class { IClassMapper classMapper = ClassMapperFactory.GetMapper <T>(tableName); IPropertyMap persistedMap = classMapper.GetPersistedMap(); if (persistedMap == null) { throw new Exception(string.Format("{0}没有映射持久化标识列,无法使用此方法!", typeof(T).FullName)); } bool isPersisted = (bool)persistedMap.PropertyInfo.GetValue(entity, null); return(isPersisted ? Update(entity, tableName, commandTimeout) : Insert(entity, tableName, commandTimeout)); }
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); }