예제 #1
0
        /// <summary>
        /// 通过附加在指定类及其属性上的特性获取相关Schema缓存信息
        /// </summary>
        public static SchemaCache CreateByAttribute(Type type)
        {
            SchemaCache cache = new SchemaCache();

            cache.Type             = type;
            cache.PropertyAccessor = new PropertyAccessor();


            //获取类符合要求的属性
            cache.PropertyInfos = cache.Type.GetProperties()?.Where(PropertyFilter)?.ToArray();
            cache.TableSchema   = GetTableSchema(cache.Type);
            if (cache.PropertyInfos != null)
            {
                cache.PropertyAccessor.Initlialize(cache.PropertyInfos.Length);
                foreach (PropertyInfo propertyInfo in cache.PropertyInfos)
                {
                    ColumnSchema schema = GetColumnSchema(propertyInfo);
                    cache.PropertyAccessor.BuildingGetPropertyCache(propertyInfo);
                    cache.PropertyAccessor.BuildingSetPropertyCache(propertyInfo);
                    cache.ColumnSchemas.Add(propertyInfo.Name, schema);
                    cache.ColumnNames.Add(schema.Name);
                }
            }
            return(cache);
        }
        ///【置于调用链末尾】 对当前的查询做分页操作。sorter  分页字段,此处返回 model 的指定属性。start 起始位置。 count 获取数量。asc 默认升序,置为 false 改为降序
        /// </summary>
        public static IDriver <T> Page <T>(this IDriver <T> driver, Expression <Func <T, Object> > sorter, Int32 start, Int32 count, Boolean asc = true)
            where T : class
        {
            String           sortColName = null;
            SchemaCache      schema      = Table <T> .Schema;
            MemberExpression expression  = null;

            if (sorter.Body.NodeType != ExpressionType.MemberAccess)
            {
                //由于函数签名为 Object 类型 所以值类型 属性会发生装箱
                if (sorter.Body.NodeType == ExpressionType.Convert)
                {
                    UnaryExpression unaryExp = sorter.Body as UnaryExpression;
                    expression = unaryExp.Operand as MemberExpression;
                }
                else
                {
                    throw new ArgumentException(nameof(sorter));
                }
            }
            else
            {
                expression = sorter.Body as MemberExpression;
            }
            sortColName = schema.GetColumnName(expression.Member.Name);
            return(Page <T>(driver, sortColName, start, count, asc));
        }
예제 #3
0
        internal static void AnalyingIn <T>(DbProvider dbProvider, Expression <Func <T, Object> > filter, SQLComponent component, Object[] values)
            where T : class
        {
            MemberExpression expression;
            ExpressionType   noteType = filter.Body.NodeType;;

            switch (noteType)
            {
            case ExpressionType.Convert:
                UnaryExpression unary = filter.Body as UnaryExpression;
                expression = (MemberExpression)unary.Operand;
                break;

            case ExpressionType.MemberAccess:
            {
                expression = filter.Body as MemberExpression;
            }
            break;

            default:
                throw new ArgumentException(nameof(filter));;
            }


            SchemaCache schema     = Table <T> .Schema;
            var         colSchema  = schema.GetColumnSchema(expression.Member.Name);
            String      columnName = colSchema.Name;

            columnName = String.Format(dbProvider.ConflictFreeFormat, columnName);
            columnName = String.Format(CommonFormat.COLUMN_FORMAT, schema.TableName, columnName);

            StringBuilder inParaNames = new StringBuilder();
            Int32         count       = values.Length;
            String        paraSymbol  = "inPara";

            for (Int32 i = 0; i < count; ++i)
            {
                String paraName = dbProvider.Prefix + paraSymbol + ParaCounter <T> .CountString;
                inParaNames.Append(paraName);
                if (i != (count - 1))
                {
                    inParaNames.Append(SqlKeyWord.COMMA);
                }
                DbParameter parameter = dbProvider.DbParameter(
                    paraName, values[i], colSchema.DbType
                    );
                component.AddParameter(parameter);
            }
            component.AppendSQL(columnName);
            component.AppendSQL(SqlKeyWord.IN);
            component.AppendSQLFormat(CommonFormat.BRACKET_FORMAT, inParaNames.ToString());
        }
        /// <summary>
        /// 增大指定数值字段的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tableObj"></param>
        /// <param name="field"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        public static IDriver <T> Increment <T>(this Table <T> tableObj, Expression <Func <T, Object> > field, Double number = 1) where T : class
        {
            IDriver <T> driver = tableObj.DbProvider.Driver <T>();


            var expression = (field.Body as UnaryExpression);
            var mexp       = expression.Operand as MemberExpression;

            if (mexp == null)
            {
                throw new ArgumentException(nameof(field));
            }
            SchemaCache schema     = Table <T> .Schema;
            var         colSchema  = schema.GetColumnSchema(mexp.Member.Name);
            String      columnName = OrmAssistor.BuildColumnName(colSchema, tableObj.DbProvider.ConflictFreeFormat, schema.TableName);
            String      setSql     = columnName + SqlKeyWord.EQUAL + columnName + SqlKeyWord.PLUS + number.ToString();
            String      sql        = String.Format(BasicSqlFormat.UPDATE_FORMAT, schema.TableName, setSql);

            driver.SQLComponent.AppendSQL(sql);
            return(driver);
        }