From cfe594bd42e2c7202c08062c46e36f56a4fb9ec4 Mon Sep 17 00:00:00 2001 From: Alexey Kopytov Date: Sat, 23 Jan 2021 15:05:22 +0300 Subject: [PATCH] SQL API: fix prepared statements with FLOAT/DOUBLE type arguments SQL API failed to prepare correctly SQL statements containing floating point arguments. --- src/lua/internal/sysbench.sql.lua | 8 +++----- tests/include/api_sql_common.sh | 19 ++++++++++++++++--- tests/t/api_sql_mysql.t | 8 ++++++-- tests/t/api_sql_pgsql.t | 14 +++++++++----- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/lua/internal/sysbench.sql.lua b/src/lua/internal/sysbench.sql.lua index 160c00d..1bb16d0 100644 --- a/src/lua/internal/sysbench.sql.lua +++ b/src/lua/internal/sysbench.sql.lua @@ -332,13 +332,11 @@ function sql_param.set(self, value) if btype == sql_type.TINYINT or btype == sql_type.SMALLINT or btype == sql_type.INT or - btype == sql_type.BIGINT - then - self.buffer[0] = value - elseif btype == sql_type.FLOAT or + btype == sql_type.BIGINT or + btype == sql_type.FLOAT or btype == sql_type.DOUBLE then - self.buffer[1] = value + self.buffer[0] = value elseif btype == sql_type.CHAR or btype == sql_type.VARCHAR then diff --git a/tests/include/api_sql_common.sh b/tests/include/api_sql_common.sh index 959e304..aefb1b5 100644 --- a/tests/include/api_sql_common.sh +++ b/tests/include/api_sql_common.sh @@ -59,25 +59,27 @@ function event() print(string.format("%s %s", con:query_row("SELECT b, a FROM t2 ORDER BY b"))) print('--') con:query("DROP TABLE t2") - con:query("ALTER TABLE t ADD COLUMN b CHAR(10)") + con:query("ALTER TABLE t ADD COLUMN b CHAR(10), ADD COLUMN c FLOAT") e, m = pcall(con.prepare, con, "SELECT * FROM nonexisting") print(m) print('--') - local stmt = con:prepare("UPDATE t SET a = a + ?, b = ?") + local stmt = con:prepare("UPDATE t SET a = a + ?, b = ?, c = ?") local a = stmt:bind_create(sysbench.sql.type.INT) local b = stmt:bind_create(sysbench.sql.type.CHAR, 10) + local c = stmt:bind_create(sysbench.sql.type.FLOAT) print(a) print(b) + print(c) e, m = pcall(stmt.bind_create, stmt, sysbench.sql.type.DATE) print(m) print(stmt:bind_param()) - stmt:bind_param(a, b) + stmt:bind_param(a, b, c) a:set(100) rs1 = stmt:execute() print(rs1) @@ -85,6 +87,14 @@ function event() b:set("01234567890") rs2 = stmt:execute() print(rs2) + a:set(300) + b:set("09876543210") + c:set(0.9) + rs3 = stmt:execute() + print(rs3) + + e, m = pcall(rs3.free, rs) + print(m) e, m = pcall(rs2.free, rs) print(m) @@ -92,6 +102,9 @@ function event() e, m = pcall(rs1.free, rs) print(m) + rs = con:query("SELECT a, b, c FROM t") + print(unpack(rs:fetch_row(), 1, rs.nfields)) + stmt:close() print('--') diff --git a/tests/t/api_sql_mysql.t b/tests/t/api_sql_mysql.t index 53ca242..79b0dbf 100644 --- a/tests/t/api_sql_mysql.t +++ b/tests/t/api_sql_mysql.t @@ -47,21 +47,25 @@ SQL Lua API + MySQL tests -- + Unsupported argument type: 8 nil + ALERT: attempt to free an invalid result set db_free_results() failed db_free_results() failed + db_free_results() failed + 601\t0987654321\t0.9 (esc) -- - (last message repeated 1 times) + (last message repeated 2 times) ALERT: attempt to use an already closed connection */api_sql.lua:*: SQL API error (glob) ALERT: attempt to close an already closed connection -- 4 - 301 400 0123456789 0123456789 + 601 700 0987654321 0987654321 -- 1 2 diff --git a/tests/t/api_sql_pgsql.t b/tests/t/api_sql_pgsql.t index d23286c..e8bd3ed 100644 --- a/tests/t/api_sql_pgsql.t +++ b/tests/t/api_sql_pgsql.t @@ -49,21 +49,25 @@ SQL Lua API + PostgreSQL tests -- + Unsupported argument type: 8 nil + ALERT: attempt to free an invalid result set db_free_results() failed db_free_results() failed + db_free_results() failed + 601\t0987654321\t0.9 (esc) -- - (last message repeated 1 times) + (last message repeated 2 times) ALERT: attempt to use an already closed connection */api_sql.lua:*: SQL API error (glob) ALERT: attempt to close an already closed connection -- 4 - 301 400 0123456789 0123456789 + 601 700 0987654321 0987654321 -- 1 2 @@ -73,17 +77,17 @@ SQL Lua API + PostgreSQL tests connection creation failed -- - FATAL: PQexec() failed: 7 null value in column "a" violates not-null constraint + FATAL: PQexec() failed: 7 null value in column "a" *violates not-null constraint (glob) FATAL: failed query was: INSERT INTO t VALUES (NULL) Got an error descriptor: { connection = , query = "INSERT INTO t VALUES (NULL)", - sql_errmsg = 'null value in column "a" violates not-null constraint', + sql_errmsg = 'null value in column "a" *violates not-null constraint', (glob) sql_errno = 0, sql_state = "23502" } - */api_sql.lua:*: SQL error, errno = 0, state = '23502': null value in column "a" violates not-null constraint (glob) + */api_sql.lua:*: SQL error, errno = 0, state = '23502': null value in column "a" *violates not-null constraint (glob) FATAL: PQexec() failed: 7 value too long for type character(1) FATAL: failed query was: INSERT INTO t VALUES ('test') Got an error descriptor: