private InsertSelectStmt(WithClause with
                          , bool isReplaceStmt
                          , ConflictType onConflict
                          , bool hasIntoKeyword
                          , Table table
                          , UnqualifiedColumnNames columns
                          , IQuery query
                          , UnqualifiedColumnNames conflictColumns
                          , string constraintName
                          , Assignments updateaAsignments
                          , Predicate updateWhere
                          , Comments comments)
 {
     this.Comments          = comments;
     this.With              = with;
     this.IsReplaceStmt     = isReplaceStmt;
     this.OnConflict        = onConflict;
     this.HasIntoKeyword    = hasIntoKeyword;
     this.Table             = table;
     this.Columns           = columns;
     this.Query             = query;
     this.ConflictColumns   = conflictColumns;
     this.ConstraintName    = constraintName;
     this.UpdateAssignments = updateaAsignments;
     this.UpdateWhere       = updateWhere;
     if (updateaAsignments == null && updateWhere != null)
     {
         throw new System.ArgumentException(
                   "UpdateaAsignments must be not null, if updateWhere is not null. ");
     }
 }
Beispiel #2
0
 public SelectStmt(WithClause with, IQuery query, ForUpdateClause forUpdate)
 {
     this.With      = with;
     this.Query     = query;
     this.ForUpdate = forUpdate;
     this.Comments  = new Comments();
 }
 internal InsertSelectStmt(WithClause with
                           , bool isReplaceStmt
                           , ConflictType onConflict
                           , bool hasIntoKeyword
                           , Table table
                           , UnqualifiedColumnNames columns
                           , IQuery query
                           , string constraintName
                           , Assignments updateaAsignments
                           , Predicate updateWhere
                           , Comments comments)
     : this(with
            , isReplaceStmt
            , onConflict
            , hasIntoKeyword
            , table
            , columns
            , query
            , null
            , constraintName
            , updateaAsignments
            , updateWhere
            , comments)
 {
 }
        /// <summary>
        ///     Renders the select statement, including the with clause.
        /// </summary>
        /// <param name="sb">The SQL text formatter.</param>
        public void RenderSqlStatement(SqlBuilderContext sb)
        {
            if (WithClause != null)
            {
                WithClause.RenderSql(sb);
            }

            RootQuery.RenderSql(sb);
        }
 protected void HandleWithClauseUsingNaturalJoin(WithClause withClause, Dictionary <ObjectReference, RelationType> relationTypeDict)
 {
     relationTypeDict[withClause.ObjectReference] = WithTypeToRelationType(withClause.Type, RelationType.None);
     _columns =
         _columns.Concat(
             _schema.FindTable(withClause.ObjectReference.GetName()).Columns.Select(
                 c => new ObjectReference(c.ActualName, withClause.ObjectReference)))
         .ToArray();
 }
Beispiel #6
0
 internal DeleteStmt(WithClause with
                     , bool hasFromKeyword
                     , Table table
                     , Table table2
                     , Predicate where
                     , Comments comments)
 {
     this.Comments       = comments;
     this.With           = with;
     this.HasFromKeyword = hasFromKeyword;
     this.Table          = table;
     this.Table2         = table2;
     _where = where;
     this.SetParent(where);
 }
        protected void InferWithType(WithClause withClause)
        {
            var objectReference = withClause.ObjectReference;

            while (!ReferenceEquals(objectReference.GetOwner(), null))
            {
                var toTable   = _schema.FindTable(objectReference.GetName());
                var fromTable = _schema.FindTable(objectReference.GetOwner().GetName());
                if (_schema.GetRelationType(fromTable.ActualName, toTable.ActualName) == RelationType.OneToMany)
                {
                    withClause.Type = WithType.Many;
                    return;
                }
                objectReference = objectReference.GetOwner();
            }
        }
Beispiel #8
0
 internal UpdateStmt(WithClause with
                     , ConflictType onConflict
                     , Table table
                     , Assignments assignments
                     , Table table2
                     , Predicate where
                     , Comments comments)
 {
     this.Comments    = comments;
     this.With        = with;
     this.OnConflict  = onConflict;
     this.Table       = table;
     this.Assignments = assignments;
     this.Table2      = table2;
     _where           = where;
     this.SetParent(where);
 }
 internal InsertSelectStmt(WithClause with
                           , bool isReplaceStmt
                           , ConflictType onConflict
                           , bool hasIntoKeyword
                           , Table table
                           , UnqualifiedColumnNames columns
                           , IQuery query
                           , Comments comments)
 {
     this.Comments       = comments;
     this.With           = with;
     this.IsReplaceStmt  = isReplaceStmt;
     this.OnConflict     = onConflict;
     this.HasIntoKeyword = hasIntoKeyword;
     this.Table          = table;
     this.Columns        = columns;
     this.Query          = query;
 }
Beispiel #10
0
 internal MergeStmt(WithClause with
                    , Table table
                    , Table usingTable
                    , AliasedQuery usingQuery
                    , Predicate constraint
                    , MergeUpdateClause updateClause
                    , MergeInsertClause insertClause
                    , bool updateBeforeInsert
                    , Comments comments)
 {
     this.Comments           = comments;
     this.With               = with;
     this.Table              = table;
     this.UsingTable         = usingTable;
     this.UsingQuery         = usingQuery;
     this.Constraint         = constraint;
     this.UpdateClause       = updateClause;
     this.InsertClause       = insertClause;
     this.UpdateBeforeInsert = updateBeforeInsert;
 }
 internal InsertSelectStmt(WithClause with
                           , bool isReplaceStmt
                           , ConflictType onConflict
                           , bool hasIntoKeyword
                           , Table table
                           , UnqualifiedColumnNames columns
                           , IQuery query
                           , Comments comments)
     : this(with
            , isReplaceStmt
            , onConflict
            , hasIntoKeyword
            , table
            , columns
            , query
            , null
            , null
            , null
            , null
            , comments)
 {
 }
 void IVisitor.VisitBefore(WithClause withClause)
 {
     this.ParentExists(withClause);
 }
 void IVisitor.VisitAfter(WithClause withClause)
 {
     this.ParentExists(withClause);
 }
Beispiel #14
0
 private void HandleWithClauseUsingNaturalJoin(WithClause withClause, Dictionary<ObjectReference, RelationType> relationTypeDict)
 {
     relationTypeDict[withClause.ObjectReference] = WithTypeToRelationType(withClause.Type, RelationType.None);
     _columns =
         _columns.Concat(
             _schema.FindTable(withClause.ObjectReference.GetName()).Columns.Select(
                 c => new ObjectReference(c.ActualName, withClause.ObjectReference)))
             .ToArray();
 }
Beispiel #15
0
 private void HandleWithClauseUsingAssociatedJoinClause(Dictionary<ObjectReference, RelationType> relationTypeDict, WithClause withClause)
 {
     var joinClause =
         _query.Clauses.OfType<JoinClause>().FirstOrDefault(
             j => j.Table.GetAliasOrName() == withClause.ObjectReference.GetAliasOrName());
     if (joinClause != null)
     {
         _columns =
             _columns.Concat(
                 _schema.FindTable(joinClause.Table.GetName()).Columns.Select(
                     c => new ObjectReference(c.ActualName, joinClause.Table)))
                 .ToArray();
         relationTypeDict[joinClause.Table] = WithTypeToRelationType(withClause.Type, RelationType.OneToMany);
     }
 }
Beispiel #16
0
 private void InferWithType(WithClause withClause)
 {
     var objectReference = withClause.ObjectReference;
     while (!ReferenceEquals(objectReference.GetOwner(), null))
     {
         var toTable = _schema.FindTable(objectReference.GetName());
         var fromTable = _schema.FindTable(objectReference.GetOwner().GetName());
         if (_schema.GetRelationType(fromTable.ActualName, toTable.ActualName) == RelationType.OneToMany)
         {
             withClause.Type = WithType.Many;
             return;
         }
         objectReference = objectReference.GetOwner();
     }
 }
Beispiel #17
0
 private bool TryApplyWithClause(WithClause clause, QueryCommand cmd)
 {
     cmd.Expand.Add(clause.ObjectReference.GetName());
     return true;
 }
        /// <summary>
        ///     Renders a temp table or table variable, and inserts the query into it.
        /// </summary>
        /// <param name="sb">The SQL text formatter.</param>
        public void RenderTempTable(SqlBuilderContext sb)
        {
            if (string.IsNullOrEmpty(Name))
            {
                throw new Exception("SqlSelectStatement.Name must be set when rendering as a temp table.");
            }

            bool tableVariable = Name.StartsWith("@");
            bool tempTable     = Name.StartsWith("#");

            if (!tableVariable && !tempTable)
            {
                throw new Exception("Table name must start with @ or # for a table variable, or temp table, respectively.");
            }

            // Table declaration
            if (tableVariable)
            {
                sb.AppendOnNewLine("declare " + Name + " as table");
            }
            else
            {
                sb.AppendOnNewLine("create table " + Name);
            }

            // Column declarations
            sb.AppendOnNewLine("(");
            sb.Indent( );
            var first = new First( );
            int col   = 0;

            foreach (SqlSelectItem item in RootQuery.SelectClause.Items)
            {
                string alias = item.Alias;
                if (string.IsNullOrEmpty(item.Alias))
                {
                    alias = "col" + (col++).ToString(CultureInfo.InvariantCulture);
                }
                //    throw new Exception("Queries rendered in temp tables must have an alias for every column.");

                if (!first)
                {
                    sb.Append(",");
                }

                const string colType = "bigint";                 // for now
                const string options = " primary key";           // for now
                sb.AppendOnNewLine(alias + " " + colType + options);
            }
            sb.EndIndent( );
            sb.AppendOnNewLine(")");

            // Insert query contents into temp table
            if (WithClause != null)
            {
                WithClause.RenderSql(sb);
            }
            sb.AppendOnNewLine("insert into " + Name);
            sb.Indent( );
            RootQuery.RenderSql(sb);
            sb.EndIndent( );
        }
        protected void HandleWithClauseUsingAssociatedJoinClause(Dictionary <ObjectReference, RelationType> relationTypeDict, WithClause withClause)
        {
            var joinClause =
                _query.Clauses.OfType <JoinClause>().FirstOrDefault(
                    j => j.Table.GetAliasOrName() == withClause.ObjectReference.GetAliasOrName());

            if (joinClause != null)
            {
                _columns =
                    _columns.Concat(
                        _schema.FindTable(joinClause.Table.GetName()).Columns.Select(
                            c => new ObjectReference(c.ActualName, joinClause.Table)))
                    .ToArray();
                relationTypeDict[joinClause.Table] = WithTypeToRelationType(withClause.Type, RelationType.OneToMany);
            }
        }
Beispiel #20
0
 private bool TryApplyWithClause(WithClause clause, QueryCommand cmd)
 {
     cmd.Expand.Add(clause.ObjectReference.GetName());
     return(true);
 }