Inheritance: IPreparable, ISerializable, ISqlFormattable
Esempio n. 1
0
        private static void AddSourceToClause(FromClause clause, IFromSourceNode source)
        {
            string alias = null;
            if (source.Alias != null)
                alias = source.Alias.Text;

            if (source is FromTableSourceNode) {
                var tableSource = (FromTableSourceNode)source;
                clause.AddTable(alias, tableSource.TableName.Name);
            } else if (source is FromQuerySourceNode) {
                var querySource = (FromQuerySourceNode)source;
                var queryExpression = (SqlQueryExpression) Build(querySource.Query);
                clause.AddSubQuery(alias, queryExpression);
            }
        }
Esempio n. 2
0
        object IPreparable.Prepare(IExpressionPreparer preparer)
        {
            var clause = new FromClause();

            // Prepare expressions in the JoiningSet first
            int size = joinParts.Count;

            for (int i = 0; i < size; ++i)
            {
                var part = joinParts[i];
                var exp  = part.OnExpression;
                if (exp != null)
                {
                    exp = exp.Prepare(preparer);
                    if (part.SubQuery != null)
                    {
                        part = new JoinPart(part.JoinType, part.SubQuery, exp);
                    }
                    else
                    {
                        part = new JoinPart(part.JoinType, part.TableName, exp);
                    }
                }

                clause.joinParts.Add(part);
            }

            // Prepare the StatementTree sub-queries in the from tables
            for (int i = 0; i < fromTables.Count; i++)
            {
                var table         = fromTables[i];
                var preparedTable = (FromTable)((IPreparable)table).Prepare(preparer);

                if (i < tableNames.Count)
                {
                    var tableAlias = tableNames[i];
                    clause.tableNames.Insert(i, tableAlias);
                }

                clause.fromTables.Insert(i, preparedTable);
            }

            return(clause);
        }
Esempio n. 3
0
            public override SqlQueryExpression Deserialize(BinaryReader reader)
            {
                var selectColumns = DeserializeSelectColumns(reader);

                var queryExp = new SqlQueryExpression(selectColumns);

                var isDistinct = reader.ReadByte() == 1;

                queryExp.Distinct = isDistinct;

                var hasFrom = reader.ReadByte() == 1;

                if (hasFrom)
                {
                    queryExp.FromClause = FromClause.Deserialize(reader);
                }

                var hasWhere = reader.ReadByte() == 1;

                if (hasWhere)
                {
                    queryExp.WhereExpression = SqlExpression.Deserialize(reader);
                }

                var hasGroupMax = reader.ReadByte() == 1;

                if (hasGroupMax)
                {
                    queryExp.GroupMax = ObjectName.Deserialize(reader);
                }

                var hasHaving = reader.ReadByte() == 1;

                if (hasHaving)
                {
                    queryExp.HavingExpression = SqlExpression.Deserialize(reader);
                }

                return(queryExp);
            }
Esempio n. 4
0
 public SqlQueryExpression(IEnumerable<SelectColumn> selectColumns)
 {
     SelectColumns = selectColumns;
     FromClause = new FromClause();
 }
Esempio n. 5
0
        private void SetFromTableInClause(FromClause clause, IFromSourceNode source, JoinNode join)
        {
            AddSourceToClause(clause, source);

            if (join != null) {
                var joinType = JoinType.Inner;
                if (!String.IsNullOrEmpty(join.JoinType))
                    joinType = GetJoinType(join.JoinType);

                SqlExpression onExpression = null;
                if (join.OnExpression != null)
                    onExpression = Build(join.OnExpression);

                clause.Join(joinType, onExpression);

                SetFromTableInClause(clause, join.Source, join.NextJoin);
            }
        }
Esempio n. 6
0
 private void SetFromClause(FromClause clause, FromClauseNode node)
 {
     SetFromTableInClause(clause, node.Source, node.Join);
 }
Esempio n. 7
0
        private void PrintFromClause(FromClause fromClause)
        {
            if (fromClause == null || fromClause.IsEmpty)
            {
                return;
            }

            builder.Append("FROM ");

            var tables = fromClause.AllTables.ToList();

            for (int i = 0; i < tables.Count; i++)
            {
                var source = tables[i];

                JoinPart joinPart = null;

                if (i > 0)
                {
                    joinPart = fromClause.GetJoinPart(i - 1);
                    if (joinPart != null)
                    {
                        if (joinPart.JoinType == JoinType.Inner)
                        {
                            builder.Append(" INNER JOIN ");
                        }
                        else if (joinPart.JoinType == JoinType.Right)
                        {
                            builder.Append(" RIGHT OUTER JOIN ");
                        }
                        else if (joinPart.JoinType == JoinType.Left)
                        {
                            builder.Append(" LEFT OUTER JOIN ");
                        }
                        else if (joinPart.JoinType == JoinType.Full)
                        {
                            builder.Append(" FULL OUTER JOINT ");
                        }
                    }
                }

                if (source.IsSubQuery)
                {
                    builder.Append("(");
                    Visit(source.SubQuery);
                    builder.Append(")");
                }
                else
                {
                    builder.Append(source.Name);
                }

                if (!String.IsNullOrEmpty(source.Alias))
                {
                    builder.Append(" AS ");
                    builder.Append(source.Alias);
                }

                if (i < tables.Count - 1)
                {
                    if (joinPart == null)
                    {
                        builder.Append(", ");
                    }
                    else
                    {
                        builder.Append(" ON ");
                        Visit(joinPart.OnExpression);
                    }
                }
            }
        }
Esempio n. 8
0
 public void TableReference(FromClause fromClause)
 {
     switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
     case K_ONLY:
       mcc_consume_token(K_ONLY);
       mcc_consume_token(154);
       QueryTableExpression(fromClause);
       mcc_consume_token(155);
       break;
     case K_TABLE:
     case S_IDENTIFIER:
     case S_BIND:
     case S_QUOTED_IDENTIFIER:
     case 150:
     case 154:
       QueryTableExpression(fromClause);
       break;
     default:
       mcc_la1[171] = mcc_gen;
       mcc_consume_token(-1);
       throw new ParseException();
     }
 }
Esempio n. 9
0
 public void Join(FromClause fromClause)
 {
     Expression onExpression;
     switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
     case 156:
       mcc_consume_token(156);
       fromClause.AddJoin(JoinType.Inner);
       switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
       case K_ONLY:
       case K_TABLE:
       case S_IDENTIFIER:
       case S_BIND:
       case S_QUOTED_IDENTIFIER:
       case 150:
       case 154:
     TableReference(fromClause);
     break;
       default:
     mcc_la1[177] = mcc_gen;
     ;
     break;
       }
       break;
     case K_INNER:
     case S_IDENTIFIER:
       switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
       case K_INNER:
     mcc_consume_token(K_INNER);
     break;
       default:
     mcc_la1[178] = mcc_gen;
     ;
     break;
       }
       ID("JOIN");
       TableDeclaration(fromClause);
       mcc_consume_token(K_ON);
       onExpression = SQLExpression();
       fromClause.AddPreviousJoin(JoinType.Inner, onExpression);
       switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
       case K_INNER:
       case K_LEFT:
       case K_RIGHT:
       case S_IDENTIFIER:
       case 156:
     Join(fromClause);
     break;
       default:
     mcc_la1[179] = mcc_gen;
     ;
     break;
       }
       break;
     case K_LEFT:
       mcc_consume_token(K_LEFT);
       switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
       case 178:
     mcc_consume_token(178);
     break;
       default:
     mcc_la1[180] = mcc_gen;
     ;
     break;
       }
       ID("JOIN");
       TableDeclaration(fromClause);
       mcc_consume_token(K_ON);
       onExpression = SQLExpression();
       fromClause.AddPreviousJoin(JoinType.Left, onExpression);
       switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
       case K_INNER:
       case K_LEFT:
       case K_RIGHT:
       case S_IDENTIFIER:
       case 156:
     Join(fromClause);
     break;
       default:
     mcc_la1[181] = mcc_gen;
     ;
     break;
       }
       break;
     case K_RIGHT:
       mcc_consume_token(K_RIGHT);
       switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
       case 178:
     mcc_consume_token(178);
     break;
       default:
     mcc_la1[182] = mcc_gen;
     ;
     break;
       }
       ID("JOIN");
       TableDeclaration(fromClause);
       mcc_consume_token(K_ON);
       onExpression = SQLExpression();
       fromClause.AddPreviousJoin(JoinType.Right, onExpression);
       switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
       case K_INNER:
       case K_LEFT:
       case K_RIGHT:
       case S_IDENTIFIER:
       case 156:
     Join(fromClause);
     break;
       default:
     mcc_la1[183] = mcc_gen;
     ;
     break;
       }
       break;
     default:
       mcc_la1[184] = mcc_gen;
       mcc_consume_token(-1);
       throw new ParseException();
     }
 }
Esempio n. 10
0
 public void FromClause(FromClause fromClause)
 {
     mcc_consume_token(K_FROM);
     TableReference(fromClause);
     while (true) {
       switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
       case 156:
     ;
     break;
       default:
     mcc_la1[170] = mcc_gen;
     goto label_29;
       }
       mcc_consume_token(156);
       TableReference(fromClause);
     }label_29: ;
 }
Esempio n. 11
0
 public SqlQueryExpression(IEnumerable <SelectColumn> selectColumns)
 {
     SelectColumns = selectColumns;
     FromClause    = new FromClause();
 }
Esempio n. 12
0
        internal override void AppendTo(SqlStringBuilder builder)
        {
            builder.Append("SELECT ");
            if (Distinct)
            {
                builder.Append("DISTINCT ");
            }

            var columns = SelectColumns.ToArray();
            var sz      = columns.Length;

            for (int i = 0; i < sz; i++)
            {
                columns[i].AppendTo(builder);

                if (i < sz - 1)
                {
                    builder.Append(", ");
                }
            }

            builder.Append(" ");

            if (FromClause != null)
            {
                FromClause.AppendTo(builder);
            }

            if (WhereExpression != null)
            {
                builder.Append(" WHERE ");
                WhereExpression.AppendTo(builder);
            }

            if (GroupBy != null)
            {
                var groupBy = GroupBy.ToList();
                builder.Append(" GROUP BY ");

                for (int i = 0; i < groupBy.Count; i++)
                {
                    groupBy[i].AppendTo(builder);

                    if (i < groupBy.Count - 1)
                    {
                        builder.Append(", ");
                    }
                }

                if (HavingExpression != null)
                {
                    builder.Append(" HVAING ");
                    HavingExpression.AppendTo(builder);
                }
            }

            if (GroupMax != null)
            {
                builder.Append(" GROUP MAX ");
                GroupMax.AppendTo(builder);
            }

            // TODO: COMPOSITE ...
        }
Esempio n. 13
0
 private FromClause VisitFromClause(FromClause fromClause)
 {
     // TODO:
     return(fromClause);
 }
        private void PrintFromClause(FromClause fromClause)
        {
            if (fromClause == null || fromClause.IsEmpty)
                return;

            builder.Append("FROM ");

            var tables = fromClause.AllTables.ToList();
            for (int i = 0; i < tables.Count; i++) {
                var source = tables[i];

                JoinPart joinPart = null;

                if (i > 0) {
                    joinPart = fromClause.GetJoinPart(i - 1);
                    if (joinPart != null) {
                        if (joinPart.JoinType == JoinType.Inner) {
                            builder.Append(" INNER JOIN ");
                        } else if (joinPart.JoinType == JoinType.Right) {
                            builder.Append(" RIGHT OUTER JOIN ");
                        } else if (joinPart.JoinType == JoinType.Left) {
                            builder.Append(" LEFT OUTER JOIN ");
                        } else if (joinPart.JoinType == JoinType.Full) {
                            builder.Append(" FULL OUTER JOINT ");
                        }
                    }
                }

                if (source.IsSubQuery) {
                    builder.Append("(");
                    Visit(source.SubQuery);
                    builder.Append(")");
                } else {
                    builder.Append(source.Name);
                }

                if (!String.IsNullOrEmpty(source.Alias)) {
                    builder.Append(" AS ");
                    builder.Append(source.Alias);
                }

                if (i < tables.Count - 1) {
                    if (joinPart == null) {
                        builder.Append(", ");
                    } else {
                        builder.Append(" ON ");
                        Visit(joinPart.OnExpression);
                    }
                }
            }
        }
Esempio n. 15
0
 public void QueryTableExpression(FromClause fromClause)
 {
     TableDeclaration(fromClause);
     while (true) {
       switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
       case K_INNER:
       case K_LEFT:
       case K_RIGHT:
       case S_IDENTIFIER:
       case 156:
     ;
     break;
       default:
     mcc_la1[172] = mcc_gen;
     goto label_30;
       }
       Join(fromClause);
     }label_30: ;
 }
Esempio n. 16
0
 private FromClause VisitFromClause(FromClause fromClause)
 {
     // TODO:
     return fromClause;
 }
Esempio n. 17
0
 public void TableDeclaration(FromClause fromClause)
 {
     ObjectName name = null, alias = null; TableSelectExpression selectExp = null;
     switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
     case S_IDENTIFIER:
     case S_QUOTED_IDENTIFIER:
       name = TableName();
       break;
     case K_TABLE:
       TableCollectionExpression();
       break;
     default:
       mcc_la1[173] = mcc_gen;
       if (mcc_2_34(3)) {
     mcc_consume_token(154);
     selectExp = SubQuery();
     mcc_consume_token(155);
       } else {
     switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
     case 154:
       mcc_consume_token(154);
       TableReference(fromClause);
       mcc_consume_token(155);
       break;
     case S_BIND:
     case 150:
       BindVariable();
       break;
     default:
       mcc_la1[174] = mcc_gen;
       mcc_consume_token(-1);
       throw new ParseException();
     }
       }
       break;
     }
     switch ((mcc_ntk==-1)?mcc_mntk():mcc_ntk) {
     case S_IDENTIFIER:
     case S_QUOTED_IDENTIFIER:
       alias = ObjectName();
       break;
     default:
       mcc_la1[175] = mcc_gen;
       ;
       break;
     }
       fromClause.AddTableDeclaration(name, selectExp, alias);
 }
Esempio n. 18
0
        object IPreparable.Prepare(IExpressionPreparer preparer)
        {
            var clause = new FromClause();

            // Prepare expressions in the JoiningSet first
            int size = joinParts.Count;
            for (int i = 0; i < size; ++i) {
                var part = joinParts[i];
                var exp = part.OnExpression;
                if (exp != null) {
                    exp = exp.Prepare(preparer);
                    if (part.SubQuery != null) {
                        part = new JoinPart(part.JoinType, part.SubQuery,  exp);
                    } else {
                        part = new JoinPart(part.JoinType, part.TableName, exp);
                    }
                }

                clause.joinParts.Add(part);
            }

            // Prepare the StatementTree sub-queries in the from tables
            for (int i = 0; i < fromTables.Count; i++) {
                var table = fromTables[i];
                var preparedTable = (FromTable) ((IPreparable) table).Prepare(preparer);

                if (i < tableNames.Count) {
                    var tableAlias = tableNames[i];
                    clause.tableNames.Insert(i, tableAlias);
                }

                clause.fromTables.Insert(i, preparedTable);
            }

            return clause;
        }
Esempio n. 19
0
            public static FromClause Build(PlSqlParser.FromClauseContext context)
            {
                if (context == null)
                    return null;

                var clause = new FromClause();

                var list = context.tableRefList();
                if (list.IsEmpty)
                    throw new ParseCanceledException("No source set in FROM clause");

                var tableRefs = list.tableRef().Select(FormTableRef);

                bool joinSeen = false;
                bool first = true;

                foreach (var tableRef in tableRefs) {
                    if (joinSeen)
                        throw new ParseCanceledException("Invalid join clause");

                    var source = tableRef.Source;
                    if (source.SubQuery != null) {
                        clause.AddSubQuery(source.Alias, source.SubQuery);
                    } else if (source.TableName != null) {
                        clause.AddTable(source.Alias, source.TableName);
                    }

                    foreach (var joinNode in tableRef.Join) {
                        var joinSource = joinNode.Source;

                        if (joinSource.SubQuery != null) {
                            clause.AddSubQuery(joinSource.Alias, joinSource.SubQuery);
                        } else if (joinSource.TableName != null) {
                            clause.AddTable(joinSource.Alias, joinSource.TableName);
                        }

                        clause.Join(joinNode.JoinType, joinNode.OnExpression);
                        joinSeen = true;
                    }

                    if (!first && !joinSeen) {
                        clause.Join(JoinType.Inner, null);
                    }

                    first = false;
                }

                return clause;
            }