testOdbcDriver.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:158k
源码类别:
MySQL数据库
开发平台:
Visual C++
- /* Copyright (C) 2003 MySQL AB
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
- /* Copyright (C) 2003 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
- /*
- * testOdbcDriver
- *
- * Test of ODBC and SQL using a fixed set of tables.
- */
- #include <ndb_global.h>
- #undef test
- #include <ndb_version.h>
- #include <kernel/ndb_limits.h>
- #include <Bitmask.hpp>
- #include <kernel/AttributeList.hpp>
- #ifdef ndbODBC
- #include <NdbApi.hpp>
- #endif
- #include <sqlext.h>
- #undef BOOL
- #include <NdbMain.h>
- #include <NdbOut.hpp>
- #include <NdbThread.h>
- #include <NdbMutex.h>
- #include <NdbCondition.h>
- #include <NdbTick.h>
- #include <NdbSleep.h>
- #ifdef ndbODBC
- #include <NdbTest.hpp>
- #else
- #define NDBT_OK 0
- #define NDBT_FAILED 1
- #define NDBT_WRONGARGS 2
- static int
- NDBT_ProgramExit(int rcode)
- {
- const char* rtext = "Unknown";
- switch (rcode) {
- case NDBT_OK:
- rtext = "OK";
- break;
- case NDBT_FAILED:
- rtext = "Failed";
- break;
- case NDBT_WRONGARGS:
- rtext = "Wrong arguments";
- break;
- };
- ndbout_c("nNDBT_ProgramExit: %d - %sn", rcode, rtext);
- return rcode;
- }
- #endif
- #ifdef DMALLOC
- #include <dmalloc.h>
- #endif
- #define arraySize(x) (sizeof(x)/sizeof(x[0]))
- #define SQL_ATTR_NDB_TUPLES_FETCHED 66601
- // options
- #define MAX_THR 128 // max threads
- struct Opt {
- const char* m_name[100];
- unsigned m_namecnt;
- bool m_core;
- unsigned m_depth;
- const char* m_dsn;
- unsigned m_errs;
- const char* m_fragtype;
- unsigned m_frob;
- const char* m_home;
- unsigned m_loop;
- bool m_nogetd;
- bool m_noputd;
- bool m_nosort;
- unsigned m_scale;
- bool m_serial;
- const char* m_skip[100];
- unsigned m_skipcnt;
- unsigned m_subloop;
- const char* m_table;
- unsigned m_threads;
- unsigned m_trace;
- unsigned m_v;
- Opt() :
- m_namecnt(0),
- m_core(false),
- m_depth(5),
- m_dsn("NDB"),
- m_errs(0),
- m_fragtype(0),
- m_frob(0),
- m_home(0),
- m_loop(1),
- m_nogetd(false),
- m_noputd(false),
- m_nosort(false),
- m_scale(100),
- m_serial(false),
- m_skipcnt(0),
- m_subloop(1),
- m_table(0),
- m_threads(1),
- m_trace(0),
- m_v(1) {
- for (unsigned i = 0; i < arraySize(m_name); i++)
- m_name[i] = 0;
- for (unsigned i = 0; i < arraySize(m_skip); i++)
- m_skip[i] = 0;
- }
- };
- static Opt opt;
- static void listCases();
- static void listTables();
- static void printusage()
- {
- Opt d;
- ndbout
- << "usage: testOdbcDriver [options]" << endl
- << "-case name run only named tests (substring match - can be repeated)" << endl
- << "-core dump core on failure" << endl
- << "-depth N join depth - default " << d.m_depth << endl
- << "-dsn string data source name - default " << d.m_dsn << endl
- << "-errs N allow N errors before quitting - default " << d.m_errs << endl
- << "-fragtype t fragment type single/small/medium/large" << d.m_errs << endl
- << "-frob X case-dependent tweak (number)" << endl
- << "-home dir set NDB_HOME (contains Ndb.cfg)" << endl
- << "-loop N loop N times (0 = forever) - default " << d.m_loop << endl
- << "-nogetd do not use SQLGetData - default " << d.m_nogetd << endl
- << "-noputd do not use SQLPutData - default " << d.m_noputd << endl
- << "-nosort no order-by in verify scan (checks non-Pk values only)" << endl
- << "-scale N row count etc - default " << d.m_scale << endl
- << "-serial run multi-threaded test cases one at a time" << endl
- << "-skip name skip named tests (substring match - can be repeated)" << endl
- << "-subloop N loop count per case (same threads) - default " << d.m_subloop << endl
- << "-table T do only table T (table name on built-in list)" << endl
- << "-threads N number of threads (max " << MAX_THR << ") - default " << d.m_threads << endl
- << "-trace N trace in NDB ODBC driver - default " << d.m_trace << endl
- << "-v N verbosity - default " << d.m_v << endl
- ;
- listCases();
- listTables();
- }
- static void
- fatal(const char* fmt, ...)
- {
- va_list ap;
- char buf[200];
- va_start(ap, fmt);
- vsnprintf(buf, sizeof(buf), fmt, ap);
- va_end(ap);
- ndbout << buf << endl;
- if (opt.m_errs != 0) {
- opt.m_errs--;
- return;
- }
- if (opt.m_core)
- abort();
- NDBT_ProgramExit(NDBT_FAILED);
- exit(1);
- }
- static void
- cleanprint(const char* s, unsigned n)
- {
- for (unsigned i = 0; i < n; i++) {
- char b[10];
- if (0x20 < s[i] && s[i] <= 0x7e)
- sprintf(b, "%c", s[i]);
- else
- sprintf(b, "\%02x", (unsigned)s[i]);
- ndbout << b;
- }
- }
- // global mutex
- static NdbMutex my_mutex = NDB_MUTEX_INITIALIZER;
- static void lock_mutex() { NdbMutex_Lock(&my_mutex); }
- static void unlock_mutex() { NdbMutex_Unlock(&my_mutex); }
- // semaphore zeroed before each call to a test routine
- static unsigned my_sema = 0;
- // print mutex
- static NdbMutex out_mutex = NDB_MUTEX_INITIALIZER;
- static NdbOut& lock(NdbOut& out) { NdbMutex_Lock(&out_mutex); return out; }
- static NdbOut& unlock(NdbOut& out) { NdbMutex_Unlock(&out_mutex); return out; }
- static unsigned
- urandom(unsigned n)
- {
- assert(n != 0);
- unsigned i = random();
- return i % n;
- }
- // test cases
- struct Test;
- struct Case {
- enum Mode {
- Single = 1, // single thread
- Serial = 2, // all threads but one at a time
- Thread = 3 // all threads in parallel
- };
- const char* m_name;
- void (*m_func)(Test& test);
- Mode m_mode;
- unsigned m_stuff;
- const char* m_desc;
- Case(const char* name, void (*func)(Test& test), Mode mode, unsigned stuff, const char* desc) :
- m_name(name),
- m_func(func),
- m_mode(mode),
- m_stuff(stuff),
- m_desc(desc) {
- }
- const char* modename() const {
- const char* s = "?";
- if (m_mode == Case::Single)
- return "Single";
- if (m_mode == Case::Serial)
- return "Serial";
- if (m_mode == Case::Thread)
- return "Thread";
- return "?";
- }
- bool matchcase() const {
- if (opt.m_namecnt == 0)
- return ! skipcase();
- for (unsigned i = 0; i < opt.m_namecnt; i++) {
- if (strstr(m_name, opt.m_name[i]) != 0)
- return ! skipcase();
- }
- return false;
- }
- private:
- bool skipcase() const {
- for (unsigned i = 0; i < opt.m_skipcnt; i++) {
- if (strstr(m_name, opt.m_skip[i]) != 0)
- return true;
- }
- return false;
- }
- };
- // calculate values
- struct Calc {
- enum { m_mul = 1000000 };
- unsigned m_no;
- unsigned m_base;
- unsigned m_salt; // modifies non-PK values
- bool m_const; // base non-PK values on PK of row 0
- Calc(unsigned no) :
- m_no(no),
- m_salt(0),
- m_const(false) {
- m_base = m_no * m_mul;
- }
- void calcPk(unsigned rownum, char* v, unsigned n) const {
- char b[10];
- sprintf(b, "%08x", m_base + rownum);
- for (unsigned i = 0; i < n; i++) {
- char c = i < n - 1 ? b[i % 8] : 0;
- v[i] = c;
- }
- }
- void calcPk(unsigned rownum, long* v) const {
- *v = m_base + rownum;
- }
- void hashPk(unsigned* hash, const char* v, unsigned n) const {
- for (unsigned i = 0; i < n; i++) {
- *hash ^= (v[i] << i);
- }
- }
- void hashPk(unsigned* hash, long v) const {
- *hash ^= v;
- }
- void calcNk(unsigned hash, char* v, unsigned n, SQLINTEGER* ind, bool null) const {
- unsigned m = hash % n;
- for (unsigned i = 0; i < n; i++) {
- char c = i < m ? 'a' + (hash + i) % ('z' - 'a' + 1) : i < n - 1 ? ' ' : 0;
- v[i] = c;
- }
- *ind = null && hash % 9 == 0 ? SQL_NULL_DATA : SQL_NTS;
- }
- void calcNk(unsigned hash, long* v, SQLINTEGER* ind, bool null) const {
- *v = long(hash);
- *ind = null && hash % 7 == 0 ? SQL_NULL_DATA : 0;
- }
- void calcNk(unsigned hash, double* v, SQLINTEGER* ind, bool null) const {
- *v = long(hash) / 1000.0;
- *ind = null && hash % 5 == 0 ? SQL_NULL_DATA : 0;
- }
- bool verify(const char* v1, SQLINTEGER ind1, const char* v2, SQLINTEGER ind2, unsigned n) {
- if (ind1 == SQL_NULL_DATA && ind2 == SQL_NULL_DATA)
- return true;
- if (ind1 != SQL_NULL_DATA && ind2 != SQL_NULL_DATA)
- if (memcmp(v1, v2, n) == 0)
- return true;
- if (ind1 == SQL_NULL_DATA)
- v1 = "NULL";
- if (ind2 == SQL_NULL_DATA)
- v2 = "NULL";
- ndbout << "verify failed: got ";
- if (ind1 == SQL_NULL_DATA)
- ndbout << "NULL";
- else
- cleanprint(v1, n);
- ndbout << " != ";
- if (ind2 == SQL_NULL_DATA)
- ndbout << "NULL";
- else
- cleanprint(v2, n);
- ndbout << endl;
- return false;
- }
- bool verify(long v1, SQLINTEGER ind1, long v2, SQLINTEGER ind2) {
- char buf1[40], buf2[40];
- if (ind1 == SQL_NULL_DATA && ind2 == SQL_NULL_DATA)
- return true;
- if (ind1 != SQL_NULL_DATA && ind2 != SQL_NULL_DATA)
- if (v1 == v2)
- return true;
- if (ind1 == SQL_NULL_DATA)
- strcpy(buf1, "NULL");
- else
- sprintf(buf1, "%ld", v1);
- if (ind2 == SQL_NULL_DATA)
- strcpy(buf2, "NULL");
- else
- sprintf(buf2, "%ld", v2);
- ndbout << "verify failed: got " << buf1 << " != " << buf2 << endl;
- return false;
- }
- bool verify(double v1, SQLINTEGER ind1, double v2, SQLINTEGER ind2) {
- char buf1[40], buf2[40];
- if (ind1 == SQL_NULL_DATA && ind2 == SQL_NULL_DATA)
- return true;
- if (ind1 != SQL_NULL_DATA && ind2 != SQL_NULL_DATA)
- if (fabs(v1 - v2) < 1) // XXX
- return true;
- if (ind1 == SQL_NULL_DATA)
- strcpy(buf1, "NULL");
- else
- sprintf(buf1, "%.10f", v1);
- if (ind2 == SQL_NULL_DATA)
- strcpy(buf2, "NULL");
- else
- sprintf(buf2, "%.10f", v2);
- ndbout << "verify failed: got " << buf1 << " != " << buf2 << endl;
- return false;
- }
- };
- #if defined(NDB_SOLARIS) || defined(NDB_LINUX) || defined(NDB_MACOSX)
- #define HAVE_SBRK
- #else
- #undef HAVE_SBRK
- #endif
- struct Timer {
- Timer() :
- m_cnt(0),
- m_calls(0),
- m_on(0),
- m_msec(0)
- #ifndef NDB_WIN32
- ,
- m_brk(0),
- m_incr(0)
- #endif
- {
- }
- void timerOn() {
- m_cnt = 0;
- m_calls = 0;
- m_on = NdbTick_CurrentMillisecond();
- #ifdef HAVE_SBRK
- m_brk = (int)sbrk(0);
- #endif
- }
- void timerOff() {
- m_msec = NdbTick_CurrentMillisecond() - m_on;
- if (m_msec <= 0)
- m_msec = 1;
- #ifdef HAVE_SBRK
- m_incr = (int)sbrk(0) - m_brk;
- if (m_incr < 0)
- m_incr = 0;
- #endif
- }
- void timerCnt(unsigned cnt) {
- m_cnt += cnt;
- }
- void timerCnt(const Timer& timer) {
- m_cnt += timer.m_cnt;
- m_calls += timer.m_calls;
- }
- friend NdbOut& operator<<(NdbOut& out, const Timer& timer) {
- out << timer.m_cnt << " ( " << 1000 * timer.m_cnt / timer.m_msec << "/sec )";
- #ifdef HAVE_SBRK
- out << " - " << timer.m_incr << " sbrk";
- if (opt.m_namecnt != 0) { // per case meaningless if many cases
- if (timer.m_cnt > 0)
- out << " ( " << timer.m_incr / timer.m_cnt << "/cnt )";
- }
- #endif
- out << " - " << timer.m_calls << " calls";
- return out;
- }
- protected:
- unsigned m_cnt; // count rows or whatever
- unsigned m_calls; // count ODBC function calls
- NDB_TICKS m_on;
- unsigned m_msec;
- #ifdef HAVE_SBRK
- int m_brk;
- int m_incr;
- #endif
- };
- #define MAX_MESSAGE 500
- #define MAX_DIAG 20
- struct Diag {
- char m_state[5+1];
- SQLINTEGER m_native;
- char m_message[MAX_MESSAGE];
- unsigned m_flag; // temp use
- Diag() {
- strcpy(m_state, "00000");
- m_native = 0;
- memset(m_message, 0, sizeof(m_message));
- m_flag = 0;
- }
- const char* text() {
- snprintf(m_buf, sizeof(m_buf), "%s %d '%s'", m_state, (int)m_native, m_message);
- return m_buf;
- }
- void getDiag(SQLSMALLINT type, SQLHANDLE handle, unsigned k, unsigned count) {
- int ret;
- SQLSMALLINT length = -1;
- memset(m_message, 0, MAX_MESSAGE);
- ret = SQLGetDiagRec(type, handle, k, (SQLCHAR*)m_state, &m_native, (SQLCHAR*)m_message, MAX_MESSAGE, &length);
- if (k <= count && ret != SQL_SUCCESS)
- fatal("SQLGetDiagRec %d of %d: return %d != SQL_SUCCESS", k, count, (int)ret);
- if (k <= count && strlen(m_message) != length)
- fatal("SQLGetDiagRec %d of %d: message length %d != %d", k, count, strlen(m_message), length);
- if (k > count && ret != SQL_NO_DATA)
- fatal("SQLGetDiagRec %d of %d: return %d != SQL_NO_DATA", k, count, (int)ret);
- m_flag = 0;
- }
- private:
- char m_buf[MAX_MESSAGE];
- };
- struct Diags {
- Diag m_diag[MAX_DIAG];
- SQLINTEGER m_diagCount;
- SQLINTEGER m_rowCount;
- SQLINTEGER m_functionCode;
- void getDiags(SQLSMALLINT type, SQLHANDLE handle) {
- int ret;
- m_diagCount = -1;
- ret = SQLGetDiagField(type, handle, 0, SQL_DIAG_NUMBER, &m_diagCount, SQL_IS_INTEGER, 0);
- if (ret == SQL_INVALID_HANDLE)
- return;
- if (ret != SQL_SUCCESS)
- fatal("SQLGetDiagField: return %d != SQL_SUCCESS", (int)ret);
- if (m_diagCount < 0 || m_diagCount > MAX_DIAG)
- fatal("SQLGetDiagField: count %d", (int)m_diagCount);
- for (unsigned k = 0; k < MAX_DIAG; k++) {
- m_diag[k].getDiag(type, handle, k + 1, m_diagCount);
- if (k == m_diagCount + 1)
- break;
- }
- m_rowCount = -1;
- m_functionCode = SQL_DIAG_UNKNOWN_STATEMENT;
- if (type == SQL_HANDLE_STMT) {
- ret = SQLGetDiagField(type, handle, 0, SQL_DIAG_ROW_COUNT, &m_rowCount, SQL_IS_INTEGER, 0);
- #ifndef iODBC
- if (ret != SQL_SUCCESS)
- fatal("SQLGetDiagField: return %d != SQL_SUCCESS", (int)ret);
- #endif
- ret = SQLGetDiagField(type, handle, 0, SQL_DIAG_DYNAMIC_FUNCTION_CODE, &m_functionCode, SQL_IS_INTEGER, 0);
- }
- }
- void showDiags() {
- for (unsigned k = 0; 0 <= m_diagCount && k < m_diagCount; k++) {
- Diag& diag = m_diag[k];
- ndbout << "diag " << k + 1;
- ndbout << (diag.m_flag ? " [*]" : " [ ]");
- ndbout << " " << diag.text() << endl;
- if (k > 10)
- abort();
- }
- }
- };
- struct Exp {
- int m_ret;
- const char* m_state;
- SQLINTEGER m_native;
- Exp() : m_ret(SQL_SUCCESS), m_state(""), m_native(0) {}
- Exp(int ret, const char* state) : m_ret(ret), m_state(state) {}
- };
- struct Test : Calc, Timer, Diags {
- Test(unsigned no, unsigned loop) :
- Calc(no),
- m_loop(loop),
- m_stuff(0),
- m_perf(false),
- ccp(0) {
- exp(SQL_SUCCESS, 0, 0, true);
- }
- unsigned m_loop; // current loop
- Exp m_expList[20]; // expected results
- unsigned m_expCount;
- int m_ret; // actual return code
- int m_stuff; // the stuff of abuse
- bool m_perf; // check no diags on success
- const Case* ccp; // current case
- void exp(int ret, const char* state, SQLINTEGER native, bool reset) {
- if (reset)
- m_expCount = 0;
- unsigned i = m_expCount++;
- assert(i < arraySize(m_expList) - 1);
- m_expList[i].m_ret = ret;
- m_expList[i].m_state = state == 0 ? "" : state;
- m_expList[i].m_native = native;
- }
- void runCase(const Case& cc) {
- ccp = &cc;
- if (opt.m_v >= 3)
- ndbout << cc.m_name << ": start" << endl;
- m_rowCount = -1;
- NDB_TICKS m_ms1 = NdbTick_CurrentMillisecond();
- m_salt = m_loop | (16 << cc.m_stuff);
- m_const = cc.m_stuff == 0;
- m_stuff = cc.m_stuff;
- (*cc.m_func)(*this);
- NDB_TICKS m_ms2 = NdbTick_CurrentMillisecond();
- }
- void run(SQLSMALLINT type, SQLHANDLE handle, int line, int ret) {
- m_calls++;
- m_ret = ret;
- if (m_perf && (m_ret == SQL_SUCCESS))
- return;
- m_diagCount = 0;
- if (handle != SQL_NULL_HANDLE)
- getDiags(type, handle);
- if (m_diagCount <= 0 && (ret != SQL_SUCCESS && ret != SQL_INVALID_HANDLE && ret != SQL_NEED_DATA && ret != SQL_NO_DATA)) {
- fatal("%s: thr %d line %d: ret=%d but no diag records", ccp->m_name, m_no, line, ret);
- }
- for (unsigned k = 0; 0 <= m_diagCount && k < m_diagCount; k++) {
- Diag& diag = m_diag[k];
- bool match = false;
- for (unsigned i = 0; i < m_expCount; i++) {
- if (strcmp(diag.m_state, m_expList[i].m_state) == 0 && (diag.m_native % 10000 == m_expList[i].m_native % 10000 || m_expList[i].m_native == -1)) {
- match = true;
- diag.m_flag = 0;
- continue;
- }
- diag.m_flag = 1; // mark unexpected
- }
- if (! match) {
- showDiags();
- fatal("%s: thr %d line %d: unexpected diag [*] ret=%d cnt=%d", ccp->m_name, m_no, line, (int)ret, (int)m_diagCount);
- }
- }
- bool match = false;
- for (unsigned i = 0; i < m_expCount; i++) {
- if (ret == m_expList[i].m_ret) {
- match = true;
- break;
- }
- }
- if (! match) {
- showDiags();
- fatal("%s: thr %d line %d: ret=%d not expected", ccp->m_name, m_no, line, ret);
- }
- // reset expected to success
- exp(SQL_SUCCESS, 0, 0, true);
- }
- void chk(SQLSMALLINT type, SQLHANDLE handle, int line, bool match, const char* fmt, ...) {
- if (match)
- return;
- va_list ap;
- va_start(ap, fmt);
- char buf[500];
- vsnprintf(buf, sizeof(buf), fmt, ap);
- va_end(ap);
- fatal("%s: thr %d line %d: check failed - %s", ccp->m_name, m_no, line, buf);
- }
- };
- #define HNull 0, SQL_NULL_HANDLE, __LINE__
- #define HEnv(h) SQL_HANDLE_ENV, h, __LINE__
- #define HDbc(h) SQL_HANDLE_DBC, h, __LINE__
- #define HStmt(h) SQL_HANDLE_STMT, h, __LINE__
- #define HDesc(h) SQL_HANDLE_DESC, h, __LINE__
- // string support
- #define MAX_SQL 20000
- static void
- scopy(char*& ptr, const char* fmt, ...)
- {
- va_list ap;
- va_start(ap, fmt);
- vsprintf(ptr, fmt, ap);
- va_end(ap);
- ptr += strlen(ptr);
- }
- static bool
- blankeq(const char* s1, const char* s2, bool caseSensitive = false)
- {
- unsigned n1 = strlen(s1);
- unsigned n2 = strlen(s2);
- unsigned i = 0;
- char c1 = 0;
- char c2 = 0;
- while (i < n1 || i < n2) {
- c1 = i < n1 ? s1[i] : 0x20;
- if (! caseSensitive && 'a' <= c1 && c1 <= 'z')
- c1 -= 'a' - 'A';
- c2 = i < n2 ? s2[i] : 0x20;
- if (! caseSensitive && 'a' <= c2 && c2 <= 'z')
- c2 -= 'a' - 'A';
- if (c1 != c2)
- break;
- i++;
- }
- return c1 == c2;
- }
- // columns and tables
- struct Col {
- enum Type {
- Char = SQL_CHAR,
- Varchar = SQL_VARCHAR,
- Int = SQL_INTEGER,
- Bigint = SQL_BIGINT,
- Real = SQL_REAL,
- Double = SQL_DOUBLE
- };
- enum CType {
- CChar = SQL_C_CHAR,
- CLong = SQL_C_SLONG,
- CDouble = SQL_C_DOUBLE
- };
- enum Cons {
- Null, // nullable
- NotNull, // not nullable
- Pk // part of primary key
- };
- const char* m_name;
- Type m_type;
- unsigned m_length;
- Cons m_cons;
- CType m_ctype;
- Col() :
- m_type((Type)999) {
- }
- Col(const char* name, Type type, unsigned length, Cons cons, CType ctype) :
- m_name(name),
- m_type(type),
- m_length(length),
- m_cons(cons),
- m_ctype(ctype) {
- }
- unsigned size() const {
- switch (m_type) {
- case Char:
- case Varchar:
- return m_length;
- case Int:
- return 4;
- case Bigint:
- return 8;
- case Real:
- return 4;
- case Double:
- return 8;
- }
- assert(false);
- return 0;
- }
- unsigned csize() const { // size as char plus terminating null
- switch (m_ctype) {
- case CChar:
- return m_length + 1;
- case CLong:
- return 12;
- case CDouble:
- return 24;
- }
- assert(false);
- return 0;
- }
- void typespec(char*& ptr) const {
- switch (m_type) {
- case Char:
- scopy(ptr, "char(%d)", m_length);
- return;
- case Varchar:
- scopy(ptr, "varchar(%d)", m_length);
- return;
- case Int:
- scopy(ptr, "int");
- return;
- case Bigint:
- scopy(ptr, "bigint");
- return;
- case Real:
- scopy(ptr, "real");
- return;
- case Double:
- scopy(ptr, "float");
- return;
- }
- assert(false);
- }
- SQLSMALLINT type() const {
- return (SQLSMALLINT)m_type;
- }
- SQLSMALLINT ctype() const {
- return (SQLSMALLINT)m_ctype;
- }
- void create(char*& ptr, bool pk) const {
- scopy(ptr, "%s", m_name);
- scopy(ptr, " ");
- typespec(ptr);
- if (m_cons == Pk && pk) {
- scopy(ptr, " primary key");
- }
- if (m_cons == NotNull) {
- scopy(ptr, " not null");
- }
- }
- };
- static Col ColUndef;
- struct Tab {
- const char* m_name;
- const Col* m_colList;
- unsigned m_colCount;
- unsigned m_pkCount;
- unsigned* m_pkIndex;
- unsigned m_nkCount;
- unsigned* m_nkIndex;
- char m_upperName[20];
- Tab(const char* name, const Col* colList, unsigned colCount) :
- m_name(name),
- m_colList(colList),
- m_colCount(colCount) {
- m_pkCount = 0;
- m_nkCount = 0;
- for (unsigned i = 0; i < m_colCount; i++) {
- const Col& col = m_colList[i];
- if (col.m_cons == Col::Pk)
- m_pkCount++;
- else
- m_nkCount++;
- }
- m_pkIndex = new unsigned[m_pkCount];
- m_nkIndex = new unsigned[m_nkCount];
- unsigned pk = 0;
- unsigned nk = 0;
- for (unsigned i = 0; i < m_colCount; i++) {
- const Col& col = m_colList[i];
- if (col.m_cons == Col::Pk)
- m_pkIndex[pk++] = i;
- else
- m_nkIndex[nk++] = i;
- }
- assert(pk == m_pkCount && nk == m_nkCount);
- strcpy(m_upperName, m_name);
- for (char* p = m_upperName; *p != 0; p++) {
- if ('a' <= *p && *p <= 'z')
- *p -= 'a' - 'A';
- }
- }
- ~Tab() {
- delete[] m_pkIndex;
- delete[] m_nkIndex;
- }
- void drop(char*& ptr) const {
- scopy(ptr, "drop table %s", m_name);
- }
- void create(char*& ptr) const {
- scopy(ptr, "create table %s (", m_name);
- for (unsigned i = 0; i < m_colCount; i++) {
- if (i > 0)
- scopy(ptr, ", ");
- const Col& col = m_colList[i];
- col.create(ptr, m_pkCount == 1);
- }
- if (m_pkCount != 1) {
- scopy(ptr, ", primary key (");
- for (unsigned i = 0; i < m_pkCount; i++) {
- const Col& col = m_colList[m_pkIndex[i]];
- if (i > 0)
- scopy(ptr, ", ");
- scopy(ptr, "%s", col.m_name);
- }
- scopy(ptr, ")");
- }
- scopy(ptr, ")");
- }
- void wherePk(char*& ptr) const {
- scopy(ptr, " where");
- for (unsigned i = 0; i < m_pkCount; i++) {
- const Col& col = m_colList[m_pkIndex[i]];
- if (i > 0)
- scopy(ptr, " and");
- scopy(ptr, " %s = ?", col.m_name);
- }
- }
- void whereRange(char*& ptr) const {
- scopy(ptr, " where");
- for (unsigned i = 0; i < m_pkCount; i++) {
- const Col& col = m_colList[m_pkIndex[i]];
- if (i > 0)
- scopy(ptr, " and");
- scopy(ptr, " ? <= %s", col.m_name);
- scopy(ptr, " and ");
- scopy(ptr, "%s < ?", col.m_name);
- }
- }
- void orderPk(char*& ptr) const {
- scopy(ptr, " order by");
- for (unsigned i = 0; i < m_pkCount; i++) {
- const Col& col = m_colList[m_pkIndex[i]];
- if (i > 0)
- scopy(ptr, ", ");
- else
- scopy(ptr, " ");
- scopy(ptr, "%s", col.m_name);
- }
- }
- void selectPk(char*& ptr) const {
- scopy(ptr, "select * from %s", m_name);
- wherePk(ptr);
- }
- void selectAll(char*& ptr) const {
- scopy(ptr, "select * from %s", m_name);
- }
- void selectRange(char*& ptr, bool sort) const {
- selectAll(ptr);
- whereRange(ptr);
- if (sort)
- orderPk(ptr);
- }
- void selectCount(char*& ptr) const {
- scopy(ptr, "select count(*) from %s", m_name);
- }
- void insertAll(char*& ptr) const {
- scopy(ptr, "insert into %s values (", m_name);
- for (unsigned i = 0; i < m_colCount; i++) {
- if (i > 0)
- scopy(ptr, ", ");
- scopy(ptr, "?");
- }
- scopy(ptr, ")");
- }
- void updatePk(char*& ptr) const {
- scopy(ptr, "update %s set", m_name);
- for (unsigned i = 0; i < m_nkCount; i++) {
- const Col& col = m_colList[m_nkIndex[i]];
- if (i > 0)
- scopy(ptr, ", ");
- else
- scopy(ptr, " ");
- scopy(ptr, "%s = ?", col.m_name);
- }
- wherePk(ptr);
- }
- void updateRange(char*& ptr) const {
- scopy(ptr, "update %s set", m_name);
- for (unsigned i = 0; i < m_nkCount; i++) {
- const Col& col = m_colList[m_nkIndex[i]];
- if (i > 0)
- scopy(ptr, ", ");
- else
- scopy(ptr, " ");
- scopy(ptr, "%s = ?", col.m_name); // XXX constant for now
- }
- whereRange(ptr);
- }
- void deleteAll(char*& ptr) const {
- scopy(ptr, "delete from %s", m_name);
- }
- void deletePk(char*& ptr) const {
- scopy(ptr, "delete from %s", m_name);
- wherePk(ptr);
- }
- void deleteRange(char*& ptr) const {
- scopy(ptr, "delete from %s", m_name);
- whereRange(ptr);
- }
- // simple
- void insertDirect(char*& ptr, unsigned n) const {
- scopy(ptr, "insert into %s values (", m_name);
- for (unsigned i = 0; i < m_colCount; i++) {
- const Col& col = m_colList[i];
- if (i > 0)
- scopy(ptr, ", ");
- if (col.m_type == Col::Char || col.m_type == Col::Varchar) {
- scopy(ptr, "'");
- for (unsigned i = 0; i <= n % col.m_length; i++)
- scopy(ptr, "%c", 'a' + (n + i) % 26);
- scopy(ptr, "'");
- } else if (col.m_type == Col::Int || col.m_type == Col::Bigint) {
- scopy(ptr, "%u", n);
- } else if (col.m_type == Col::Real || col.m_type == Col::Double) {
- scopy(ptr, "%.3f", n * 0.001);
- } else {
- assert(false);
- }
- }
- scopy(ptr, ")");
- }
- void whereDirect(char*& ptr, unsigned n) const {
- scopy(ptr, " where");
- for (unsigned i = 0; i < m_pkCount; i++) {
- const Col& col = m_colList[m_pkIndex[i]];
- if (i > 0)
- scopy(ptr, ", ");
- else
- scopy(ptr, " ");
- scopy(ptr, "%s = ", col.m_name);
- if (col.m_type == Col::Char || col.m_type == Col::Varchar) {
- scopy(ptr, "'");
- for (unsigned i = 0; i <= n % col.m_length; i++)
- scopy(ptr, "%c", 'a' + (n + i) % 26);
- scopy(ptr, "'");
- } else if (col.m_type == Col::Int || col.m_type == Col::Bigint) {
- scopy(ptr, "%u", n);
- } else {
- assert(false);
- }
- }
- }
- void countDirect(char*& ptr, unsigned n) const {
- scopy(ptr, "select count(*) from %s", m_name);
- whereDirect(ptr, n);
- }
- void deleteDirect(char*& ptr, unsigned n) const {
- scopy(ptr, "delete from %s", m_name);
- whereDirect(ptr, n);
- }
- // joins
- void selectCart(char*& ptr, unsigned cnt) const {
- scopy(ptr, "select count(*) from");
- for (unsigned j = 0; j < cnt; j++) {
- if (j > 0)
- scopy(ptr, ",");
- scopy(ptr, " %s", m_name);
- scopy(ptr, " t%u", j);
- }
- }
- void selectJoin(char*& ptr, unsigned cnt) const {
- scopy(ptr, "select * from");
- for (unsigned j = 0; j < cnt; j++) {
- if (j > 0)
- scopy(ptr, ",");
- scopy(ptr, " %s", m_name);
- scopy(ptr, " t%u", j);
- }
- for (unsigned i = 0; i < m_pkCount; i++) {
- const Col& col = m_colList[m_pkIndex[i]];
- for (unsigned j = 0; j < cnt - 1; j++) {
- if (i == 0 && j == 0)
- scopy(ptr, " where");
- else
- scopy(ptr, " and");
- scopy(ptr, " t%u.%s = t%u.%s", j, col.m_name, j + 1, col.m_name);
- }
- }
- }
- // check if selected on command line
- bool optok() const {
- return opt.m_table == 0 || strcasecmp(m_name, opt.m_table) == 0;
- }
- };
- // the test tables
- static Col col0[] = {
- Col( "a", Col::Bigint, 0, Col::Pk, Col::CLong ),
- Col( "b", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c", Col::Char, 4, Col::NotNull, Col::CChar ),
- Col( "d", Col::Double, 0, Col::Null, Col::CDouble ),
- Col( "e", Col::Char, 40, Col::Null, Col::CChar ),
- Col( "f", Col::Char, 10, Col::Null, Col::CChar )
- };
- static Col col1[] = {
- Col( "c0", Col::Int, 0, Col::Pk, Col::CLong ),
- Col( "c1", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c2", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c3", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c4", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c5", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c6", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c7", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c8", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c9", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c10", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c11", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c12", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c13", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c14", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c15", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c16", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c17", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c18", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c19", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c20", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c21", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c22", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c23", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c24", Col::Int, 0, Col::NotNull, Col::CLong ),
- Col( "c25", Col::Int, 0, Col::NotNull, Col::CLong )
- };
- static Col col2[] = {
- Col( "a", Col::Int, 0, Col::Pk, Col::CLong ),
- Col( "c", Col::Char, 8000, Col::NotNull, Col::CChar )
- };
- static Col col3[] = {
- Col( "a", Col::Int, 0, Col::Pk, Col::CLong ),
- Col( "c1", Col::Varchar, 1, Col::Null, Col::CChar ),
- Col( "c2", Col::Varchar, 2, Col::Null, Col::CChar ),
- Col( "c3", Col::Varchar, 3, Col::Null, Col::CChar ),
- Col( "c4", Col::Varchar, 4, Col::Null, Col::CChar ),
- Col( "c5", Col::Varchar, 10, Col::Null, Col::CChar ),
- Col( "c6", Col::Varchar, 40, Col::Null, Col::CChar ),
- Col( "c7", Col::Varchar, 255, Col::Null, Col::CChar ),
- Col( "c8", Col::Varchar, 4000, Col::Null, Col::CChar )
- };
- static Col col4[] = {
- Col( "a", Col::Char, 8, Col::Pk, Col::CChar ),
- Col( "b", Col::Char, 8, Col::NotNull, Col::CChar ),
- };
- static Tab tabList[] = {
- #define colList(x) x, arraySize(x)
- Tab( "tt00", colList(col0) ),
- Tab( "tt01", colList(col1) ), // fl鋝kbench special
- Tab( "tt02", colList(col2) ),
- Tab( "tt03", colList(col3) ),
- Tab( "tt04", colList(col4) )
- #undef colList
- };
- static const unsigned tabCount = arraySize(tabList);
- static const unsigned maxColCount = 100; // per table - keep up to date
- static bool
- findTable()
- {
- for (unsigned i = 0; i < tabCount; i++) {
- const Tab& tab = tabList[i];
- if (tab.optok())
- return true;
- }
- return false;
- }
- static void
- listTables()
- {
- ndbout << "tables:" << endl;
- for (unsigned i = 0; i < tabCount; i++) {
- const Tab& tab = tabList[i];
- if (i > 0)
- ndbout << " ";
- ndbout << tab.m_name;
- }
- ndbout << endl;
- }
- // data fields and rows
- struct Fld {
- const Col& m_col;
- union {
- char* m_char;
- long m_long;
- double m_double;
- };
- SQLINTEGER m_ind;
- SQLINTEGER m_need; // constant
- Fld() :
- m_col(ColUndef),
- m_need(0) {
- }
- Fld(const Col& col) :
- m_col(col),
- m_need(SQL_LEN_DATA_AT_EXEC(0)) {
- switch (m_col.m_ctype) {
- case Col::CChar:
- m_char = new char[m_col.csize()];
- memset(m_char, 0, m_col.csize());
- break;
- case Col::CLong:
- m_long = 0;
- break;
- case Col::CDouble:
- m_double = 0.0;
- break;
- }
- m_ind = -1;
- }
- ~Fld() {
- switch (m_col.m_ctype) {
- case Col::CChar:
- delete[] m_char;
- break;
- case Col::CLong:
- break;
- case Col::CDouble:
- break;
- }
- }
- void zero() {
- switch (m_col.m_ctype) {
- case Col::CChar:
- memset(m_char, 0x1f, m_col.csize());
- break;
- case Col::CLong:
- m_long = 0x1f1f1f1f;
- break;
- case Col::CDouble:
- m_double = 1111111.1111111;
- break;
- }
- m_ind = -1;
- }
- // copy values from another field
- void copy(const Fld& fld) {
- assert(&m_col == &fld.m_col);
- switch (m_col.m_ctype) {
- case Col::CChar:
- memcpy(m_char, fld.m_char, m_col.csize());
- break;
- case Col::CLong:
- m_long = fld.m_long;
- break;
- case Col::CDouble:
- m_double = fld.m_double;
- break;
- default:
- assert(false);
- break;
- }
- m_ind = fld.m_ind;
- }
- SQLPOINTER caddr() {
- switch (m_col.m_ctype) {
- case Col::CChar:
- return (SQLPOINTER)m_char;
- case Col::CLong:
- return (SQLPOINTER)&m_long;
- case Col::CDouble:
- return (SQLPOINTER)&m_double;
- }
- assert(false);
- return 0;
- }
- SQLINTEGER* ind() {
- return &m_ind;
- }
- SQLINTEGER* need() {
- m_need = SQL_LEN_DATA_AT_EXEC(0);
- return &m_need;
- }
- void calcPk(const Test& test, unsigned rownum) {
- switch (m_col.m_ctype) {
- case Col::CChar:
- test.calcPk(rownum, m_char, m_col.csize());
- m_ind = SQL_NTS;
- return;
- case Col::CLong:
- test.calcPk(rownum, &m_long);
- m_ind = 0;
- return;
- case Col::CDouble:
- assert(false);
- return;
- }
- assert(false);
- }
- void hashPk(const Test& test, unsigned* hash) const {
- switch (m_col.m_ctype) {
- case Col::CChar:
- test.hashPk(hash, m_char, m_col.csize());
- return;
- case Col::CLong:
- test.hashPk(hash, m_long);
- return;
- case Col::CDouble:
- assert(false);
- return;
- }
- assert(false);
- }
- void calcNk(const Test& test, unsigned hash) {
- bool null = m_col.m_cons == Col::Null;
- switch (m_col.m_ctype) {
- case Col::CChar:
- test.calcNk(hash, m_char, m_col.csize(), &m_ind, null);
- return;
- case Col::CLong:
- test.calcNk(hash, &m_long, &m_ind, null);
- return;
- case Col::CDouble:
- test.calcNk(hash, &m_double, &m_ind, null);
- return;
- }
- assert(false);
- }
- bool verify(Test& test, const Fld& fld) {
- assert(&m_col == &fld.m_col);
- switch (m_col.m_ctype) {
- case Col::CChar:
- return test.verify(m_char, m_ind, fld.m_char, fld.m_ind, m_col.csize());
- case Col::CLong:
- return test.verify(m_long, m_ind, fld.m_long, fld.m_ind);
- case Col::CDouble:
- return test.verify(m_double, m_ind, fld.m_double, fld.m_ind);
- }
- assert(false);
- return false;
- }
- // debug
- void print() const {
- if (m_ind == SQL_NULL_DATA)
- ndbout << "NULL";
- else {
- switch (m_col.m_ctype) {
- case Col::CChar:
- ndbout << m_char;
- break;
- case Col::CLong:
- ndbout << (int)m_long;
- break;
- case Col::CDouble:
- ndbout << m_double;
- break;
- }
- }
- }
- };
- struct Row {
- const Tab& m_tab;
- Fld* m_fldList;
- Row(const Tab& tab) :
- m_tab(tab) {
- m_fldList = new Fld[m_tab.m_colCount];
- for (unsigned i = 0; i < m_tab.m_colCount; i++) {
- const Col& col = m_tab.m_colList[i];
- void* place = &m_fldList[i];
- new (place) Fld(col);
- }
- }
- ~Row() {
- delete[] m_fldList;
- }
- // copy values from another row
- void copy(const Row& row) {
- assert(&m_tab == &row.m_tab);
- for (unsigned i = 0; i < m_tab.m_colCount; i++) {
- Fld& fld = m_fldList[i];
- fld.copy(row.m_fldList[i]);
- }
- }
- // primary key value is determined by row number
- void calcPk(Test& test, unsigned rownum) {
- for (unsigned i = 0; i < m_tab.m_pkCount; i++) {
- Fld& fld = m_fldList[m_tab.m_pkIndex[i]];
- fld.calcPk(test, rownum);
- }
- }
- // other fields are determined by primary key value
- void calcNk(Test& test) {
- unsigned hash = test.m_salt;
- for (unsigned i = 0; i < m_tab.m_pkCount; i++) {
- Fld& fld = m_fldList[m_tab.m_pkIndex[i]];
- fld.hashPk(test, &hash);
- }
- for (unsigned i = 0; i < m_tab.m_colCount; i++) {
- const Col& col = m_tab.m_colList[i];
- if (col.m_cons == Col::Pk)
- continue;
- Fld& fld = m_fldList[i];
- fld.calcNk(test, hash);
- }
- }
- // verify against another row
- bool verifyPk(Test& test, const Row& row) const {
- assert(&m_tab == &row.m_tab);
- for (unsigned i = 0; i < m_tab.m_pkCount; i++) {
- Fld& fld = m_fldList[m_tab.m_pkIndex[i]];
- if (! fld.verify(test, row.m_fldList[m_tab.m_pkIndex[i]])) {
- ndbout << "verify failed: tab=" << m_tab.m_name << " col=" << fld.m_col.m_name << endl;
- return false;
- }
- }
- return true;
- }
- bool verifyNk(Test& test, const Row& row) const {
- assert(&m_tab == &row.m_tab);
- for (unsigned i = 0; i < m_tab.m_nkCount; i++) {
- Fld& fld = m_fldList[m_tab.m_nkIndex[i]];
- if (! fld.verify(test, row.m_fldList[m_tab.m_nkIndex[i]])) {
- ndbout << "verify failed: tab=" << m_tab.m_name << " col=" << fld.m_col.m_name << endl;
- return false;
- }
- }
- return true;
- }
- bool verify(Test& test, const Row& row) const {
- return verifyPk(test, row) && verifyNk(test, row);
- }
- // debug
- void print() const {
- ndbout << "row";
- for (unsigned i = 0; i < m_tab.m_colCount; i++) {
- ndbout << " " << i << "=";
- Fld& fld = m_fldList[i];
- fld.print();
- }
- ndbout << endl;
- }
- };
- // set ODBC version - required
- static void
- setVersion(Test& test, SQLHANDLE hEnv)
- {
- test.run(HEnv(hEnv), SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER));
- }
- // set autocommit
- static void
- setAutocommit(Test& test, SQLHANDLE hDbc, bool on)
- {
- SQLUINTEGER value = on ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF;
- test.run(HDbc(hDbc), SQLSetConnectAttr(hDbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)value, SQL_IS_UINTEGER));
- SQLUINTEGER value2 = (SQLUINTEGER)-1;
- test.run(HDbc(hDbc), SQLGetConnectAttr(hDbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)&value2, SQL_IS_UINTEGER, 0));
- test.chk(HDbc(hDbc), value2 == value, "got %u != %u", (unsigned)value2, (unsigned)value);
- }
- // subroutines - migrate simple common routines here
- static void
- allocEnv(Test& test, SQLHANDLE& hEnv)
- {
- test.run(HNull, SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv));
- setVersion(test, hEnv);
- }
- static void
- allocDbc(Test& test, SQLHANDLE hEnv, SQLHANDLE& hDbc)
- {
- test.run(HEnv(hEnv), SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc));
- }
- static void
- allocConn(Test& test, SQLHANDLE hEnv, SQLHANDLE& hDbc)
- {
- allocDbc(test, hEnv, hDbc);
- #ifdef unixODBC
- test.exp(SQL_SUCCESS_WITH_INFO, "IM003", 0, false); // unicode??
- test.exp(SQL_SUCCESS_WITH_INFO, "01000", 0, false); // version??
- #endif
- test.run(HDbc(hDbc), SQLConnect(hDbc, (SQLCHAR*)opt.m_dsn, SQL_NTS, (SQLCHAR*)"user", SQL_NTS, (SQLCHAR*)"pass", SQL_NTS));
- }
- static void
- allocStmt(Test& test, SQLHANDLE hDbc, SQLHANDLE& hStmt)
- {
- test.run(HDbc(hDbc), SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt));
- }
- static void
- allocAll(Test& test, SQLHANDLE& hEnv, SQLHANDLE& hDbc, SQLHANDLE& hStmt)
- {
- allocEnv(test, hEnv);
- allocConn(test, hEnv, hDbc);
- allocStmt(test, hDbc, hStmt);
- }
- static void
- allocAll(Test& test, SQLHANDLE& hEnv, SQLHANDLE& hDbc, SQLHANDLE* hStmtList, unsigned nStmt)
- {
- allocEnv(test, hEnv);
- allocConn(test, hEnv, hDbc);
- for (unsigned i = 0; i < nStmt; i++)
- allocStmt(test, hDbc, hStmtList[i]);
- }
- static void
- freeEnv(Test& test, SQLHANDLE hEnv)
- {
- test.run(HNull, SQLFreeHandle(SQL_HANDLE_ENV, hEnv));
- }
- static void
- freeDbc(Test& test, SQLHANDLE hEnv, SQLHANDLE hDbc)
- {
- test.run(HEnv(hEnv), SQLFreeHandle(SQL_HANDLE_DBC, hDbc));
- }
- static void
- freeConn(Test& test, SQLHANDLE hEnv, SQLHANDLE hDbc)
- {
- test.run(HDbc(hDbc), SQLDisconnect(hDbc));
- test.run(HEnv(hEnv), SQLFreeHandle(SQL_HANDLE_DBC, hDbc));
- }
- static void
- freeStmt(Test& test, SQLHANDLE hDbc, SQLHANDLE hStmt)
- {
- test.run(HDbc(hDbc), SQLFreeHandle(SQL_HANDLE_STMT, hStmt));
- }
- static void
- freeAll(Test& test, SQLHANDLE hEnv, SQLHANDLE hDbc, SQLHANDLE hStmt)
- {
- freeStmt(test, hDbc, hStmt);
- freeConn(test, hEnv, hDbc);
- freeEnv(test, hEnv);
- }
- static void
- freeAll(Test& test, SQLHANDLE hEnv, SQLHANDLE hDbc, SQLHANDLE* hStmtList, unsigned nStmt)
- {
- for (unsigned i = 0; i < nStmt; i++)
- freeStmt(test, hDbc, hStmtList[i]);
- freeConn(test, hEnv, hDbc);
- freeEnv(test, hEnv);
- }
- #define chkTuplesFetched(/*Test&*/ _test, /*SQLHANDLE*/ _hStmt, /*SQLUINTEGER*/ _countExp)
- do {
- SQLUINTEGER _count = (SQLUINTEGER)-1;
- getTuplesFetched(_test, _hStmt, &_count);
- test.chk(HStmt(_hStmt), _count == _countExp, "tuples: got %ld != %ld", (long)_count, (long)_countExp);
- } while (0)
- static void
- getTuplesFetched(Test& test, SQLHANDLE hStmt, SQLUINTEGER* count)
- {
- *count = (SQLUINTEGER)-1;
- test.run(HStmt(hStmt), SQLGetStmtAttr(hStmt, SQL_ATTR_NDB_TUPLES_FETCHED, count, SQL_IS_POINTER, 0));
- }
- static void
- selectCount(Test& test, SQLHANDLE hStmt, const char* sql, long* count)
- {
- if (opt.m_v >= 3)
- ndbout << "SQL: " << sql << endl;
- test.run(HStmt(hStmt), SQLPrepare(hStmt, (SQLCHAR*)sql, SQL_NTS));
- SQLINTEGER ind;
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 1, SQL_C_SLONG, count, 0, &ind));
- ind = -1;
- *count = -1;
- test.run(HStmt(hStmt), SQLExecute(hStmt));
- unsigned k = 0;
- while (1) {
- if (k == 1)
- test.exp(SQL_NO_DATA, 0, 0, true);
- test.run(HStmt(hStmt), SQLFetch(hStmt));
- if (k == 1)
- break;
- k++;
- }
- test.chk(HStmt(hStmt), ind == sizeof(long), "got %d != %d", (int)ind, (int)sizeof(long));
- test.chk(HStmt(hStmt), *count >= 0, "got %ld < 0", *count);
- chkTuplesFetched(test, hStmt, *count);
- #ifndef iODBC
- //
- test.run(HStmt(hStmt), SQLCloseCursor(hStmt));
- #else
- test.run(HStmt(hStmt), SQLFreeStmt(hStmt, SQL_CLOSE));
- #endif
- }
- static void
- selectCount(Test& test, SQLHANDLE hStmt, const Tab& tab, long* count)
- {
- static char sql[MAX_SQL], *sqlptr; // XXX static or core
- tab.selectCount(sqlptr = sql);
- selectCount(test, hStmt, sql, count);
- }
- static void
- verifyCount(Test& test, SQLHANDLE hStmt, const Tab& tab, long countExp)
- {
- long count = -1;
- selectCount(test, hStmt, tab, &count);
- test.chk(HStmt(hStmt), count == countExp, "got %ld != %ld", count, countExp);
- }
- #define chkRowCount(/*Test&*/ _test, /*SQLHANDLE*/ _hStmt, /*SQLINTEGER*/ _countExp)
- do {
- SQLINTEGER _count = -1;
- getRowCount(_test, _hStmt, &_count);
- test.chk(HStmt(_hStmt), _count == _countExp, "rowcount: got %ld != %ld", (long)_count, (long)_countExp);
- } while (0)
- static void
- getRowCount(Test& test, SQLHANDLE hStmt, SQLINTEGER* count)
- {
- *count = -1;
- test.run(HStmt(hStmt), SQLRowCount(hStmt, count));
- }
- // handle allocation
- static void
- testAlloc(Test& test)
- {
- const unsigned n1 = (opt.m_scale >> 8) & 0xf; // default 500 = 0x1f4
- const unsigned n2 = (opt.m_scale >> 4) & 0xf;
- const unsigned n3 = (opt.m_scale >> 0) & 0xf;
- const unsigned count = n1 + n1 * n2 + n1 * n2 * n3;
- SQLHANDLE hEnvList[0xf];
- SQLHANDLE hDbcList[0xf][0xf];
- SQLHANDLE hStmtList[0xf][0xf][0xf];
- for (unsigned i1 = 0; i1 < n1; i1++) {
- SQLHANDLE& hEnv = hEnvList[i1];
- test.run(HNull, SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv));
- test.run(HEnv(hEnv), SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER));
- for (unsigned i2 = 0; i2 < n2; i2++) {
- SQLHANDLE& hDbc = hDbcList[i1][i2];
- test.run(HEnv(hEnv), SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc));
- #ifdef unixODBC
- test.exp(SQL_SUCCESS_WITH_INFO, "IM003", 0, false); // unicode??
- test.exp(SQL_SUCCESS_WITH_INFO, "01000", 0, false); // version??
- #endif
- test.run(HDbc(hDbc), SQLConnect(hDbc, (SQLCHAR*)opt.m_dsn, SQL_NTS, (SQLCHAR*)"user", SQL_NTS, (SQLCHAR*)"pass", SQL_NTS));
- // some attributes
- test.exp(SQL_ERROR, "HY092", -1, true); // read-only attribute
- test.run(HDbc(hDbc), SQLSetConnectAttr(hDbc, SQL_ATTR_AUTO_IPD, (SQLPOINTER)SQL_TRUE, SQL_IS_UINTEGER));
- test.exp(SQL_ERROR, "HYC00", -1, true); // not supported
- test.run(HDbc(hDbc), SQLSetConnectAttr(hDbc, SQL_ATTR_TXN_ISOLATION, (SQLPOINTER)SQL_TXN_SERIALIZABLE, SQL_IS_UINTEGER));
- test.run(HDbc(hDbc), SQLSetConnectAttr(hDbc, SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER)"DEFAULT", strlen("DEFAULT")));
- for (unsigned i3 = 0; i3 < n3; i3++) {
- SQLHANDLE& hStmt = hStmtList[i1][i2][i3];
- test.run(HDbc(hDbc), SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt));
- SQLHANDLE ipd0, ipd1;
- SQLHANDLE ird0, ird1;
- SQLHANDLE apd0, apd1, apd2;
- SQLHANDLE ard0, ard1, ard2;
- // get
- ipd0 = ird0 = apd0 = ard0 = 0;
- test.run(HStmt(hStmt), SQLGetStmtAttr(hStmt, SQL_ATTR_IMP_PARAM_DESC, &ipd0, SQL_IS_POINTER, 0));
- test.run(HStmt(hStmt), SQLGetStmtAttr(hStmt, SQL_ATTR_IMP_ROW_DESC, &ird0, SQL_IS_POINTER, 0));
- test.run(HStmt(hStmt), SQLGetStmtAttr(hStmt, SQL_ATTR_APP_PARAM_DESC, &apd0, SQL_IS_POINTER, 0));
- test.run(HStmt(hStmt), SQLGetStmtAttr(hStmt, SQL_ATTR_APP_ROW_DESC, &ard0, SQL_IS_POINTER, 0));
- #ifndef unixODBC
- test.chk(HStmt(hStmt), ipd0 != 0, "got 0");
- test.chk(HStmt(hStmt), ird0 != 0, "got 0");
- test.chk(HStmt(hStmt), apd0 != 0, "got 0");
- test.chk(HStmt(hStmt), ard0 != 0, "got 0");
- #endif
- // alloc
- ipd1 = ird1 = apd1 = ard1 = 0;
- test.run(HDbc(hDbc), SQLAllocHandle(SQL_HANDLE_DESC, hDbc, &ipd1));
- test.run(HDbc(hDbc), SQLAllocHandle(SQL_HANDLE_DESC, hDbc, &ird1));
- test.run(HDbc(hDbc), SQLAllocHandle(SQL_HANDLE_DESC, hDbc, &apd1));
- test.run(HDbc(hDbc), SQLAllocHandle(SQL_HANDLE_DESC, hDbc, &ard1));
- test.chk(HDbc(hDbc), ipd1 != 0 && ird1 != 0 && apd1 != 0 && ard1 != 0, "got null");
- // set
- test.exp(SQL_ERROR, "HY092", -1, true); // read-only attribute
- test.run(HStmt(hStmt), SQLSetStmtAttr(hStmt, SQL_ATTR_IMP_PARAM_DESC, ipd1, SQL_IS_POINTER));
- test.exp(SQL_ERROR, "HY092", -1, true); // read-only attribute
- test.run(HStmt(hStmt), SQLSetStmtAttr(hStmt, SQL_ATTR_IMP_ROW_DESC, ird1, SQL_IS_POINTER));
- test.run(HStmt(hStmt), SQLSetStmtAttr(hStmt, SQL_ATTR_APP_PARAM_DESC, apd1, SQL_IS_POINTER));
- test.run(HStmt(hStmt), SQLSetStmtAttr(hStmt, SQL_ATTR_APP_ROW_DESC, ard1, SQL_IS_POINTER));
- // get
- apd2 = ard2 = 0;
- test.run(HStmt(hStmt), SQLGetStmtAttr(hStmt, SQL_ATTR_APP_PARAM_DESC, &apd2, SQL_IS_POINTER, 0));
- test.run(HStmt(hStmt), SQLGetStmtAttr(hStmt, SQL_ATTR_APP_ROW_DESC, &ard2, SQL_IS_POINTER, 0));
- test.chk(HStmt(hStmt), apd2 == apd1, "got %x != %x", (unsigned)apd2, (unsigned)apd1);
- test.chk(HStmt(hStmt), ard2 == ard1, "got %x != %x", (unsigned)ard2, (unsigned)ard1);
- // free
- test.run(HDbc(hDbc), SQLFreeHandle(SQL_HANDLE_DESC, ipd1));
- test.run(HDbc(hDbc), SQLFreeHandle(SQL_HANDLE_DESC, ird1));
- test.run(HDbc(hDbc), SQLFreeHandle(SQL_HANDLE_DESC, apd1));
- test.run(HDbc(hDbc), SQLFreeHandle(SQL_HANDLE_DESC, ard1));
- }
- }
- }
- test.timerCnt(count);
- if (opt.m_v >= 3)
- ndbout << "allocated " << count << endl;
- for (unsigned i1 = 0; i1 < n1; i1++) {
- SQLHANDLE& hEnv = hEnvList[i1];
- for (unsigned i2 = 0; i2 < n2; i2++) {
- SQLHANDLE& hDbc = hDbcList[i1][i2];
- if (i2 % 2 == 0) {
- for (unsigned i3 = 0; i3 < n3; i3++) {
- SQLHANDLE& hStmt = hStmtList[i1][i2][i3];
- test.run(HDbc(hDbc), SQLFreeHandle(SQL_HANDLE_STMT, hStmt));
- }
- } else {
- // cleaned up by SQLDisconnect
- }
- test.run(HDbc(hDbc), SQLDisconnect(hDbc));
- test.run(HEnv(hEnv), SQLFreeHandle(SQL_HANDLE_DBC, hDbc));
- }
- test.run(HNull, SQLFreeHandle(SQL_HANDLE_ENV, hEnv));
- }
- test.timerCnt(count);
- if (opt.m_v >= 3)
- ndbout << "freed " << count << endl;
- }
- // create tables
- static void
- testCreate(Test& test)
- {
- SQLHANDLE hEnv, hDbc, hStmt;
- allocAll(test, hEnv, hDbc, hStmt);
- char sql[MAX_SQL], *sqlptr;
- for (unsigned i = 0; i < tabCount; i++) {
- Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- // drop
- tab.drop(sqlptr = sql);
- if (opt.m_v >= 2)
- ndbout << "SQL: " << sql << endl;
- test.run(HStmt(hStmt), SQLPrepare(hStmt, (SQLCHAR*)sql, SQL_NTS));
- test.exp(SQL_ERROR, "IM000", 2040709, false);
- test.run(HStmt(hStmt), SQLExecute(hStmt));
- if (test.m_ret == SQL_SUCCESS)
- test.chk(HStmt(hStmt), test.m_functionCode == SQL_DIAG_DROP_TABLE, "got %d != %d", test.m_functionCode, SQL_DIAG_DROP_TABLE);
- if (test.m_ret == SQL_SUCCESS && opt.m_v >= 2)
- ndbout << "table " << tab.m_name << " dropped" << endl;
- if (test.m_ret != SQL_SUCCESS && opt.m_v >= 2)
- ndbout << "table " << tab.m_name << " does not exist" << endl;
- test.timerCnt(1);
- // create
- tab.create(sqlptr = sql);
- if (opt.m_v >= 2)
- ndbout << "SQL: " << sql << endl;
- test.run(HStmt(hStmt), SQLPrepare(hStmt, (SQLCHAR*)sql, SQL_NTS));
- test.exp(SQL_ERROR, "IM000", 2040721, false);
- test.run(HStmt(hStmt), SQLExecute(hStmt));
- if (test.m_ret == SQL_SUCCESS)
- test.chk(HStmt(hStmt), test.m_functionCode == SQL_DIAG_CREATE_TABLE, "got %d != %d", test.m_functionCode, SQL_DIAG_CREATE_TABLE);
- if (test.m_ret == SQL_SUCCESS && opt.m_v >= 2)
- ndbout << "table " << tab.m_name << " created" << endl;
- if (test.m_ret != SQL_SUCCESS && opt.m_v >= 2)
- ndbout << "table " << tab.m_name << " already exists" << endl;
- test.timerCnt(1);
- }
- freeAll(test, hEnv, hDbc, hStmt);
- }
- // prepare without execute
- static void
- testPrepare(Test& test)
- {
- SQLHANDLE hEnv, hDbc, hStmt;
- allocAll(test, hEnv, hDbc, hStmt);
- char sql[MAX_SQL], *sqlptr;
- for (unsigned cnt = opt.m_depth; cnt <= opt.m_depth; cnt++) {
- for (unsigned i = 0; i < tabCount; i++) {
- Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- tab.selectJoin(sqlptr = sql, cnt);
- if (opt.m_v >= 2)
- ndbout << "SQL: " << sql << endl;
- test.run(HStmt(hStmt), SQLPrepare(hStmt, (SQLCHAR*)sql, SQL_NTS));
- SQLSMALLINT colCount = -1;
- SQLSMALLINT colExp = cnt * tab.m_colCount;
- test.run(HStmt(hStmt), SQLNumResultCols(hStmt, &colCount));
- test.chk(HStmt(hStmt), colCount == colExp, "got %d != %d", (int)colCount, (int)colExp);
- test.timerCnt(1);
- }
- }
- freeAll(test, hEnv, hDbc, hStmt);
- }
- // catalog functions
- static void
- testCatalog(Test& test)
- {
- SQLHANDLE hEnv, hDbc, hStmt;
- allocAll(test, hEnv, hDbc, hStmt);
- odbc_typeinfo: {
- long type[] = {
- SQL_CHAR, SQL_VARCHAR, SQL_SMALLINT, SQL_INTEGER, SQL_BIGINT, SQL_REAL, SQL_DOUBLE
- };
- unsigned rows[] = {
- 1, 1, 2, 2, 2, 1, 1 // 2 for signed and unsigned
- };
- for (unsigned i = 0; i < arraySize(type); i++) {
- test.run(HStmt(hStmt), SQLGetTypeInfo(hStmt, type[i]));
- long dataType = 0;
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 2, SQL_C_SLONG, &dataType, 0, 0));
- unsigned k = 0;
- while (1) {
- if (k == rows[i])
- test.exp(SQL_NO_DATA, 0, 0, true);
- test.run(HStmt(hStmt), SQLFetch(hStmt));
- if (k == rows[i])
- break;
- test.chk(HStmt(hStmt), dataType == type[i], "got %ld != %ld", dataType, type[i]);
- test.timerCnt(1);
- k++;
- }
- #ifndef iODBC
- test.run(HStmt(hStmt), SQLCloseCursor(hStmt));
- #else
- freeStmt(test, hDbc, hStmt);
- allocStmt(test, hDbc, hStmt);
- #endif
- }
- if (opt.m_v >= 2)
- ndbout << "found " << (UintPtr)arraySize(type) << " data types" << endl;
- test.run(HStmt(hStmt), SQLFreeStmt(hStmt, SQL_UNBIND));
- }
- odbc_tables: {
- unsigned found[tabCount];
- for (unsigned i = 0; i < tabCount; i++)
- found[i] = 0;
- test.run(HStmt(hStmt), SQLTables(hStmt, (SQLCHAR*)0, 0, (SQLCHAR*)0, 0, (SQLCHAR*)0, 0, (SQLCHAR*)0, 0));
- char tableName[200] = "";
- char tableType[200] = "";
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 3, SQL_C_CHAR, tableName, sizeof(tableName), 0));
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 4, SQL_C_CHAR, tableType, sizeof(tableType), 0));
- unsigned cnt = 0;
- while (1) {
- test.exp(SQL_NO_DATA, 0, 0, false);
- test.run(HStmt(hStmt), SQLFetch(hStmt));
- if (test.m_ret == SQL_NO_DATA)
- break;
- test.timerCnt(1);
- cnt++;
- if (! blankeq(tableType, "TABLE"))
- continue;
- for (unsigned i = 0; i < tabCount; i++) {
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- if (! blankeq(tab.m_name, tableName))
- continue;
- test.chk(HStmt(hStmt), found[i] == 0, "duplicate table %s", tab.m_name);
- found[i]++;
- }
- }
- #ifndef iODBC
- test.run(HStmt(hStmt), SQLCloseCursor(hStmt));
- #else
- freeStmt(test, hDbc, hStmt);
- allocStmt(test, hDbc, hStmt);
- #endif
- for (unsigned i = 0; i < tabCount; i++) {
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- test.chk(HStmt(hStmt), found[i] == 1, "table %s not found", tab.m_name);
- }
- if (opt.m_v >= 2)
- ndbout << "found " << cnt << " tables" << endl;
- test.run(HStmt(hStmt), SQLFreeStmt(hStmt, SQL_UNBIND));
- }
- odbc_columns: {
- unsigned found[tabCount][maxColCount];
- for (unsigned i = 0; i < tabCount; i++) {
- for (unsigned j = 0; j < maxColCount; j++)
- found[i][j] = 0;
- }
- test.run(HStmt(hStmt), SQLColumns(hStmt, (SQLCHAR*)0, 0, (SQLCHAR*)0, 0, (SQLCHAR*)0, 0, (SQLCHAR*)0, 0));
- char tableName[200] = "";
- char columnName[200] = "";
- long dataType = 0;
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 3, SQL_C_CHAR, tableName, sizeof(tableName), 0));
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 4, SQL_C_CHAR, columnName, sizeof(columnName), 0));
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 5, SQL_C_SLONG, &dataType, 0, 0));
- unsigned cnt = 0;
- while (1) {
- test.exp(SQL_NO_DATA, 0, 0, false);
- test.run(HStmt(hStmt), SQLFetch(hStmt));
- if (test.m_ret == SQL_NO_DATA)
- break;
- test.timerCnt(1);
- cnt++;
- for (unsigned i = 0; i < tabCount; i++) {
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- if (! blankeq(tab.m_name, tableName))
- continue;
- bool columnFound = false;
- for (unsigned j = 0; j < tab.m_colCount; j++) {
- const Col& col = tab.m_colList[j];
- if (! blankeq(col.m_name, columnName))
- continue;
- test.chk(HStmt(hStmt), found[i][j] == 0, "duplicate column %s.%s", tableName, columnName);
- found[i][j]++;
- columnFound = true;
- }
- test.chk(HStmt(hStmt), columnFound, "unknown column %s.%s", tableName, columnName);
- }
- }
- #ifndef iODBC
- test.run(HStmt(hStmt), SQLCloseCursor(hStmt));
- #else
- freeStmt(test, hDbc, hStmt);
- allocStmt(test, hDbc, hStmt);
- #endif
- for (unsigned i = 0; i < tabCount; i++) {
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- for (unsigned j = 0; j < tab.m_colCount; j++) {
- const Col& col = tab.m_colList[j];
- test.chk(HStmt(hStmt), found[i][j] == 1, "column %s.%s not found", tab.m_name, col.m_name);
- }
- }
- if (opt.m_v >= 2)
- ndbout << "found " << cnt << " columns" << endl;
- test.run(HStmt(hStmt), SQLFreeStmt(hStmt, SQL_UNBIND));
- }
- odbc_primarykeys: {
- // table patterns are no allowed
- for (unsigned i = 0; i < tabCount; i++) {
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- char tmp[200]; // p.i.t.a
- strcpy(tmp, tab.m_name);
- for (char* a = tmp; *a != 0; a++) {
- if ('a' <= *a && *a <= 'z')
- *a -= 'a' - 'A';
- }
- test.run(HStmt(hStmt), SQLPrimaryKeys(hStmt, (SQLCHAR*)0, 0, (SQLCHAR*)0, 0, (SQLCHAR*)tmp, SQL_NTS));
- char tableName[200] = "";
- char columnName[200] = "";
- long keySeq = -1;
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 3, SQL_C_CHAR, tableName, sizeof(tableName), 0));
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 4, SQL_C_CHAR, columnName, sizeof(columnName), 0));
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 5, SQL_C_SLONG, &keySeq, 0, 0));
- unsigned cnt = 0;
- while (1) {
- if (cnt == tab.m_pkCount)
- test.exp(SQL_NO_DATA, 0, 0, true);
- test.run(HStmt(hStmt), SQLFetch(hStmt));
- if (test.m_ret == SQL_NO_DATA)
- break;
- test.chk(HStmt(hStmt), keySeq == 1 + cnt, "got %ld != %u", keySeq, 1 + cnt);
- const Col& col = tab.m_colList[tab.m_pkIndex[keySeq - 1]];
- test.chk(HStmt(hStmt), blankeq(columnName, col.m_name), "got %s != %s", columnName, col.m_name);
- test.timerCnt(1);
- cnt++;
- }
- #ifndef iODBC
- test.run(HStmt(hStmt), SQLCloseCursor(hStmt));
- #else
- freeStmt(test, hDbc, hStmt);
- allocStmt(test, hDbc, hStmt);
- #endif
- }
- test.run(HStmt(hStmt), SQLFreeStmt(hStmt, SQL_UNBIND));
- }
- freeAll(test, hEnv, hDbc, hStmt);
- }
- // insert
- static void
- testInsert(Test& test)
- {
- SQLHANDLE hEnv, hDbc, hStmtList[tabCount];
- allocAll(test, hEnv, hDbc, hStmtList, tabCount);
- char sql[MAX_SQL], *sqlptr;
- for (unsigned i = 0; i < tabCount; i++) {
- SQLHANDLE& hStmt = hStmtList[i];
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- // prepare
- tab.insertAll(sqlptr = sql);
- if (opt.m_v >= 2)
- ndbout << "SQL: " << sql << endl;
- test.run(HStmt(hStmt), SQLPrepare(hStmt, (SQLCHAR*)sql, SQL_NTS));
- SQLSMALLINT parCount = -1;
- test.run(HStmt(hStmt), SQLNumParams(hStmt, &parCount));
- test.chk(HStmt(hStmt), parCount == tab.m_colCount, "got %d != %d", (int)parCount, (int)tab.m_colCount);
- // bind parameters
- Row row(tab);
- for (unsigned j = 0; j < tab.m_colCount; j++) {
- Fld& fld = row.m_fldList[j];
- const Col& col = fld.m_col;
- // every other at-exec
- SQLPOINTER caddr;
- SQLINTEGER* ind;
- if (opt.m_noputd || j % 2 == 0) {
- caddr = fld.caddr();
- ind = fld.ind();
- } else {
- caddr = (SQLPOINTER)j;
- ind = fld.need();
- }
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + j, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, caddr, col.csize(), ind));
- }
- // bind columns (none)
- SQLSMALLINT colCount = -1;
- test.run(HStmt(hStmt), SQLNumResultCols(hStmt, &colCount));
- test.chk(HStmt(hStmt), colCount == 0, "got %d != 0", (int)colCount);
- // execute
- for (unsigned k = 0; k < opt.m_scale; k++) {
- if (k % 5 == 0) {
- // rebind
- unsigned j = 0;
- Fld& fld = row.m_fldList[j];
- const Col& col = fld.m_col;
- // every other at-exec
- SQLPOINTER caddr;
- SQLINTEGER* ind;
- if (opt.m_noputd || j % 2 == 0) {
- caddr = fld.caddr();
- ind = fld.ind();
- } else {
- caddr = (SQLPOINTER)j;
- ind = fld.need();
- }
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + j, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, caddr, col.csize(), ind));
- }
- row.calcPk(test, k);
- row.calcNk(test);
- unsigned needData = opt.m_noputd ? 0 : tab.m_colCount / 2;
- if (needData)
- test.exp(SQL_NEED_DATA, 0, 0, true);
- test.run(HStmt(hStmt), SQLExecute(hStmt));
- test.chk(HStmt(hStmt), test.m_functionCode == SQL_DIAG_INSERT, "got %d != %d", test.m_functionCode, SQL_DIAG_INSERT);
- if (needData) {
- while (1) {
- SQLPOINTER jPtr = (SQLPOINTER)999;
- if (needData)
- test.exp(SQL_NEED_DATA, 0, 0, true);
- // completes SQLExecute on success
- test.run(HStmt(hStmt), SQLParamData(hStmt, &jPtr));
- if (! needData)
- break;
- unsigned j = (unsigned)jPtr;
- test.chk(HStmt(hStmt), j < tab.m_colCount && j % 2 != 0, "got %u 0x%x", j, j);
- Fld& fld = row.m_fldList[j];
- const Col& col = fld.m_col;
- SQLSMALLINT ctype = col.ctype();
- if (k % 2 == 0 || ctype != Col::CChar)
- test.run(HStmt(hStmt), SQLPutData(hStmt, fld.caddr(), *fld.ind()));
- else {
- // put in pieces
- unsigned size = col.csize() - 1; // omit null terminator
- char* caddr = (char*)(fld.caddr());
- unsigned off = 0;
- while (off < size) {
- unsigned m = size / 7; // bytes to put
- if (m == 0)
- m = 1;
- if (m > size - off)
- m = size - off;
- bool putNull = (*fld.ind() == SQL_NULL_DATA);
- // no null terminator
- SQLINTEGER len = putNull ? SQL_NULL_DATA : (int)m;
- test.run(HStmt(hStmt), SQLPutData(hStmt, caddr + off, len));
- if (putNull)
- break;
- off += m;
- }
- }
- needData--;
- }
- }
- chkRowCount(test, hStmt, 1);
- chkTuplesFetched(test, hStmt, 0);
- }
- test.timerCnt(opt.m_scale);
- if (opt.m_v >= 3)
- ndbout << "inserted " << opt.m_scale << " into " << tab.m_name << endl;
- }
- freeAll(test, hEnv, hDbc, hStmtList, tabCount);
- }
- // count
- static void
- testCount(Test& test)
- {
- SQLHANDLE hEnv, hDbc, hStmtList[tabCount];
- allocAll(test, hEnv, hDbc, hStmtList, tabCount);
- for (unsigned i = 0; i < tabCount; i++) {
- SQLHANDLE& hStmt = hStmtList[i];
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- long count = -1;
- selectCount(test, hStmt, tab, &count);
- test.chk(HStmt(hStmt), count == opt.m_scale * opt.m_threads, "got %ld != %u", count, opt.m_scale * opt.m_threads);
- test.timerCnt(count);
- if (opt.m_v >= 3)
- ndbout << "counted " << (int)count << " rows in " << tab.m_name << endl;
- }
- // scan all at same time
- char sql[MAX_SQL], *sqlptr;
- for (unsigned i = 0; i < tabCount; i++) {
- SQLHANDLE& hStmt = hStmtList[i];
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- tab.selectAll(sqlptr = sql);
- test.run(HStmt(hStmt), SQLExecDirect(hStmt, (SQLCHAR*)sql, SQL_NTS));
- }
- unsigned k = 0;
- while (1) {
- for (unsigned i = 0; i < tabCount; i++) {
- SQLHANDLE& hStmt = hStmtList[i];
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- if (k == opt.m_scale * opt.m_threads)
- test.exp(SQL_NO_DATA, 0, 0, true);
- test.run(HStmt(hStmt), SQLFetch(hStmt));
- if (k != opt.m_scale * opt.m_threads) {
- chkTuplesFetched(test, hStmt, k + 1);
- test.timerCnt(1);
- } else {
- chkTuplesFetched(test, hStmt, k);
- test.exp(SQL_NO_DATA, 0, 0, true);
- test.run(HStmt(hStmt), SQLMoreResults(hStmt));
- }
- }
- if (k == opt.m_scale * opt.m_threads)
- break;
- k++;
- }
- if (opt.m_v >= 3)
- ndbout << "scanned " << opt.m_scale << " rows from each table" << endl;
- freeAll(test, hEnv, hDbc, hStmtList, tabCount);
- }
- // update
- static void
- testUpdatePk(Test& test)
- {
- SQLHANDLE hEnv, hDbc, hStmtList[tabCount];
- allocAll(test, hEnv, hDbc, hStmtList, tabCount);
- char sql[MAX_SQL], *sqlptr;
- for (unsigned i = 0; i < tabCount; i++) {
- SQLHANDLE& hStmt = hStmtList[i];
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- // prepare
- tab.updatePk(sqlptr = sql);
- if (opt.m_v >= 2)
- ndbout << "SQL: " << sql << endl;
- test.run(HStmt(hStmt), SQLPrepare(hStmt, (SQLCHAR*)sql, SQL_NTS));
- // bind parameters
- Row row(tab);
- SQLSMALLINT parCount = -1;
- test.run(HStmt(hStmt), SQLNumParams(hStmt, &parCount));
- test.chk(HStmt(hStmt), parCount == tab.m_colCount, "got %d != %d", (int)parCount, (int)tab.m_colCount);
- for (unsigned j = 0; j < tab.m_nkCount; j++) {
- Fld& fld = row.m_fldList[tab.m_nkIndex[j]];
- const Col& col = fld.m_col;
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + j, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, fld.caddr(), col.csize(), fld.ind()));
- }
- for (unsigned j = 0; j < tab.m_pkCount; j++) {
- Fld& fld = row.m_fldList[tab.m_pkIndex[j]];
- const Col& col = fld.m_col;
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + tab.m_nkCount + j, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, fld.caddr(), col.csize(), fld.ind()));
- }
- // bind columns (none)
- SQLSMALLINT colCount = -1;
- test.run(HStmt(hStmt), SQLNumResultCols(hStmt, &colCount));
- test.chk(HStmt(hStmt), colCount == 0, "got %d != 0", (int)colCount);
- // execute
- for (unsigned k = 0; k < opt.m_scale; k++) {
- if (k % 5 == 0) {
- unsigned j = 0;
- Fld& fld = row.m_fldList[tab.m_nkIndex[j]];
- const Col& col = fld.m_col;
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + j, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, fld.caddr(), col.csize(), fld.ind()));
- }
- row.calcPk(test, k);
- row.calcNk(test);
- test.run(HStmt(hStmt), SQLExecute(hStmt));
- test.chk(HStmt(hStmt), test.m_functionCode == SQL_DIAG_UPDATE_WHERE, "got %d != %d", test.m_functionCode, SQL_DIAG_UPDATE_WHERE);
- chkRowCount(test, hStmt, 1);
- // direct update, no read has been necessary
- chkTuplesFetched(test, hStmt, 0);
- }
- test.timerCnt(opt.m_scale);
- if (opt.m_v >= 3)
- ndbout << "updated " << opt.m_scale << " in " << tab.m_name << endl;
- }
- freeAll(test, hEnv, hDbc, hStmtList, tabCount);
- }
- static void
- testUpdateScan(Test& test)
- {
- SQLHANDLE hEnv, hDbc, hStmtList[tabCount];
- allocAll(test, hEnv, hDbc, hStmtList, tabCount);
- char sql[MAX_SQL], *sqlptr;
- for (unsigned i = 0; i < tabCount; i++) {
- SQLHANDLE& hStmt = hStmtList[i];
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- // prepare
- tab.updateRange(sqlptr = sql);
- if (opt.m_v >= 2)
- ndbout << "SQL: " << sql << endl;
- test.run(HStmt(hStmt), SQLPrepare(hStmt, (SQLCHAR*)sql, SQL_NTS));
- // bind parameters
- Row row(tab); // for set clause
- Row rowlo(tab); // for pk ranges
- Row rowhi(tab);
- SQLSMALLINT parCount = -1;
- test.run(HStmt(hStmt), SQLNumParams(hStmt, &parCount));
- test.chk(HStmt(hStmt), parCount == tab.m_nkCount + 2 * tab.m_pkCount, "got %d != %d", (int)parCount, (int)tab.m_nkCount + 2 * (int)tab.m_pkCount);
- for (unsigned j = 0; j < tab.m_nkCount; j++) {
- const Col& col = tab.m_colList[tab.m_nkIndex[j]];
- Fld& fld = row.m_fldList[tab.m_nkIndex[j]];
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + j, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, fld.caddr(), col.csize(), fld.ind()));
- }
- bool canInterp = true;
- for (unsigned j = 0; j < tab.m_pkCount; j++) {
- const Col& col = tab.m_colList[tab.m_pkIndex[j]];
- Fld& fldlo = rowlo.m_fldList[tab.m_pkIndex[j]];
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + tab.m_nkCount + 2 * j + 0, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, fldlo.caddr(), col.csize(), fldlo.ind()));
- Fld& fldhi = rowhi.m_fldList[tab.m_pkIndex[j]];
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + tab.m_nkCount + 2 * j + 1, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, fldhi.caddr(), col.csize(), fldhi.ind()));
- if (col.m_type != Col::Char)
- canInterp = false; // XXX no unsigned yet
- }
- // execute
- row.calcPk(test, 0);
- row.calcNk(test);
- rowlo.calcPk(test, 0);
- rowhi.calcPk(test, test.m_mul); // sucks
- test.run(HStmt(hStmt), SQLExecute(hStmt));
- test.chk(HStmt(hStmt), test.m_functionCode == SQL_DIAG_UPDATE_WHERE, "got %d != %d", test.m_functionCode, SQL_DIAG_UPDATE_WHERE);
- chkRowCount(test, hStmt, opt.m_scale);
- chkTuplesFetched(test, hStmt, canInterp ? opt.m_scale : opt.m_scale * opt.m_threads);
- test.timerCnt(opt.m_scale);
- if (opt.m_v >= 3)
- ndbout << "updated " << opt.m_scale << " in " << tab.m_name << endl;
- }
- freeAll(test, hEnv, hDbc, hStmtList, tabCount);
- }
- // verify
- static void
- testVerifyPk(Test& test)
- {
- SQLHANDLE hEnv, hDbc, hStmtList[tabCount];
- allocAll(test, hEnv, hDbc, hStmtList, tabCount);
- char sql[MAX_SQL], *sqlptr;
- for (unsigned i = 0; i < tabCount; i++) {
- SQLHANDLE& hStmt = hStmtList[i];
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- // prepare
- tab.selectPk(sqlptr = sql);
- if (opt.m_v >= 2)
- ndbout << "SQL: " << sql << endl;
- test.run(HStmt(hStmt), SQLPrepare(hStmt, (SQLCHAR*)sql, SQL_NTS));
- // use same row for input and output
- Row row(tab);
- // bind parameters
- SQLSMALLINT parCount = -1;
- test.run(HStmt(hStmt), SQLNumParams(hStmt, &parCount));
- test.chk(HStmt(hStmt), parCount == tab.m_pkCount, "got %d != %d", (int)parCount, (int)tab.m_pkCount);
- for (unsigned j = 0; j < tab.m_pkCount; j++) {
- Fld& fld = row.m_fldList[tab.m_pkIndex[j]];
- const Col& col = fld.m_col;
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + j, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, fld.caddr(), col.csize(), fld.ind()));
- }
- // bind columns
- SQLSMALLINT colCount = -1;
- test.run(HStmt(hStmt), SQLNumResultCols(hStmt, &colCount));
- test.chk(HStmt(hStmt), colCount == tab.m_colCount, "got %d != %d", (int)colCount, (int)tab.m_colCount);
- for (unsigned j = 0; j < tab.m_colCount; j++) {
- Fld& fld = row.m_fldList[j];
- const Col& col = fld.m_col;
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 1 + j, col.ctype(), fld.caddr(), col.csize(), fld.ind()));
- }
- // row for SQLGetData
- Row rowGet(tab);
- // reference row
- Row rowRef(tab);
- // execute
- for (unsigned k = 0; k < opt.m_scale; k++) {
- if (k % 5 == 0) {
- // rebind
- unsigned j = 0;
- Fld& fld = row.m_fldList[tab.m_pkIndex[j]];
- const Col& col = fld.m_col;
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + j, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, fld.caddr(), col.csize(), fld.ind()));
- }
- row.calcPk(test, k);
- test.run(HStmt(hStmt), SQLExecute(hStmt));
- test.chk(HStmt(hStmt), test.m_functionCode == SQL_DIAG_SELECT_CURSOR, "got %d != %d", test.m_functionCode, SQL_DIAG_SELECT_CURSOR);
- // fetch
- for (unsigned k2 = 0; ; k2++) {
- if (k2 == 1)
- test.exp(SQL_NO_DATA, 0, 0, true);
- test.run(HStmt(hStmt), SQLFetch(hStmt));
- chkTuplesFetched(test, hStmt, 1);
- if (k2 == 1)
- break;
- rowRef.calcPk(test, k);
- test.chk(HStmt(hStmt), row.verifyPk(test, rowRef), "verify row=%d", k);
- if (test.m_const)
- rowRef.calcPk(test, 0);
- rowRef.calcNk(test);
- test.chk(HStmt(hStmt), row.verifyNk(test, rowRef), "verify row=%d", k);
- // SQLGetData is supported independent of SQLBindCol
- if (opt.m_nogetd)
- continue;
- for (unsigned j = 0; j < tab.m_colCount; j++) {
- Fld& fld = rowGet.m_fldList[j];
- fld.zero();
- const Col& col = fld.m_col;
- // test both variants
- SQLSMALLINT ctype = k % 2 == 0 ? col.ctype() : SQL_ARD_TYPE;
- if (ctype != Col::CChar)
- test.run(HStmt(hStmt), SQLGetData(hStmt, 1 + j, ctype, fld.caddr(), col.csize(), fld.ind()));
- else {
- // get in pieces
- unsigned size = col.csize() - 1; // omit null terminator
- char* caddr = (char*)(fld.caddr());
- unsigned off = 0;
- while (off < size) {
- unsigned m = size / 3; // bytes to get
- if (m == 0)
- m = 1;
- if (m > size - off)
- m = size - off;
- bool getNull = (rowRef.m_fldList[j].m_ind == SQL_NULL_DATA);
- if (off + m < size && ! getNull)
- test.exp(SQL_SUCCESS_WITH_INFO, "01004", -1, true);
- // include null terminator in buffer size
- test.run(HStmt(hStmt), SQLGetData(hStmt, 1 + j, ctype, caddr + off, m + 1, fld.ind()));
- int ind = *fld.ind();
- if (getNull) {
- test.chk(HStmt(hStmt), ind == SQL_NULL_DATA, "got %d", ind);
- break;
- }
- test.chk(HStmt(hStmt), ind == size - off, "got %d != %u", ind, size - off);
- off += m;
- }
- }
- }
- rowRef.calcPk(test, k);
- test.chk(HStmt(hStmt), rowGet.verifyPk(test, rowRef), "verify row=%d", k);
- if (test.m_const)
- rowRef.calcPk(test, 0);
- rowRef.calcNk(test);
- test.chk(HStmt(hStmt), rowGet.verifyNk(test, rowRef), "verify row=%d", k);
- // SQLGetData again
- for (unsigned j = 0; j < tab.m_colCount; j++) {
- Fld& fld = rowGet.m_fldList[j];
- const Col& col = fld.m_col;
- // test both variants
- SQLSMALLINT ctype = k % 2 == 0 ? col.ctype() : SQL_ARD_TYPE;
- // expect no more data
- test.exp(SQL_NO_DATA, 0, 0, true);
- test.run(HStmt(hStmt), SQLGetData(hStmt, 1 + j, ctype, fld.caddr(), col.csize(), fld.ind()));
- }
- }
- test.run(HStmt(hStmt), SQLCloseCursor(hStmt));
- }
- test.timerCnt(opt.m_scale);
- if (opt.m_v >= 3)
- ndbout << "verified " << opt.m_scale << " from " << tab.m_name << endl;
- }
- freeAll(test, hEnv, hDbc, hStmtList, tabCount);
- }
- static void
- testVerifyScan(Test& test)
- {
- SQLHANDLE hEnv, hDbc, hStmtList[tabCount];
- allocAll(test, hEnv, hDbc, hStmtList, tabCount);
- char sql[MAX_SQL], *sqlptr;
- for (unsigned i = 0; i < tabCount; i++) {
- SQLHANDLE& hStmt = hStmtList[i];
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- // prepare
- tab.selectRange(sqlptr = sql, ! opt.m_nosort);
- if (opt.m_v >= 2)
- ndbout << "SQL: " << sql << endl;
- test.run(HStmt(hStmt), SQLPrepare(hStmt, (SQLCHAR*)sql, SQL_NTS));
- // bind parameters
- Row rowlo(tab); // use available PK fields..
- Row rowhi(tab); // since we have no other way for now
- SQLSMALLINT parCount = -1;
- test.run(HStmt(hStmt), SQLNumParams(hStmt, &parCount));
- test.chk(HStmt(hStmt), parCount == 2 * tab.m_pkCount, "got %d != %d", (int)parCount, 2 * (int)tab.m_pkCount);
- for (unsigned j = 0; j < tab.m_pkCount; j++) {
- const Col& col = tab.m_colList[tab.m_pkIndex[j]];
- Fld& fldlo = rowlo.m_fldList[tab.m_pkIndex[j]];
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + 2 * j + 0, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, fldlo.caddr(), col.csize(), fldlo.ind()));
- Fld& fldhi = rowhi.m_fldList[tab.m_pkIndex[j]];
- test.run(HStmt(hStmt), SQLBindParameter(hStmt, 1 + 2 * j + 1, SQL_PARAM_INPUT, col.ctype(), col.type(), col.size(), 0, fldhi.caddr(), col.csize(), fldhi.ind()));
- }
- // bind columns
- Row row(tab);
- SQLSMALLINT colCount = -1;
- test.run(HStmt(hStmt), SQLNumResultCols(hStmt, &colCount));
- test.chk(HStmt(hStmt), colCount == tab.m_colCount, "got %d != %d", (int)colCount, (int)tab.m_colCount);
- for (unsigned j = 0; j < tab.m_colCount; j++) {
- Fld& fld = row.m_fldList[j];
- const Col& col = fld.m_col;
- test.run(HStmt(hStmt), SQLBindCol(hStmt, 1 + j, col.ctype(), fld.caddr(), col.csize(), fld.ind()));
- }
- // execute
- rowlo.calcPk(test, 0);
- rowhi.calcPk(test, test.m_mul); // sucks
- test.run(HStmt(hStmt), SQLExecute(hStmt));
- test.chk(HStmt(hStmt), test.m_functionCode == SQL_DIAG_SELECT_CURSOR, "got %d != %d", test.m_functionCode, SQL_DIAG_SELECT_CURSOR);
- // reference row
- Row rowRef(tab);
- // fetch
- unsigned k = 0;
- SQLUINTEGER rowCount1 = (SQLUINTEGER)-1;
- test.run(HStmt(hStmt), SQLSetStmtAttr(hStmt, SQL_ATTR_ROWS_FETCHED_PTR, &rowCount1, SQL_IS_POINTER));
- while (1) {
- unsigned countExp;
- if (k == opt.m_scale) {
- countExp = k;
- test.exp(SQL_NO_DATA, 0, 0, true);
- } else {
- countExp = k + 1;
- }
- test.run(HStmt(hStmt), SQLFetch(hStmt));
- // let me count the ways..
- chkRowCount(test, hStmt, countExp);
- test.chk(HStmt(hStmt), rowCount1 == countExp, "got %lu != %u", rowCount1, countExp);
- SQLUINTEGER rowCount2 = (SQLUINTEGER)-1;
- test.run(HStmt(hStmt), SQLGetStmtAttr(hStmt, SQL_ATTR_ROW_NUMBER, &rowCount2, SQL_IS_POINTER, 0));
- test.chk(HStmt(hStmt), rowCount2 == countExp, "got %lu != %u", rowCount2, countExp);
- if (k == opt.m_scale)
- break;
- if (! opt.m_nosort) {
- // expecting k-th row
- rowRef.calcPk(test, k);
- test.chk(HStmt(hStmt), row.verifyPk(test, rowRef), "verify row=%d", k);
- if (test.m_const)
- rowRef.calcPk(test, 0);
- rowRef.calcNk(test);
- test.chk(HStmt(hStmt), row.verifyNk(test, rowRef), "verify row=%d", k);
- } else {
- // expecting random row
- rowRef.copy(row);
- test.chk(HStmt(hStmt), row.verifyNk(test, rowRef), "verify row=%d", k);
- }
- k++;
- }
- test.timerCnt(opt.m_scale);
- if (opt.m_v >= 3)
- ndbout << "verified " << opt.m_scale << " from " << tab.m_name << endl;
- }
- freeAll(test, hEnv, hDbc, hStmtList, tabCount);
- }
- // self-join (scan followed by pk lookups)
- static void
- testJoin(Test& test)
- {
- SQLHANDLE hEnv, hDbc, hStmtList[tabCount];
- allocAll(test, hEnv, hDbc, hStmtList, tabCount);
- char sql[MAX_SQL], *sqlptr;
- for (unsigned cnt = opt.m_depth; cnt <= opt.m_depth; cnt++) {
- for (unsigned i = 0; i < tabCount; i++) {
- SQLHANDLE& hStmt = hStmtList[i];
- Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- tab.selectJoin(sqlptr = sql, cnt);
- if (opt.m_v >= 2)
- ndbout << "SQL: " << sql << endl;
- test.run(HStmt(hStmt), SQLExecDirect(hStmt, (SQLCHAR*)sql, SQL_NTS));
- }
- unsigned k = 0;
- while (1) {
- for (unsigned i = 0; i < tabCount; i++) {
- SQLHANDLE& hStmt = hStmtList[i];
- const Tab& tab = tabList[i];
- if (! tab.optok())
- continue;
- if (k == opt.m_scale * opt.m_threads)
- test.exp(SQL_NO_DATA, 0, 0, true);
- test.run(HStmt(hStmt), SQLFetch(hStmt));
- if (k == opt.m_scale * opt.m_threads) {
- chkTuplesFetched(test, hStmt, k * opt.m_depth);
- test.run(HStmt(hStmt), SQLCloseCursor(hStmt));
- } else {
- chkTuplesFetched(test, hStmt, (k + 1) * opt.m_depth);
- test.timerCnt(1);
- }
- }
- if (k == opt.m_scale * opt.m_threads)
- break;