Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix client prepare with string parameter #412

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/db_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,8 @@ int db_parse_arguments(void)
int db_print_value(db_bind_t *var, char *buf, int buflen)
{
int n;
char *p;
char *end;
db_time_t *tm;

if (var->is_null != NULL && *var->is_null)
Expand Down Expand Up @@ -881,7 +883,16 @@ int db_print_value(db_bind_t *var, char *buf, int buflen)
break;
case DB_TYPE_CHAR:
case DB_TYPE_VARCHAR:
n = snprintf(buf, buflen, "'%s'", (char *)var->buffer);
if (buflen < (int)var->max_len + 3)
return -1;
n = 0;
p = (char *)var->buffer;
end = p + var->max_len;
buf[n++] = '\'';
while (*p && p < end)
buf[n++] = *p++;
buf[n++] = '\'';
buf[n] = '\0';
break;
case DB_TYPE_DATE:
tm = (db_time_t *)var->buffer;
Expand Down
41 changes: 38 additions & 3 deletions tests/include/api_sql_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ end
EOF
# Reset --mysql-socket if it's specified on the command line, otherwise sysbench
# will assume --mysql-host=localhost
sysbench $SB_ARGS --mysql-host="non-existing" --pgsql-host="non-existing" \
--mysql-socket= \
run
sysbench $SB_ARGS $DB_NON_EXISTING run

# Error hooks
cat >$CRAMTMP/api_sql.lua <<EOF
Expand Down Expand Up @@ -274,3 +272,40 @@ end
EOF

sysbench $SB_ARGS

cat <<EOF
########################################################################
# Client prepare with string parameter is broken
########################################################################
EOF

cat >$CRAMTMP/api_sql.lua <<EOF
con = sysbench.sql.driver():connect()
con:query("create table t (a char(8))")

function print_result(query)
print('--')
local rs = con:query(query)
for i = 1, rs.nrows do
local a = unpack(rs:fetch_row(), 1, rs.nfields)
print(a)
end
end

local v = '12345678'

con:query(("insert into t (a) values ('%s')"):format(v))
print_result("select a from t")

local stmt = con:prepare("insert into t (a) values (?)")
local p = stmt:bind_create(sysbench.sql.type.CHAR, 8)
stmt:bind_param(p)

p:set(v)
stmt:execute()
print_result("select a from t")

con:query("drop table t")
EOF

sysbench --db-ps-mode=disable $SB_ARGS
18 changes: 17 additions & 1 deletion tests/include/mysql_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,24 @@ then
exit 80
fi

MYSQL_ARGS=`echo -n $SBTEST_MYSQL_ARGS | sed -- 's/--mysql-db=//;s/--mysql-/--/g'`

if echo -n $SBTEST_MYSQL_ARGS | grep -q -- --mysql-db; then
MYSQL_DB=""
else
MYSQL_DB=sbtest
fi

if echo -n $SBTEST_MYSQL_ARGS | grep -q -- --mysql-user; then
MYSQL_USER=
else
MYSQL_USER=-usbtest
fi

function db_show_table() {
mysql -uroot sbtest -Nse "SHOW CREATE TABLE $1\G"
mysql $MYSQL_ARGS $MYSQL_USER $MYSQL_DB -Nse "SHOW CREATE TABLE $1\G"
}

DB_DRIVER_ARGS="--db-driver=mysql $SBTEST_MYSQL_ARGS"

DB_NON_EXISTING="--mysql-host=non-existing --mysql-socket="
2 changes: 2 additions & 0 deletions tests/include/pgsql_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ EOF
}

DB_DRIVER_ARGS="--db-driver=pgsql $SBTEST_PGSQL_ARGS"

DB_NON_EXISTING="--pgsql-host=non-existing"
10 changes: 9 additions & 1 deletion tests/t/api_sql_mysql.t
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ SQL Lua API + MySQL tests
--
reconnects = 1
FATAL: unable to connect to MySQL server on host 'non-existing', port 3306, aborting...
FATAL: error 2005: Unknown MySQL server host 'non-existing' (0)
FATAL: error 2005: Unknown MySQL server host 'non-existing' (2)
connection creation failed
--
FATAL: mysql_drv_query() returned error 1048 (Column 'a' cannot be null) for query 'INSERT INTO t VALUES (NULL)'
Expand Down Expand Up @@ -133,6 +133,14 @@ SQL Lua API + MySQL tests
########################################################################
1
2
########################################################################
# Client prepare with string parameter is broken
########################################################################
--
12345678
--
12345678
12345678
########################################################################
# GH-304: Benchmark Stored Procedure with sysbench
########################################################################
Expand Down
8 changes: 8 additions & 0 deletions tests/t/api_sql_pgsql.t
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,11 @@ SQL Lua API + PostgreSQL tests
########################################################################
1
2
########################################################################
# Client prepare with string parameter is broken
########################################################################
--
12345678
--
12345678
12345678