Esempio n. 1
0
        internal 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);
            }
        }
        private string GetUpdateStatement(TypeSchema entityInfo, out List <PropertyInfo> fieldPropertyList)
        {
            fieldPropertyList = new List <PropertyInfo>();
            string sets  = "";
            string query = "";

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                if (query != "")
                {
                    query += " AND ";
                }
                query += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
                fieldPropertyList.Add(mfi.ProInfo);
            }
            foreach (SchemaItem mfi in entityInfo.GetNeedUpdateFieldInfos())
            {
                if (sets != "")
                {
                    sets += ",";
                }
                sets += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
                fieldPropertyList.Add(mfi.ProInfo);
            }
            return(string.Format("{0} WHERE {1}", sets, query));
        }
Esempio n. 3
0
        internal static void CheckEntityKey(object entity)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entity.GetType());

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                if (mfi.ProInfo.GetValue(entity, null) == null)
                {
                    throw new ORMException(ErrorMessages.PrimaryKeyIsNull);
                }
            }
        }
 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);
 }
Esempio n. 5
0
        private DbCommand GetDbCommand(Criteria criteria, params SortInfo[] orderBy)
        {
            TypeSchema          entityInfo        = ORMSchemaCache.GetTypeSchema(this.ObjectType);
            List <PropertyInfo> fieldPropertyList = new List <PropertyInfo>();
            string             sqlFields          = GetSelectStatement(entityInfo);
            List <DbParameter> parameters;
            string             sqlWhere   = criteria.GenerateExpression(this.ObjectType, out parameters, DbAccess);
            string             sqlOrder   = ToOrderByClause(this.ObjectType, orderBy);
            StringBuilder      sqlBuilder = new StringBuilder();

            if (NeedPaged)
            {
                if (sqlOrder == "")
                {
                    foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
                    {
                        if (sqlOrder != "")
                        {
                            sqlOrder += ",";
                        }
                        else
                        {
                            sqlOrder += "ORDER BY ";
                        }
                        sqlOrder += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + " ASC";
                    }
                }
                sqlBuilder.AppendLine("WITH [TMP] AS(");
                sqlBuilder.AppendLine("SELECT *, ROW_NUMBER() OVER (" + sqlOrder + ") AS [ROW]");
                sqlBuilder.AppendLine("FROM(");
                sqlBuilder.AppendLine(string.Format("SELECT {0}", sqlFields));
                sqlBuilder.AppendLine(string.Format("WHERE 1=1 {0}", sqlWhere));
                sqlBuilder.AppendLine(") AS [A] )");
                sqlBuilder.AppendLine("SELECT * FROM [TMP]");
                sqlBuilder.AppendLine("INNER JOIN (SELECT COUNT([ROW]) AS [TotalCount] FROM [TMP]) AS [B] ON 1 = 1");
                sqlBuilder.AppendLine(string.Format(" AND ROW > {0} AND ROW <= {1}", PageSize * (PageIndex - 1), PageSize * PageIndex));
            }
            else
            {
                sqlBuilder.AppendLine(string.Format("SELECT {0}", sqlFields));
                sqlBuilder.AppendLine(string.Format("WHERE 1=1 {0}", sqlWhere));
                sqlBuilder.AppendLine(sqlOrder);
            }
            DbCommand dbCommand = GetDbCommandByEntity(fieldPropertyList, null);

            dbCommand.Parameters.AddRange(parameters.ToArray());
            dbCommand.CommandText = sqlBuilder.ToString();
            return(dbCommand);
        }
Esempio n. 6
0
        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));
        }
        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);
                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;
        }
        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);
                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);
        }
 private string GetUpdateStatement(TypeSchema entityInfo,out List<PropertyInfo> fieldPropertyList)
 {
     fieldPropertyList = new List<PropertyInfo>();
     string sets = "";
     string query = "";
     foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
     {
         if (query != "") query += " AND ";
         query += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
         fieldPropertyList.Add(mfi.ProInfo);
     }
     foreach (SchemaItem mfi in entityInfo.GetNeedUpdateFieldInfos())
     {
         if (sets != "") sets += ",";
         sets += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
         fieldPropertyList.Add(mfi.ProInfo);
     }
     return string.Format("{0} WHERE {1}", sets, query);
 }