Exemplo n.º 1
0
 private object Aggregate(BasicFieldInfo field, AggregateType aggregateType, bool isDistinct)
 {
     if (!_mapping.Equals(field.TableMapping))
     {
         throw new LightDataException(RE.FieldIsNotMatchDataMapping);
     }
     return(_context.Aggregate(field.DataField, aggregateType, _query, isDistinct, _level));
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        public virtual 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) {
                    string alias = null;
                    if (obj is DataFieldInfo)
                    {
                        foreach (KeyValuePair <string, DataFieldInfo> kv in dataFieldInfoDictionary)
                        {
                            if (Object.ReferenceEquals(obj, kv.Value))
                            {
                                alias = kv.Key;
                                break;
                            }
                        }
                    }
                    else 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(orderString);
        }
Exemplo n.º 5
0
        public virtual string GetQueryString(DataEntityMapping mapping, QueryExpression query, out DataParameter[] parameters)
        {
            string queryString = null;

            parameters = null;
            if (query != null)
            {
                if (!query.IgnoreConsistency && !mapping.Equals(query.TableMapping))
                {
                    throw new LightDataException(RE.DataMappingIsNotMatchQueryExpression);
                }
                queryString = string.Format("where {0}", query.CreateSqlString(this, out parameters));
            }
            return(queryString);
        }
Exemplo n.º 6
0
        public override 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) {
                    return(null);
                })));
            }
            return(havingString);
        }
Exemplo n.º 7
0
        public virtual string GetOrderString(DataEntityMapping mapping, OrderExpression order)
        {
            string orderString = null;

            DataParameter[] 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));
            }
            return(orderString);
        }
Exemplo n.º 8
0
        public virtual IDbCommand CreateDynamicAggregateCommand(DataEntityMapping mapping, Dictionary <string, DataFieldInfo> dataFieldInfoDictionary, Dictionary <string, AggregateFunction> aggregateFunctionDictionary, QueryExpression query, AggregateHavingExpression having, OrderExpression order)
        {
            if (dataFieldInfoDictionary == null || dataFieldInfoDictionary.Count == 0)
            {
                throw new LightDataException(RE.DynamicAggregateFieldIsNotExists);
            }
            StringBuilder sql = new StringBuilder();

            StringBuilder select  = new StringBuilder();
            StringBuilder groupby = new StringBuilder();

            List <DataParameter> parameterlist = new List <DataParameter> ();

            bool flat = false;

            foreach (KeyValuePair <string, DataFieldInfo> kv in dataFieldInfoDictionary)
            {
                if (!mapping.Equals(kv.Value.TableMapping))
                {
                    throw new LightDataException(RE.DataMappingIsNotMatchAggregateField);
                }
                string groupbyField = kv.Value.CreateDataFieldSql(this);
                string aliasName    = CreateDataFieldSql(kv.Key);
                string selectField  = string.Format("{0} as {1}", groupbyField, aliasName);
                if (!flat)
                {
                    flat = true;
                }
                else
                {
                    selectField  = "," + selectField;
                    groupbyField = "," + groupbyField;
                }
                select.Append(selectField);
                groupby.Append(groupbyField);
            }
            foreach (KeyValuePair <string, AggregateFunction> kv in aggregateFunctionDictionary)
            {
                if (kv.Value.TableMapping != null && !mapping.Equals(kv.Value.TableMapping))
                {
                    throw new LightDataException(RE.DataMappingIsNotMatchAggregateField);
                }

                DataParameter[] aggparameters = null;
                string          aggField      = kv.Value.CreateSqlString(this, out aggparameters);
                string          aliasName     = CreateDataFieldSql(kv.Key);
                string          selectField   = string.Format(",{0} as {1}", aggField, aliasName);
                select.Append(selectField);
                parameterlist.AddRange(aggparameters);
            }

            sql.AppendFormat("select {0} from {1}", select, CreateDataTableSql(mapping.TableName));

            DataParameter[] queryparameters = null;
            string          queryString     = GetQueryString(mapping, query, out queryparameters);

            DataParameter[] havingparameters = null;
            string          havingString     = GetHavingString(mapping, having, out havingparameters, aggregateFunctionDictionary);

            DataParameter[] orderbyparameters = null;
            string          orderString       = GetOrderString(mapping, order, out orderbyparameters, dataFieldInfoDictionary, aggregateFunctionDictionary);

            if (!string.IsNullOrEmpty(queryString))
            {
                sql.AppendFormat(" {0}", queryString);
                parameterlist.AddRange(queryparameters);
            }

            sql.AppendFormat(" group by {0}", groupby);

            if (!string.IsNullOrEmpty(havingString))
            {
                sql.AppendFormat(" {0}", havingString);
                parameterlist.AddRange(havingparameters);
            }

            if (!string.IsNullOrEmpty(orderString))
            {
                sql.AppendFormat(" {0}", orderString);
                parameterlist.AddRange(orderbyparameters);
            }

            IDbCommand command = BuildCommand(sql.ToString(), parameterlist.ToArray());

            return(command);
        }