-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
614 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,291 @@ | ||
# | ||
# Tests for LAST_INSERT_IDS function | ||
# | ||
# | ||
# Should not be callable as a regular function | ||
# | ||
SELECT LAST_INSERT_IDS(); | ||
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 near 'LAST_INSERT_IDS()' at line 1 | ||
SELECT LAST_INSERT_IDS(10); | ||
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 near 'LAST_INSERT_IDS(10)' at line 1 | ||
SELECT LAST_INSERT_IDS("foo", "bar"); | ||
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 near 'LAST_INSERT_IDS("foo", "bar")' at line 1 | ||
# | ||
# Table-returning functions must be aliased | ||
# | ||
SELECT * FROM LAST_INSERT_IDS(); | ||
ERROR 42000: Every table function must have an alias | ||
# | ||
# With no inserted rows, should return empty table | ||
# | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
EXPLAIN SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
id select_type table partitions type possible_keys key key_len ref rows filtered Extra | ||
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: last_insert_ids; Using temporary | ||
Warnings: | ||
Note 1003 /* select#1 */ select `ids`.`insert_id` AS `insert_id` from last_insert_ids() `ids` | ||
# | ||
# In the case of a single insert, should match LAST_INSERT_ID | ||
# | ||
CREATE TABLE t1 (a INT AUTO_INCREMENT PRIMARY KEY); | ||
INSERT INTO t1 VALUES (); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
1 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
1 | ||
INSERT INTO t1 VALUES (); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
2 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
2 | ||
# | ||
# Explicitly-provided value is ignored by LAST_INSERT_IDS | ||
# | ||
INSERT INTO t1 VALUES (5); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
2 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
2 | ||
INSERT INTO t1 VALUES (); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
6 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
0 | ||
6 | ||
# | ||
# CREATE TABLE ... SELECT FROM LAST_INSERT_IDS | ||
# | ||
INSERT INTO t1 VALUES (), (); | ||
CREATE TABLE t2 AS SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
SELECT * FROM t2; | ||
insert_id | ||
7 | ||
8 | ||
DROP TABLE t2; | ||
# | ||
# INSERT ... SELECT LAST_INSERT_IDS | ||
# | ||
INSERT INTO t1 VALUES (); | ||
TRUNCATE TABLE t1; | ||
INSERT INTO t1 SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
# | ||
# PREPARE / EXECUTE | ||
# | ||
PREPARE s1 FROM 'SELECT * FROM LAST_INSERT_IDS() AS ids'; | ||
EXECUTE s1; | ||
insert_id | ||
1 | ||
2 | ||
0 | ||
INSERT INTO t1 VALUES (); | ||
EXECUTE s1; | ||
insert_id | ||
10 | ||
DEALLOCATE PREPARE s1; | ||
# | ||
# Multiple values returned by LAST_INSERT_IDS | ||
# | ||
INSERT INTO t1 VALUES (),(),(),(),(); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
11 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
11 | ||
12 | ||
13 | ||
14 | ||
15 | ||
# | ||
# Test statement binlog format | ||
# | ||
SET SESSION binlog_format = 'STATEMENT'; | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
11 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
11 | ||
12 | ||
13 | ||
14 | ||
15 | ||
INSERT INTO t1 VALUES (); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
16 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
16 | ||
INSERT INTO t1 VALUES (),(),(),(),(); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
17 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
17 | ||
18 | ||
19 | ||
20 | ||
21 | ||
# | ||
# Test mixed binlog format | ||
# | ||
SET SESSION binlog_format = 'MIXED'; | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
17 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
17 | ||
18 | ||
19 | ||
20 | ||
21 | ||
INSERT INTO t1 VALUES (); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
22 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
22 | ||
INSERT INTO t1 VALUES (),(),(),(),(); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
23 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
23 | ||
24 | ||
25 | ||
26 | ||
27 | ||
# | ||
# TODO: interaction with LAST_INSERT_ID(arg) | ||
# | ||
SET SESSION binlog_format = 'ROW'; | ||
SELECT LAST_INSERT_ID(100); | ||
LAST_INSERT_ID(100) | ||
100 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
23 | ||
24 | ||
25 | ||
26 | ||
27 | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
100 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
23 | ||
24 | ||
25 | ||
26 | ||
27 | ||
# | ||
# Test multi-statement transactions | ||
# | ||
BEGIN; | ||
INSERT INTO t1 VALUES (); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
28 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
28 | ||
INSERT INTO t1 VALUES (),(); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
29 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
29 | ||
30 | ||
COMMIT; | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
29 | ||
SELECT * FROM LAST_INSERT_IDS() as ids; | ||
insert_id | ||
29 | ||
30 | ||
# | ||
# Test rolledback multi-statement transactions | ||
# | ||
BEGIN; | ||
INSERT INTO t1 VALUES (),(); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
31 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
31 | ||
32 | ||
ROLLBACK; | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
31 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
31 | ||
32 | ||
# | ||
# Test failures to insert | ||
# | ||
CREATE TABLE t2 (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, | ||
b INT NOT NULL UNIQUE); | ||
INSERT INTO t2 VALUES (NULL, 0), (NULL, 1); | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
1 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
1 | ||
2 | ||
INSERT INTO t2 VALUES (NULL, 0); | ||
ERROR 23000: Duplicate entry '0' for key 't2.b' | ||
SELECT LAST_INSERT_ID(); | ||
LAST_INSERT_ID() | ||
1 | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
1 | ||
2 | ||
DROP TABLE t2; | ||
# | ||
# Accessing LAST_INSERT_IDS in a triggger | ||
# | ||
CREATE TRIGGER trg1 AFTER INSERT ON t1 FOR EACH ROW | ||
BEGIN | ||
CREATE TABLE t2 SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
END| | ||
ERROR HY000: Explicit or implicit commit is not allowed in stored function or trigger. | ||
CREATE TABLE t2 (a INT); | ||
CREATE TRIGGER trg1 AFTER INSERT ON t1 FOR EACH ROW | ||
BEGIN | ||
INSERT INTO t2 SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
END| | ||
INSERT INTO t1 VALUES (); | ||
SELECT * FROM LAST_INSERT_IDS() AS ids; | ||
insert_id | ||
33 | ||
0 | ||
0 | ||
SELECT * FROM t2; | ||
a | ||
1 | ||
2 | ||
DROP TABLE t2; | ||
DROP TRIGGER trg1; | ||
DROP TABLE t1; |
Oops, something went wrong.