Skip to content

Commit 6e4fa7e

Browse files
committed
MDEV-36390: Minor refactoring of the method get_expr_query at the classes sp_instr_cpush/sp_instr_cursor_copy_struct
Duplicated code from methods sp_instr_cpush::get_expr_query sp_instr_cursor_copy_struct::get_expr_query were extracted to the standalone function get_cursor_query Additionally, added correct parsing of a cursor definition with a new line or TAB instead of the space immediately after FOR/IS.
1 parent cc831f1 commit 6e4fa7e

File tree

3 files changed

+211
-20
lines changed

3 files changed

+211
-20
lines changed

mysql-test/main/sp_validation.result

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,4 +1993,96 @@ f1()
19931993
# Clean up
19941994
DROP FUNCTION f1;
19951995
DROP TABLE t1;
1996+
#
1997+
# MDEV-36390: Minor refactoring of the method get_expr_query at the classes sp_instr_cpush/sp_instr_cursor_copy_struct
1998+
#
1999+
CREATE TABLE t1 (a INT);
2000+
INSERT INTO t1 VALUES (1);
2001+
CREATE OR REPLACE PROCEDURE p1()
2002+
BEGIN
2003+
DECLARE va INT;
2004+
# Check that the TAB character after the clause FOR is skipped and
2005+
# the body of cursor is remembered correctly for subsequent re-parsing
2006+
DECLARE cur CURSOR FOR SELECT a FROM t1;
2007+
OPEN cur;
2008+
FETCH cur INTO va;
2009+
SELECT va;
2010+
CLOSE cur;
2011+
END;
2012+
$
2013+
CREATE OR REPLACE PROCEDURE p2()
2014+
BEGIN
2015+
DECLARE va INT;
2016+
# Check that the newline character after the clause FOR is skipped and
2017+
# the body of cursor is remembered correctly for subsequent re-parsing
2018+
DECLARE cur CURSOR FOR
2019+
SELECT a FROM t1;
2020+
OPEN cur;
2021+
FETCH cur INTO va;
2022+
SELECT va;
2023+
CLOSE cur;
2024+
END;
2025+
$
2026+
CREATE OR REPLACE PROCEDURE p3()
2027+
BEGIN
2028+
DECLARE va INT;
2029+
# Check that C-style comment and the newline character after
2030+
# the clause FOR is skipped and the body of cursor is remembered
2031+
# correctly for subsequent re-parsing
2032+
DECLARE cur CURSOR FOR /* Explicit comment */
2033+
SELECT a FROM t1;
2034+
OPEN cur;
2035+
FETCH cur INTO va;
2036+
SELECT va;
2037+
CLOSE cur;
2038+
END;
2039+
$
2040+
CREATE OR REPLACE PROCEDURE p4()
2041+
BEGIN
2042+
DECLARE va INT;
2043+
# Check that SQL-style comment and the newline character after
2044+
# the clause FOR is skipped and the body of cursor is remembered
2045+
# correctly for subsequent re-parsing
2046+
DECLARE cur CURSOR FOR -- Explicit comment
2047+
SELECT a FROM t1;
2048+
OPEN cur;
2049+
FETCH cur INTO va;
2050+
SELECT va;
2051+
CLOSE cur;
2052+
END;
2053+
$
2054+
CALL p1();
2055+
va
2056+
1
2057+
CALL p2();
2058+
va
2059+
1
2060+
CALL p3();
2061+
va
2062+
1
2063+
CALL p4();
2064+
va
2065+
1
2066+
ALTER TABLE t1 COMMENT 'The Comment 1';
2067+
# The following statements will run re-parsing of
2068+
# cursor declaration statements inside the stored
2069+
# procedures p1, p2, p3, p4.
2070+
CALL p1();
2071+
va
2072+
1
2073+
CALL p2();
2074+
va
2075+
1
2076+
CALL p3();
2077+
va
2078+
1
2079+
CALL p4();
2080+
va
2081+
1
2082+
# Clean up
2083+
DROP PROCEDURE p1;
2084+
DROP PROCEDURE p2;
2085+
DROP PROCEDURE p3;
2086+
DROP PROCEDURE p4;
2087+
DROP TABLE t1;
19962088
SET sql_mode = default;

mysql-test/main/sp_validation.test

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,5 +2795,96 @@ SELECT f1();
27952795
DROP FUNCTION f1;
27962796
DROP TABLE t1;
27972797

2798+
--echo #
2799+
--echo # MDEV-36390: Minor refactoring of the method get_expr_query at the classes sp_instr_cpush/sp_instr_cursor_copy_struct
2800+
--echo #
2801+
CREATE TABLE t1 (a INT);
2802+
INSERT INTO t1 VALUES (1);
2803+
2804+
--delimiter $
2805+
2806+
CREATE OR REPLACE PROCEDURE p1()
2807+
BEGIN
2808+
DECLARE va INT;
2809+
# Check that the TAB character after the clause FOR is skipped and
2810+
# the body of cursor is remembered correctly for subsequent re-parsing
2811+
DECLARE cur CURSOR FOR SELECT a FROM t1;
2812+
2813+
OPEN cur;
2814+
FETCH cur INTO va;
2815+
SELECT va;
2816+
CLOSE cur;
2817+
END;
2818+
$
2819+
2820+
CREATE OR REPLACE PROCEDURE p2()
2821+
BEGIN
2822+
DECLARE va INT;
2823+
# Check that the newline character after the clause FOR is skipped and
2824+
# the body of cursor is remembered correctly for subsequent re-parsing
2825+
DECLARE cur CURSOR FOR
2826+
SELECT a FROM t1;
2827+
2828+
OPEN cur;
2829+
FETCH cur INTO va;
2830+
SELECT va;
2831+
CLOSE cur;
2832+
END;
2833+
$
2834+
2835+
CREATE OR REPLACE PROCEDURE p3()
2836+
BEGIN
2837+
DECLARE va INT;
2838+
# Check that C-style comment and the newline character after
2839+
# the clause FOR is skipped and the body of cursor is remembered
2840+
# correctly for subsequent re-parsing
2841+
DECLARE cur CURSOR FOR /* Explicit comment */
2842+
SELECT a FROM t1;
2843+
2844+
OPEN cur;
2845+
FETCH cur INTO va;
2846+
SELECT va;
2847+
CLOSE cur;
2848+
END;
2849+
$
2850+
2851+
CREATE OR REPLACE PROCEDURE p4()
2852+
BEGIN
2853+
DECLARE va INT;
2854+
# Check that SQL-style comment and the newline character after
2855+
# the clause FOR is skipped and the body of cursor is remembered
2856+
# correctly for subsequent re-parsing
2857+
DECLARE cur CURSOR FOR -- Explicit comment
2858+
SELECT a FROM t1;
2859+
2860+
OPEN cur;
2861+
FETCH cur INTO va;
2862+
SELECT va;
2863+
CLOSE cur;
2864+
END;
2865+
$
2866+
2867+
--delimiter ;
2868+
2869+
CALL p1();
2870+
CALL p2();
2871+
CALL p3();
2872+
CALL p4();
2873+
ALTER TABLE t1 COMMENT 'The Comment 1';
2874+
--echo # The following statements will run re-parsing of
2875+
--echo # cursor declaration statements inside the stored
2876+
--echo # procedures p1, p2, p3, p4.
2877+
CALL p1();
2878+
CALL p2();
2879+
CALL p3();
2880+
CALL p4();
2881+
2882+
--echo # Clean up
2883+
DROP PROCEDURE p1;
2884+
DROP PROCEDURE p2;
2885+
DROP PROCEDURE p3;
2886+
DROP PROCEDURE p4;
2887+
DROP TABLE t1;
2888+
27982889
SET sql_mode = default;
27992890
--enable_ps2_protocol

sql/sp_instr.h

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,32 @@ class sp_instr_hreturn : public sp_instr_jump
12511251
}; // class sp_instr_hreturn : public sp_instr_jump
12521252

12531253

1254+
/**
1255+
Get a query text associated with the cursor.
1256+
*/
1257+
1258+
static inline LEX_CSTRING get_cursor_query(const LEX_CSTRING &cursor_stmt)
1259+
{
1260+
/*
1261+
Lexer on processing the clause CURSOR FOR / CURSOR IS doesn't
1262+
move a pointer on cpp_buf after the token FOR/IS so skip it explicitly
1263+
in order to get correct value of cursor's query string.
1264+
*/
1265+
1266+
if (strncasecmp(cursor_stmt.str, "FOR", 3) == 0 &&
1267+
my_isspace(current_thd->variables.character_set_client,
1268+
cursor_stmt.str[3]))
1269+
return LEX_CSTRING{cursor_stmt.str + 4, cursor_stmt.length - 4};
1270+
1271+
if (strncasecmp(cursor_stmt.str, "IS", 2) == 0 &&
1272+
my_isspace(current_thd->variables.character_set_client,
1273+
cursor_stmt.str[2]))
1274+
return LEX_CSTRING{cursor_stmt.str + 3, cursor_stmt.length - 3};
1275+
1276+
return cursor_stmt;
1277+
}
1278+
1279+
12541280
/**
12551281
This is DECLARE CURSOR
12561282
*/
@@ -1311,16 +1337,7 @@ class sp_instr_cpush : public sp_lex_instr, public sp_cursor
13111337
protected:
13121338
LEX_CSTRING get_expr_query() const override
13131339
{
1314-
/*
1315-
Lexer on processing the clause CURSOR FOR / CURSOR IS doesn't
1316-
move a pointer on cpp_buf after the token FOR/IS so skip it explicitly
1317-
in order to get correct value of cursor's query string.
1318-
*/
1319-
if (strncasecmp(m_cursor_stmt.str, "FOR ", 4) == 0)
1320-
return LEX_CSTRING{m_cursor_stmt.str + 4, m_cursor_stmt.length - 4};
1321-
if (strncasecmp(m_cursor_stmt.str, "IS ", 3) == 0)
1322-
return LEX_CSTRING{m_cursor_stmt.str + 3, m_cursor_stmt.length - 3};
1323-
return m_cursor_stmt;
1340+
return get_cursor_query(m_cursor_stmt);
13241341
}
13251342

13261343
bool on_after_expr_parsing(THD *) override
@@ -1452,16 +1469,7 @@ class sp_instr_cursor_copy_struct: public sp_lex_instr
14521469
protected:
14531470
LEX_CSTRING get_expr_query() const override
14541471
{
1455-
/*
1456-
Lexer on processing the clause CURSOR FOR / CURSOR IS doesn't
1457-
move a pointer on cpp_buf after the token FOR/IS so skip it explicitly
1458-
in order to get correct value of cursor's query string.
1459-
*/
1460-
if (strncasecmp(m_cursor_stmt.str, "FOR ", 4) == 0)
1461-
return LEX_CSTRING{m_cursor_stmt.str + 4, m_cursor_stmt.length - 4};
1462-
if (strncasecmp(m_cursor_stmt.str, "IS ", 3) == 0)
1463-
return LEX_CSTRING{m_cursor_stmt.str + 3, m_cursor_stmt.length - 3};
1464-
return m_cursor_stmt;
1472+
return get_cursor_query(m_cursor_stmt);
14651473
}
14661474

14671475
bool on_after_expr_parsing(THD *) override

0 commit comments

Comments
 (0)