Beispiel #1
0
        public static Statement CreateStatement(Delete delete, IEntityMapping mapping)
        {
            Statement statement = null;
            var       writer    = SqlWriter.CreateWriter().Delete;

            if (!delete.AllowMultiple)
            {
                writer.Top(1);
            }

            writer.From.QuotedName(delete.Table.Name);

            switch (delete.Filter)
            {
            case Delete.FilterType.Where:
                statement = WhereWriter <TEntity> .CreateStatement(delete.Where, mapping);

                writer.Where.Write(statement.Text);
                break;

            case Delete.FilterType.Select:
                var keyColumn = mapping.Key.GetColumnName();
                statement = SelectWriter <TEntity> .CreateStatement(delete.Select, mapping);

                writer.Where.Exists.OpenBlock.Trim().Select.QuotedName(keyColumn).From.OpenBlock.Trim().
                Write(statement.Text).Trim().CloseBlock.As.SubQueryAlias.Where.SubQueryColumn(keyColumn).Equal.QuotedName(delete.Table.Name, keyColumn).Trim().CloseBlock.Flush();
                break;
            }

            return(new Statement(writer.ToString(), Statement.StatementType.Text,
                                 Statement.ResultType.None, statement.Parameters));
        }
Beispiel #2
0
        public static Statement CreateTableColumnsStatement(string tableName)
        {
            var writer = WriteSelectColumns(SqlWriter.CreateWriter()).
                         Where.Write(System.Columns.Aliased.ObjectId).Equal.ObjectId(tableName);

            return(new Statement(writer.ToString(), Statement.StatementType.Text, Statement.ResultType.Multiple));
        }
Beispiel #3
0
 private Statement Write(Action write)
 {
     _sql        = SqlWriter.CreateWriter();
     _parameters = new Dictionary <string, object>();
     write();
     return(new Statement(_sql.ToString(), Statement.StatementType.Text, _parameters));
 }
Beispiel #4
0
        public static Statement CreateAddNonClusteredIndexStatement(string tableName, params Index.Column[] columns)
        {
            var indexName = string.Format("IX_{0}_{1}", tableName, string.Join("_", columns.Select(x => x.Name)));
            var writer    = SqlWriter.CreateWriter().Create.NonClustered.Index.QuotedName(indexName).On.QuotedName(tableName).OpenBlock.Trim();
            var first     = true;

            foreach (var column in columns)
            {
                if (!first)
                {
                    writer.Trim().Comma.Flush();
                }
                writer.QuotedName(column.Name).Do(column.Descending, x => x.Descending.Flush(), x => x.Ascending.Flush()).Flush();
                first = false;
            }
            writer.Trim().CloseBlock.Flush();
            return(new Statement(writer.ToString(), Statement.StatementType.Text, Statement.ResultType.None));
        }
Beispiel #5
0
        public static Statement CreateSharedColumnsStatement(Select select, Table target = null)
        {
            var sourceTables = select.GetSourceTables().Select(x => x.From.Table.Name).ToList();

            if (target != null)
            {
                sourceTables.Add(target.Name);
            }
            var targetName = target != null ? target.Name : sourceTables.First();
            var statement  = CreateColumnsIntersectionStatement(sourceTables);

            var writer = WriteSelectColumns(SqlWriter.CreateWriter(), sourceTables).
                         Join.OpenBlock.Trim().Write(statement.Text).Trim().CloseBlock.SubQueryAlias.On.
                         SubQueryColumn(System.Columns.Name).Equal.Write(System.Columns.Aliased.Name).And.
                         Write(System.Columns.Aliased.ObjectId).Equal.ObjectId(targetName);

            return(new Statement(writer.ToString(), Statement.StatementType.Text, Statement.ResultType.Multiple));
        }
Beispiel #6
0
        public static Statement CreateTableCreateStatement(string tableName, params Column[] columns)
        {
            var writer = SqlWriter.CreateWriter().Create.Table.QuotedName(tableName).OpenBlock.Trim();
            var first  = true;

            foreach (var column in columns)
            {
                if (!first)
                {
                    writer.Trim().Comma.Flush();
                }
                WriteColumnDefinition(writer, column);
                first = false;
            }
            var primaryKey = columns.FirstOrDefault(x => x.Key != Column.KeyType.None);

            if (primaryKey != null)
            {
                writer.Trim().Comma.PrimaryKeyConstraint(
                    tableName, primaryKey.Name, primaryKey.Key == Column.KeyType.ClusteredPrimaryKey);
            }
            writer.Trim().CloseBlock.Flush();
            return(new Statement(writer.ToString(), Statement.StatementType.Text, Statement.ResultType.None));
        }
Beispiel #7
0
        public static Statement CreateRemoveNonClusteredIndexStatement(string tableName, string indexName)
        {
            var writer = SqlWriter.CreateWriter().IfIndexExists(tableName, indexName).Drop.Index.QuotedName(indexName).On.QuotedName(tableName);

            return(new Statement(writer.ToString(), Statement.StatementType.Text, Statement.ResultType.None));
        }
Beispiel #8
0
        public static Statement CreateRemoveColumnStatement(string tableName, string columnName)
        {
            var writer = SqlWriter.CreateWriter().IfColumnExists(tableName, columnName).Alter.Table.QuotedName(tableName).Drop.Column.QuotedName(columnName);

            return(new Statement(writer.ToString(), Statement.StatementType.Text, Statement.ResultType.None));
        }
Beispiel #9
0
        public static Statement CreateDeleteTableStatement(string tableName)
        {
            var writer = SqlWriter.CreateWriter().If.TableExists(tableName).Drop.Table.QuotedName(tableName);

            return(new Statement(writer.ToString(), Statement.StatementType.Text, Statement.ResultType.None));
        }
Beispiel #10
0
        public static Statement CreateProcedureExistsStatement(string name)
        {
            var writer = SqlWriter.CreateWriter().Select.ProcedureExistsValue(name);

            return(new Statement(writer.ToString(), Statement.StatementType.Text, Statement.ResultType.Scalar));
        }