private DbCommand GetDbCommand(Type entityType, params SortInfo[] orderBy) { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entityType); List <PropertyInfo> fieldPropertyList = new List <PropertyInfo>(); string tableName = entityInfo.MappingTableAttribute.TableName; string sqlSelect = string.Format("SELECT {0}", GetSelectStatement(entityInfo)); DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, null); sqlSelect = sqlSelect + this.ToOrderByClause(entityType, orderBy); dbCommand.CommandText = sqlSelect; return(dbCommand); }
public static string GetEntityInfoMessage(object entity) { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entity.GetType()); StringBuilder infoBuilder = new StringBuilder(); infoBuilder.AppendLine("Entity Type: " + entity.GetType().FullName); foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos()) { PropertyInfo property = mfi.ProInfo; infoBuilder.AppendLine("[" + property.Name + "]: " + property.GetValue(entity, null)); } return(infoBuilder.ToString()); }
public static void EntityIsMappingDatabase(Type type, string message) { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(type); if (entityInfo.MappingTableAttribute == null) { throw new ORMException(message); } if (entityInfo.GetKeyFieldInfos() == null || entityInfo.GetKeyFieldInfos().Count == 0) { throw new ORMException(message); } }
/// <summary> /// 得到新增数据库中某一对象的DbCommand,modifyCommandCreator的属性Entity不能为空 /// </summary> /// <exception cref="RException(DatabaseErrorCode.CommandTextIsEmpty - 0x00010102)"> /// 属性Entity为null时,引发此异常 /// </exception> /// <returns>新增数据库中某一对象的DbCommand,DbCommand的参数为所传入的Entity的属性</returns> public override DbCommand GetDbCommand() { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(Entity.GetType()); List <PropertyInfo> fieldPropertyList = null; string sqlInsert = string.Format("INSERT INTO {0} {1}" , GetQuotedName(entityInfo.MappingTableAttribute.TableName) , GetInsertStatement(entityInfo, out fieldPropertyList)); DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, this.Entity); dbCommand.CommandText = sqlInsert; return(dbCommand); }
private DbCommand GetDbCommand(object entity, string[] filedProterties, params SortInfo[] orderBy) { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entity.GetType()); List <PropertyInfo> fieldPropertyList = null; string tableName = entityInfo.MappingTableAttribute.TableName; string sqlSelect = string.Format("SELECT {0}", GetSelectStatement(entityInfo, out fieldPropertyList, filedProterties)); DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, entity); sqlSelect = sqlSelect + this.ToOrderByClause(entity.GetType(), orderBy); dbCommand.CommandText = sqlSelect; return(dbCommand); }
/// <summary> /// 得到修改数据库中某一对象的DbCommand,modifyCommandCreator的属性Entity不能为空 /// </summary> /// <exception cref="RException(DatabaseErrorCode.CommandTextIsEmpty - 0x00010102)"> /// 属性Entity为null时,引发此异常 /// </exception> /// <returns>修改数据库中某一对象的DbCommand,DbCommand的参数为所传入的Entity的属性</returns> public override DbCommand GetDbCommand() { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(Entity.GetType()); List <PropertyInfo> fieldPropertyList = null; string sqlUpdate = string.Format("UPDATE {0} SET {1}" , GetQuotedName(entityInfo.MappingTableAttribute.TableName) , GetUpdateStatement(entityInfo, out fieldPropertyList)); DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, Entity); dbCommand.CommandText = sqlUpdate; return(dbCommand); }
/// <summary> /// 构建无where条件查询的sql语句 /// </summary> /// <returns>sql语句</returns> private string GetSelectStatement(TypeSchema entityInfo) { string fields = ""; foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos()) { if (fields != "") { fields += ","; } fields += GetQuotedName(mfi.MappingFieldAttribute.FieldName); } return(string.Format("{0} FROM {1}", fields, this.GetQuotedName(entityInfo.MappingTableAttribute.TableName))); }
private DbCommand GetDbCommand(Criteria cri) { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(EntityType); List <PropertyInfo> fieldPropertyList = new List <PropertyInfo>(); DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, null); List <DbParameter> parameters; string sqlDelete = string.Format("DELETE FROM {0} WHERE 1=1 {1}" , GetQuotedName(entityInfo.MappingTableAttribute.TableName) , cri.GenerateExpression(EntityType, out parameters, DbAccess)); dbCommand.Parameters.AddRange(parameters.ToArray()); dbCommand.CommandText = sqlDelete; return(dbCommand); }
private DbCommand GetDbCommand(Criteria criteria, params SortInfo[] orderBy) { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(this.ObjectType); List <PropertyInfo> fieldPropertyList = new List <PropertyInfo>(); string sqlSelect = string.Format("SELECT {0}", GetSelectStatement(entityInfo)); DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, null); List <DbParameter> parameters; sqlSelect = sqlSelect + " WHERE 1=1 " + criteria.GenerateExpression(this.ObjectType, out parameters, DbAccess); sqlSelect = sqlSelect + this.ToOrderByClause(this.ObjectType, orderBy); dbCommand.Parameters.AddRange(parameters.ToArray()); dbCommand.CommandText = sqlSelect; return(dbCommand); }
private string GetDeleteStatement(TypeSchema entityInfo, out List <PropertyInfo> fieldPropertyList) { string query = ""; fieldPropertyList = new List <PropertyInfo>(); foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos()) { if (query != "") { query += " AND "; } query += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name; fieldPropertyList.Add(mfi.ProInfo); } return(string.Format("WHERE {0}", query)); }
internal static TypeSchema GetTypeSchema(Type objectType) { TypeSchema entityInfo = null; if (!_entityInfos.TryGetValue(objectType, out entityInfo)) { lock (o) { if (!_entityInfos.TryGetValue(objectType, out entityInfo)) { entityInfo = new TypeSchema(objectType); _entityInfos.Add(objectType, entityInfo); } } } return(entityInfo); }
protected DbCommand GetDbCommandByKeyValue(TypeSchema entityInfo, object keyValue) { DbCommand dbCommand = DbAccess.CreateDbCommand(); foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos()) { Type type = mfi.ProInfo.PropertyType; type = Nullable.GetUnderlyingType(type) ?? type; DbType dbType = ORMHelper.GetDbTypeByName(type.Name); DbParameter parameter = DbAccess.CreateDbParameter(); parameter.ParameterName = "@" + mfi.ProInfo.Name; parameter.DbType = dbType; //parameter.Value = Convert.ChangeType(keyValue, fieldProperty.PropertyType); parameter.Value = Convert.ChangeType(keyValue, type); dbCommand.Parameters.Add(parameter); } return(dbCommand); }
/// <summary> /// 构建select count()语句 /// </summary> /// <returns></returns> private DbCommand GetCountDbCommand(object entity, string[] filterPerproties) { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entity.GetType()); List <PropertyInfo> fieldPropertyList = null; string sqlSelect = ""; if (filterPerproties == null) { sqlSelect = string.Format("SELECT COUNT(*) FROM {0}", this.GetQuotedName(entityInfo.MappingTableAttribute.TableName)); fieldPropertyList = new List <PropertyInfo>();//初始化fieldPropertyList,仅仅为了给GetDbCommandByEntity提供无元素的List } else { sqlSelect = string.Format("SELECT {0}", GetCountSelectStatement(entityInfo, out fieldPropertyList, filterPerproties)); } DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, entity); dbCommand.CommandText = sqlSelect; return(dbCommand); }
internal virtual void Init(Type objectType, IDbAccess dbAccess) { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(objectType); SchemaItem fieldInfo = entityInfo.GetFieldInfoByPropertyName(this._propertyName); if (fieldInfo == null) { throw new ArgumentException(string.Format("{0} - Property \"{1}\" not define a mapping field.", this.GetType(), _propertyName)); } Type type = fieldInfo.ProInfo.PropertyType; type = Nullable.GetUnderlyingType(type) ?? type; if (type.IsEnum) { this._dbType = DbType.Int32; } else { this._dbType = ORMHelper.GetDbTypeByName(type.Name); } this._fieldName = fieldInfo.MappingFieldAttribute.FieldName; this._dbAccess = dbAccess; }
private string ToOrderByClause(Type entityType, params SortInfo[] orderBy) { TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entityType); string orderByStr = ""; if (null != orderBy && orderBy.Length > 0) { foreach (SortInfo tempSortInfo in orderBy) { SchemaItem fieldInfo = entityInfo.GetFieldInfoByPropertyName(tempSortInfo.PropertyName); if (fieldInfo == null) { throw new ArgumentException("Sort property not define a mapping field. - \"" + tempSortInfo.PropertyName + "\""); } string tempName = GetQuotedName(fieldInfo.MappingFieldAttribute.FieldName); if (tempSortInfo.Direction == SortDirection.Desc) { orderByStr += "," + tempName + " Desc"; } else { orderByStr += "," + tempName; } } if (orderByStr.Length > 0) { orderByStr = orderByStr.Substring(1); //remove the first char ','. } return(string.Format(" ORDER BY {0}", orderByStr)); } else { return(orderByStr); } }
internal static List <SchemaItem> GetSchemaItems(Type objectType) { TypeSchema entityInfo = GetTypeSchema(objectType); return(entityInfo.GetAllFieldInfos()); }