Skip to content

Commit

Permalink
ODBC-203 The fix and the testcase.
Browse files Browse the repository at this point in the history
The problem was occured only with data fetched as SQL_C_WCHAR. That
happened because for statemnt handdles after 1st one there wasn't STMT_ATTR_UPDATE_MAX_LENGTH attribute set, and getting data as a widestring depends on max_length.
Also changed similar ODBC-169 testcase to test data values, and not only
column and row count.
Added to test framework optional automatic unicode connection(with
SQLDriverConnectW) and statement allocation in that connection.
  • Loading branch information
lawrinn committed Dec 2, 2018
1 parent f1e0cd2 commit 20e0a50
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 80 deletions.
2 changes: 1 addition & 1 deletion ma_bulk.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ SQLRETURN MADB_SetBulkOperLengthArr(MADB_Stmt *Stmt, MADB_DescRecord *CRec, SQLL
MaBind->length= MADB_REALLOC(MaBind->length, Stmt->Bulk.ArraySize*sizeof(long));
if (MaBind->length == NULL)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);;
return MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
}
}

Expand Down
21 changes: 19 additions & 2 deletions ma_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ void CloseMultiStatements(MADB_Stmt *Stmt)
}


MYSQL_STMT* MADB_NewStmtHandle(MADB_Stmt *Stmt)
{
static const my_bool UpdateMaxLength= 1;
MYSQL_STMT* stmt= mysql_stmt_init(Stmt->Connection->mariadb);

if (stmt != NULL)
{
mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &UpdateMaxLength);
}
else
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
}

return stmt;
}

/* Required, but not sufficient condition */
BOOL QueryIsPossiblyMultistmt(MADB_QUERY *Query)
{
Expand Down Expand Up @@ -75,7 +92,7 @@ unsigned int GetMultiStatements(MADB_Stmt *Stmt, BOOL ExecDirect)

while (p < Stmt->Query.RefinedText + Stmt->Query.RefinedLength)
{
Stmt->MultiStmts[i]= i == 0 ? Stmt->stmt : mysql_stmt_init(Stmt->Connection->mariadb);
Stmt->MultiStmts[i]= i == 0 ? Stmt->stmt : MADB_NewStmtHandle(Stmt);
MDBUG_C_PRINT(Stmt->Connection, "-->inited&preparing %0x(%d,%s)", Stmt->MultiStmts[i], i, p);

if (mysql_stmt_prepare(Stmt->MultiStmts[i], p, (unsigned long)strlen(p)))
Expand All @@ -88,7 +105,7 @@ unsigned int GetMultiStatements(MADB_Stmt *Stmt, BOOL ExecDirect)
prepare "multi-statement". */
if (i == 0 && Stmt->Error.NativeError !=1295 /*ER_UNSUPPORTED_PS*/)
{
Stmt->stmt= mysql_stmt_init(Stmt->Connection->mariadb);
Stmt->stmt= MADB_NewStmtHandle(Stmt);
if (mysql_stmt_prepare(Stmt->stmt, STMT_STRING(Stmt), (unsigned long)strlen(STMT_STRING(Stmt))))
{
mysql_stmt_close(Stmt->stmt);
Expand Down
1 change: 1 addition & 0 deletions ma_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define _ma_helper_h_

void CloseMultiStatements(MADB_Stmt *Stmt);
MYSQL_STMT* MADB_NewStmtHandle(MADB_Stmt *Stmt);
BOOL QueryIsPossiblyMultistmt(MADB_QUERY *Query);
int SqlRtrim(char *StmtStr, int Length);
unsigned int GetMultiStatements(MADB_Stmt *Stmt, BOOL ExecDirect);
Expand Down
17 changes: 6 additions & 11 deletions ma_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include <ma_odbc.h>

struct st_ma_stmt_methods MADB_StmtMethods; /* declared at the end of file */
static my_bool UpdateMaxLength= 1;


/* {{{ MADB_StmtInit */
SQLRETURN MADB_StmtInit(MADB_Dbc *Connection, SQLHANDLE *pHStmt)
Expand All @@ -36,7 +34,7 @@ SQLRETURN MADB_StmtInit(MADB_Dbc *Connection, SQLHANDLE *pHStmt)

LOCK_MARIADB(Connection);

if (!(Stmt->stmt= mysql_stmt_init(Stmt->Connection->mariadb)) ||
if (!(Stmt->stmt= MADB_NewStmtHandle(Stmt)) ||
!(Stmt->IApd= MADB_DescInit(Connection, MADB_DESC_APD, FALSE)) ||
!(Stmt->IArd= MADB_DescInit(Connection, MADB_DESC_ARD, FALSE)) ||
!(Stmt->IIpd= MADB_DescInit(Connection, MADB_DESC_IPD, FALSE)) ||
Expand All @@ -46,7 +44,6 @@ SQLRETURN MADB_StmtInit(MADB_Dbc *Connection, SQLHANDLE *pHStmt)
}

MDBUG_C_PRINT(Stmt->Connection, "-->inited %0x", Stmt->stmt);
mysql_stmt_attr_set(Stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &UpdateMaxLength);

Stmt->PutParam= -1;
Stmt->Methods= &MADB_StmtMethods;
Expand Down Expand Up @@ -393,17 +390,15 @@ void MADB_StmtReset(MADB_Stmt *Stmt)
{
MDBUG_C_PRINT(Stmt->Connection, "-->closing %0x", Stmt->stmt);
mysql_stmt_close(Stmt->stmt);
Stmt->stmt= mysql_stmt_init(Stmt->Connection->mariadb);
mysql_stmt_attr_set(Stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &UpdateMaxLength);
Stmt->stmt= MADB_NewStmtHandle(Stmt);

MDBUG_C_PRINT(Stmt->Connection, "-->inited %0x", Stmt->stmt);
}
}
else
{
CloseMultiStatements(Stmt);
Stmt->stmt= mysql_stmt_init(Stmt->Connection->mariadb);
mysql_stmt_attr_set(Stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &UpdateMaxLength);
Stmt->stmt= MADB_NewStmtHandle(Stmt);

MDBUG_C_PRINT(Stmt->Connection, "-->inited %0x", Stmt->stmt);
}
Expand Down Expand Up @@ -474,8 +469,8 @@ SQLRETURN MADB_RegularPrepare(MADB_Stmt *Stmt)
MDBUG_C_PRINT(Stmt->Connection, "mysql_stmt_close(%0x)", Stmt->stmt);
mysql_stmt_close(Stmt->stmt);

Stmt->stmt= mysql_stmt_init(Stmt->Connection->mariadb);
mysql_stmt_attr_set(Stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &UpdateMaxLength);
Stmt->stmt= MADB_NewStmtHandle(Stmt);

UNLOCK_MARIADB(Stmt->Connection);

MDBUG_C_PRINT(Stmt->Connection, "mysql_stmt_init(%0x)->%0x", Stmt->Connection->mariadb, Stmt->stmt);
Expand Down Expand Up @@ -1082,7 +1077,7 @@ SQLRETURN MADB_StmtExecute(MADB_Stmt *Stmt, BOOL ExecDirect)
}
if (StatementNr > 0)
{
Stmt->stmt= mysql_stmt_init(Stmt->Connection->mariadb);
Stmt->stmt= MADB_NewStmtHandle(Stmt);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion test/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ ODBC_TEST(t_odbc139)
CHECK_ENV_RC(Env, SQLAllocConnect(Env, &Hdbc));

CHECK_DBC_RC(Hdbc, SQLSetConnectAttr(Hdbc, SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER)"test", 4));
Hstmt= DoConnect(Hdbc, NULL, NULL, NULL, 0, NULL, &Compression, NULL, NULL);
Hstmt= DoConnect(Hdbc, FALSE, NULL, NULL, NULL, 0, NULL, &Compression, NULL, NULL);


Thread= CreateThread(NULL, 0, FireQueryInThread, Hstmt, 0, NULL);
Expand Down
2 changes: 1 addition & 1 deletion test/catalog1.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ ODBC_TEST(t_sqltables)
}

IS(AllocEnvConn(&Env, &hdbc1));
Stmt1= DoConnect(hdbc1, NULL, NULL, NULL, 0, "mariadbodbc_sqltables", 0, NULL, NULL);
Stmt1= DoConnect(hdbc1, FALSE, NULL, NULL, NULL, 0, "mariadbodbc_sqltables", 0, NULL, NULL);
FAIL_IF(Stmt1 == NULL, "");

OK_SIMPLE_STMT(Stmt1, "CREATE TABLE t1 (a int)");
Expand Down
2 changes: 1 addition & 1 deletion test/catalog2.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ ODBC_TEST(t_bug50195)

CHECK_ENV_RC(Env, SQLAllocConnect(Env, &hdbc1));

hstmt1= DoConnect(hdbc1, my_dsn, "bug50195", "a", 0, NULL, NULL, NULL, NULL);
hstmt1= DoConnect(hdbc1, FALSE, my_dsn, "bug50195", "a", 0, NULL, NULL, NULL, NULL);

if (hstmt1 == NULL)
{
Expand Down
2 changes: 1 addition & 1 deletion test/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ ODBC_TEST(odbc143)
is_num(Length, sizeof(SQLWCHAR));

AllocEnvConn(&Env, &Hdbc1);
Stmt1= DoConnect(Hdbc1, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL);
Stmt1= DoConnect(Hdbc1, FALSE, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL);
FAIL_IF(Stmt1 == NULL, "Could not connect and/or allocate");

OK_SIMPLE_STMT(Stmt1, "SET @@SESSION.sql_mode='ANSI_QUOTES'");
Expand Down
45 changes: 37 additions & 8 deletions test/multistatement.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ ODBC_TEST(t_odbc74)
OK_SIMPLE_STMT(Stmt, "TRUNCATE TABLE odbc74");

AllocEnvConn(&Env, &hdbc1);
Stmt1= DoConnect(hdbc1, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL);
Stmt1= DoConnect(hdbc1, FALSE, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL);
FAIL_IF(Stmt1 == NULL, "Could not connect and/or allocate");

OK_SIMPLE_STMT(Stmt1, "SET @@SESSION.sql_mode='NO_BACKSLASH_ESCAPES'");
Expand Down Expand Up @@ -508,12 +508,31 @@ ODBC_TEST(t_odbc177)

ODBC_TEST(t_odbc169)
{
SQLCHAR Query[][80]= {"SELECT 1 Col1; SELECT * from t_odbc169", "SELECT * from t_odbc169; SELECT * from t_odbc169 ORDER BY col1 DESC",
SQLCHAR Query[][80]= {"SELECT 1 Col1; SELECT * from t_odbc169", "SELECT * from t_odbc169 ORDER BY col1 DESC; SELECT col3, col2 from t_odbc169",
"INSERT INTO t_odbc169 VALUES(8, 7, 'Row #4');SELECT * from t_odbc169"};
unsigned int i, j= 0, ExpectedRows[]= {1, 3, 3, 3, 0, 4, 1};
char Expected[][3][7]={ {"1", "", "" }, /* RS 1*/
{"1", "2", "Row 1"}, /* RS 2*/
{"3", "4", "Row 2"},
{"5", "6", "Row 3"},
{"5", "6", "Row 3"}, /* RS 3*/
{"3", "4", "Row 2"},
{"1", "2", "Row 1"},
{"Row 1", "2" , ""}, /* RS 4*/
{"Row 2", "4" , ""},
{"Row 3", "6" , ""},

/* RS 5 is empty */
{"1", "2", "Row 1" }, /* RS 6*/
{"3", "4", "Row 2" },
{"5", "6", "Row 3" },
{"8", "7", "Row #4"}
};
unsigned int i, RsIndex= 0, ExpectedRows[]= {1, 3, 3, 3, 0, 4, 1};
SQLLEN Rows, ExpRowCount[]= {0, 0, 0, 0, 1, 0, 0};
SQLSMALLINT ColumnsCount, expCols[]= {1, 3, 3, 3, 0, 3, 1};
SQLSMALLINT ColumnsCount, expCols[]= {1, 3, 3, 2, 0, 3, 1};
SQLRETURN rc;
SQLSMALLINT Column, Row= 0;
SQLCHAR ColumnData[MAX_ROW_DATA_LEN]={ 0 };

OK_SIMPLE_STMT(Stmt, "DROP TABLE IF EXISTS t_odbc169");

Expand All @@ -527,14 +546,24 @@ ODBC_TEST(t_odbc169)

do {
CHECK_STMT_RC(Stmt, SQLRowCount(Stmt, &Rows));
is_num(Rows, ExpRowCount[j]);
is_num(Rows, ExpRowCount[RsIndex]);
CHECK_STMT_RC(Stmt, SQLNumResultCols(Stmt, &ColumnsCount));
is_num(ColumnsCount, expCols[j]);
is_num(ColumnsCount, expCols[RsIndex]);

is_num(ma_print_result_getdata_ex(Stmt, FALSE), ExpectedRows[j]);
Rows= 0;
while (SQL_SUCCEEDED(SQLFetch(Stmt)))
{
for (Column= 0; Column < ColumnsCount; ++Column)
{
IS_STR(my_fetch_str(Stmt, ColumnData, Column + 1), Expected[Row][Column], strlen(Expected[Row][Column]));
}
++Row;
++Rows;
}
is_num(Rows, ExpectedRows[RsIndex]);

rc= SQLMoreResults(Stmt);
++j;
++RsIndex;
} while (rc != SQL_NO_DATA);

CHECK_STMT_RC(Stmt, SQLFreeStmt(Stmt, SQL_CLOSE));
Expand Down
Loading

0 comments on commit 20e0a50

Please sign in to comment.