/* * Tests DATA_AT_EXEC for simple types */ #include #include #include "common.h" int main(int argc, char** argv) { SQLRETURN rc, rc2; HSTMT hstmt = SQL_NULL_HSTMT; SQLCHAR *sql; SQLINTEGER myint = 1337, myint_get = 0; SQLBIGINT mybigint = 1337, mybigint_get = 0; SQL_NUMERIC_STRUCT mydecimal, mydecimal_get; memset(&mydecimal, 0, sizeof(mydecimal)); memset(&mydecimal_get, 0, sizeof(mydecimal_get)); mydecimal.scale = 4; mydecimal.sign = 1; mydecimal.precision = 14; //set to 1337.0000 mydecimal.val[0] = 0x90; mydecimal.val[1] = 0x02; mydecimal.val[2] = 0xCC; SQLINTEGER i1 = 1, i2 = 2, i3 = 3; SQLLEN ind = SQL_DATA_AT_EXEC; test_connect(); rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt); if (!SQL_SUCCEEDED(rc)) { print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); exit(1); } sql = "DROP TABLE IF EXISTS dataatexec_basictypes"; rc = SQLExecDirect(hstmt, sql, SQL_NTS); CHECK_STMT_RESULT(rc, "SQLExecDirect failed while dropping previous table", hstmt); sql = "CREATE TABLE dataatexec_basictypes (myint int4, mybigint bigint, mydecimal decimal(14, 4))"; rc = SQLExecDirect(hstmt, sql, SQL_NTS); CHECK_STMT_RESULT(rc, "SQLExecDirect failed while creating table", hstmt); sql = "INSERT INTO dataatexec_basictypes VALUES(?, ?, ?)"; rc = SQLPrepare(hstmt, sql, SQL_NTS); CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt); rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &i1, sizeof(SQLINTEGER), &ind); CHECK_STMT_RESULT(rc, "SQLBindParam (SQLINTEGER) failed", hstmt); rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, 0, 0, &i2, sizeof(SQLBIGINT), &ind); CHECK_STMT_RESULT(rc, "SQLBindParam (SQLBIGINT) failed", hstmt); rc = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_NUMERIC, SQL_NUMERIC, 0, 0, &i3, sizeof(SQL_NUMERIC_STRUCT), &ind); CHECK_STMT_RESULT(rc, "SQLBindParam (SQL_NUMERIC_STRUCT) failed", hstmt); rc2 = SQLExecute(hstmt); if (rc2 != SQL_NEED_DATA) { printf("SQLExcecute should have returned SQL_NEED_DATA.\n"); exit(1); } while (rc2 == SQL_NEED_DATA) { SQLPOINTER p = NULL; rc2 = SQLParamData(hstmt, &p); if (SQL_SUCCEEDED(rc2)) break; switch (*(SQLINTEGER*)p) { case 1: rc = SQLPutData(hstmt, &myint, sizeof(SQLINTEGER)); CHECK_STMT_RESULT(rc, "SQLPutData (SQLINTEGER) failed", hstmt); break; case 2: rc = SQLPutData(hstmt, &mybigint, sizeof(SQLBIGINT)); CHECK_STMT_RESULT(rc, "SQLPutData (SQLBIGINT) failed", hstmt); break; case 3: rc = SQLPutData(hstmt, &mydecimal, sizeof(SQL_NUMERIC_STRUCT)); CHECK_STMT_RESULT(rc, "SQLPutData (SQLINTEGER) failed", hstmt); break; default: printf("SQLParamData returned unexpected value.\n"); exit(1); break; } } rc = SQLFreeStmt(hstmt, SQL_CLOSE); CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); sql = "SELECT myint, mybigint, mydecimal FROM dataatexec_basictypes"; rc = SQLPrepare(hstmt, sql, SQL_NTS); CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt); rc = SQLBindCol(hstmt, 1, SQL_C_SLONG, &myint_get, 0, NULL); CHECK_STMT_RESULT(rc, "SQLBindCol (SQLINTEGER) failed", hstmt); rc = SQLBindCol(hstmt, 2, SQL_C_SBIGINT, &mybigint_get, 0, NULL); CHECK_STMT_RESULT(rc, "SQLBindCol (SQLBIGINT) failed", hstmt); rc = SQLBindCol(hstmt, 3, SQL_C_NUMERIC, &mydecimal_get, 0, NULL); CHECK_STMT_RESULT(rc, "SQLBindCol (SQL_NUMERIC_STRUCT) failed", hstmt); rc = SQLExecute(hstmt); CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt); rc = SQLFetch(hstmt); CHECK_STMT_RESULT(rc, "SQLFetch failed", hstmt); int fail = 0; if (myint != myint_get) { printf("myint <> myint_get\n"); fail = 1; } if (mybigint != mybigint_get) { printf("mybigint <> mybigint_get\n"); fail = 1; } if (memcmp(mydecimal.val, mydecimal_get.val, SQL_MAX_NUMERIC_LEN) != 0) { printf("mydecimal <> mydecimal_get\n"); fail = 1; } if (fail) exit(1); SQLFreeStmt(hstmt, SQL_CLOSE); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); test_disconnect(); }