Skip to content

Commit cce76fe

Browse files
committed
ADD CONSTRAINT IF NOT EXISTS didn't work in SP
"if not exists" must be stored in a separate read-only property
1 parent a5eff04 commit cce76fe

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

mysql-test/main/constraints.result

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ t1 CREATE TABLE `t1` (
183183
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
184184
DROP PROCEDURE sp;
185185
DROP TABLE t1;
186+
#
186187
# End of 10.2 tests
188+
#
187189
create table t1 (a int check (a>10)) select 100 as 'a';
188190
show create table t1;
189191
Table Create Table
@@ -201,3 +203,35 @@ a
201203
19
202204
ccc
203205
drop table t1;
206+
create table t1 (a int, b int);
207+
create procedure sp() alter table t1 add constraint if not exists foo check (b > 0);
208+
call sp;
209+
show create table t1;
210+
Table Create Table
211+
t1 CREATE TABLE `t1` (
212+
`a` int(11) DEFAULT NULL,
213+
`b` int(11) DEFAULT NULL,
214+
CONSTRAINT `foo` CHECK (`b` > 0)
215+
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
216+
call sp;
217+
Warnings:
218+
Note 1826 Duplicate CHECK constraint name 'foo'
219+
show create table t1;
220+
Table Create Table
221+
t1 CREATE TABLE `t1` (
222+
`a` int(11) DEFAULT NULL,
223+
`b` int(11) DEFAULT NULL,
224+
CONSTRAINT `foo` CHECK (`b` > 0)
225+
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
226+
call sp;
227+
Warnings:
228+
Note 1826 Duplicate CHECK constraint name 'foo'
229+
show create table t1;
230+
Table Create Table
231+
t1 CREATE TABLE `t1` (
232+
`a` int(11) DEFAULT NULL,
233+
`b` int(11) DEFAULT NULL,
234+
CONSTRAINT `foo` CHECK (`b` > 0)
235+
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
236+
drop procedure sp;
237+
drop table t1;

mysql-test/main/constraints.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ show create table t1;
151151
DROP PROCEDURE sp;
152152
DROP TABLE t1;
153153

154+
--echo #
154155
--echo # End of 10.2 tests
156+
--echo #
155157

156158
#
157159
# Check that we don't lose constraints as part of CREATE ... SELECT
@@ -172,3 +174,18 @@ insert into t1 values ("ccc");
172174
insert into t1 values ("");
173175
select * from t1;
174176
drop table t1;
177+
178+
#
179+
# add if not exists in SP
180+
#
181+
182+
create table t1 (a int, b int);
183+
create procedure sp() alter table t1 add constraint if not exists foo check (b > 0);
184+
call sp;
185+
show create table t1;
186+
call sp;
187+
show create table t1;
188+
call sp;
189+
show create table t1;
190+
drop procedure sp;
191+
drop table t1;

sql/field.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,6 @@ static inline const char *vcol_type_name(enum_vcol_info_type type)
558558
#define VCOL_AUTO_INC 16
559559
#define VCOL_IMPOSSIBLE 32
560560
#define VCOL_NEXTVAL 64 /* NEXTVAL is not implemented for vcols */
561-
#define VCOL_CHECK_CONSTRAINT_IF_NOT_EXISTS 128
562561

563562
#define VCOL_NOT_STRICTLY_DETERMINISTIC \
564563
(VCOL_NON_DETERMINISTIC | VCOL_TIME_FUNC | VCOL_SESSION_FUNC)
@@ -590,6 +589,7 @@ class Virtual_column_info: public Sql_alloc,
590589
bool stored_in_db;
591590
bool utf8; /* Already in utf8 */
592591
bool automatic_name;
592+
bool if_not_exists;
593593
Item *expr;
594594
Lex_ident name; /* Name of constraint */
595595
/* see VCOL_* (VCOL_FIELD_REF, ...) */

sql/sql_lex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4375,7 +4375,7 @@ struct LEX: public Query_tables_list
43754375
bool if_not_exists)
43764376
{
43774377
constr->name= name;
4378-
constr->flags= if_not_exists ? VCOL_CHECK_CONSTRAINT_IF_NOT_EXISTS : 0;
4378+
constr->if_not_exists= if_not_exists;
43794379
alter_info.check_constraint_list.push_back(constr);
43804380
return false;
43814381
}

sql/sql_table.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6928,10 +6928,8 @@ handle_if_exists_options(THD *thd, TABLE *table, Alter_info *alter_info,
69286928

69296929
while ((check=it++))
69306930
{
6931-
if (!(check->flags & VCOL_CHECK_CONSTRAINT_IF_NOT_EXISTS) &&
6932-
check->name.length)
6931+
if (!check->if_not_exists && check->name.length)
69336932
continue;
6934-
check->flags= 0;
69356933
for (c= share->field_check_constraints;
69366934
c < share->table_check_constraints ; c++)
69376935
{

0 commit comments

Comments
 (0)