/* BUG 5778 test case */ #include #include #include #include #include void CheckRC(SQLRETURN rc, char* msg, char* filename, int lineno); #define CHECKRC(FUNCSHN,MSG) CheckRC(FUNCSHN, MSG, __FILE__, __LINE__) SQLHENV hEnv = SQL_NULL_HENV; SQLHDBC hDbc = SQL_NULL_HDBC; SQLHSTMT hStmt = SQL_NULL_HSTMT; SQLCHAR* connString; SQLCHAR connOut[255]; /* buffer for connection output */ SQLSMALLINT szConnOut; /* num bytes returned in connOut */ void doDrop() { CHECKRC( SQLAllocStmt(hDbc, &hStmt), "SQLAllocStmt()" ); CHECKRC( SQLExecDirect(hStmt, (SQLCHAR*)"DROP TABLE IF EXISTS bug5778", SQL_NTS), "SQLExecDirect()" ); CHECKRC( SQLFreeStmt(hStmt, SQL_DROP), "SQLFreeStmt()" ); } void doCreate() { CHECKRC( SQLAllocStmt(hDbc, &hStmt), "SQLAllocStmt()" ); CHECKRC( SQLExecDirect(hStmt, (SQLCHAR*)"CREATE TABLE bug5778 ( col1 INT, col2 INT, col3 INT )", SQL_NTS), "SQLExecDirect()" ); CHECKRC( SQLFreeStmt(hStmt, SQL_DROP), "SQLFreeStmt()" ); } void doTest( SQLCHAR *pszSQL ) { short numResultCol; CHECKRC( SQLAllocStmt(hDbc, &hStmt), "SQLAllocStmt()" ); CHECKRC( SQLPrepare(hStmt, pszSQL, SQL_NTS), "SQLPrepare()" ); CHECKRC( SQLNumResultCols(hStmt,&numResultCol), "SQLNumResultCols()"); /**** * At this point, even though we have not actually bound any params nor * have we even run SQLExecute(), you will see a "blank" row in the * database for INSERT! *****/ CHECKRC( SQLFreeStmt(hStmt, SQL_DROP), "SQLFreeStmt()" ); } //-------- main() ----------- int main(int argc, char** argv) { int nPrompt = SQL_DRIVER_NOPROMPT; if (argc < 2 || strlen(argv[1]) == 0) { connString = (SQLCHAR *)"DRIVER=MySQL ODBC 3.51 Driver;"; nPrompt = SQL_DRIVER_PROMPT; } else connString = (SQLCHAR *)argv[1]; if (SQLAllocEnv(&hEnv) != SQL_SUCCESS) { fprintf(stderr, "Unable to allocate SQLHENV\n"); return(1); } CHECKRC( SQLAllocConnect(hEnv, &hDbc), "SQLAllocConnect()" ); CHECKRC( SQLDriverConnect(hDbc, (HWND)1 /* 1 is ok with portable gui */, connString, SQL_NTS, connOut, 255, &szConnOut, nPrompt), "SQLDriverConnect()" ); doDrop(); doCreate(); doTest( (SQLCHAR*)"INSERT INTO bug5778 VALUES (?, ?, ?)" ); doTest( (SQLCHAR*)"SELECT * FROM bug5778" ); CHECKRC( SQLDisconnect(hDbc), "SQLDisconnect()" ); CHECKRC( SQLFreeConnect(hDbc), "SQLFreeConnect()" ); CHECKRC( SQLFreeEnv(hEnv), "SQLFreeEnv()" ); return 0; } //---- Helper function ------------- void CheckRC(SQLRETURN rc, char* msg, char* filename, int lineno) { #define MSG_LNG 512 SQLCHAR szSqlState[MSG_LNG]; SQLINTEGER pfNativeError; SQLCHAR szErrorMsg[MSG_LNG]; SQLSMALLINT pcbErrorMsg; SQLRETURN ret = SQL_SUCCESS; bool error = false; if (rc != SQL_SUCCESS && rc != SQL_NO_DATA_FOUND) { if (rc != SQL_SUCCESS_WITH_INFO) { // An error, not a warning fprintf(stderr, "\nERROR (%s:%d): %s\n", filename, lineno, msg); error = true; } // Find the reason for the err/warning while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) { ret = SQLError(hEnv, hDbc, hStmt, szSqlState, &pfNativeError, szErrorMsg, MSG_LNG, &pcbErrorMsg); switch (ret) { case SQL_SUCCESS: // This means SQLError() returned normally if (rc == SQL_SUCCESS_WITH_INFO) { fprintf(stderr, "\nINFO: (%s:%d): %s\n", filename, lineno, msg); } fprintf(stderr, "*** %s\n*** ODBC Code: %s, " "Driver Code: %ld\n\n", szErrorMsg, szSqlState, pfNativeError); break; case SQL_NO_DATA_FOUND: // from SQLError() break; default: fprintf(stderr, "*** SQLError() failed with code=%d\n\n", ret); break; } } } if (error) exit(1); }