Exemple #1
0
        protected SqlBuilder InitSqlBuilder(Query query)
        {
            SqlBuilder builder = new SqlBuilder();

            builder.SetInternalBinding(internalBinding);

            builder.FromTableName  = _tableName ?? query.TableName;
            builder.FromTableAlias = TableAlias ?? String.Empty;

            builder.Limit       = query.Limit;
            builder.Offset      = query.Offset;
            builder.Page        = query.Page;
            builder.HasGroupBys = query.HasGroupBys;
            builder.UnionSqls   = query.UnionSqls;
            if (query.UnionParameters != null && query.UnionParameters.Count > 0)
            {
                foreach (SqlParameter param in query.UnionParameters)
                {
                    builder.AddToBindParams(param.ParameterName, param.Value, param.SqlDbType);
                }
            }

            Dictionary <string, bool> uniqueParameters = new Dictionary <string, bool>();

            if (null == query.Selects)
            {
                Query.SelectItemCollection extQuerys = new Query.SelectItemCollection();
                extQuerys.Add("*");
                query.Selects = extQuerys;
            }
            for (int i = 0; i < query.Selects.Count; i++)
            {
                Query.SelectItem item = query.Selects[i];

                string key = item.Value;
                if (key.StartsWith("("))
                {
                    // use method-base subquery
                    // this is for more complex subqueries
                    // parameters are passed to handler. up to handler to addtobindparams
                    builder.Selects.Add(SubQuery(key, item.Parameters, builder));
                }
                else
                {
                    // stand subquery. Simple text only
                    string alias;

                    if (null != SelectMap && SelectMap.ContainsKey(key))
                    {
                        alias = SelectMap[key];
                    }
                    else
                    {
                        alias = key;
                    }

                    bool     useFormat   = alias.Contains("{");
                    object[] formatInput = null;

                    if (item.Parameters != null && item.Parameters.Length > 0)
                    {
                        if (useFormat)
                        {
                            formatInput = new object[item.Parameters.Length];
                        }

                        var j = 0;
                        foreach (string paramName in item.Parameters)
                        {
                            if (uniqueParameters.ContainsKey(paramName))
                            {
                                continue;
                            }
                            uniqueParameters.Add(paramName, true);
                            Query.ParameterItem param = query.Parameters[paramName.Trim()];

                            builder.AddToBindParams(paramName.Trim(), param.Value, param.Type);

                            if (useFormat)
                            {
                                switch (param.Type)
                                {
                                case SqlDbType.Int:
                                    formatInput[j] = int.Parse(param.Value);
                                    break;

                                case SqlDbType.Float:
                                    formatInput[j] = double.Parse(param.Value);
                                    break;

                                case SqlDbType.NVarChar:
                                    formatInput[j] = param.Value;
                                    break;

                                default:
                                    throw new Exception("Unsupported parameter type");
                                }
                            }

                            j++;
                        }
                    }
                    builder.Selects.Add(useFormat ? string.Format(alias, formatInput) : alias);
                }
            }



            foreach (KeyValuePair <string, string> item in query.Ands)
            {
                if (FilterMap.ContainsKey(item.Key))
                {
                    FilterMap[item.Key](item.Value, builder);
                }
                else
                {
                    throw new Exception("invalid query key: " + item.Key);
                }
            }

            foreach (Query.SqlWhereItem item in query.Wheres.Items)
            {
                builder.Wheres.Add(item);
                if (item.Parameters != null)
                {
                    foreach (string paramName in item.Parameters)
                    {
                        Query.ParameterItem param = query.Parameters[paramName.Trim()];
                        builder.AddToBindParams(paramName.Trim(), param.Value, param.Type);
                    }
                }
            }
            // merge with query's where parts if any

            if (builder.HasGroupBys)
            {
                BuildGroupBys(query.GroupBys, builder);
            }

            BuildOrderBys(query.OrderBys, builder);
            return(builder);
        }
Exemple #2
0
        public static void SetEntityExtendQuery(Query.SelectItemCollection extQuerys)
        {
            string tableName = BaseTO.GetTableName <T>();

            entityExtendQuery.ThreadSafeWrite <string, Query.SelectItemCollection>(tableName, extQuerys, "entityExtendQuery");
        }