Example #1
0
        /// <summary>
        /// Query and return data list
        /// </summary>
        /// <typeparam name="T">Data type.</typeparam>
        /// <param name="region">Query region</param>
        /// <param name="cancellationToken">CancellationToken.</param>
        /// <returns>Data list</returns>
        private async Task <List <T> > QueryListAsync <T>(Region region, CancellationToken cancellationToken = default(CancellationToken))
        {
            List <T> list = await _context.QueryDataDefineListAsync <T>(DataEntityMapping.GetEntityMapping(typeof(T)), _level, _command, region, null, null, cancellationToken);

            Callback();
            return(list);
        }
Example #2
0
        /// <summary>
        /// Query and return first data
        /// </summary>
        /// <typeparam name="T">Data type.</typeparam>
        /// <param name="cancellationToken">CancellationToken.</param>
        /// <returns>First data</returns>
        public async Task <T> QueryFirstAsync <T>(CancellationToken cancellationToken = default(CancellationToken))
        {
            T target = await _context.QueryDataDefineSingleAsync <T>(DataEntityMapping.GetEntityMapping(typeof(T)), _level, _command, 0, null, null, cancellationToken);

            Callback();
            return(target);
        }
Example #3
0
        /// <summary>
        /// Query and return first data
        /// </summary>
        /// <typeparam name="T">Data type.</typeparam>
        /// <returns>First data</returns>
        public T QueryFirst <T>()
        {
            T target = _context.QueryDataDefineSingle <T>(DataEntityMapping.GetEntityMapping(typeof(T)), _level, _command, 0, null, null);

            Callback();
            return(target);
        }
Example #4
0
        /// <summary>
        /// Query and return data enumerable
        /// </summary>
        /// <typeparam name="T">Data type.</typeparam>
        /// <param name="region">Data region</param>
        /// <returns>Data enumerable</returns>
        private IEnumerable <T> Query <T>(Region region)
        {
            IEnumerable <T> enumable = _context.QueryDataDefineReader <T>(DataEntityMapping.GetEntityMapping(typeof(T)), _level, _command, region, null, null);

            Callback();
            return(enumable);
        }
Example #5
0
 /// <summary>
 /// 统计行数
 /// </summary>
 /// <param name="mapping">数据映射表</param>
 /// <param name="query">查询表达式</param>
 /// <param name="level"></param>
 /// <returns>查询行数</returns>
 internal object AggregateCount(DataEntityMapping mapping, QueryExpression query, SafeLevel level)
 {
     using (IDbCommand command = _dataBase.Factory.CreateAggregateCountCommand(mapping, query))
     {
         return(ExecuteScalar(command, level));
     }
 }
Example #6
0
        public SingleParameterLambdaState(ParameterExpression parameter)
        {
            _singleEntityName = parameter.Name;
            var entityMapping = DataEntityMapping.GetEntityMapping(parameter.Type);

            _singleEntityMap = entityMapping.GetRelationMap();
        }
Example #7
0
        public override int Delete()
        {
            var mapping      = DataEntityMapping.GetTableMapping(typeof(T));
            var queryCommand = _context.Database.QueryDelete(_context, mapping, _query);

            return(_context.ExecuteNonQuery(queryCommand.Command, _level));
        }
Example #8
0
        public override async Task <int> InsertAsync <K>(CancellationToken cancellationToken = default)
        {
            var insertMapping = DataEntityMapping.GetTableMapping(typeof(K));
            var queryCommand  = _context.Database.SelectInsert(_context, insertMapping, _mapping, _query, _order);

            return(await _context.ExecuteNonQueryAsync(queryCommand.Command, _level, cancellationToken));
        }
Example #9
0
        public virtual string GetHavingString(DataEntityMapping mapping, AggregateHavingExpression having, out DataParameter[] parameters, Dictionary <string, AggregateFunction> aggregateFunctionDictionary)
        {
            string havingString = null;

            parameters = null;
            if (having != null)
            {
                if (!having.IgnoreConsistency && !mapping.Equals(having.TableMapping))
                {
                    throw new LightDataException(RE.DataMappingIsNotMatchAggregationExpression);
                }
                havingString = string.Format("having {0}", having.CreateSqlString(this, out parameters, new GetAliasHandler(delegate(object obj) {
                    string alias = null;
                    if (obj is AggregateFunction)
                    {
                        foreach (KeyValuePair <string, AggregateFunction> kv in aggregateFunctionDictionary)
                        {
                            if (Object.ReferenceEquals(obj, kv.Value))
                            {
                                alias = kv.Key;
                                break;
                            }
                        }
                    }
                    else
                    {
                        throw new LightDataException(RE.UnknowOrderType);
                    }
                    return(alias);
                })));
            }
            return(havingString);
        }
        public LightMathFunctionDataFieldInfo(DataEntityMapping mapping, MathFunction function,
                                              params object[] argsObjects)
            : base(mapping)
        {
            if (argsObjects == null || argsObjects.Length == 0)
            {
                throw new ArgumentNullException(nameof(argsObjects));
            }
            if (function == MathFunction.Atan2 || function == MathFunction.Max || function == MathFunction.Min ||
                function == MathFunction.Pow)
            {
                if (argsObjects.Length != 2)
                {
                    throw new ArgumentNullException(nameof(argsObjects));
                }
            }

            if (function == MathFunction.Log || function == MathFunction.Round)
            {
                if (argsObjects.Length > 2)
                {
                    throw new ArgumentNullException(nameof(argsObjects));
                }
            }

            _function    = function;
            _argsObjects = argsObjects;
        }
Example #11
0
        public override async Task <int> DeleteAsync(CancellationToken cancellationToken = default)
        {
            var mapping      = DataEntityMapping.GetTableMapping(typeof(T));
            var queryCommand = _context.Database.QueryDelete(_context, mapping, _query);

            return(await _context.ExecuteNonQueryAsync(queryCommand.Command, _level, cancellationToken));
        }
 public LightBinaryQueryExpression(DataEntityMapping mapping, QueryPredicate predicate, object left, object right)
     : base(mapping)
 {
     _predicate = predicate;
     _left      = left;
     _right     = right;
 }
        public override string CreateRandomOrderBySql(DataEntityMapping mapping)
        {
            Random rnd             = new Random(unchecked ((int)DateTime.Now.Ticks));
            int    intRandomNumber = rnd.Next() * -1;

            string field = "";

            foreach (DataFieldMapping df in mapping.GetFieldMappings())
            {
                if (df.ObjectType == typeof(int))
                {
                    field = CreateDataFieldSql(df.Name);
                    break;
                }
            }
            if (string.IsNullOrEmpty(field))
            {
                foreach (DataFieldMapping df in mapping.GetFieldMappings())
                {
                    if (df.ObjectType == typeof(string))
                    {
                        field = string.Format("len({0})", CreateDataFieldSql(df.Name));
                        break;
                    }
                }
            }
            if (string.IsNullOrEmpty(field))
            {
                throw new LightDataException(RE.TypfOfDataFieldIsNotString);
            }
            return(string.Format("rnd({0}*{1})", intRandomNumber, field));
        }
 public LightMathCalculateDataFieldInfo(DataEntityMapping mapping, MathOperator opera, object left, object right)
     : base(mapping)
 {
     _opera = opera;
     _left  = left;
     _right = right;
 }
Example #15
0
        /// <summary>
        /// Query and return data list
        /// </summary>
        /// <typeparam name="T">Data type.</typeparam>
        /// <param name="region">Query region</param>
        /// <returns>Data list</returns>
        private List <T> QueryList <T>(Region region)
        {
            List <T> list = _context.QueryDataDefineList <T>(DataEntityMapping.GetEntityMapping(typeof(T)), _level, _command, region, null, null);

            Callback();
            return(list);
        }
Example #16
0
        protected override IDbCommand CreateSelectBaseCommand(DataEntityMapping mapping, string customSelect, QueryExpression query, OrderExpression order, Region region)
        {
            if (region == null)
            {
                return(base.CreateSelectBaseCommand(mapping, customSelect, query, order, null));
            }

            StringBuilder sql = new StringBuilder();

            DataParameter[] parameters;
            string          queryString = GetQueryString(mapping, query, out parameters);
            string          orderString = GetOrderString(mapping, order);
            bool            distinct    = false;

            if (customSelect.StartsWith("distinct ", StringComparison.OrdinalIgnoreCase))
            {
                distinct     = true;
                customSelect = customSelect.Substring(9);
            }
            if (region.Start == 0)
            {
                sql.AppendFormat("select {3}top {2} {0} from {1}", customSelect, CreateDataTableSql(mapping.TableName), region.Size, distinct ? "distinct " : string.Empty);
                if (!string.IsNullOrEmpty(queryString))
                {
                    sql.AppendFormat(" {0}", queryString);
                }
                if (!string.IsNullOrEmpty(orderString))
                {
                    sql.AppendFormat(" {0}", orderString);
                }
            }
            else
            {
//				example
//				select *
//				from (
//				select row_number()over(order by __tc__)__rn__,*
//				from (select top 开始位置+10 0 __tc__,* from Student where Age>18 order by Age)t
//				)tt
//				where __rn__>开始位置
//
                StringBuilder innerSQL      = new StringBuilder();
                string        tempCount     = CreateCustomFiledName();
                string        tempRowNumber = CreateCustomFiledName();
                innerSQL.AppendFormat("select {4}top {2} {0},0 {3} from {1}", customSelect, CreateDataTableSql(mapping.TableName), region.Start + region.Size, tempCount, distinct ? "distinct " : string.Empty);
                if (!string.IsNullOrEmpty(queryString))
                {
                    innerSQL.AppendFormat(" {0}", queryString);
                }
                if (!string.IsNullOrEmpty(orderString))
                {
                    innerSQL.AppendFormat(" {0}", orderString);
                }
                sql.AppendFormat("select {1} from (select a.*,row_number()over(order by {3}) {4} from ({0})a )b where {4}>{2}",
                                 innerSQL, customSelect, region.Start, tempCount, tempRowNumber);
            }
            IDbCommand command = BuildCommand(sql.ToString(), parameters);

            return(command);
        }
Example #17
0
        public override int Insert <K>()
        {
            var insertMapping = DataEntityMapping.GetTableMapping(typeof(K));
            var queryCommand  = _context.Database.SelectInsert(_context, insertMapping, _mapping, _query, _order);

            return(_context.ExecuteNonQuery(queryCommand.Command, _level));
        }
Example #18
0
 public AggregateLambdaState(ParameterExpression parameter, AggregateModel model)
 {
     aggregateModel = model;
     aggregateName  = parameter.Name;
     aggregateType  = parameter.Type;
     entityMapping  = model.EntityMapping;
 }
        public override string GetOrderString(DataEntityMapping mapping, OrderExpression order, out DataParameter[] parameters, Dictionary <string, DataFieldInfo> dataFieldInfoDictionary, Dictionary <string, AggregateFunction> aggregateFunctionDictionary)
        {
            string orderString = null;

            parameters = null;
            if (order != null)
            {
                if (order.IgnoreConsistency)
                {
                    RandomOrderExpression random = order as RandomOrderExpression;
                    if (random != null)
                    {
                        random.SetTableMapping(mapping);
                    }
                }
                if (!order.IgnoreConsistency && !mapping.Equals(order.TableMapping))
                {
                    throw new LightDataException(RE.DataMappingIsNotMatchOrderExpression);
                }
                orderString = string.Format("order by {0}", order.CreateSqlString(this, out parameters, new GetAliasHandler(delegate(object obj) {
                    return(null);
                })));
            }
            return(orderString);
        }
Example #20
0
 public LightStringFunctionDataFieldInfo(DataEntityMapping mapping, StringFunction function, object callObject, params object [] argsObjects)
     : base(mapping)
 {
     if (callObject == null)
     {
         throw new ArgumentNullException(nameof(callObject));
     }
     if (function == StringFunction.ToLower || function == StringFunction.ToUpper || function == StringFunction.Trim)
     {
         if (argsObjects != null && argsObjects.Length > 0)
         {
             throw new ArgumentNullException(nameof(argsObjects));
         }
     }
     if (function == StringFunction.Replace)
     {
         if (argsObjects == null || argsObjects.Length != 2)
         {
             throw new ArgumentNullException(nameof(argsObjects));
         }
     }
     if (function == StringFunction.Substring || function == StringFunction.IndexOf)
     {
         if (argsObjects == null || argsObjects.Length > 2)
         {
             throw new ArgumentNullException(nameof(argsObjects));
         }
     }
     _function    = function;
     _callObject  = callObject;
     _argsObjects = argsObjects;
 }
Example #21
0
 /// <summary>
 /// 动态统计数据到数据表中
 /// </summary>
 /// <param name="mapping">数据映射</param>
 /// <param name="dataFieldInfoDictionary">统计字段信息</param>
 /// <param name="aggregateFunctionDictionary">统计方法信息</param>
 /// <param name="query">查询表达式</param>
 /// <param name="having">统计查询表达式</param>
 /// <param name="order">排序表达式</param>
 /// <param name="level">安全级别</param>
 /// <returns>数据表</returns>
 internal DataTable QueryDynamicAggregateTable(DataEntityMapping mapping, Dictionary <string, DataFieldInfo> dataFieldInfoDictionary, Dictionary <string, AggregateFunction> aggregateFunctionDictionary, QueryExpression query, AggregateHavingExpression having, OrderExpression order, SafeLevel level)
 {
     using (IDbCommand command = _dataBase.Factory.CreateDynamicAggregateCommand(mapping, dataFieldInfoDictionary, aggregateFunctionDictionary, query, having, order))
     {
         return(QueryDataTable(command, null, level));
     }
 }
Example #22
0
 internal BasicFieldInfo(DataEntityMapping tableMapping)
 {
     if (tableMapping == null)
     {
         throw new ArgumentNullException(nameof(tableMapping));
     }
     TableMapping = tableMapping;
 }
Example #23
0
 public LightSubQueryDataFieldInfo(DataEntityMapping mapping, DataFieldInfo field, DataFieldInfo selectField, QueryCollectionPredicate predicate, QueryExpression expression)
     : base(mapping)
 {
     _field       = field;
     _selectField = selectField;
     _predicate   = predicate;
     _expression  = expression;
 }
Example #24
0
        public RelateParameterLambdaState(ParameterExpression parameter, LambdaState state)
        {
            var entityMapping = DataEntityMapping.GetEntityMapping(parameter.Type);

            _singleEntityName = parameter.Name;
            _singleEntityMap  = entityMapping.GetRelationMap();
            _state            = state;
        }
Example #25
0
 public bool Equals(DataEntityMapping mapping)
 {
     if (mapping == null)
     {
         return(false);
     }
     return(ObjectType == mapping.ObjectType);
 }
Example #26
0
        public SingleParameterLambdaState(ParameterExpression parameter)
        {
            this.singleEntityName = parameter.Name;
            Type type = parameter.Type;
            DataEntityMapping entityMapping = DataEntityMapping.GetEntityMapping(type);

            this.singleEntityMap = entityMapping.GetRelationMap();
        }
Example #27
0
 public LightStringMatchDataFieldInfo(DataEntityMapping mapping, bool starts, bool ends, object left, object right)
     : base(mapping)
 {
     _starts = starts;
     _ends   = ends;
     _left   = left;
     _right  = right;
 }
Example #28
0
        public override async Task <int> UpdateAsync(Expression <Func <T, T> > expression, CancellationToken cancellationToken = default)
        {
            var mapping      = DataEntityMapping.GetTableMapping(typeof(T));
            var updator      = LambdaExpressionExtend.CreateMassUpdateExpression(expression);
            var queryCommand = _context.Database.QueryUpdate(_context, mapping, updator, _query);

            return(await _context.ExecuteNonQueryAsync(queryCommand.Command, _level, cancellationToken));
        }
Example #29
0
        public override int Update(Expression <Func <T, T> > expression)
        {
            var mapping      = DataEntityMapping.GetTableMapping(typeof(T));
            var updator      = LambdaExpressionExtend.CreateMassUpdateExpression(expression);
            var queryCommand = _context.Database.QueryUpdate(_context, mapping, updator, _query);

            return(_context.ExecuteNonQuery(queryCommand.Command, _level));
        }
 public void SetTableMapping(DataEntityMapping mapping)
 {
     if (mapping == null)
     {
         throw new ArgumentNullException(nameof(mapping));
     }
     TableMapping = mapping;
 }