int main(void) { SQLHDESC Descriptor; SQLINTEGER ind; SQLTCHAR name[128]; SQLSMALLINT si; odbc_use_version3 = 1; odbc_connect(); odbc_command("create table #tmp1 (i int)"); /* get IRD */ CHKGetStmtAttr(SQL_ATTR_IMP_ROW_DESC, &Descriptor, sizeof(Descriptor), &ind, "S"); CHKGetDescRec(-1, name, ODBC_VECTOR_SIZE(name), &si, NULL, NULL, NULL, NULL, NULL, NULL, "E"); /* TODO here should be NO_DATA cause we are requesting bookmark */ /* CHKGetDescRec(0, name, sizeof(name), &si, NULL, NULL, NULL, NULL, NULL, NULL, "No"); */ CHKGetDescRec(1, name, ODBC_VECTOR_SIZE(name), &si, NULL /*Type*/, NULL /*SubType*/, NULL /*Length*/, NULL/*Precision*/, NULL /*Scale*/, NULL /*Nullable*/, "No"); odbc_command("SELECT name FROM sysobjects"); CHKGetDescRec(1, name, ODBC_VECTOR_SIZE(name), &si, NULL /*Type*/, NULL /*SubType*/, NULL /*Length*/, NULL/*Precision*/, NULL /*Scale*/, NULL /*Nullable*/, "S"); odbc_disconnect(); ODBC_FREE(); return 0; }
int main(int argc, char *argv[]) { SQLSMALLINT num_params, cols; SQLLEN count; SQLINTEGER id; odbc_use_version3 = 1; odbc_connect(); odbc_command("create table #tester (id int not null, name varchar(20) not null)"); odbc_command("insert into #tester(id, name) values(1, 'abc')"); odbc_command("insert into #tester(id, name) values(2, 'duck')"); CHKPrepare(T("SELECT * FROM #tester WHERE id = ?"), SQL_NTS, "S"); CHKR(SQLNumParams, (odbc_stmt, &num_params), "S"); assert(num_params == 1); id = 1; CHKBindParameter(1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &id, sizeof(id), NULL, "S"); CHKExecute("S"); CHKR(SQLFreeStmt, (odbc_stmt, SQL_RESET_PARAMS), "S"); CHKRowCount(&count, "S"); CHKNumResultCols(&cols, "S"); assert(cols == 2); odbc_disconnect(); return 0; }
static void TestProc(const char *type, const char *expected) { char sql[256]; odbc_command("IF OBJECT_ID('stat_proc') IS NOT NULL DROP PROC stat_proc"); sprintf(sql, "CREATE PROC stat_proc(@t %s) AS RETURN 0", type); odbc_command(sql); column = "@t"; CHKProcedureColumns(T(catalog), LEN(catalog), T(schema), LEN(schema), T(proc), LEN(proc), T(column), LEN(column), "SI"); CHKFetch("SI"); ReadCol(6); if (strcmp(output, expected) != 0) { fprintf(stderr, "Got \"%s\" expected \"%s\"\n", output, expected); odbc_disconnect(); exit(1); } CHKCloseCursor("SI"); ODBC_FREE(); }
static void TestTable(const char *type, const char *expected) { char sql[256]; odbc_command("IF OBJECT_ID('stat_t') IS NOT NULL DROP TABLE stat_t"); sprintf(sql, "CREATE TABLE stat_t(t %s)", type); odbc_command(sql); column = "t"; table = "stat_t"; CHKColumns(T(catalog), LEN(catalog), T(schema), LEN(schema), T(table), LEN(table), T(column), LEN(column), "SI"); CHKFetch("SI"); ReadCol(5); if (strcmp(output, expected) != 0) { fprintf(stderr, "Got \"%s\" expected \"%s\"\n", output, expected); odbc_disconnect(); exit(1); } CHKCloseCursor("SI"); ODBC_FREE(); }
int main(int argc, char *argv[]) { SQLSMALLINT len; const char * const*p; SQLINTEGER n; if (odbc_read_login_info()) exit(1); /* connect string using DSN */ init_connect(); sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;DATABASE=%s;ClientCharset=UTF-8;", odbc_server, odbc_user, odbc_password, odbc_database); CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp)/sizeof(SQLTCHAR), &len, SQL_DRIVER_NOPROMPT, "SI"); if (!odbc_driver_is_freetds()) { odbc_disconnect(); printf("Driver is not FreeTDS, exiting\n"); return 0; } if (!odbc_db_is_microsoft() || odbc_db_version_int() < 0x08000000u) { odbc_disconnect(); printf("Test for MSSQL only\n"); return 0; } CHKAllocStmt(&odbc_stmt, "S"); /* create test table */ sprintf(tmp, "IF OBJECT_ID(N'%s') IS NOT NULL DROP TABLE %s", table_name, table_name); odbc_command(tmp); sprintf(tmp, "CREATE TABLE %s (k int, c NCHAR(10), vc NVARCHAR(10))", table_name); odbc_command(tmp); /* insert with INSERT statements */ for (n = 1, p = strings; p[0] && p[1]; p += 2, ++n) { sprintf(tmp, "INSERT INTO %s VALUES (%d,N'%s',N'%s')", table_name, (int) n, p[0], p[1]); odbc_command(tmp); } /* check rows */ for (n = 1, p = strings_hex; p[0] && p[1]; p += 2, ++n) { sprintf(tmp, "IF NOT EXISTS(SELECT * FROM %s WHERE k = %d AND c = %s AND vc = %s) SELECT 1", table_name, (int) n, p[0], p[1]); CheckNoRow(tmp); } TestBinding(0); TestBinding(1); /* cleanup */ sprintf(tmp, "IF OBJECT_ID(N'%s') IS NOT NULL DROP TABLE %s", table_name, table_name); odbc_command(tmp); odbc_disconnect(); printf("Done.\n"); return 0; }
static int Test(int bind_before) { SQLSMALLINT ReturnCode = 0; SQLSMALLINT InParam = 5; SQLSMALLINT OutParam = 1; char OutString[OUTSTRING_LEN]; SQLLEN cbReturnCode = 0, cbInParam = 0, cbOutParam = 0; SQLLEN cbOutString = SQL_NTS; odbc_connect(); /* drop proc */ odbc_command("IF OBJECT_ID('spTestProc') IS NOT NULL DROP PROC spTestProc"); /* create proc */ odbc_command(sp_define); if (!bind_before) CHKPrepare(T(SP_TEXT), strlen(SP_TEXT), "S"); CHKBindParameter(1, SQL_PARAM_OUTPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &ReturnCode, 0, &cbReturnCode, "S"); CHKBindParameter(2, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &InParam, 0, &cbInParam, "S"); CHKBindParameter(3, SQL_PARAM_OUTPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &OutParam, 0, &cbOutParam, "S"); OutString[0] = '\0'; strcpy(OutString, "Test"); /* Comment this line and we get an error! Why? */ CHKBindParameter(4, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, OUTSTRING_LEN, 0, OutString, OUTSTRING_LEN, &cbOutString, "S"); if (bind_before) CHKPrepare(T(SP_TEXT), strlen(SP_TEXT), "S"); CHKExecute("S"); odbc_command("DROP PROC spTestProc"); printf("Output:\n"); printf(" Return Code = %d\n", (int) ReturnCode); printf(" InParam = %d\n", (int) InParam); printf(" OutParam = %d\n", (int) OutParam); printf(" OutString = %s\n", OutString); if (InParam != OutParam) { fprintf(stderr, "InParam != OutParam\n"); return 1; } if (strcmp(OutString, "This is cool!") != 0) { fprintf(stderr, "Bad string returned\n"); return 1; } odbc_disconnect(); ODBC_FREE(); return 0; }
int main(int argc, char **argv) { if (tds_mutex_init(&mtx)) return 1; if (odbc_read_login_info()) exit(1); /* * prepare our odbcinst.ini * is better to do it before connect cause uniODBC cache INIs * the name must be odbcinst.ini cause unixODBC accept only this name */ if (odbc_driver[0]) { FILE *f = fopen("odbcinst.ini", "w"); if (f) { fprintf(f, "[FreeTDS]\nDriver = %s\nThreading = 0\n", odbc_driver); fclose(f); /* force iODBC */ setenv("ODBCINSTINI", "./odbcinst.ini", 1); setenv("SYSODBCINSTINI", "./odbcinst.ini", 1); /* force unixODBC (only directory) */ setenv("ODBCSYSINI", ".", 1); } } odbc_use_version3 = 1; odbc_connect(); odbc_command("IF OBJECT_ID('tab1') IS NOT NULL DROP TABLE tab1"); odbc_command("CREATE TABLE tab1 ( k INT, vc VARCHAR(200) )"); printf(">> Creating tab1...\n"); odbc_command("DECLARE @i INT\n" "SET @i = 1\n" "WHILE @i <= 2000 BEGIN\n" "INSERT INTO tab1 VALUES ( @i, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' )\n" "SET @i = @i + 1\n" "END"); while (CHKMoreResults("SNo") == SQL_SUCCESS) continue; printf(">> ...done.\n"); odbc_reset_statement(); Test(0, 0); Test(1, 0); Test(0, 1); Test(1, 1); odbc_command("DROP TABLE tab1"); odbc_disconnect(); return 0; }
static void Init(void) { int i; char sql[128]; odbc_command("CREATE TABLE #cursor6_test (i INT, c VARCHAR(20))"); for (i = 1; i <= 10; ++i) { sprintf(sql, "INSERT INTO #cursor6_test(i,c) VALUES(%d, 'a%db%dc%d')", i, i, i, i); odbc_command(sql); } }
int main(int argc, char **argv) { int i; SQLLEN bytes_returned; /* do not allocate so big memory in stack */ buf = (unsigned char *) malloc(TEST_BUF_LEN); odbc_connect(); odbc_command("create table " TEST_TABLE_NAME " (im IMAGE)"); odbc_command("SET TEXTSIZE 1000000"); /* populate test buffer with ramp */ for (i = 0; i < TEST_BUF_LEN; i++) { buf[i] = BYTE_AT(i); } /* insert test pattern into database */ if (test_insert(buf, TEST_BUF_LEN) == -1) { clean_up(); return -1; } memset(buf, 0, TEST_BUF_LEN); /* read test pattern from database */ if (test_select(buf, TEST_BUF_LEN, &bytes_returned) == -1) { clean_up(); return -1; } /* compare inserted and read back test patterns */ if (bytes_returned != TEST_BUF_LEN) { show_error("main(): comparing buffers", "Mismatch in input and output pattern sizes."); clean_up(); return -1; } for (i = 0; i < TEST_BUF_LEN; ++i) { if (buf[i] != BYTE_AT(i)) { printf("mismatch at pos %d %d != %d\n", i, buf[i], BYTE_AT(i)); show_error("main(): comparing buffers", "Mismatch in input and output patterns."); clean_up(); return -1; } } printf("Input and output buffers of length %d match.\nTest passed.\n", TEST_BUF_LEN); clean_up(); return 0; }
int main(int argc, char **argv) { #define ARRAY_SIZE 10 SQLCHAR v_dec[ARRAY_SIZE][21]; SQLLEN v_ind[ARRAY_SIZE]; SQLULEN nrows; odbc_use_version3 = 1; odbc_connect(); odbc_check_cursor(); odbc_command("IF OBJECT_ID('mytab1') IS NOT NULL DROP TABLE mytab1"); odbc_command("CREATE TABLE mytab1 ( k INT, d DECIMAL(10,2))"); odbc_command("INSERT INTO mytab1 VALUES ( 201, 111.11 )"); /*SQLExecDirect(m_hstmt, (SQLCHAR *) "insert into mytab1 values ( 202, 222.22 )", SQL_NTS); */ odbc_command("INSERT INTO mytab1 VALUES ( 202, null )"); odbc_reset_statement(); CHKSetStmtAttr(SQL_ATTR_CURSOR_SCROLLABLE, (SQLPOINTER) SQL_NONSCROLLABLE, SQL_IS_UINTEGER, "S"); CHKSetStmtAttr(SQL_ATTR_CURSOR_SENSITIVITY, (SQLPOINTER) SQL_SENSITIVE, SQL_IS_UINTEGER, "S"); CHKSetStmtAttr(SQL_ATTR_ROW_BIND_TYPE, (SQLPOINTER) SQL_BIND_BY_COLUMN, SQL_IS_UINTEGER, "S"); CHKSetStmtAttr(SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) ARRAY_SIZE, SQL_IS_UINTEGER, "S"); CHKSetStmtAttr(SQL_ATTR_ROWS_FETCHED_PTR, (SQLPOINTER) & (nrows), SQL_IS_UINTEGER, "S"); CHKPrepare(T("SELECT SUM(d) FROM mytab1"), SQL_NTS, "S"); CHKExecute("I"); #if 0 CHKMoreResults("S"); /* skip warning*/ #endif CHKBindCol(1, SQL_C_CHAR, v_dec, 21, v_ind, "S"); CHKFetch("S"); printf("fetch 1: rows fetched = %d\n", (int) nrows); printf("fetch 1: value = [%s]\n", v_dec[0]); CHKFetch("No"); CHKMoreResults("No"); odbc_command("drop table mytab1"); odbc_disconnect(); return 0; }
/* stripped down version of TestInput for NULLs */ static void NullInput(SQLSMALLINT out_c_type, SQLSMALLINT out_sql_type, const char *param_type) { char sbuf[1024]; SQLLEN out_len = SQL_NULL_DATA; odbc_reset_statement(); /* create a table with a column of that type */ odbc_reset_statement(); sprintf(sbuf, "CREATE TABLE #tmp_insert (col %s NULL)", param_type); odbc_command(sbuf); if (use_cursors) { odbc_reset_statement(); CHKSetStmtAttr(SQL_ATTR_CURSOR_SCROLLABLE, (SQLPOINTER) SQL_SCROLLABLE, 0, "S"); CHKSetStmtAttr(SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_DYNAMIC, 0, "S"); } /* insert data using prepared statements */ sprintf(sbuf, "INSERT INTO #tmp_insert VALUES(?)"); if (exec_direct) { CHKBindParameter(1, SQL_PARAM_INPUT, out_c_type, out_sql_type, 20, 0, NULL, 1, &out_len, "S"); CHKExecDirect(T(sbuf), SQL_NTS, "SNo"); } else { if (prepare_before) CHKPrepare(T(sbuf), SQL_NTS, "S"); CHKBindParameter(1, SQL_PARAM_INPUT, out_c_type, out_sql_type, 20, 0, NULL, 1, &out_len, "S"); if (!prepare_before) CHKPrepare(T(sbuf), SQL_NTS, "S"); CHKExecute("SNo"); } /* check if row is present */ odbc_reset_statement(); if (!odbc_db_is_microsoft() && strcmp(param_type, "TEXT") == 0) odbc_command("SELECT * FROM #tmp_insert WHERE col LIKE ''"); else odbc_command("SELECT * FROM #tmp_insert WHERE col IS NULL"); CHKFetch("S"); CHKFetch("No"); CHKMoreResults("No"); odbc_command("DROP TABLE #tmp_insert"); ODBC_FREE(); }
int main(int argc, char *argv[]) { int i; SQLLEN cnamesize; const char *command; SQLCHAR output[256]; odbc_connect(); odbc_command("if object_id('tempdb..#odbctestdata') is not null drop table #odbctestdata"); command = "create table #odbctestdata (" "col1 varchar(30) not null," "col2 int not null," "col3 float not null," "col4 numeric(18,6) not null," "col5 datetime not null," "col6 text not null)"; odbc_command(command); command = "insert #odbctestdata values (" "'ABCDEFGHIJKLMNOP'," "123456," "1234.56," "123456.78," "'Sep 11 2001 10:00AM'," "'just to check returned length...')"; odbc_command(command); odbc_command("select * from #odbctestdata"); CHKFetch("SI"); for (i = 1; i <= 6; i++) { CHKGetData(i, SQL_C_CHAR, output, sizeof(output), &cnamesize, "S"); printf("output data >%s< len_or_ind = %d\n", output, (int) cnamesize); if (cnamesize != strlen((char *) output)) return 1; } CHKFetch("No"); CHKCloseCursor("SI"); odbc_command("drop table #odbctestdata"); odbc_disconnect(); printf("Done.\n"); return 0; }
int main(int argc, char *argv[]) { SQLLEN cb = SQL_NTS; odbc_use_version3 = 1; odbc_connect(); odbc_command_with_result(odbc_stmt, "drop proc sp_paramcore_test"); odbc_command("create proc sp_paramcore_test @s varchar(100) output as select @s = '12345'"); /* here we pass a NULL buffer for input SQL_NTS */ CHKBindParameter(1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, OUTSTRING_LEN, 0, NULL, OUTSTRING_LEN, &cb, "S"); cb = SQL_NTS; CHKExecDirect(T(SP_TEXT), SQL_NTS, "E"); odbc_reset_statement(); /* here we pass a NULL buffer for input */ CHKBindParameter(1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_VARCHAR, 18, 0, NULL, OUTSTRING_LEN, &cb, "S"); cb = 1; CHKExecDirect(T(SP_TEXT), SQL_NTS, "E"); odbc_reset_statement(); odbc_command("drop proc sp_paramcore_test"); odbc_command("create proc sp_paramcore_test @s numeric(10,2) output as select @s = 12345.6"); odbc_reset_statement(); #if 0 /* this fails even on native platforms */ /* here we pass a NULL buffer for output */ cb = sizeof(SQL_NUMERIC_STRUCT); SQLBindParameter(odbc_stmt, 1, SQL_PARAM_OUTPUT, SQL_C_NUMERIC, SQL_NUMERIC, 18, 0, NULL, OUTSTRING_LEN, &cb); odbc_read_error(); cb = 1; odbc_command_with_result(odbc_stmt, SP_TEXT); odbc_read_error(); odbc_reset_statement(); #endif odbc_command("drop proc sp_paramcore_test"); odbc_disconnect(); printf("Done successfully!\n"); return 0; }
static void Test(int use_sql) { odbc_command_with_result(odbc_stmt, "DROP TABLE #a"); odbc_command("CREATE TABLE #a(x int)"); odbc_command("INSERT INTO #a VALUES(123)"); Test0(use_sql, "CREATE TABLE #test(i int, c varchar(6))", "INSERT INTO #test(c, i) VALUES('%s', %d)", "SELECT x AS i, c FROM #test, #a"); Test0(use_sql, "CREATE TABLE #test(i int, c varchar(6))", "INSERT INTO #test(c, i) VALUES('%s', %d)", "SELECT i, c FROM #test"); if (odbc_db_is_microsoft()) { Test0(use_sql, "CREATE TABLE #test(i int identity(1,1), c varchar(6))", "INSERT INTO #test(c) VALUES('%s')", "SELECT i, c FROM #test"); Test0(use_sql, "CREATE TABLE #test(i int primary key, c varchar(6))", "INSERT INTO #test(c, i) VALUES('%s', %d)", "SELECT i, c FROM #test"); } Test0(use_sql, "CREATE TABLE #test(i int, c varchar(6))", "INSERT INTO #test(c, i) VALUES('%s', %d)", "SELECT i, c, c + 'xxx' FROM #test"); }
int main(int argc, char *argv[]) { char buf[102]; SQLLEN ind; int failed = 0; odbc_use_version3 = 1; odbc_connect(); CHKBindCol(1, SQL_C_DEFAULT, buf, 100, &ind, "S"); odbc_command("SELECT CONVERT(NCHAR(10), 'Pippo 123')"); /* get data */ memset(buf, 0, sizeof(buf)); CHKFetch("S"); SQLMoreResults(odbc_stmt); SQLMoreResults(odbc_stmt); odbc_disconnect(); if (strcmp(buf, "Pippo 123 ") != 0) { fprintf(stderr, "Wrong results '%s'\n", buf); failed = 1; } return failed ? 1 : 0; }
int main(int argc, char **argv) { odbc_use_version3 = 1; odbc_connect(); odbc_command("create table #test_output (id int, msg text)"); Test("?", SQL_INTEGER, "?", SQL_LONGVARCHAR); Test("123", SQL_INTEGER, "?", SQL_LONGVARCHAR); Test("?", SQL_INTEGER, "'foo'", SQL_LONGVARCHAR); Test("?", SQL_INTEGER, "?", SQL_VARCHAR); /* * Sybase cannot pass this test without complicated query parsing. * Query with blob columns cannot be prepared so prepared query must * be emulated loosing column informations from server and Sybase do * not convert implicitly VARCHAR to INT */ if (odbc_db_is_microsoft()) Test("?", SQL_VARCHAR, "?", SQL_LONGVARCHAR); else ++test_num; odbc_disconnect(); return 0; }
int main(void) { odbc_connect(); odbc_command("CREATE TABLE #warning(name varchar(20), value int null)"); odbc_command("INSERT INTO #warning VALUES('a', NULL)"); Test(one_null_with_warning); Test("SELECT SUM(value) FROM #warning"); odbc_disconnect(); printf("Done.\n"); return 0; }
static void DoTest(int n) { SQLCHAR output[256]; SQLSMALLINT colType; SQLULEN colSize; SQLSMALLINT colScale, colNullable; SQLLEN dataSize; TIMESTAMP_STRUCT ts; odbc_command("select convert(datetime, '2002-12-27 18:43:21')"); CHKFetch("SI"); CHKDescribeCol(1, (SQLTCHAR*)output, sizeof(output)/sizeof(SQLWCHAR), NULL, &colType, &colSize, &colScale, &colNullable, "S"); if (n == 0) { memset(&ts, 0, sizeof(ts)); CHKGetData(1, SQL_C_TIMESTAMP, &ts, sizeof(ts), &dataSize, "S"); sprintf((char *) output, "%04d-%02d-%02d %02d:%02d:%02d.000", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second); } else { CHKGetData(1, SQL_C_CHAR, output, sizeof(output), &dataSize, "S"); } printf("Date returned: %s\n", output); if (strcmp((char *) output, "2002-12-27 18:43:21.000") != 0) { fprintf(stderr, "Invalid returned date\n"); exit(1); } CHKFetch("No"); CHKCloseCursor("SI"); }
static int test_insert(void *buf, SQLLEN buflen) { SQLHSTMT odbc_stmt = SQL_NULL_HSTMT; SQLLEN strlen_or_ind; const char *qstr = "insert into " TEST_TABLE_NAME " values (?)"; assert(odbc_conn); assert(odbc_env); /* allocate new statement handle */ CHKAllocHandle(SQL_HANDLE_STMT, odbc_conn, &odbc_stmt, "SI"); /* execute query */ CHKPrepare(T(qstr), SQL_NTS, "SI"); strlen_or_ind = buflen; CHKBindParameter(1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARBINARY, (SQLUINTEGER) (-1), 0, buf, buflen, &strlen_or_ind, "SI"); CHKExecute("SI"); /* this command shouldn't fail */ odbc_command("DECLARE @i INT"); SQLFreeHandle(SQL_HANDLE_STMT, odbc_stmt); return 0; }
int main(void) { int i; char cmd[128 + 110*10]; printf("SQLWCHAR size is: %d\n", (int) sizeof(SQLWCHAR)); odbc_use_version3 = 1; odbc_connect(); /* this test do not work with Sybase */ if (!odbc_db_is_microsoft()) { odbc_disconnect(); return 0; } strcpy(cmd, "create procedure #proc_longerror as\nbegin\nraiserror('"); for (i = 0; i < 110; ++i) strcat(cmd, "reallylong"); strcat(cmd, " error', 16, 1)\nend\n"); odbc_command(cmd); CHKR2(SQLExecDirectW, (odbc_stmt, odbc_get_sqlwchar(&odbc_buf, "{CALL #proc_longerror}"), SQL_NTS), SQL_HANDLE_STMT, odbc_stmt, "E"); extract_error(odbc_stmt, SQL_HANDLE_STMT); odbc_disconnect(); return 0; }
int main(void) { odbc_use_version3 = 1; odbc_mark_sockets_opened(); odbc_connect(); CHKSetConnectAttr(SQL_ATTR_AUTOCOMMIT, (void *) SQL_AUTOCOMMIT_OFF, 0, "S"); odbc_command("SELECT 1"); CHKMoreResults("No"); if (!close_last_socket()) { fprintf(stderr, "Error closing connection\n"); return 1; } CHKEndTran(SQL_HANDLE_DBC, odbc_conn, SQL_ROLLBACK, "E"); /* the error should be written in the connection, not in the statement */ ReadErrorConn(); if (strcmp(odbc_sqlstate, "08S01") != 0 || strstr(odbc_err, "Write to the server") == NULL) { odbc_disconnect(); fprintf(stderr, "Unexpected error message %s %s\n", odbc_sqlstate, odbc_err); return 1; } odbc_disconnect(); return 0; }
static int CheckDirtyRead(void) { SQLRETURN RetCode; /* transaction 1 try to change a row but not commit */ odbc_command("UPDATE test_transaction SET t = 'second' WHERE n = 1"); SWAP_CONN(); /* second transaction try to fetch uncommited row */ RetCode = odbc_command2("SELECT * FROM test_transaction WHERE t = 'second' AND n = 1", "SE"); if (RetCode == SQL_ERROR) { EndTransaction(SQL_ROLLBACK); SWAP_CONN(); EndTransaction(SQL_ROLLBACK); return 0; /* no dirty read */ } CHKFetch("S"); CHKFetch("No"); SQLMoreResults(odbc_stmt); EndTransaction(SQL_ROLLBACK); SWAP_CONN(); EndTransaction(SQL_ROLLBACK); return 1; }
static void exec_direct(const char *stmt) { SQLHSTMT odbc_stmt = SQL_NULL_HSTMT; CHKAllocHandle(SQL_HANDLE_STMT, (SQLHANDLE) odbc_conn, (SQLHANDLE *) & odbc_stmt, "S"); odbc_command(stmt); CHKFreeHandle(SQL_HANDLE_STMT, (SQLHANDLE) odbc_stmt, "S"); }
static void DoTest(int prepared) { odbc_command("create table #odbctestdata (i int)"); /* test that 2 empty result set are returned correctly */ if (!prepared) { odbc_command("select * from #odbctestdata select * from #odbctestdata"); } else { CHKPrepare(T("select * from #odbctestdata select * from #odbctestdata"), SQL_NTS, "S"); CHKExecute("S"); } CHKFetch("No"); CHKMoreResults("S"); printf("Getting next recordset\n"); CHKFetch("No"); CHKMoreResults("No"); /* test that skipping a no empty result go to other result set */ odbc_command("insert into #odbctestdata values(123)"); if (!prepared) { odbc_command("select * from #odbctestdata select * from #odbctestdata"); } else { CHKPrepare(T("select * from #odbctestdata select * from #odbctestdata"), SQL_NTS, "S"); CHKExecute("S"); } CHKMoreResults("S"); printf("Getting next recordset\n"); CHKFetch("S"); CHKFetch("No"); CHKMoreResults("No"); odbc_command("drop table #odbctestdata"); ODBC_FREE(); }
int main(int argc, char *argv[]) { int i; odbc_use_version3 = 1; odbc_connect(); odbc_command("CREATE TABLE #odbc_test(i INT, t TEXT)"); for (i = 0; i < 10; ++i) { char buf[128]; sprintf(buf, "INSERT INTO #odbc_test(i, t) VALUES(%d, '%crow number %d')", i + 1, 'a' + i, i * 13); odbc_command(buf); } odbc_reset_statement(); test_query = "SELECT * FROM #odbc_test ORDER BY i"; printf("test line %d\n", __LINE__); query_test("S", "VVVVVVVVVV"); test_query = "SELECT * FROM #odbc_test WHERE i < 7 ORDER BY i"; printf("test line %d\n", __LINE__); query_test("S", "VVVVVV"); /* binding row */ test_query = "SELECT * FROM #odbc_test ORDER BY i"; record_bind = 1; printf("test line %d\n", __LINE__); query_test("S", "VVVVVVVVVV"); /* row and truncation */ trunc = 1; printf("test line %d\n", __LINE__); query_test("I", "!!!!!!!!!!"); /* TODO bind offset, SQLGetData, no bind, error */ odbc_disconnect(); printf("Success!.\n"); return 0; }
int main(int argc, char *argv[]) { SQLSMALLINT len, type; SQLTCHAR name[128]; odbc_connect(); odbc_command("create table #dc (col_name int, name2 varchar(100))"); odbc_command("select * from #dc"); len = 0x1234; CHKDescribeCol(1, NULL, 0, &len, &type, NULL, NULL, NULL, "S"); check(len == 8); len = 0x1234; CHKDescribeCol(2, name, 0, &len, &type, NULL, NULL, NULL, "I"); check(len == 5); len = 0x1234; CHKDescribeCol(1, NULL, 2, &len, &type, NULL, NULL, NULL, "S"); check(len == 8); len = 0x1234; strcpy((char *) name, "xxx"); CHKDescribeCol(2, name, 3, &len, &type, NULL, NULL, NULL, "I"); check(len == 5 && strcmp(C(name), "na") == 0); len = 0x1234; strcpy((char *) name, "xxx"); CHKDescribeCol(1, name, 1, &len, &type, NULL, NULL, NULL, "I"); check(len == 8 && strcmp(C(name), "") == 0); len = 0x1234; strcpy((char *) name, "xxx"); CHKDescribeCol(2, name, 6, &len, &type, NULL, NULL, NULL, "S"); check(len == 5 && strcmp(C(name), "name2") == 0); odbc_disconnect(); if (g_result == 0) printf("Done.\n"); return g_result; }
static void test_params(void) { #define ARRAY_SIZE 2 const rows_set_t *p; SQLULEN len; SQLUINTEGER *ids = MALLOC_N(SQLUINTEGER,ARRAY_SIZE); SQLLEN *id_lens = MALLOC_N(SQLLEN,ARRAY_SIZE); unsigned long int h, l; unsigned int n; for (n = 0; n < ARRAY_SIZE; ++n) { ids[n] = n; id_lens[n] = 0; } /* test setting just some test pointers */ set_ipd_params1(int2ptr(0x01020304)); check_ipd_params(); set_ipd_params2(int2ptr(0xabcdef12)); check_ipd_params(); set_ipd_params3(int2ptr(0x87654321)); check_ipd_params(); /* now see results */ for (p = param_set; *p != NULL; ++p) { odbc_reset_statement(); len = 0xdeadbeef; len <<= 16; len <<= 16; len |= 12345678; (*p)(&len); check_ipd_params(); CHKSetStmtAttr(SQL_ATTR_PARAMSET_SIZE, (void *) int2ptr(ARRAY_SIZE), 0, "S"); CHKBindParameter(1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 5, 0, ids, 0, id_lens, "S"); odbc_command("INSERT INTO #tmp1(i) VALUES(?)"); SQLMoreResults(odbc_stmt); for (n = 0; n < ARRAY_SIZE; ++n) SQLMoreResults(odbc_stmt); l = len; len >>= 16; h = len >> 16; l &= 0xfffffffflu; if (h != 0 || l != 2) { fprintf(stderr, "Wrong number returned in param rows high %lu low %lu\n", h, l); exit(1); } } free(ids); free(id_lens); }
int main(int argc, char **argv) { odbc_connect(); odbc_command_with_result(odbc_stmt, "DROP TABLE test"); odbc_command("CREATE TABLE test(i int, c varchar(40))"); insert_test_man(); odbc_command("DELETE FROM test"); insert_test_auto(); odbc_command("DROP TABLE test"); odbc_disconnect(); return 0; }
int main(int argc, char *argv[]) { int i; odbc_connect(); odbc_command("create table #timeout(i int)"); odbc_command("insert into #timeout values(1)"); for (i = 0; i < 2; ++i) { printf("Loop %d\n", i); CHKSetStmtAttr(SQL_ATTR_QUERY_TIMEOUT, (SQLPOINTER) 10, SQL_IS_UINTEGER, "S"); CHKPrepare(T("select * from #timeout"), SQL_NTS, "S"); CHKExecute("S"); do { while (CHKFetch("SNo") == SQL_SUCCESS) ; } while (CHKMoreResults("SNo") == SQL_SUCCESS); if (i == 0) { printf("Sleep 15 seconds to test if timeout occurs\n"); tds_sleep_s(15); } SQLFreeStmt(odbc_stmt, SQL_CLOSE); SQLFreeStmt(odbc_stmt, SQL_UNBIND); SQLFreeStmt(odbc_stmt, SQL_RESET_PARAMS); SQLCloseCursor(odbc_stmt); } odbc_disconnect(); ODBC_FREE(); return 0; }
static void Test2(int nocount, int second_select) { char sql[512]; g_nocount = nocount; g_second_select = second_select; /* this test do not work with Sybase */ if (!odbc_db_is_microsoft()) return; sprintf(sql, create_proc, nocount ? " SET NOCOUNT ON\n" : "", second_select ? " SELECT 'Here is the last row' AS LastResult\n" : ""); odbc_command(sql); Test(5); Test(11); odbc_command("DROP PROC #tmp1"); }