Exemplo n.º 1
0
        protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
        {
            var table = dropTable.Table;

            if (table.PhysicalName.StartsWith("#"))
            {
                AppendIndent().Append("DROP TABLE ");
                BuildPhysicalTable(table, null);
                StringBuilder.AppendLine();
            }
            else
            {
                if (dropTable.IfExists)
                {
                    StringBuilder.Append("IF (OBJECT_ID(N'");
                    BuildPhysicalTable(table, null);
                    StringBuilder.AppendLine("', N'U') IS NOT NULL)");
                    Indent++;
                }

                AppendIndent().Append("DROP TABLE ");
                BuildPhysicalTable(table, null);

                if (dropTable.IfExists)
                {
                    Indent--;
                }
            }
        }
Exemplo n.º 2
0
            public static void Query(IDataContext dataContext, string tableName, string databaseName, string schemaName, bool ifExists)
            {
                var sqlTable  = new SqlTable <T>(dataContext.MappingSchema);
                var dropTable = new SqlDropTableStatement(ifExists);

                if (tableName != null)
                {
                    sqlTable.PhysicalName = tableName;
                }
                if (databaseName != null)
                {
                    sqlTable.Database = databaseName;
                }
                if (schemaName != null)
                {
                    sqlTable.Schema = schemaName;
                }

                dropTable.Table = sqlTable;

                var query = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    Statement = dropTable,
                                } }
                };

                SetNonQueryQuery(query);

                query.GetElement(dataContext, Expression.Constant(null), null);
            }
Exemplo n.º 3
0
        protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
        {
            var table = dropTable.Table !;

            BuildTag(dropTable);

            if (dropTable.Table.TableOptions.HasDropIfExists())
            {
                var defaultDatabaseName =
                    table.PhysicalName !.StartsWith("#") || table.TableOptions.IsTemporaryOptionSet() ?
                    "[tempdb]" : null;

                StringBuilder.Append("IF (OBJECT_ID(N'");
                BuildPhysicalTable(table, alias: null, defaultDatabaseName: defaultDatabaseName);
                StringBuilder.AppendLine("', N'U') IS NOT NULL)");
                Indent++;
            }

            AppendIndent().Append("DROP TABLE ");
            BuildPhysicalTable(table, alias: null);

            if (dropTable.Table.TableOptions.HasDropIfExists())
            {
                Indent--;
            }
        }
Exemplo n.º 4
0
        protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
        {
            var table = dropTable.Table !;

            if (dropTable.Table.TableOptions.HasDropIfExists())
            {
                var defaultDatabaseName = IsTemporary(table) ? "tempdb" : null;

                _skipBrackets = true;
                StringBuilder.Append("IF (OBJECT_ID(N'");
                BuildPhysicalTable(table, null, defaultDatabaseName: defaultDatabaseName);
                StringBuilder.AppendLine("') IS NOT NULL)");
                _skipBrackets = false;

                Indent++;
            }

            AppendIndent().Append("DROP TABLE ");
            BuildPhysicalTable(table, null);

            if (dropTable.Table.TableOptions.HasDropIfExists())
            {
                Indent--;
            }
        }
Exemplo n.º 5
0
        protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
        {
            var table = dropTable.Table !;

            BuildTag(dropTable);

            if (dropTable.Table.TableOptions.HasDropIfExists())
            {
                _skipBrackets = true;
                StringBuilder.Append("IF (OBJECT_ID(N'");
                BuildPhysicalTable(table, null);
                StringBuilder.AppendLine("') IS NOT NULL)");
                _skipBrackets = false;

                Indent++;
            }

            AppendIndent().Append("DROP TABLE ");
            BuildPhysicalTable(table, null);

            if (dropTable.Table.TableOptions.HasDropIfExists())
            {
                Indent--;
            }
        }
Exemplo n.º 6
0
            public static async Task QueryAsync(IDataContext dataContext, string tableName, string databaseName, string ownerName, CancellationToken token)
            {
                var sqlTable  = new SqlTable <T>(dataContext.MappingSchema);
                var dropTable = new SqlDropTableStatement();

                if (tableName != null)
                {
                    sqlTable.PhysicalName = tableName;
                }
                if (databaseName != null)
                {
                    sqlTable.Database = databaseName;
                }
                if (ownerName != null)
                {
                    sqlTable.Owner = ownerName;
                }

                dropTable.Table = sqlTable;

                var query = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    Statement = dropTable,
                                } }
                };

                SetNonQueryQuery(query);

                await query.GetElementAsync(dataContext, Expression.Constant(null), null, token);
            }
Exemplo n.º 7
0
            public static async Task QueryAsync(
                IDataContext dataContext,
                string tableName, string databaseName, string schemaName,
                bool ifExists,
                CancellationToken token)
            {
                var sqlTable  = new SqlTable <T>(dataContext.MappingSchema);
                var dropTable = new SqlDropTableStatement(ifExists);

                if (tableName != null)
                {
                    sqlTable.PhysicalName = tableName;
                }
                if (databaseName != null)
                {
                    sqlTable.Database = databaseName;
                }
                if (schemaName != null)
                {
                    sqlTable.Schema = schemaName;
                }

                dropTable.Table = sqlTable;

                var query = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    Statement = dropTable,
                                } }
                };

                SetNonQueryQuery(query);

                await query.GetElementAsync(dataContext, Expression.Constant(null), null, token).ConfigureAwait(Common.Configuration.ContinueOnCapturedContext);
            }
Exemplo n.º 8
0
        protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
        {
            var table = dropTable.Table;

            AppendIndent().Append("DROP TABLE ");
            BuildPhysicalTable(table, null);
            StringBuilder.AppendLine();
        }
Exemplo n.º 9
0
 protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
 {
     if (_identityField == null)
     {
         base.BuildDropTableStatement(dropTable);
     }
     else
     {
         StringBuilder
         .Append("DROP TRIGGER TIDENTITY_")
         .Append(dropTable.Table.PhysicalName)
         .AppendLine();
     }
 }
Exemplo n.º 10
0
            public static async Task QueryAsync(
                IDataContext dataContext,
                string?tableName,
                string?serverName,
                string?databaseName,
                string?schemaName,
                bool?ifExists,
                TableOptions tableOptions,
                CancellationToken token)
            {
                var sqlTable  = new SqlTable <T>(dataContext.MappingSchema);
                var dropTable = new SqlDropTableStatement(sqlTable);

                if (tableName != null)
                {
                    sqlTable.PhysicalName = tableName;
                }
                if (serverName != null)
                {
                    sqlTable.Server = serverName;
                }
                if (databaseName != null)
                {
                    sqlTable.Database = databaseName;
                }
                if (schemaName != null)
                {
                    sqlTable.Schema = schemaName;
                }
                if (tableOptions.IsSet())
                {
                    sqlTable.TableOptions = tableOptions;
                }

                sqlTable.Set(ifExists, TableOptions.DropIfExists);

                var query = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    Statement = dropTable,
                                } }
                };

                SetNonQueryQuery(query);

                await query.GetElementAsync(dataContext, ExpressionInstances.UntypedNull, null, null, token).ConfigureAwait(Common.Configuration.ContinueOnCapturedContext);
            }
Exemplo n.º 11
0
            public static void Query(
                IDataContext dataContext,
                string?tableName,
                string?serverName,
                string?databaseName,
                string?schemaName,
                bool?ifExists,
                TableOptions tableOptions)
            {
                var sqlTable  = new SqlTable <T>(dataContext.MappingSchema);
                var dropTable = new SqlDropTableStatement(sqlTable);

                if (tableName != null)
                {
                    sqlTable.PhysicalName = tableName;
                }
                if (serverName != null)
                {
                    sqlTable.Server = serverName;
                }
                if (databaseName != null)
                {
                    sqlTable.Database = databaseName;
                }
                if (schemaName != null)
                {
                    sqlTable.Schema = schemaName;
                }
                if (tableOptions.IsSet())
                {
                    sqlTable.TableOptions = tableOptions;
                }

                sqlTable.Set(ifExists, TableOptions.DropIfExists);

                var query = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    Statement = dropTable
                                } }
                };

                SetNonQueryQuery(query);

                query.GetElement(dataContext, Expression.Constant(null), null, null);
            }
Exemplo n.º 12
0
        protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
        {
            if (_identityField == null)
            {
                base.BuildDropTableStatement(dropTable);
            }
            else
            {
                var schemaPrefix = string.IsNullOrWhiteSpace(dropTable.Table.Schema)
                                        ? string.Empty
                                        : dropTable.Table.Schema + ".";

                StringBuilder
                .Append("DROP TRIGGER ")
                .Append(schemaPrefix)
                .Append("TIDENTITY_")
                .Append(dropTable.Table.PhysicalName)
                .AppendLine();
            }
        }
Exemplo n.º 13
0
        protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
        {
            var table = dropTable.Table;

            if (table.PhysicalName.StartsWith("#"))
            {
                AppendIndent().Append("DROP TABLE ");
                BuildPhysicalTable(table, null);
                StringBuilder.AppendLine();
            }
            else
            {
                StringBuilder.Append("IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'");
                BuildPhysicalTable(table, null);
                StringBuilder.AppendLine("') AND type in (N'U'))");

                AppendIndent().Append("BEGIN DROP TABLE ");
                BuildPhysicalTable(table, null);
                StringBuilder.AppendLine(" END");
            }
        }
Exemplo n.º 14
0
        protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
        {
            var table = dropTable.Table !;

            if (dropTable.Table.TableOptions.HasDropIfExists())
            {
                AppendIndent().Append(@"BEGIN
	DECLARE CONTINUE HANDLER FOR SQLSTATE '42704' BEGIN END;
	EXECUTE IMMEDIATE 'DROP TABLE "    );
                BuildPhysicalTable(table, null);
                StringBuilder.AppendLine(
                    @"';
END");
            }
            else
            {
                AppendIndent().Append("DROP TABLE ");
                BuildPhysicalTable(table, null);
                StringBuilder.AppendLine();
            }
        }
Exemplo n.º 15
0
        protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
        {
            // implementation use following approach: http://www.firebirdfaq.org/faq69/
            StringBuilder
            .AppendLine("EXECUTE BLOCK AS BEGIN");

            Indent++;

            if (_identityField != null)
            {
                BuildDropWithSchemaCheck("TRIGGER", "rdb$triggers", "rdb$trigger_name", "TIDENTITY_" + dropTable.Table.PhysicalName);
                BuildDropWithSchemaCheck("GENERATOR", "rdb$generators", "rdb$generator_name", "GIDENTITY_" + dropTable.Table.PhysicalName);
            }

            BuildDropWithSchemaCheck("TABLE", "rdb$relations", "rdb$relation_name", dropTable.Table.PhysicalName);

            Indent--;

            StringBuilder
            .AppendLine("END");
        }
Exemplo n.º 16
0
            public static void Query(
                IDataContext dataContext,
                string?tableName,
                string?serverName,
                string?databaseName,
                string?schemaName,
                bool?ifExists,
                TableOptions tableOptions)
            {
                var sqlTable  = new SqlTable <T>(dataContext.MappingSchema);
                var dropTable = new SqlDropTableStatement(sqlTable);

                if (tableName != null || schemaName != null || databaseName != null || databaseName != null)
                {
                    sqlTable.TableName = new(
                        tableName ?? sqlTable.TableName.Name,
                        Server  : serverName ?? sqlTable.TableName.Server,
                        Database : databaseName ?? sqlTable.TableName.Database,
                        Schema  : schemaName ?? sqlTable.TableName.Schema);
                }

                if (tableOptions.IsSet())
                {
                    sqlTable.TableOptions = tableOptions;
                }

                sqlTable.Set(ifExists, TableOptions.DropIfExists);

                var query = new Query <int>(dataContext, null)
                {
                    Queries = { new QueryInfo {
                                    Statement = dropTable
                                } }
                };

                SetNonQueryQuery(query);

                query.GetElement(dataContext, ExpressionInstances.UntypedNull, null, null);
            }
Exemplo n.º 17
0
 protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
 {
     BuildDropTableStatementIfExists(dropTable);
 }
Exemplo n.º 18
0
        protected override void BuildDropTableStatement(SqlDropTableStatement dropTable)
        {
            var identityField = dropTable.Table !.Fields.Values.FirstOrDefault(f => f.IsIdentity);

            if (identityField == null && dropTable.IfExists == false)
            {
                base.BuildDropTableStatement(dropTable);
                return;
            }

            // implementation use following approach: http://www.firebirdfaq.org/faq69/
            StringBuilder
            .AppendLine("EXECUTE BLOCK AS BEGIN");

            Indent++;

            if (identityField != null)
            {
                BuildDropWithSchemaCheck("TRIGGER", "rdb$triggers", "rdb$trigger_name", "TIDENTITY_" + dropTable.Table.PhysicalName);
                BuildDropWithSchemaCheck("GENERATOR", "rdb$generators", "rdb$generator_name", "GIDENTITY_" + dropTable.Table.PhysicalName);
            }

            BuildDropWithSchemaCheck("TABLE", "rdb$relations", "rdb$relation_name", dropTable.Table.PhysicalName !);

            Indent--;

            StringBuilder
            .AppendLine("END");

            void BuildDropWithSchemaCheck(string objectName, string schemaTable, string nameColumn, string identifier)
            {
                if (dropTable.IfExists)
                {
                    AppendIndent().AppendFormat("IF (EXISTS(SELECT 1 FROM {0} WHERE {1} = ", schemaTable, nameColumn);

                    var identifierValue = identifier;

                    // if identifier is not quoted, it must be converted to upper case to match record in rdb$relation_name
                    if (FirebirdConfiguration.IdentifierQuoteMode == FirebirdIdentifierQuoteMode.None ||
                        FirebirdConfiguration.IdentifierQuoteMode == FirebirdIdentifierQuoteMode.Auto && IsValidIdentifier(identifierValue))
                    {
                        identifierValue = identifierValue.ToUpper();
                    }

                    BuildValue(null, identifierValue);

                    StringBuilder
                    .AppendLine(")) THEN");

                    Indent++;
                }

                AppendIndent().Append("EXECUTE STATEMENT ");

                var dropCommand = new StringBuilder();

                dropCommand
                .Append("DROP ")
                .Append(objectName)
                .Append(" ");
                Convert(dropCommand, identifier, ConvertType.NameToQueryTable);

                BuildValue(null, dropCommand.ToString());

                StringBuilder.AppendLine(";");

                if (dropTable.IfExists)
                {
                    Indent--;
                }
            }
        }
Exemplo n.º 19
0
 public override void Visit(SqlDropTableStatement codeObject)
 {
 }