Skip to content

Commit 77ee904

Browse files
authored
Feature #4203 - DROP [IF EXISTS] (#7946)
1 parent 2f44407 commit 77ee904

File tree

5 files changed

+210
-85
lines changed

5 files changed

+210
-85
lines changed

doc/sql.extensions/README.ddl.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,32 @@ Only users with administrator rights can use this option.
629629
(Dmitry Sibiryakov)
630630

631631
If is used twice, an error is returned.
632+
633+
634+
DDL enhancements in Firebird v6.
635+
--------------------------------
636+
637+
1) DROP [IF EXISTS]
638+
639+
Using subclause IF EXISTS, it's now possible to try to drop objects and do not get errors when they di not exists.
640+
641+
DROP EXCEPTION [IF EXISTS] <exception>
642+
DROP INDEX [IF EXISTS] <index>
643+
DROP PROCEDURE [IF EXISTS] <procedure>
644+
DROP TABLE [IF EXISTS] <table>
645+
DROP TRIGGER [IF EXISTS] <trigger>
646+
DROP VIEW [IF EXISTS] <view>
647+
DROP FILTER [IF EXISTS] <filter>
648+
DROP DOMAIN [IF EXISTS] <domain>
649+
DROP [EXTERNAL] FUNCTION [IF EXISTS] <function>
650+
DROP SHADOW [IF EXISTS] <shadow>
651+
DROP ROLE [IF EXISTS] <role>
652+
DROP GENERATOR [IF EXISTS] <generator>
653+
DROP SEQUENCE [IF EXISTS] <sequence>
654+
DROP COLLATION [IF EXISTS] <collation>
655+
DROP USER [IF EXISTS] <user> [USING PLUGIN <plugin>]
656+
DROP PACKAGE [IF EXISTS] <package>
657+
DROP PACKAGE BODY [IF EXISTS] <package>
658+
DROP [GLOBAL] MAPPING [IF EXISTS] <mapping>
659+
ALTER TABLE <table> DROP [IF EXISTS] <column>
660+
ALTER TABLE <table> DROP CONSTRAINT [IF EXISTS] <constraint>

src/dsql/DdlNodes.epp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4335,7 +4335,7 @@ void DropCollationNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScratc
43354335
executeDdlTrigger(tdbb, dsqlScratch, transaction, DTW_AFTER, DDL_TRIGGER_DROP_COLLATION,
43364336
name, NULL);
43374337
}
4338-
else
4338+
else if (!silent)
43394339
status_exception::raise(Arg::Gds(isc_dyn_collation_not_found) << Arg::Str(name));
43404340

43414341
savePoint.release(); // everything is ok
@@ -5406,7 +5406,7 @@ void DropDomainNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScratch,
54065406
executeDdlTrigger(tdbb, dsqlScratch, transaction, DTW_AFTER, DDL_TRIGGER_DROP_DOMAIN,
54075407
name, NULL);
54085408
}
5409-
else
5409+
else if (!silent)
54105410
{
54115411
// msg 89: "Domain not found"
54125412
status_exception::raise(Arg::PrivateDyn(89));
@@ -6236,7 +6236,7 @@ void RelationNode::FieldDefinition::store(thread_db* tdbb, jrd_tra* transaction)
62366236
// done by code and system triggers. See the functional description of
62376237
// deleteKeyConstraint function for detail.
62386238
void RelationNode::deleteLocalField(thread_db* tdbb, jrd_tra* transaction,
6239-
const MetaName& relationName, const MetaName& fieldName)
6239+
const MetaName& relationName, const MetaName& fieldName, bool silent)
62406240
{
62416241
AutoCacheRequest request(tdbb, drq_l_dep_flds, DYN_REQUESTS);
62426242
bool found = false;
@@ -6358,7 +6358,7 @@ void RelationNode::deleteLocalField(thread_db* tdbb, jrd_tra* transaction,
63586358
}
63596359
END_FOR
63606360

6361-
if (!found)
6361+
if (!found && !silent)
63626362
{
63636363
// msg 176: "column %s does not exist in table/view %s"
63646364
status_exception::raise(Arg::PrivateDyn(176) << fieldName << relationName);
@@ -7828,7 +7828,7 @@ void AlterRelationNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScratc
78287828
Arg::Gds(isc_dsql_construct_err));
78297829
}
78307830

7831-
deleteLocalField(tdbb, transaction, name, clause->name);
7831+
deleteLocalField(tdbb, transaction, name, clause->name, clause->silent);
78327832
break;
78337833
}
78347834

@@ -7840,8 +7840,8 @@ void AlterRelationNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScratc
78407840
case Clause::TYPE_DROP_CONSTRAINT:
78417841
{
78427842
CreateDropConstraint& dropConstraint = constraints.add();
7843-
dropConstraint.name =
7844-
static_cast<const DropConstraintClause*>(i->getObject())->name;
7843+
dropConstraint.name = static_cast<const DropConstraintClause*>(i->getObject())->name;
7844+
dropConstraint.silent = static_cast<const DropConstraintClause*>(i->getObject())->silent;
78457845
break;
78467846
}
78477847

@@ -7933,7 +7933,7 @@ void AlterRelationNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScratc
79337933
}
79347934
END_FOR
79357935

7936-
if (!found)
7936+
if (!found && !constraint->silent)
79377937
{
79387938
// msg 130: "CONSTRAINT %s does not exist."
79397939
status_exception::raise(Arg::PrivateDyn(130) << constraint->name);
@@ -9222,7 +9222,7 @@ void CreateAlterViewNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScra
92229222
for (dsql_fld* relField = modifyingView->rel_fields; relField; relField = relField->fld_next)
92239223
{
92249224
if (!modifiedFields.exist(relField))
9225-
deleteLocalField(tdbb, transaction, name, relField->fld_name);
9225+
deleteLocalField(tdbb, transaction, name, relField->fld_name, false);
92269226
}
92279227
}
92289228

@@ -10154,7 +10154,7 @@ void DropIndexNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScratch, j
1015410154
executeDdlTrigger(tdbb, dsqlScratch, transaction, DTW_AFTER, DDL_TRIGGER_DROP_INDEX,
1015510155
name, NULL);
1015610156
}
10157-
else
10157+
else if (!silent)
1015810158
{
1015910159
// msg 48: "Index not found"
1016010160
status_exception::raise(Arg::PrivateDyn(48));
@@ -10283,7 +10283,7 @@ void DropFilterNode::execute(thread_db* tdbb, DsqlCompilerScratch* /*dsqlScratch
1028310283
}
1028410284
END_FOR
1028510285

10286-
if (!found)
10286+
if (!found && !silent)
1028710287
{
1028810288
// msg 37: "Blob Filter %s not found"
1028910289
status_exception::raise(Arg::PrivateDyn(37) << name);
@@ -10990,19 +10990,24 @@ void MappingNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScratch, jrd
1099010990
break;
1099110991

1099210992
case MAP_MOD:
10993-
case MAP_DROP:
1099410993
case MAP_COMMENT:
1099510994
if (!found)
1099610995
(Arg::Gds(isc_map_not_exists) << name).raise();
1099710996
break;
10997+
10998+
case MAP_DROP:
10999+
if (!found && !silentDrop)
11000+
(Arg::Gds(isc_map_not_exists) << name).raise();
11001+
break;
1099811002
}
1099911003

11000-
fb_assert(ddlTriggerAction > 0 || op == MAP_COMMENT);
11004+
fb_assert(ddlTriggerAction > 0 || op == MAP_COMMENT || (op == MAP_DROP && silentDrop));
1100111005
if (ddlTriggerAction > 0)
1100211006
executeDdlTrigger(tdbb, dsqlScratch, transaction, DTW_AFTER, ddlTriggerAction, name, NULL);
1100311007

1100411008
if (op != MAP_COMMENT)
1100511009
DFW_post_work(transaction, dfw_clear_cache, NULL, Mapping::MAPPING_CACHE);
11010+
1100611011
savePoint.release(); // everything is ok
1100711012
}
1100811013

@@ -11083,7 +11088,7 @@ void DropRoleNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScratch, jr
1108311088
executeDdlTrigger(tdbb, dsqlScratch, transaction, DTW_AFTER, DDL_TRIGGER_DROP_ROLE,
1108411089
name, NULL);
1108511090
}
11086-
else
11091+
else if (!silent)
1108711092
{
1108811093
// msg 155: "Role %s not found"
1108911094
status_exception::raise(Arg::PrivateDyn(155) << name);

src/dsql/DdlNodes.h

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ class DropCollationNode : public DdlNode
918918

919919
public:
920920
MetaName name;
921+
bool silent = false;
921922
};
922923

923924

@@ -1025,6 +1026,7 @@ class DropDomainNode : public DdlNode
10251026

10261027
public:
10271028
MetaName name;
1029+
bool silent = false;
10281030
};
10291031

10301032

@@ -1301,6 +1303,7 @@ class RelationNode : public DdlNode
13011303

13021304
MetaName name;
13031305
Firebird::AutoPtr<Constraint> create;
1306+
bool silent = false;
13041307
};
13051308

13061309
struct Clause
@@ -1484,13 +1487,13 @@ class RelationNode : public DdlNode
14841487
{
14851488
explicit DropColumnClause(MemoryPool& p)
14861489
: Clause(p, TYPE_DROP_COLUMN),
1487-
name(p),
1488-
cascade(false)
1490+
name(p)
14891491
{
14901492
}
14911493

14921494
MetaName name;
1493-
bool cascade;
1495+
bool cascade = false;
1496+
bool silent = false;
14941497
};
14951498

14961499
struct DropConstraintClause : public Clause
@@ -1502,12 +1505,13 @@ class RelationNode : public DdlNode
15021505
}
15031506

15041507
MetaName name;
1508+
bool silent = false;
15051509
};
15061510

15071511
RelationNode(MemoryPool& p, RelationSourceNode* aDsqlNode);
15081512

15091513
static void deleteLocalField(thread_db* tdbb, jrd_tra* transaction,
1510-
const MetaName& relationName, const MetaName& fieldName);
1514+
const MetaName& relationName, const MetaName& fieldName, bool silent);
15111515

15121516
static void addToPublication(thread_db* tdbb, jrd_tra* transaction,
15131517
const MetaName& tableName, const MetaName& pubTame);
@@ -1858,6 +1862,7 @@ class DropIndexNode : public DdlNode
18581862

18591863
public:
18601864
MetaName name;
1865+
bool silent = false;
18611866
};
18621867

18631868

@@ -1946,6 +1951,7 @@ class DropFilterNode : public DdlNode
19461951

19471952
public:
19481953
MetaName name;
1954+
bool silent = false;
19491955
};
19501956

19511957

@@ -2081,16 +2087,7 @@ class MappingNode : public DdlNode, private ExecInSecurityDb
20812087
: DdlNode(p),
20822088
name(p, nm),
20832089
fromUtf8(p),
2084-
plugin(NULL),
2085-
db(NULL),
2086-
fromType(NULL),
2087-
from(NULL),
2088-
to(NULL),
2089-
comment(NULL),
2090-
op(o),
2091-
mode('#'),
2092-
global(false),
2093-
role(false)
2090+
op(o)
20942091
{
20952092
}
20962093

@@ -2117,16 +2114,17 @@ class MappingNode : public DdlNode, private ExecInSecurityDb
21172114
public:
21182115
MetaName name;
21192116
Firebird::string fromUtf8;
2120-
MetaName* plugin;
2121-
MetaName* db;
2122-
MetaName* fromType;
2123-
IntlString* from;
2124-
MetaName* to;
2125-
Firebird::string* comment;
2117+
MetaName* plugin = nullptr;
2118+
MetaName* db = nullptr;
2119+
MetaName* fromType = nullptr;
2120+
IntlString* from = nullptr;
2121+
MetaName* to = nullptr;
2122+
Firebird::string* comment = nullptr;
21262123
OP op;
2127-
char mode; // * - any source, P - plugin, M - mapping, S - any serverwide plugin
2128-
bool global;
2129-
bool role;
2124+
char mode = '#'; // * - any source, P - plugin, M - mapping, S - any serverwide plugin
2125+
bool global = false;
2126+
bool role = false;
2127+
bool silentDrop = false;
21302128
};
21312129

21322130

@@ -2152,6 +2150,7 @@ class DropRoleNode : public DdlNode
21522150

21532151
public:
21542152
MetaName name;
2153+
bool silent = false;
21552154
};
21562155

21572156

src/dsql/parse-conflicts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
71 shift/reduce conflicts, 22 reduce/reduce conflicts.
1+
94 shift/reduce conflicts, 22 reduce/reduce conflicts.

0 commit comments

Comments
 (0)