Skip to content

Commit 041c282

Browse files
AliSQLAliSQL
authored andcommitted
[Feature] Issue#51 Support oracle sequence syntax
Description ----------- Support Oracle database sequence object query syntax: select seqName.nextval from dual; select seqName.currval from dual;
1 parent 9d4d580 commit 041c282

9 files changed

Lines changed: 360 additions & 3 deletions

mysql-test/r/feature_sequence.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ select * for s2;
186186
currval nextval minvalue maxvalue start increment cache cycle round
187187
2 2 1 9223372036854775807 1 1 10000 0 0
188188
select * for t2;
189-
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
189+
ERROR HY000: 's_db.t2' is not a SEQUENCE
190190
select * from s2, t2;
191191
currval nextval minvalue maxvalue start increment cache cycle round id
192192
0 10002 1 9223372036854775807 1 1 10000 0 0 1
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
include/master-slave.inc
2+
Warnings:
3+
Note #### Sending passwords in plain text without SSL/TLS is extremely insecure.
4+
Note #### Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
5+
[connection master]
6+
create database s_db;
7+
grant all on s_db.* to normal_1@'%' identified by 'pass';
8+
grant all on test.* to normal_2@'%' identified by 'pass';
9+
grant all on s_db.* to normal_3@'%' identified by 'pass';
10+
grant all on test.* to normal_4@'%' identified by 'pass';
11+
create table s_db.t1(id int, col1 int)engine=innodb;
12+
set global read_only=on;
13+
###########################################
14+
master and slave sync sequence.
15+
###########################################
16+
use s_db;
17+
create sequence s1;
18+
show create table s1;
19+
Table Create Table
20+
s1 CREATE SEQUENCE `s1` (
21+
`currval` bigint(21) NOT NULL COMMENT 'current value',
22+
`nextval` bigint(21) NOT NULL COMMENT 'next value',
23+
`minvalue` bigint(21) NOT NULL COMMENT 'min value',
24+
`maxvalue` bigint(21) NOT NULL COMMENT 'max value',
25+
`start` bigint(21) NOT NULL COMMENT 'start value',
26+
`increment` bigint(21) NOT NULL COMMENT 'increment value',
27+
`cache` bigint(21) NOT NULL COMMENT 'cache size',
28+
`cycle` bigint(21) NOT NULL COMMENT 'cycle state',
29+
`round` bigint(21) NOT NULL COMMENT 'already how many round'
30+
) ENGINE=InnoDB DEFAULT CHARSET=latin1
31+
use s_db;
32+
show create table s1;
33+
Table Create Table
34+
s1 CREATE SEQUENCE `s1` (
35+
`currval` bigint(21) NOT NULL COMMENT 'current value',
36+
`nextval` bigint(21) NOT NULL COMMENT 'next value',
37+
`minvalue` bigint(21) NOT NULL COMMENT 'min value',
38+
`maxvalue` bigint(21) NOT NULL COMMENT 'max value',
39+
`start` bigint(21) NOT NULL COMMENT 'start value',
40+
`increment` bigint(21) NOT NULL COMMENT 'increment value',
41+
`cache` bigint(21) NOT NULL COMMENT 'cache size',
42+
`cycle` bigint(21) NOT NULL COMMENT 'cycle state',
43+
`round` bigint(21) NOT NULL COMMENT 'already how many round'
44+
) ENGINE=InnoDB DEFAULT CHARSET=latin1
45+
use s_db;
46+
drop sequence s1;
47+
###########################################
48+
test keyword
49+
###########################################
50+
use s_db;
51+
create table t_2(currval int, nextval int);
52+
select t_2.currval from dual;
53+
ERROR HY000: 's_db.t_2' is not a SEQUENCE
54+
select currval from t_2;
55+
currval
56+
drop table t_2;
57+
###########################################
58+
dml with func
59+
###########################################
60+
use s_db;
61+
create sequence s1 start with 1 minvalue 1 maxvalue 7 cache 2 cycle increment by 2;
62+
select s1.currval from dual;
63+
ERROR HY000: Sequence 's_db.s1' is not yet defined in this session
64+
select s1.currval from dual;
65+
ERROR HY000: Sequence 's_db.s1' is not yet defined in this session
66+
select s1.nextval from dual;
67+
s1.nextval
68+
1
69+
insert into t1 select s1.nextval, s1.currval from dual;
70+
select * from t1;
71+
id col1
72+
3 3
73+
commit;
74+
drop sequence s1;
75+
###########################################
76+
func, trigger, procedure with sequence func
77+
###########################################
78+
use s_db;
79+
create sequence s1 start with 1 minvalue 1 cache 2 cycle increment by 2;
80+
CREATE FUNCTION `test_func_1` () RETURNS int
81+
BEGIN
82+
RETURN (select s1.nextval from dual);
83+
END$$
84+
CREATE FUNCTION `test_func_2` () RETURNS int
85+
BEGIN
86+
RETURN (select s1.currval from dual);
87+
END$$
88+
select test_func_2();
89+
ERROR HY000: Sequence 's_db.s1' is not yet defined in this session
90+
select test_func_1();
91+
test_func_1()
92+
1
93+
select test_func_2();
94+
test_func_2()
95+
1
96+
select test_func_1();
97+
test_func_1()
98+
3
99+
select test_func_2();
100+
test_func_2()
101+
3
102+
drop function test_func_1;
103+
drop function test_func_2;
104+
set autocommit=0;
105+
select * from t1;
106+
id col1
107+
3 3
108+
CREATE PROCEDURE test_proc_1()
109+
BEGIN
110+
insert into t1 select s1.nextval, s1.currval from dual;
111+
insert into t1 select s1.nextval, s1.currval from dual;
112+
END$$
113+
call test_proc_1();
114+
select * from t1;
115+
id col1
116+
3 3
117+
5 5
118+
7 7
119+
rollback;
120+
select * from t1;
121+
id col1
122+
3 3
123+
select s1.nextval;
124+
s1.nextval
125+
9
126+
commit;
127+
drop procedure test_proc_1;
128+
create table t2(id int)engine=innodb;
129+
CREATE TRIGGER test_tri_1
130+
AFTER INSERT ON t1
131+
FOR EACH ROW
132+
BEGIN INSERT INTO t2 select s1.nextval from dual;
133+
END$$
134+
insert into t1 select s1.nextval, s1.currval from dual;
135+
insert into t1 select s1.nextval, s1.currval from dual;
136+
select * from t1;
137+
id col1
138+
3 3
139+
11 11
140+
15 15
141+
select * from t2;
142+
id
143+
13
144+
17
145+
commit;
146+
drop trigger test_tri_1;
147+
drop table t2;
148+
use s_db;
149+
drop database s_db;
150+
drop user normal_1@'%';
151+
drop user normal_2@'%';
152+
drop user normal_3@'%';
153+
drop user normal_4@'%';
154+
include/rpl_end.inc

mysql-test/t/feature_sequence.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ commit;
179179

180180
select * for s2;
181181

182-
--error ER_SYNTAX_ERROR
182+
--error ER_NOT_SEQUENCE
183183
select * for t2;
184184

185185
select * from s2, t2;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--binlog_format=row --query_cache_type=1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--binlog_format=row --query_cache_type=1 --read_only=true
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
--source include/have_binlog_format_row.inc
2+
--source include/master-slave.inc
3+
4+
connection master;
5+
create database s_db;
6+
grant all on s_db.* to normal_1@'%' identified by 'pass';
7+
grant all on test.* to normal_2@'%' identified by 'pass';
8+
grant all on s_db.* to normal_3@'%' identified by 'pass';
9+
grant all on test.* to normal_4@'%' identified by 'pass';
10+
11+
create table s_db.t1(id int, col1 int)engine=innodb;
12+
13+
--sync_slave_with_master
14+
15+
connect(m_normal_1, 127.0.0.1, normal_1, pass, s_db, $MASTER_MYPORT);
16+
connect(m_normal_2, 127.0.0.1, normal_2, pass, test, $MASTER_MYPORT);
17+
18+
connect(s_normal_3, 127.0.0.1, normal_3, pass, s_db, $SLAVE_MYPORT);
19+
connect(s_normal_4, 127.0.0.1, normal_4, pass, test, $SLAVE_MYPORT);
20+
21+
connection slave;
22+
set global read_only=on;
23+
24+
--echo ###########################################
25+
--echo master and slave sync sequence.
26+
--echo ###########################################
27+
connection master;
28+
use s_db;
29+
30+
create sequence s1;
31+
show create table s1;
32+
33+
--sync_slave_with_master
34+
connection slave;
35+
use s_db;
36+
show create table s1;
37+
38+
connection master;
39+
use s_db;
40+
41+
drop sequence s1;
42+
43+
44+
--echo ###########################################
45+
--echo test keyword
46+
--echo ###########################################
47+
connection master;
48+
use s_db;
49+
create table t_2(currval int, nextval int);
50+
51+
--error ER_NOT_SEQUENCE
52+
select t_2.currval from dual;
53+
54+
select currval from t_2;
55+
56+
drop table t_2;
57+
58+
59+
--sync_slave_with_master
60+
61+
--echo ###########################################
62+
--echo dml with func
63+
--echo ###########################################
64+
connection m_normal_1;
65+
use s_db;
66+
67+
create sequence s1 start with 1 minvalue 1 maxvalue 7 cache 2 cycle increment by 2;
68+
69+
--error ER_SEQUENCE_NOT_DEFINED
70+
select s1.currval from dual;
71+
72+
--error ER_SEQUENCE_NOT_DEFINED
73+
select s1.currval from dual;
74+
75+
select s1.nextval from dual;
76+
77+
insert into t1 select s1.nextval, s1.currval from dual;
78+
select * from t1;
79+
80+
commit;
81+
82+
drop sequence s1;
83+
84+
--echo ###########################################
85+
--echo func, trigger, procedure with sequence func
86+
--echo ###########################################
87+
connection m_normal_1;
88+
use s_db;
89+
90+
create sequence s1 start with 1 minvalue 1 cache 2 cycle increment by 2;
91+
92+
delimiter $$;
93+
CREATE FUNCTION `test_func_1` () RETURNS int
94+
BEGIN
95+
RETURN (select s1.nextval from dual);
96+
END$$
97+
delimiter ;$$
98+
99+
delimiter $$;
100+
CREATE FUNCTION `test_func_2` () RETURNS int
101+
BEGIN
102+
RETURN (select s1.currval from dual);
103+
END$$
104+
delimiter ;$$
105+
106+
--error ER_SEQUENCE_NOT_DEFINED
107+
select test_func_2();
108+
109+
select test_func_1();
110+
select test_func_2();
111+
112+
select test_func_1();
113+
select test_func_2();
114+
115+
drop function test_func_1;
116+
drop function test_func_2;
117+
118+
119+
set autocommit=0;
120+
select * from t1;
121+
delimiter $$;
122+
CREATE PROCEDURE test_proc_1()
123+
BEGIN
124+
insert into t1 select s1.nextval, s1.currval from dual;
125+
insert into t1 select s1.nextval, s1.currval from dual;
126+
END$$
127+
delimiter ;$$
128+
129+
130+
call test_proc_1();
131+
select * from t1;
132+
133+
rollback;
134+
select * from t1;
135+
select s1.nextval;
136+
commit;
137+
138+
drop procedure test_proc_1;
139+
140+
141+
create table t2(id int)engine=innodb;
142+
143+
delimiter $$;
144+
CREATE TRIGGER test_tri_1
145+
AFTER INSERT ON t1
146+
FOR EACH ROW
147+
BEGIN INSERT INTO t2 select s1.nextval from dual;
148+
END$$
149+
delimiter ;$$
150+
151+
insert into t1 select s1.nextval, s1.currval from dual;
152+
insert into t1 select s1.nextval, s1.currval from dual;
153+
154+
select * from t1;
155+
select * from t2;
156+
commit;
157+
158+
drop trigger test_tri_1;
159+
drop table t2;
160+
161+
162+
163+
connection master;
164+
use s_db;
165+
drop database s_db;
166+
drop user normal_1@'%';
167+
drop user normal_2@'%';
168+
drop user normal_3@'%';
169+
drop user normal_4@'%';
170+
171+
172+
--sync_slave_with_master
173+
--source include/rpl_end.inc

sql/share/errmsg-utf8.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7140,4 +7140,6 @@ ER_PK_INDEX_CANT_BE_INVISIBLE
71407140
eng "A primary key index cannot be invisible"
71417141
ER_SEQUENCE_NOT_DEFINED
71427142
eng "Sequence '%-.192s.%-.192s' is not yet defined in this session"
7143+
ER_NOT_SEQUENCE
7144+
eng "'%-.192s.%-.192s' is not a SEQUENCE"
71437145

sql/sql_base.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3265,7 +3265,7 @@ bool open_table(THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx)
32653265
#endif
32663266
if (table_list->sequence_read && !table->s->is_sequence)
32673267
{
3268-
my_error(ER_SYNTAX_ERROR, MYF(0));
3268+
my_error(ER_NOT_SEQUENCE, MYF(0), table->s->db.str, table->s->table_name.str);
32693269
DBUG_RETURN(true);
32703270
}
32713271

sql/sql_yacc.yy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14116,6 +14116,32 @@ simple_ident_q:
1411614116

1411714117
$$= trg_fld;
1411814118
}
14119+
else if (!my_strcasecmp(system_charset_info, $3.str, "nextval"))
14120+
{
14121+
TABLE_LIST *table;
14122+
SELECT_LEX *sel= lex->current_select;
14123+
if (!(table= sel->add_table_to_list(thd, new Table_ident($1), NULL,
14124+
TL_OPTION_SEQUENCE,
14125+
TL_READ,
14126+
MDL_SHARED_READ,
14127+
NULL, NULL, NULL, true)))
14128+
MYSQL_YYABORT;
14129+
if (!($$= new (thd->mem_root) Item_func_nextval(thd, table)))
14130+
MYSQL_YYABORT;
14131+
}
14132+
else if (!my_strcasecmp(system_charset_info, $3.str, "currval"))
14133+
{
14134+
TABLE_LIST *table;
14135+
SELECT_LEX *sel= lex->current_select;
14136+
if (!(table= sel->add_table_to_list(thd, new Table_ident($1), NULL,
14137+
TL_OPTION_SEQUENCE,
14138+
TL_READ,
14139+
MDL_SHARED_READ,
14140+
NULL, NULL, NULL, true)))
14141+
MYSQL_YYABORT;
14142+
if (!($$= new (thd->mem_root) Item_func_currval(thd, table)))
14143+
MYSQL_YYABORT;
14144+
}
1411914145
else
1412014146
{
1412114147
SELECT_LEX *sel= lex->current_select;

0 commit comments

Comments
 (0)