Inheritance: ISerializable
Example #1
0
        void ISqlFormattable.AppendTo(SqlStringBuilder builder)
        {
            if (IsEmpty)
            {
                return;
            }

            builder.Append("FROM ");

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

                JoinPart joinPart = null;

                if (i > 0)
                {
                    if (!IsNaturalJoin)
                    {
                        joinPart = joinParts[i - 1];
                        if (joinPart != null &&
                            joinPart.OnExpression != 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 ");
                            }
                        }
                    }
                    else
                    {
                        builder.Append(", ");
                    }
                }

                source.AppendTo(builder);

                if (joinPart != null &&
                    joinPart.OnExpression != null)
                {
                    builder.Append(" ON ");
                    joinPart.OnExpression.AppendTo(builder);
                }
            }
        }
Example #2
0
        public virtual JoinPart VisitJoinPart(JoinPart part)
        {
            var onExpression = part.OnExpression;

            if (onExpression != null)
            {
                onExpression = Visit(onExpression);
            }

            if (part.IsQuery)
            {
                return(new JoinPart(part.JoinType, part.Query, onExpression));
            }

            return(new JoinPart(part.JoinType, part.TableName, onExpression));
        }
Example #3
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);
        }
Example #4
0
        public void Join(JoinType joinType, SqlExpression onExpression)
        {
            if (sources.Count < 1)
            {
                throw new InvalidOperationException("Cannot join on set that has no sources.");
            }

            JoinPart part;

            var source = sources[sources.Count - 1];

            if (source.IsQuery)
            {
                part = new JoinPart(joinType, source.Query, onExpression);
            }
            else
            {
                part = new JoinPart(joinType, source.TableName, onExpression);
            }

            joinParts.Add(part);
        }
Example #5
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;
        }
Example #6
0
        void ISqlFormattable.AppendTo(SqlStringBuilder builder)
        {
            if (IsEmpty)
            {
                return;
            }

            builder.Append("FROM ");

            var tables = AllTables.ToList();

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

                JoinPart joinPart = null;

                if (i > 0 && joinParts.Count > 0)
                {
                    joinPart = GetJoinPart(i - 1);
                    if (joinPart != null &&
                        joinPart.OnExpression != 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 (i > 0 &&
                    (joinPart == null || joinPart.OnExpression == null))
                {
                    builder.Append(", ");
                }

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

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

                if (joinPart != null &&
                    joinPart.OnExpression != null)
                {
                    builder.Append(" ON ");
                    joinPart.OnExpression.AppendTo(builder);
                }
            }
        }
Example #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);
                    }
                }
            }
        }