Skip to content

Commit

Permalink
MDEV-10709 Expressions as parameters to Dynamic SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Barkov committed Oct 8, 2016
1 parent 8ea2e14 commit 4c45b82
Show file tree
Hide file tree
Showing 10 changed files with 692 additions and 80 deletions.
198 changes: 198 additions & 0 deletions mysql-test/r/ps.result
Original file line number Diff line number Diff line change
Expand Up @@ -4093,3 +4093,201 @@ deallocate prepare stmt;
SET SESSION sql_mode = @save_sql_mode;
DROP TABLE t1,t2;
# End of 10.0 tests
#
# Start of 10.2 tests
#
#
# MDEV-10709 Expressions as parameters to Dynamic SQL
#
#
# Using a simple expressions as an EXECUTE parameter
#
PREPARE stmt FROM 'SELECT ? FROM DUAL';
EXECUTE stmt USING 10;
?
10
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM 'SELECT ? FROM DUAL';
EXECUTE stmt USING TO_BASE64('xxx');
?
eHh4
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM 'SELECT ?+? FROM DUAL';
EXECUTE stmt USING 10, 10 + 10;
?+?
30
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM 'SELECT CONCAT(?,?) FROM DUAL';
EXECUTE stmt USING 'xxx', CONCAT('yyy','zzz');
CONCAT(?,?)
xxxyyyzzz
DEALLOCATE PREPARE stmt;
#
# Testing disallowed expressions in USING
#
PREPARE stmt FROM 'SELECT ? FROM DUAL';
EXECUTE stmt USING (SELECT 1);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1)' at line 1
DEALLOCATE PREPARE stmt;
CREATE FUNCTION f1() RETURNS VARCHAR(10) RETURN 'test';
PREPARE stmt FROM 'SELECT ? FROM DUAL';
EXECUTE stmt USING f1();
ERROR 42000: EXECUTE..USING does not support subqueries or stored functions
DEALLOCATE PREPARE stmt;
DROP FUNCTION f1;
#
# Testing erroneous expressions in USING
#
PREPARE stmt FROM 'SELECT ?';
EXECUTE stmt USING _latin1'a'=_latin2'a';
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,COERCIBLE) and (latin2_general_ci,COERCIBLE) for operation '='
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM 'SELECT ?';
EXECUTE stmt USING ROW(1,2);
ERROR 21000: Operand should contain 1 column(s)
DEALLOCATE PREPARE stmt;
#
# Creating tables from EXECUTE parameters
#
PREPARE stmt FROM 'CREATE TABLE t1 AS SELECT ? AS c1 FROM DUAL';
EXECUTE stmt USING 10;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` bigint(21) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
EXECUTE stmt USING 10.123;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` decimal(5,3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
EXECUTE stmt USING 10.123e0;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` double NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
EXECUTE stmt USING CURRENT_DATE;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` date DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
EXECUTE stmt USING CURRENT_TIMESTAMP;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
EXECUTE stmt USING CURRENT_TIMESTAMP(3);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` datetime(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
EXECUTE stmt USING CURRENT_TIMESTAMP(6);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` datetime(6) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
EXECUTE stmt USING CURRENT_TIME;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` time DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
EXECUTE stmt USING CURRENT_TIME(3);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` time(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
EXECUTE stmt USING CURRENT_TIME(6);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` time(6) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
DEALLOCATE PREPARE stmt;
#
# Using a user variable as an EXECUTE..USING out parameter
#
CREATE PROCEDURE p1(OUT a INT)
BEGIN
SET a:= 10;
END;
/
SET @a=1;
CALL p1(@a);
SELECT @a;
@a
10
SET @a=2;
PREPARE stmt FROM 'CALL p1(?)';
EXECUTE stmt USING @a;
SELECT @a;
@a
10
DROP PROCEDURE p1;
#
# Using an SP variable as an EXECUTE..USING out parameter
#
CREATE PROCEDURE p1 (OUT a INT)
BEGIN
SET a=10;
END;
/
CREATE PROCEDURE p2 (OUT a INT)
BEGIN
PREPARE stmt FROM 'CALL p1(?)';
EXECUTE stmt USING a;
END;
/
SET @a= 1;
CALL p2(@a);
SELECT @a;
@a
10
DROP PROCEDURE p2;
DROP PROCEDURE p1;
#
# Testing re-prepare on a table metadata update between PREPARE and EXECUTE
#
CREATE TABLE t1 (a INT);
CREATE PROCEDURE p1(a INT)
BEGIN
INSERT INTO t1 VALUES (a);
END;
/
PREPARE stmt FROM 'CALL p1(?)';
EXECUTE stmt USING 10;
SELECT * FROM t1;
a
10
CREATE TRIGGER tr1 BEFORE INSERT ON t1 FOR EACH ROW SET NEW.a=NEW.a+1;
EXECUTE stmt USING 20;
SELECT * FROM t1;
a
10
21
DEALLOCATE PREPARE stmt;
DROP PROCEDURE p1;
DROP TABLE t1;
#
# End of MDEV-10709 Expressions as parameters to Dynamic SQL
#
#
# End of 10.2 tests
#
76 changes: 76 additions & 0 deletions mysql-test/suite/binlog/r/binlog_stm_ps.result
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,79 @@ master-bin.000001 # Gtid # # BEGIN GTID #-#-#
master-bin.000001 # Query # # use `test`; insert into t1 select 100 limit 100
master-bin.000001 # Query # # COMMIT
drop table t1;
#
# MDEV-10709 Expressions as parameters to Dynamic SQL
#
FLUSH LOGS;
SET TIMESTAMP=UNIX_TIMESTAMP('2001-01-02 10:20:30.123456');
CREATE TABLE t1 (a DECIMAL(30,8));
PREPARE stmt FROM 'INSERT INTO t1 VALUES (?)';
EXECUTE stmt USING 10;
EXECUTE stmt USING 11e0;
EXECUTE stmt USING 12.1;
EXECUTE stmt USING '13';
EXECUTE stmt USING CURRENT_DATE;
EXECUTE stmt USING MAKETIME(10,20,30);
EXECUTE stmt USING CURRENT_TIME;
EXECUTE stmt USING CURRENT_TIME(3);
EXECUTE stmt USING CURRENT_TIME(6);
EXECUTE stmt USING CURRENT_TIMESTAMP;
EXECUTE stmt USING CURRENT_TIMESTAMP(3);
EXECUTE stmt USING CURRENT_TIMESTAMP(6);
SELECT * FROM t1;
a
10.00000000
11.00000000
12.10000000
13.00000000
20010102.00000000
102030.00000000
102030.00000000
102030.12300000
102030.12345600
20010102102030.00000000
20010102102030.12300000
20010102102030.12345600
include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
master-bin.000002 # Binlog_checkpoint # # master-bin.000002
master-bin.000002 # Gtid # # GTID #-#-#
master-bin.000002 # Query # # use `test`; CREATE TABLE t1 (a DECIMAL(30,8))
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (10)
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (11)
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (12.1)
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES ('13')
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (DATE'2001-01-02')
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (TIME'10:20:30')
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (TIME'10:20:30')
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (TIME'10:20:30.123')
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (TIME'10:20:30.123456')
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (TIMESTAMP'2001-01-02 10:20:30')
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (TIMESTAMP'2001-01-02 10:20:30.123')
master-bin.000002 # Query # # COMMIT
master-bin.000002 # Gtid # # BEGIN GTID #-#-#
master-bin.000002 # Query # # use `test`; INSERT INTO t1 VALUES (TIMESTAMP'2001-01-02 10:20:30.123456')
master-bin.000002 # Query # # COMMIT
DROP TABLE t1;
SET TIMESTAMP=DEFAULT;
26 changes: 26 additions & 0 deletions mysql-test/suite/binlog/t/binlog_stm_ps.test
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,29 @@ prepare s from "insert into t1 select 100 limit ?";
set @a=100; execute s using @a;
source include/show_binlog_events.inc;
drop table t1;

--echo #
--echo # MDEV-10709 Expressions as parameters to Dynamic SQL
--echo #

FLUSH LOGS;
SET TIMESTAMP=UNIX_TIMESTAMP('2001-01-02 10:20:30.123456');
CREATE TABLE t1 (a DECIMAL(30,8));
PREPARE stmt FROM 'INSERT INTO t1 VALUES (?)';
EXECUTE stmt USING 10;
EXECUTE stmt USING 11e0;
EXECUTE stmt USING 12.1;
EXECUTE stmt USING '13';
EXECUTE stmt USING CURRENT_DATE;
EXECUTE stmt USING MAKETIME(10,20,30);
EXECUTE stmt USING CURRENT_TIME;
EXECUTE stmt USING CURRENT_TIME(3);
EXECUTE stmt USING CURRENT_TIME(6);
EXECUTE stmt USING CURRENT_TIMESTAMP;
EXECUTE stmt USING CURRENT_TIMESTAMP(3);
EXECUTE stmt USING CURRENT_TIMESTAMP(6);
SELECT * FROM t1;
--let $binlog_file = LAST
source include/show_binlog_events.inc;
DROP TABLE t1;
SET TIMESTAMP=DEFAULT;
Loading

0 comments on commit 4c45b82

Please sign in to comment.