コード例 #1
0
 public ScalarSelectQueryBase(QueryBuilderBase queryBuilderBase, LambdaExpression selector)
 {
     InnerQuery       = null;
     QueryBuilderBase = queryBuilderBase;
     Selector         = selector;
     Columns          = SelectQueryBase.GetColumns(selector);
 }
コード例 #2
0
ファイル: FromQuery.cs プロジェクト: micahhahn/Passado
        public FromQueryBase(QueryBuilderBase queryBuilderBase, LambdaExpression table)
        {
            InnerQuery       = null;
            QueryBuilderBase = queryBuilderBase;
            Name             = "T1";

            var property = ExpressionHelpers.ParseSelector(table);

            Model = queryBuilderBase.DatabaseModel.Tables.First(t => t.Property.Name == property.Name);
        }
コード例 #3
0
        public InsertQueryBase(QueryBuilderBase queryBuilderBase, LambdaExpression table, LambdaExpression columns)
        {
            InnerQuery       = null;
            QueryBuilderBase = queryBuilderBase;

            var property = ExpressionHelpers.ParseSelector(table);

            Model = queryBuilderBase.DatabaseModel.Tables.First(t => t.Property.Name == property.Name);

            IntoColumns = (columns.Body as NewExpression).Arguments
                          .Select(a => Model.Columns.Single(c => c.Property.Name == (a as MemberExpression).Member.Name))
                          .ToImmutableArray();
        }
コード例 #4
0
        protected void AddMissingDataTables(
            SelectQuery query,
            ReportColumnMapping groupByColumn,
            TableRelationshipGraphBuilder graphBuilder,
            List <string> tables,
            QueryBuilderBase queryBuilderBase,
            Dictionary <string, bool> addedTables)
        {
            // build a graph of the table relationships to calculate the distance from the root table
            // include missing tables needed for joins
            // order the tables by distance
            var relationshipGraph = graphBuilder.Build(_tableMappings.GetAllTableRelationships(), _constants.RootTableName);

            graphBuilder.TrimToTables(relationshipGraph, tables);
            tables = graphBuilder.GetAllTables(relationshipGraph).Union(tables).Distinct().ToList();

            tables = tables.OrderBy(x => graphBuilder.GetDistance(relationshipGraph, x)).ToList();

            foreach (var table in tables.Distinct())
            {
                queryBuilderBase.AddFromTableJoin(query, groupByColumn, table, ref addedTables, graphBuilder, relationshipGraph);
            }
        }
コード例 #5
0
 public InsertQuery(QueryBuilderBase queryBuilderBase, LambdaExpression table, LambdaExpression columns)
     : base(queryBuilderBase, table, columns)
 {
 }
コード例 #6
0
        internal static string GetSelectQuery(DatabaseBase database, QueryBuilderBase queryBuilder, Parameters parameters)
        {
            var builder = GetBuilder(database.DatabaseType);

            return(builder.GetSelectQuery(database, queryBuilder, parameters));
        }
コード例 #7
0
 public ScalarSelectQuery(QueryBuilderBase queryBuilderBase, LambdaExpression selector)
     : base(queryBuilderBase, selector)
 {
 }
コード例 #8
0
        public string GetSelectQuery(DatabaseBase database, QueryBuilderBase queryBuilder, Parameters parameters)
        {
            var stringBuilder = new StringBuilder();

            if (queryBuilder.UnionQuery != null)
            {
                stringBuilder.Append(GetSelectQuery(database, queryBuilder.UnionQuery, parameters));
                if (queryBuilder.UnionType == UnionType.UNION)
                {
                    stringBuilder.Append(" UNION ");
                }
                else if (queryBuilder.UnionType == UnionType.UNION_ALL)
                {
                    stringBuilder.Append(" UNION ALL ");
                }
                else if (queryBuilder.UnionType == UnionType.INTERSECT)
                {
                    stringBuilder.Append(" INTERSECT ");
                }
                else
                {
                    if (queryBuilder.UnionType != UnionType.EXCEPT)
                    {
                        throw new Exception("Unknown union type: " + queryBuilder.UnionType.ToString());
                    }
                    stringBuilder.Append(" EXCEPT ");
                }
            }
            stringBuilder.Append("SELECT ");
            if (queryBuilder.IsDistinct)
            {
                stringBuilder.Append("DISTINCT ");
            }
            bool useAlias = queryBuilder.JoinList.Count > 0 && !queryBuilder.FromTable.IsTemporaryTable;
            int  index1   = 0;

            while (index1 < queryBuilder.SelectColumns.Length)
            {
                ISelectable selectable = queryBuilder.SelectColumns[index1];
                if (index1 > 0)
                {
                    stringBuilder.Append(',');
                }
                if (selectable is ColumnBase)
                {
                    stringBuilder.Append(GetColumnSql((ColumnBase)selectable));
                }
                else
                {
                    if (!(selectable is IFunction))
                    {
                        throw new Exception("Field type not supported yet");
                    }
                    stringBuilder.Append(((IFunction)selectable).GetFunctionSql(database, useAlias));
                }
                checked { ++index1; }
            }
            if (queryBuilder.IntoTable != null)
            {
                stringBuilder.Append(" INTO ");
                if (queryBuilder.IntoTable.IsTemporaryTable)
                {
                    stringBuilder.Append("TEMP ");
                }
                stringBuilder.Append(queryBuilder.IntoTable.Name);
            }
            stringBuilder.Append(" FROM ").Append(queryBuilder.FromTable.Name).Append(" AS ").Append(queryBuilder.FromTable.Alias);
            if (queryBuilder.FromHints != null && queryBuilder.FromHints.Length > 0)
            {
                throw new Exception("From table hints not supported in postgresql sql generator");
            }
            if (queryBuilder.JoinList.Count > 0)
            {
                stringBuilder.Append(" ");
                int index2 = 0;
                while (index2 < queryBuilder.JoinList.Count)
                {
                    Join join = queryBuilder.JoinList[index2];
                    if (join.JoinType == JoinType.JOIN)
                    {
                        stringBuilder.Append("JOIN ");
                    }
                    else if (join.JoinType == JoinType.LEFT)
                    {
                        stringBuilder.Append("LEFT JOIN ");
                    }
                    else
                    {
                        if (join.JoinType != JoinType.RIGHT)
                        {
                            throw new Exception("Unknown join type: " + join.JoinType.ToString());
                        }
                        stringBuilder.Append("RIGHT JOIN ");
                    }
                    stringBuilder.Append(join.Table.Name).Append(" AS ").Append(join.Table.Alias).Append(" ON ").Append(GetConditionSql(database, join.Condition, parameters));
                    if (join.Hints != null && join.Hints.Length > 0)
                    {
                        throw new Exception("Join hints not supported in postgresql sql generator");
                    }
                    checked { ++index2; }
                }
            }
            if (queryBuilder.WhereCondition != null)
            {
                stringBuilder.Append(" WHERE ").Append(GetConditionSql(database, queryBuilder.WhereCondition, parameters));
            }
            if (queryBuilder.GroupByColumns != null && queryBuilder.GroupByColumns.Length > 0)
            {
                stringBuilder.Append(" GROUP BY ");
                int index2 = 0;
                while (index2 < queryBuilder.GroupByColumns.Length)
                {
                    ISelectable selectable = queryBuilder.GroupByColumns[index2];
                    if (index2 > 0)
                    {
                        stringBuilder.Append(',');
                    }
                    if (selectable is ColumnBase)
                    {
                        ColumnBase ColumnBase = (ColumnBase)selectable;
                        stringBuilder.Append(ColumnBase.Table.Alias).Append('.').Append(ColumnBase.Name);
                    }
                    else
                    {
                        if (!(selectable is IFunction))
                        {
                            throw new Exception("column type not supported yet");
                        }
                        stringBuilder.Append(((IFunction)selectable).GetFunctionSql(database, useAlias));
                    }
                    checked { ++index2; }
                }
            }
            if (queryBuilder.HavingCondition != null)
            {
                stringBuilder.Append(" HAVING ").Append(GetConditionSql(database, queryBuilder.HavingCondition, parameters));
            }
            if (queryBuilder.OrderByColumns != null && queryBuilder.OrderByColumns.Length > 0)
            {
                stringBuilder.Append(" ORDER BY ");
                int index2 = 0;
                while (index2 < queryBuilder.OrderByColumns.Length)
                {
                    IOrderByColumn orderByColumn = queryBuilder.OrderByColumns[index2];
                    if (index2 > 0)
                    {
                        stringBuilder.Append(',');
                    }
                    bool flag = false;
                    if (queryBuilder.UnionQuery != null)
                    {
                        int index3 = 0;
                        while (index3 < queryBuilder.SelectColumns.Length)
                        {
                            if (queryBuilder.SelectColumns[index3].Equals(orderByColumn))
                            {
                                stringBuilder.Append(checked (index3 + 1).ToString());
                                flag = true;
                            }
                            checked { ++index3; }
                        }
                    }
                    if (!flag)
                    {
                        ISelectable column = orderByColumn.GetOrderByColumn.Column;
                        if (column is ColumnBase)
                        {
                            stringBuilder.Append(GetColumnSql((ColumnBase)column));
                        }
                        else
                        {
                            if (!(column is IFunction))
                            {
                                throw new Exception("Field type not supported yet");
                            }
                            stringBuilder.Append(((IFunction)column).GetFunctionSql(database, useAlias));
                        }
                    }
                    switch (orderByColumn.GetOrderByColumn.OrderBy)
                    {
                    case OrderByType.ASC:
                        stringBuilder.Append(" ASC");
                        goto case OrderByType.Default;

                    case OrderByType.DESC:
                        stringBuilder.Append(" DESC");
                        goto case OrderByType.Default;

                    case OrderByType.Default:
                        checked { ++index2; }
                        continue;

                    default:
                        throw new Exception("Unknown OrderBy type: " + orderByColumn.GetOrderByColumn.OrderBy.ToString());
                    }
                }
            }
            if (queryBuilder.TopRows.HasValue)
            {
                stringBuilder.Append(" LIMIT ").Append(queryBuilder.TopRows.Value.ToString());
            }
            if (!string.IsNullOrEmpty(queryBuilder.CustomSql))
            {
                stringBuilder.Append(" ").Append(queryBuilder.CustomSql);
            }
            return(stringBuilder.ToString());
        }
コード例 #9
0
ファイル: FromQuery.cs プロジェクト: micahhahn/Passado
 public FromQuery(QueryBuilderBase queryBuilderBase, LambdaExpression table)
     : base(queryBuilderBase, table)
 {
 }