diff --git a/ma_helper.c b/ma_helper.c index 67eacf14..8bb5f81c 100644 --- a/ma_helper.c +++ b/ma_helper.c @@ -65,15 +65,19 @@ unsigned int GetMultiStatements(MADB_Stmt *Stmt, char *StmtStr, SQLINTEGER Lengt MYSQL_STMT *stmt; char *StmtCopy= NULL; + /* mysql_stmt_init requires locking as well */ + LOCK_MARIADB(Stmt->Connection); stmt= mysql_stmt_init(Stmt->Connection->mariadb); /* if the entire stmt string passes, we don't have multistatement */ if (stmt && !mysql_stmt_prepare(stmt, StmtStr, Length)) { mysql_stmt_close(stmt); + UNLOCK_MARIADB(Stmt->Connection); return 1; } mysql_stmt_close(stmt); + /* make sure we don't have trailing whitespace or semicolon */ if (Length) { @@ -159,7 +163,11 @@ unsigned int GetMultiStatements(MADB_Stmt *Stmt, char *StmtStr, SQLINTEGER Lengt MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->MultiStmts[i]); CloseMultiStatements(Stmt); if (StmtCopy) + { my_free(StmtCopy); + } + UNLOCK_MARIADB(Stmt->Connection); + return 0; } if (mysql_stmt_param_count(Stmt->MultiStmts[i]) > MaxParams) @@ -167,6 +175,8 @@ unsigned int GetMultiStatements(MADB_Stmt *Stmt, char *StmtStr, SQLINTEGER Lengt p+= strlen(p) + 1; ++i; } + UNLOCK_MARIADB(Stmt->Connection); + if (MaxParams) Stmt->params= (MYSQL_BIND *)MADB_CALLOC(sizeof(MYSQL_BIND) * MaxParams); } diff --git a/ma_statement.c b/ma_statement.c index 35839143..7d64ab0d 100644 --- a/ma_statement.c +++ b/ma_statement.c @@ -33,16 +33,20 @@ SQLRETURN MADB_StmtInit(MADB_Dbc *Connection, SQLHANDLE *pHStmt) *pHStmt= Stmt; Stmt->Connection= Connection; + LOCK_MARIADB(Connection); + if (!(Stmt->stmt= mysql_stmt_init(Stmt->Connection->mariadb)) || - !(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)) || - !(Stmt->IIrd= MADB_DescInit(Connection, MADB_DESC_IRD, FALSE))) + !(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)) || + !(Stmt->IIrd= MADB_DescInit(Connection, MADB_DESC_IRD, FALSE))) + { goto error; + } + MDBUG_C_PRINT(Stmt->Connection, "-->inited %0x", Stmt->stmt); mysql_stmt_attr_set(Stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &UpdateMaxLength); - Stmt->Connection= Connection; Stmt->PutParam= -1; Stmt->Methods= &MADB_StmtMethods; /* default behaviour is SQL_CURSOR_STATIC */ @@ -54,10 +58,9 @@ SQLRETURN MADB_StmtInit(MADB_Dbc *Connection, SQLHANDLE *pHStmt) Stmt->Ipd= Stmt->IIpd; Stmt->Ird= Stmt->IIrd; - EnterCriticalSection(&Connection->cs); Stmt->ListItem.data= (void *)Stmt; Stmt->Connection->Stmts= list_add(Stmt->Connection->Stmts, &Stmt->ListItem); - LeaveCriticalSection(&Connection->cs); + UNLOCK_MARIADB(Connection); Stmt->Ard->Header.ArraySize= 1; return SQL_SUCCESS; @@ -65,7 +68,6 @@ SQLRETURN MADB_StmtInit(MADB_Dbc *Connection, SQLHANDLE *pHStmt) error: if (Stmt && Stmt->stmt) { - LOCK_MARIADB(Stmt->Connection); mysql_stmt_close(Stmt->stmt); UNLOCK_MARIADB(Stmt->Connection); } @@ -350,7 +352,7 @@ MYSQL_RES* FetchMetadata(MADB_Stmt *Stmt) } /* }}} */ -/* {{{ MADB_StmtReset - reseting Stmt handler for new use */ +/* {{{ MADB_StmtReset - reseting Stmt handler for new use. Has to be called inside a lock */ void MADB_StmtReset(MADB_Stmt *Stmt) { /* TODO: These all stuff has to be moved to a separate function, smth like MADB_StmtReset */ @@ -493,9 +495,10 @@ SQLRETURN MADB_StmtPrepare(MADB_Stmt *Stmt, char *StatementText, SQLINTEGER Text MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt); /* We need to close the stmt here, or it becomes unusable like in ODBC-21 */ mysql_stmt_close(Stmt->stmt); - UNLOCK_MARIADB(Stmt->Connection); Stmt->stmt= mysql_stmt_init(Stmt->Connection->mariadb); + UNLOCK_MARIADB(Stmt->Connection); + return Stmt->Error.ReturnValue; } UNLOCK_MARIADB(Stmt->Connection);