NCBI C++ ToolKit
typeinfo.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 static void
6 TestName(int index, const char *expected_name)
7 {
9  SQLTCHAR name[128];
10  char buf[256];
12 
13 #define NAME_TEST \
14  do { \
15  if (strcmp(C(name), expected_name) != 0) \
16  { \
17  sprintf(buf, "wrong name in column %d expected '%s' got '%s'", index, expected_name, C(name)); \
18  ODBC_REPORT_ERROR(buf); \
19  } \
20  } while(0)
21 
22  /* retrieve with SQLDescribeCol */
23  CHKDescribeCol(index, name, ODBC_VECTOR_SIZE(name), &len, &type, NULL, NULL, NULL, "S");
24  NAME_TEST;
25 
26  /* retrieve with SQLColAttribute */
27  CHKColAttribute(index, SQL_DESC_NAME, name, sizeof(name), &len, NULL, "S");
29  NAME_TEST;
30  CHKColAttribute(index, SQL_DESC_LABEL, name, sizeof(name), &len, NULL, "S");
31  NAME_TEST;
32  ODBC_FREE();
33 }
34 
35 static void
37 {
38  while (CHKFetch("SNo") == SQL_SUCCESS)
39  ;
40 
41  /* Sybase store procedure seems to return extra empty results */
42  while (CHKMoreResults("SNo") == SQL_SUCCESS)
43  ;
44 }
45 
46 static void
48 {
49  SQLSMALLINT out_type;
50  SQLLEN ind;
51  SQLRETURN RetCode;
52 
53  printf("CheckType %d\n", line);
54  CHKBindCol(2, SQL_C_SSHORT, &out_type, 0, &ind, "SI");
55  CHKGetTypeInfo(type, "SI");
56  RetCode = CHKFetch("SNo");
57  switch (RetCode) {
58  case SQL_SUCCESS:
59  if (expected == SQL_UNKNOWN_TYPE) {
60  fprintf(stderr, "Data not expected (type %d - %s) line %d\n", type, string_type, line);
62  exit(1);
63  }
64  if (expected != out_type) {
65  fprintf(stderr, "Got type %d expected %d. Input type %d - %s line %d\n", out_type, expected, type, string_type, line);
67  exit(1);
68  }
69  break;
70  case SQL_NO_DATA:
71  if (expected != SQL_UNKNOWN_TYPE) {
72  fprintf(stderr, "Data expected. Input type %d - %s line %d\n", type, string_type, line);
74  exit(1);
75  }
76  break;
77  }
78 
80 
82 }
83 
84 static void
85 DoTest(int version3)
86 {
87  char name[128], params[128];
88  SQLSMALLINT type, is_unsigned;
89  SQLINTEGER col_size, min_scale;
90  SQLLEN ind1, ind2, ind3, ind4, ind5, ind6;
91  int date_time_supported = 0;
92  int name_version3;
93  int tdsver;
94 
95  odbc_use_version3 = version3;
96  name_version3 = version3;
97 
98  odbc_connect();
99 
100  printf("Using ODBC version %d\n", version3 ? 3 : 2);
101 
102  tdsver = odbc_tds_version();
103 
104  /* test column name */
105  /* MS ODBC use always ODBC 3 names even in ODBC 2 mode */
106  if (!odbc_driver_is_freetds())
107  name_version3 = 1;
109  TestName(1, "TYPE_NAME");
110  TestName(2, "DATA_TYPE");
111  TestName(3, name_version3 ? "COLUMN_SIZE" : "PRECISION");
112  TestName(4, "LITERAL_PREFIX");
113  TestName(5, "LITERAL_SUFFIX");
114  TestName(6, "CREATE_PARAMS");
115  TestName(7, "NULLABLE");
116  TestName(8, "CASE_SENSITIVE");
117  TestName(9, "SEARCHABLE");
118  TestName(10, "UNSIGNED_ATTRIBUTE");
119  TestName(11, name_version3 ? "FIXED_PREC_SCALE" : "MONEY");
120  TestName(12, name_version3 ? "AUTO_UNIQUE_VALUE" : "AUTO_INCREMENT");
121  TestName(13, "LOCAL_TYPE_NAME");
122  TestName(14, "MINIMUM_SCALE");
123  TestName(15, "MAXIMUM_SCALE");
124 
125  /* TODO test these column for ODBC 3 */
126  /* ODBC 3.0 SQL_DATA_TYPE SQL_DATETIME_SUB NUM_PREC_RADIX INTERVAL_PRECISION */
127 
128  Flushodbc_stmt();
129 
130  /* TODO test if SQL_ALL_TYPES returns right numeric type for timestamp */
131 
132  /* numeric type for data */
133 
134  /* test for date/time support */
135  if (odbc_command_with_result(odbc_stmt, "select cast(getdate() as date)") == SQL_SUCCESS)
136  date_time_supported = 1;
137  if (odbc_db_is_microsoft() && tdsver < 0x703)
138  date_time_supported = 0;
140 
141 #define CHECK_TYPE(in,out) CheckType(in, out, #in, __LINE__)
142 
143  /* under Sybase this type require extra handling, check it */
145 
146  if (version3) {
147  /* MS ODBC returns S1004 (HY004), TODO support it */
148  CHECK_TYPE(SQL_TYPE_DATE, date_time_supported ? SQL_TYPE_DATE : SQL_UNKNOWN_TYPE);
149  if (!odbc_db_is_microsoft())
150  CHECK_TYPE(SQL_TYPE_TIME, date_time_supported ? SQL_TYPE_TIME : SQL_UNKNOWN_TYPE);
151  /* MS ODBC returns S1004 (HY004), TODO support it */
153  } else {
154  CHECK_TYPE(SQL_DATE, date_time_supported ? SQL_DATE : SQL_UNKNOWN_TYPE);
155  if (!odbc_db_is_microsoft())
156  CHECK_TYPE(SQL_TIME, date_time_supported ? SQL_TIME : SQL_UNKNOWN_TYPE);
157  /* TODO MS ODBC handle SQL_TIMESTAMP even for ODBC 3 */
159  }
160 
161  /* TODO implement this part of test */
162  /* varchar/nvarchar before sysname */
163 
164  /* test binding (not all column, required for Oracle) */
166  CHKBindCol(1, SQL_C_CHAR, name, sizeof(name), &ind1, "SI");
167  CHKBindCol(2, SQL_C_SSHORT, &type, 0, &ind2, "SI");
168  CHKBindCol(3, SQL_C_SLONG, &col_size, 0, &ind3, "SI");
169  CHKBindCol(6, SQL_C_CHAR, params, sizeof(params), &ind4, "SI");
170  CHKBindCol(10, SQL_C_SSHORT, &is_unsigned, 0, &ind5, "SI");
171  CHKBindCol(14, SQL_C_SSHORT, &min_scale, 0, &ind6, "SI");
172  while (CHKFetch("SNo") == SQL_SUCCESS)
173  ;
175  Flushodbc_stmt();
176 
177  /* check WVARCHAR for no pending data */
178  if (odbc_db_is_microsoft() || strncmp(odbc_db_version(), "15.00.", 6) >= 0) {
180  CHKFetch("S");
181  /* mssql returns SYSNAME as NVARCHAR */
182  if (odbc_db_is_microsoft())
183  CHKFetch("S");
184 
185  /* mssql 2008 can return a lot of NVARCHAR as new type (ie DATE)
186  * are converted automatically to NVARCHAR with former protocol
187  */
188  if (!odbc_db_is_microsoft() || tdsver >= 0x703 || odbc_db_version_int() < 0x0a000000)
189  CHKFetch("No");
190  CHKMoreResults("No");
191 
192  CHKGetTypeInfo(SQL_BINARY, "SI");
193  }
194 
195  odbc_disconnect();
196 }
197 
198 int
199 main(int argc, char *argv[])
200 {
201  DoTest(0);
202 
203  DoTest(1);
204 
205  printf("Done.\n");
206  return 0;
207 }
static SQLINTEGER ind1
Definition: compute.c:14
static SQLINTEGER ind2
Definition: compute.c:14
#define CHKMoreResults(res)
Definition: common.h:138
#define CHKGetTypeInfo(a, res)
Definition: common.h:134
#define CHKBindCol(a, b, c, d, e, res)
Definition: common.h:94
#define CHKColAttribute(a, b, c, d, e, f, res)
Definition: common.h:102
#define CHKDescribeCol(a, b, c, d, e, f, g, h, res)
Definition: common.h:104
#define ODBC_FREE()
Definition: common.h:218
#define ODBC_VECTOR_SIZE(x)
Definition: common.h:188
#define CHKFetch(res)
Definition: common.h:118
static int type
Definition: getdata.c:31
#define NULL
Definition: ncbistd.hpp:225
exit(2)
char * buf
int len
std::string string_type
The stringtype used by the parser.
Definition: muParserDef.h:234
int strncmp(const char *str1, const char *str2, size_t count)
Definition: odbc_utils.hpp:133
static const char * expected[]
Definition: bcp.c:42
HSTMT odbc_stmt
Definition: common.c:33
const char * odbc_db_version(void)
Definition: common.c:468
unsigned int odbc_db_version_int(void)
Definition: common.c:483
int odbc_disconnect(void)
Definition: common.c:290
int odbc_use_version3
Definition: common.c:34
int odbc_db_is_microsoft(void)
Definition: common.c:325
int odbc_tds_version(void)
Definition: common.c:444
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 SQLLEN
Definition: odbc.h:52
#define SQL_UNBIND
Definition: sql.h:255
#define SQL_SUCCESS
Definition: sql.h:31
SQLRETURN SQLFreeStmt(SQLHSTMT StatementHandle, SQLUSMALLINT Option)
Definition: odbc.c:4237
#define SQL_UNKNOWN_TYPE
Definition: sql.h:166
#define SQL_TYPE_TIMESTAMP
Definition: sql.h:184
#define SQL_TYPE_DATE
Definition: sql.h:182
#define SQL_DESC_NAME
Definition: sql.h:109
#define SQL_TYPE_TIME
Definition: sql.h:183
SQLRETURN SQLCloseCursor(SQLHSTMT StatementHandle)
Definition: odbc.c:4245
#define SQL_ALL_TYPES
Definition: sql.h:195
#define SQL_VARCHAR
Definition: sql.h:178
#define SQL_NO_DATA
Definition: sql.h:34
#define SQL_DESC_LABEL
Definition: sqlext.h:397
#define SQL_C_SLONG
Definition: sqlext.h:553
#define SQL_BINARY
Definition: sqlext.h:433
#define SQL_C_SSHORT
Definition: sqlext.h:554
#define SQL_DATE
Definition: sqlext.h:426
#define SQL_TIME
Definition: sqlext.h:430
#define SQL_C_CHAR
Definition: sqlext.h:511
#define SQL_TIMESTAMP
Definition: sqlext.h:431
SQLCHAR SQLTCHAR
Definition: sqltypes.h:463
long SQLINTEGER
Definition: sqltypes.h:176
signed short int SQLSMALLINT
Definition: sqltypes.h:201
SQLSMALLINT SQLRETURN
Definition: sqltypes.h:210
#define SQL_WVARCHAR
Definition: sqlucode.h:15
Definition: type.c:6
int main(int argc, char *argv[])
Definition: typeinfo.c:199
#define NAME_TEST
static void Flushodbc_stmt(void)
Definition: typeinfo.c:36
static void TestName(int index, const char *expected_name)
Definition: typeinfo.c:6
static void CheckType(SQLSMALLINT type, SQLSMALLINT expected, const char *string_type, int line)
Definition: typeinfo.c:47
static void DoTest(int version3)
Definition: typeinfo.c:85
#define CHECK_TYPE(in, out)
Modified on Thu Feb 22 17:09:54 2024 by modify_doxy.py rev. 669887