int main(int argc, char *argv[]) { int result; int retcode = 0; Connect(); /* create table */ CommandWithResult(Statement, "DROP TABLE TestTransaction"); result = CommandWithResult(Statement, "CREATE TABLE TestTransaction ( value INT )"); if (result != SQL_SUCCESS) { ODBC_REPORT_ERROR("Can't create table TestTransaction"); retcode = 1; goto cleanup; } if (!retcode) retcode = Test(1); if (!retcode) retcode = Test(0); cleanup: /* drop table */ CommandWithResult(Statement, "DROP TABLE TestTransaction"); Disconnect(); printf("Done.\n"); return retcode; }
int main(int argc, char *argv[]) { SQLINTEGER cnamesize; Connect(); /* issue print statement and test message returned */ if (CommandWithResult(Statement, "print 'Test message'") != SQL_SUCCESS_WITH_INFO) { printf("SQLExecDirect should return SQL_SUCCESS_WITH_INFO\n"); return 1; } ReadError(); if (!strstr((char *) output, "Test message")) { printf("Message invalid\n"); return 1; } /* issue invalid command and test error */ if (CommandWithResult(Statement, "SELECT donotexistsfield FROM donotexiststable") != SQL_ERROR) { printf("SQLExecDirect returned strange results\n"); return 1; } ReadError(); /* test no data returned */ if (SQLFetch(Statement) != SQL_ERROR) { printf("Row fetched ??\n"); return 1; } ReadError(); if (SQLGetData(Statement, 1, SQL_C_CHAR, output, sizeof(output), &cnamesize) != SQL_ERROR) { printf("Data ??\n"); return 1; } ReadError(); Disconnect(); printf("Done.\n"); return 0; }
int main(int argc, char *argv[]) { SQLINTEGER cbInString = SQL_NTS; char buf[256]; SQLRETURN ret; unsigned char sqlstate[6]; Connect(); Command(Statement, "CREATE TABLE #urls ( recdate DATETIME ) "); /* test implicit conversion error */ if (CommandWithResult(Statement, "INSERT INTO #urls ( recdate ) VALUES ( '2003-10-1 10:11:1 0' )") != SQL_ERROR) { fprintf(stderr, "SQLExecDirect success instead of failing!\n"); return 1; } /* test prepared implicit conversion error */ if (!SQL_SUCCEEDED(SQLPrepare(Statement, (SQLCHAR *) "INSERT INTO #urls ( recdate ) VALUES ( ? )", SQL_NTS))) { fprintf(stderr, "SQLPrepare failure!\n"); return 1; } strcpy(buf, "2003-10-1 10:11:1 0"); if (!SQL_SUCCEEDED (SQLBindParameter(Statement, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 128, 0, buf, sizeof(buf), &cbInString))) { fprintf(stderr, "SQLBindParameter failure!\n"); return 1; } if (SQLExecute(Statement) != SQL_ERROR) { fprintf(stderr, "SQLExecute succeeded instead of failing! (line %d)\n", __LINE__); return 1; } ret = SQLGetDiagRec(SQL_HANDLE_STMT, Statement, 1, sqlstate, NULL, buf, sizeof(buf), NULL); if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { fprintf(stderr, "Error not set (line %d)\n", __LINE__); return 1; } printf("err=%s\n", buf); /* try to prepare and execute a statement with error (from DBD::ODBC test) */ SQLPrepare(Statement, (SQLCHAR *) "SELECT XXNOTCOLUMN FROM sysobjects", SQL_NTS); if (SQLExecute(Statement) != SQL_ERROR) { fprintf(stderr, "SQLExecute succeeded instead of failing! (line %d)\n", __LINE__); return 1; } ret = SQLGetDiagRec(SQL_HANDLE_STMT, Statement, 1, sqlstate, NULL, buf, sizeof(buf), NULL); if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { fprintf(stderr, "Error not set (line %d) ret=%d\n", __LINE__, (int)ret); return 1; } printf("err=%s\n", buf); Disconnect(); printf("Done.\n"); return 0; }
int main(int argc, char *argv[]) { HENV env; HDBC dbc; HSTMT stmt; SQLRETURN ret; SQLINTEGER i; Connect(); /* here we can't use temporary table cause we use two connection */ CommandWithResult(Statement, "drop table test_timeout"); Command(Statement, "create table test_timeout(n numeric(18,0) primary key, t varchar(30))"); AutoCommit(SQL_AUTOCOMMIT_OFF); Command(Statement, "insert into test_timeout(n, t) values(1, 'initial')"); EndTransaction(SQL_COMMIT); Command(Statement, "update test_timeout set t = 'second' where n = 1"); /* save this connection and do another */ env = Environment; dbc = Connection; stmt = Statement; Environment = SQL_NULL_HENV; Connection = SQL_NULL_HDBC; Statement = SQL_NULL_HSTMT; Connect(); AutoCommit(SQL_AUTOCOMMIT_OFF); ret = SQLSetStmtAttr(Statement, SQL_ATTR_QUERY_TIMEOUT, (SQLPOINTER) 2, 0); if (ret != SQL_SUCCESS) ODBC_REPORT_ERROR("Error setting timeout"); i = 1; ret = SQLBindParameter(Statement, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &i, 0, NULL); if (ret != SQL_SUCCESS) ODBC_REPORT_ERROR("SQLBindParameter failure"); if (SQLPrepare(Statement, (SQLCHAR *) "update test_timeout set t = 'bad' where n = ?", SQL_NTS) != SQL_SUCCESS) ODBC_REPORT_ERROR("SQLPrepare failure"); ret = SQLExecute(Statement); if (ret != SQL_ERROR) ODBC_REPORT_ERROR("SQLExecute success ??"); EndTransaction(SQL_ROLLBACK); /* TODO should return error S1T00 Timeout expired, test error message */ ret = CommandWithResult(Statement, "update test_timeout set t = 'bad' where n = 1"); if (ret != SQL_ERROR) ODBC_REPORT_ERROR("SQLExecDirect success ??"); EndTransaction(SQL_ROLLBACK); Disconnect(); Environment = env; Connection = dbc; Statement = stmt; EndTransaction(SQL_COMMIT); /* Sybase do not accept DROP TABLE during a transaction */ AutoCommit(SQL_AUTOCOMMIT_ON); Command(Statement, "drop table test_timeout"); Disconnect(); return 0; }
static int Test(int discard_test) { SQLINTEGER out_buf; SQLLEN out_len; int result = 0; SQLLEN rows; int retcode = 0; char buf[512]; unsigned char sqlstate[6]; const char *createErrorProcedure = "CREATE PROCEDURE testerror AS\n" "SELECT value FROM TestTransaction\n" "SELECT value / (value-value) FROM TestTransaction\n"; /* select after insert is required to test data discarding */ char createProcedure[512]; sprintf(createProcedure, "CREATE PROCEDURE testinsert @value INT AS\n" "INSERT INTO TestTransaction VALUES ( @value )\n%s", discard_test ? "SELECT * FROM TestTransaction\n" : ""); /* create stored proc */ CommandWithResult(Statement, "DROP PROCEDURE testinsert"); result = CommandWithResult(Statement, createProcedure); if (result != SQL_SUCCESS) { ODBC_REPORT_ERROR("Can't create proc testinsert"); retcode = 1; goto cleanup; } /* create stored proc that generates an error */ CommandWithResult(Statement, "DROP PROCEDURE testerror"); result = CommandWithResult(Statement, createErrorProcedure); if (result != SQL_SUCCESS) { ODBC_REPORT_ERROR("Can't create proc testerror"); retcode = 1; goto cleanup; } /* Start transaction */ result = SQLSetConnectAttr(Connection, SQL_ATTR_AUTOCOMMIT, (void *) SQL_AUTOCOMMIT_OFF, 0); if (result != SQL_SUCCESS) { ODBC_REPORT_ERROR("Can't start transaction"); retcode = 1; goto cleanup; } /* Insert a value */ Command(Statement, "EXEC testinsert 1"); /* we should be able to read row count */ if (SQLRowCount(Statement, &rows) != SQL_SUCCESS) { ODBC_REPORT_ERROR("Can't get row counts"); retcode = 1; goto cleanup; } /* Commit transaction */ result = SQLEndTran(SQL_HANDLE_DBC, Connection, SQL_COMMIT); if (result != SQL_SUCCESS) { ODBC_REPORT_ERROR("Can't commit transaction"); retcode = 1; goto cleanup; } SQLCloseCursor(Statement); /* Start transaction */ result = SQLSetConnectAttr(Connection, SQL_ATTR_AUTOCOMMIT, (void *) SQL_AUTOCOMMIT_OFF, 0); if (result != SQL_SUCCESS) { ODBC_REPORT_ERROR("Can't start transaction"); retcode = 1; goto cleanup; } /* Insert another value */ Command(Statement, "EXEC testinsert 2"); /* Roll back transaction */ result = SQLEndTran(SQL_HANDLE_DBC, Connection, SQL_ROLLBACK); if (result != SQL_SUCCESS) { ODBC_REPORT_ERROR("Can't roll back transaction"); retcode = 1; goto cleanup; } /* TODO test row inserted */ result = SQLSetConnectAttr(Connection, SQL_ATTR_AUTOCOMMIT, (void *) SQL_AUTOCOMMIT_ON, 0); if (result != SQL_SUCCESS) { ODBC_REPORT_ERROR("Can't stop transaction"); retcode = 1; goto cleanup; } /* generate an error */ result = CommandWithResult(Statement, "EXEC testerror"); if (result != SQL_SUCCESS) { ODBC_REPORT_ERROR("error: SQLExecDirect: testerror"); retcode = 1; goto cleanup; } if (SQLBindCol(Statement, 1, SQL_C_SLONG, &out_buf, sizeof(out_buf), &out_len) != SQL_SUCCESS) { ODBC_REPORT_ERROR("error: SQLBindCol: testerror"); retcode = 1; goto cleanup; } while ((result = SQLFetch(Statement)) == SQL_SUCCESS) { printf("\t%ld\n", (long int) out_buf); if (out_buf != 1) { printf("error: expected to select 1 got %ld\n", (long int) out_buf); retcode = 1; goto cleanup; } } if (result != SQL_NO_DATA) { ODBC_REPORT_ERROR("error: SQLFetch: testerror"); goto cleanup; } result = SQLMoreResults(Statement); printf("SQLMoreResults returned %d\n", result); if (result != SQL_ERROR) { printf("SQLMoreResults should return error\n"); retcode = 1; goto cleanup; } result = SQLGetDiagRec(SQL_HANDLE_STMT, Statement, 1, sqlstate, NULL, (SQLCHAR *)buf, sizeof(buf), NULL); if (result != SQL_SUCCESS && result != SQL_SUCCESS_WITH_INFO) { fprintf(stderr, "Error not set (line %d)\n", __LINE__); retcode = 1; goto cleanup; } printf("err=%s\n", buf); result = SQLMoreResults(Statement); printf("SQLMoreResults returned %d\n", result); if (result != SQL_NO_DATA) { printf("SQLMoreResults should return error"); retcode = 1; goto cleanup; } cleanup: /* drop table */ CommandWithResult(Statement, "DROP PROCEDURE testinsert"); CommandWithResult(Statement, "DROP PROCEDURE testerror"); return retcode; }
static void DoTest(int n) { int res; SQLCHAR output[256]; SQLSMALLINT colType; SQLULEN colSize; SQLSMALLINT colScale, colNullable; SQLLEN dataSize; TIMESTAMP_STRUCT ts; if (CommandWithResult(Statement, "select convert(datetime, '2002-12-27 18:43:21')") != SQL_SUCCESS) ODBC_REPORT_ERROR("Unable to execute statement"); res = SQLFetch(Statement); if (res != SQL_SUCCESS && res != SQL_SUCCESS_WITH_INFO) ODBC_REPORT_ERROR("Unable to fetch row"); if (SQLDescribeCol(Statement, 1, output, sizeof(output), NULL, &colType, &colSize, &colScale, &colNullable) != SQL_SUCCESS) ODBC_REPORT_ERROR("Error getting data"); if (n == 0) { memset(&ts, 0, sizeof(ts)); if (SQLGetData(Statement, 1, SQL_C_TIMESTAMP, &ts, sizeof(ts), &dataSize) != SQL_SUCCESS) { printf("Unable to get data col %d\n", 1); CheckReturn(); exit(1); } sprintf((char *) output, "%04d-%02d-%02d %02d:%02d:%02d.000", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second); } else { if (SQLGetData(Statement, 1, SQL_C_CHAR, output, sizeof(output), &dataSize) != SQL_SUCCESS) { printf("Unable to get data col %d\n", 1); CheckReturn(); exit(1); } } printf("Date returned: %s\n", output); if (strcmp((char *) output, "2002-12-27 18:43:21.000") != 0) { printf("Invalid returned date\n"); exit(1); } res = SQLFetch(Statement); if (res != SQL_NO_DATA) ODBC_REPORT_ERROR("Unable to fetch row"); res = SQLCloseCursor(Statement); if (!SQL_SUCCEEDED(res)) ODBC_REPORT_ERROR("Unable to close cursor"); }