NCBI C++ ToolKit
array.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #include "common.h"
2 
3 #include <common/test_assert.h>
4 
5 /* Test using array binding */
6 
8 static int multiply = 90;
9 static int failure = 0;
10 
11 #define XMALLOC_N(t, n) (t*) ODBC_GET(n*sizeof(t))
12 
13 static void
14 query_test(int prepare, SQLRETURN expected, const char *expected_status)
15 {
16 #define DESC_LEN 51
17 #define ARRAY_SIZE 10
18 
20  typedef SQLCHAR desc_t[DESC_LEN];
21  desc_t *descs = XMALLOC_N(desc_t, ARRAY_SIZE);
22  SQLLEN *id_lens = XMALLOC_N(SQLLEN,ARRAY_SIZE), *desc_lens = XMALLOC_N(SQLLEN,ARRAY_SIZE);
24  unsigned *num_errors = XMALLOC_N(unsigned, ARRAY_SIZE);
25  SQLULEN processed;
26  RETCODE ret;
27  char status[20];
28  SQLTCHAR *err = (SQLTCHAR *) ODBC_GET(sizeof(odbc_err)*sizeof(SQLTCHAR));
29  SQLTCHAR *state = (SQLTCHAR *) ODBC_GET(sizeof(odbc_sqlstate)*sizeof(SQLTCHAR));
30 
33 
34  odbc_command_with_result(odbc_stmt, "drop table #tmp1");
35  odbc_command("create table #tmp1 (id tinyint, value char(20))");
36 
43 
44  processed = ARRAY_SIZE + 1;
45  for (i = 0; i < ARRAY_SIZE; i++) {
46  statuses[i] = SQL_PARAM_DIAG_UNAVAILABLE;
47  ids[i] = (i + 1) * multiply;
48  sprintf((char *) descs[i], "data %d", i * 7);
49  id_lens[i] = 0;
50  desc_lens[i] = SQL_NTS;
51  num_errors[i] = 0;
52  }
53  multiply = 90;
54 
55  if (!prepare) {
57  } else {
59  ret = SQLExecute(odbc_stmt);
60  }
61 
62  if (processed > ARRAY_SIZE) {
63  char buf[256];
64 
65  sprintf(buf, "Invalid processed number: %d", (int) processed);
67  }
68 
69  for (i = 1; CHKGetDiagRec(SQL_HANDLE_STMT, odbc_stmt, i, state, NULL, err, sizeof(odbc_err), NULL, "SINo") != SQL_NO_DATA; ++i) {
70  SQLINTEGER row;
71 
72  strcpy(odbc_err, C(err));
73  strcpy(odbc_sqlstate, C(state));
75 
76  if (row == SQL_ROW_NUMBER_UNKNOWN) continue;
77  if (row < 1 || row > ARRAY_SIZE) {
78  fprintf(stderr, "invalid row %d returned reading error number %d\n", (int) row, i);
79  exit(1);
80  }
81  ++num_errors[row-1];
82  printf("for row %2d returned '%s' %s\n", (int) row, odbc_sqlstate, odbc_err);
83  }
84 
85  for (i = 0; i < processed; ++i) {
86  int has_diag = 0;
87 
88  switch (statuses[i]) {
90  has_diag = 1;
91  case SQL_PARAM_SUCCESS:
92  status[i] = 'V';
93  break;
94 
95  case SQL_PARAM_ERROR:
96  has_diag = 1;
97  status[i] = '!';
98  break;
99 
100  case SQL_PARAM_UNUSED:
101  status[i] = ' ';
102  break;
103 
105  status[i] = '?';
106  break;
107  default:
108  fprintf(stderr, "Invalid status returned %d\n", statuses[i]);
109  exit(1);
110  }
111 
112  if (has_diag) {
113  if (!num_errors[i]) {
114  fprintf(stderr, "Diagnostics not returned for status %d\n", i);
115  failure = 1;
116  }
117  } else {
118  if (num_errors[i]) {
119  fprintf(stderr, "Diagnostics returned for status %d\n", i);
120  failure = 1;
121  }
122  }
123  }
124  status[i] = 0;
125 
126  if (ret != expected || strcmp(expected_status, status) != 0) {
127  fprintf(stderr, "Invalid result: got %d \"%s\" expected %d \"%s\" processed %d\n",
128  ret, status, expected, expected_status, (int) processed);
129  if (ret != SQL_SUCCESS)
130  odbc_read_error();
131  failure = 1;
133  return;
134  }
135 
137 }
138 
139 int
140 main(int argc, char *argv[])
141 {
142  odbc_use_version3 = 1;
143  odbc_connect();
144 
145  if (odbc_db_is_microsoft()) {
146  /* all successes */
147  test_query = T("INSERT INTO #tmp1 (id, value) VALUES (?, ?)");
148  multiply = 1;
149  query_test(0, SQL_SUCCESS, "VVVVVVVVVV");
150  multiply = 1;
151  query_test(1, SQL_SUCCESS, "VVVVVVVVVV");
152 
153  /* all errors */
154  test_query = T("INSERT INTO #tmp1 (id, value) VALUES (?, ?)");
155  multiply = 257;
156  query_test(0, SQL_ERROR, "!!!!!!!!!!");
157  multiply = 257;
158  query_test(1, SQL_SUCCESS_WITH_INFO, "!!!!!!!!!!");
159 
160  test_query = T("INSERT INTO #tmp1 (id, value) VALUES (?, ?)");
161  query_test(0, SQL_ERROR, "VV!!!!!!!!");
162  query_test(1, SQL_SUCCESS_WITH_INFO, "VV!!!!!!!!");
163 
164  test_query = T("INSERT INTO #tmp1 (id, value) VALUES (900-?, ?)");
165  query_test(0, SQL_SUCCESS_WITH_INFO, "!!!!!!!VVV");
166  query_test(1, SQL_SUCCESS_WITH_INFO, "!!!!!!!VVV");
167 
168  test_query = T("INSERT INTO #tmp1 (id) VALUES (?) UPDATE #tmp1 SET value = ?");
169  query_test(0, SQL_SUCCESS_WITH_INFO, "VVVV!V!V!V");
170  query_test(1, SQL_SUCCESS_WITH_INFO, "VV!!!!!!!!");
171 
172 #ifdef ENABLE_DEVELOPING
173  /* with result, see how SQLMoreResult work */
174  test_query = T("INSERT INTO #tmp1 (id) VALUES (?) SELECT * FROM #tmp1 UPDATE #tmp1 SET value = ?");
175  /* IMHO our driver is better here -- freddy77 */
176  query_test(0, SQL_SUCCESS, odbc_driver_is_freetds() ? "VVVVV!V!V!" : "VVVVVV!VVV");
177  query_test(1, SQL_SUCCESS, "VVVVVVVVVV");
178 #endif
179  } else {
180  /* Sybase test for conversions before executing */
181  test_query = T("INSERT INTO #tmp1 (id, value) VALUES (?/8, ?)");
182  query_test(0, SQL_SUCCESS, "VVVVVVVVVV");
183  }
184 
185  /* TODO record binding, array fetch, sqlputdata */
186 
187  odbc_disconnect();
188 
189  printf(failure ? "Failed :(\n" : "Success!\n");
190  return failure;
191 }
192 
int main(int argc, char *argv[])
Definition: array.c:140
#define DESC_LEN
static int failure
Definition: array.c:9
static int multiply
Definition: array.c:8
#define ARRAY_SIZE
static void query_test(int prepare, SQLRETURN expected, const char *expected_status)
Definition: array.c:14
#define XMALLOC_N(t, n)
Definition: array.c:11
static SQLTCHAR * test_query
Definition: array.c:7
#define ODBC_GET(s)
Definition: common.h:217
#define odbc_reset_statement()
Definition: common.h:71
#define CHKGetDiagField(a, b, c, d, e, f, g, res)
Definition: common.h:130
#define C(s)
Definition: common.h:231
#define odbc_command(cmd)
Definition: common.h:179
#define ODBC_REPORT_ERROR(msg)
Definition: common.h:76
#define T(s)
Definition: common.h:230
#define CHKGetDiagRec(a, b, c, d, e, f, g, h, res)
Definition: common.h:128
#define NULL
Definition: ncbistd.hpp:225
exit(2)
char * buf
int i
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
static const char * expected[]
Definition: bcp.c:42
HSTMT odbc_stmt
Definition: common.c:33
char odbc_sqlstate[6]
Definition: common.c:635
int odbc_disconnect(void)
Definition: common.c:290
char odbc_err[512]
Definition: common.c:634
int odbc_use_version3
Definition: common.c:34
int odbc_db_is_microsoft(void)
Definition: common.c:325
void odbc_read_error(void)
Definition: common.c:638
int odbc_driver_is_freetds(void)
Definition: common.c:345
SQLRETURN odbc_command_with_result(HSTMT stmt, const char *command)
Definition: common.c:312
static SQLRETURN odbc_connect(TDS_DBC *dbc, TDSLOGIN *login)
Definition: odbc.c:356
#define SQLULEN
Definition: odbc.h:49
#define SQLLEN
Definition: odbc.h:52
SQLRETURN SQLExecDirect(SQLHSTMT StatementHandle, SQLCHAR *StatementText, SQLINTEGER TextLength)
Definition: odbc_export.h:812
SQLRETURN SQLPrepare(SQLHSTMT StatementHandle, SQLCHAR *StatementText, SQLINTEGER TextLength)
Definition: odbc_export.h:848
#define SQL_HANDLE_STMT
Definition: sql.h:66
#define SQL_NULL_HSTMT
Definition: sql.h:277
SQLRETURN SQLExecute(SQLHSTMT StatementHandle)
Definition: odbc.c:3558
#define SQL_SUCCESS
Definition: sql.h:31
#define SQL_INTEGER
Definition: sql.h:170
SQLRETURN SQLSetStmtAttr(SQLHSTMT StatementHandle, SQLINTEGER Attribute, SQLPOINTER Value, SQLINTEGER StringLength)
#define SQL_SUCCESS_WITH_INFO
Definition: sql.h:32
#define SQL_NTS
Definition: sql.h:49
#define SQL_VARCHAR
Definition: sql.h:178
#define SQL_ERROR
Definition: sql.h:36
#define SQL_NO_DATA
Definition: sql.h:34
#define SQL_PARAM_INPUT
Definition: sqlext.h:1852
SQLRETURN SQLBindParameter(SQLHSTMT hstmt, SQLUSMALLINT ipar, SQLSMALLINT fParamType, SQLSMALLINT fCType, SQLSMALLINT fSqlType, SQLUINTEGER cbColDef, SQLSMALLINT ibScale, SQLPOINTER rgbValue, SQLINTEGER cbValueMax, SQLINTEGER *pcbValue)
#define SQL_ATTR_PARAMS_PROCESSED_PTR
Definition: sqlext.h:269
#define SQL_PARAM_BIND_BY_COLUMN
Definition: sqlext.h:314
#define SQL_C_ULONG
Definition: sqlext.h:556
#define SQL_ATTR_PARAM_STATUS_PTR
Definition: sqlext.h:268
#define SQL_PARAM_SUCCESS_WITH_INFO
Definition: sqlext.h:1821
#define SQL_ROW_NUMBER_UNKNOWN
Definition: sqlext.h:607
#define SQL_ATTR_PARAM_BIND_TYPE
Definition: sqlext.h:266
#define SQL_PARAM_UNUSED
Definition: sqlext.h:1823
#define SQL_PARAM_ERROR
Definition: sqlext.h:1822
#define SQL_ATTR_PARAMSET_SIZE
Definition: sqlext.h:270
#define SQL_PARAM_DIAG_UNAVAILABLE
Definition: sqlext.h:1824
#define SQL_PARAM_SUCCESS
Definition: sqlext.h:1820
#define SQL_DIAG_ROW_NUMBER
Definition: sqlext.h:421
#define SQL_C_CHAR
Definition: sqlext.h:511
unsigned long SQLUINTEGER
Definition: sqltypes.h:177
unsigned short SQLUSMALLINT
Definition: sqltypes.h:202
SQLCHAR SQLTCHAR
Definition: sqltypes.h:463
long SQLINTEGER
Definition: sqltypes.h:176
unsigned char SQLCHAR
Definition: sqltypes.h:125
SQLSMALLINT SQLRETURN
Definition: sqltypes.h:210
#define assert(x)
Definition: srv_diag.hpp:58
int RETCODE
Definition: sybdb.h:121
Modified on Fri Jan 05 07:23:59 2024 by modify_doxy.py rev. 669887