NCBI C++ ToolKit
type.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 struct type
6 {
8  const char *name;
9  unsigned flags;
10 };
11 
12 #define FLAG_C 1
13 #define FLAG_SQL 2
14 
15 #define TYPE_C(s) {s, #s, FLAG_C }
16 #define TYPE_SQL(s) {s, #s, FLAG_SQL }
17 #define TYPE_BOTH(s,s2) {s, #s, FLAG_SQL|FLAG_C }
18 /*
19  * Same define with test for constants
20  * #define TYPE_BOTH(s,s2) {s, #s, (FLAG_SQL|FLAG_C)+((1/!(s-s2))-1) }
21  */
22 static const struct type types[] = {
32 /* MS ODBC do not support SQL_TIMESTAMP for IPD type while we support it */
33 /* TYPE_C(SQL_C_TIMESTAMP), */
63 
71  {0, NULL}
72 };
73 
74 static const char *
76 {
77  const struct type *p = types;
78 
79  for (; p->name; ++p)
80  if (p->type == type)
81  return p->name;
82  return "(unknown)";
83 }
84 
85 static int result = 0;
86 
87 static void
88 check_msg(int check, const char *msg)
89 {
90  if (check)
91  return;
92  fprintf(stderr, "Check failed: %s\n", msg);
93  result = 1;
94 }
95 
96 int
97 main(int argc, char **argv)
98 {
99  const struct type *p;
100  char buf[16];
101  SQLINTEGER ind;
102  SQLLEN lind;
103  SQLHDESC desc;
104 
105  odbc_connect();
106 
107  /*
108  * test setting two time a descriptor
109  * success all user allocated are ARD or APD so type cheching can be done
110  * TODO freeing descriptor dissociate it from statements
111  */
112 
113  /* test C types */
114  for (p = types; p->name; ++p) {
115  if (SQL_SUCCEEDED
116  (SQLBindParameter(odbc_stmt, 1, SQL_PARAM_INPUT, p->type, SQL_VARCHAR, (SQLUINTEGER) (-1), 0, buf, 16, &lind))) {
117  SQLSMALLINT concise_type, type, code;
118  SQLHDESC desc;
119 
120  concise_type = type = code = 0;
121 
122  /* get APD */
123  SQLGetStmtAttr(odbc_stmt, SQL_ATTR_APP_PARAM_DESC, &desc, sizeof(desc), &ind);
124 
125  SQLGetDescField(desc, 1, SQL_DESC_TYPE, &type, sizeof(SQLSMALLINT), &ind);
126  SQLGetDescField(desc, 1, SQL_DESC_CONCISE_TYPE, &concise_type, sizeof(SQLSMALLINT), &ind);
128  printf("Setted type %s -> [%d (%s), %d (%s), %d]\n",
129  p->name, (int) concise_type, get_type_name(concise_type), (int) type, get_type_name(type), code);
130  check_msg(p->flags & FLAG_C, "Type not C successed to be set in APD");
131  } else {
132  SQLSMALLINT concise_type, type, code;
133  SQLHDESC desc;
134 
135  concise_type = type = code = 0;
136 
137  fprintf(stderr, "Error setting type %d (%s)\n", (int) p->type, p->name);
138 
139  concise_type = p->type;
140  SQLGetStmtAttr(odbc_stmt, SQL_ATTR_APP_PARAM_DESC, &desc, sizeof(desc), &ind);
141  if (SQL_SUCCEEDED
142  (SQLSetDescField(desc, 1, SQL_DESC_CONCISE_TYPE, int2ptr(concise_type), sizeof(SQLSMALLINT))))
143  {
144  SQLGetDescField(desc, 1, SQL_DESC_TYPE, &type, sizeof(SQLSMALLINT), &ind);
145  SQLGetDescField(desc, 1, SQL_DESC_CONCISE_TYPE, &concise_type, sizeof(SQLSMALLINT), &ind);
147  printf("Setted type %s -> [%d (%s), %d (%s), %d]\n",
148  p->name,
149  (int) concise_type, get_type_name(concise_type), (int) type, get_type_name(type), code);
150  check_msg(p->flags & FLAG_C, "Type not C successed to be set in APD");
151  } else {
152  check_msg(!(p->flags & FLAG_C), "Type C failed to be set in APD");
153  }
154  }
155  }
156 
157  printf("\n\n");
158 
159  /* test SQL types */
160  SQLGetStmtAttr(odbc_stmt, SQL_ATTR_IMP_PARAM_DESC, &desc, sizeof(desc), &ind);
161  for (p = types; p->name; ++p) {
162  SQLSMALLINT concise_type = p->type;
163 
164  if (SQL_SUCCEEDED
165  (SQLSetDescField(desc, 1, SQL_DESC_CONCISE_TYPE, int2ptr(concise_type), sizeof(SQLSMALLINT)))) {
166  SQLSMALLINT concise_type, type, code;
167 
168  concise_type = type = code = 0;
169 
170  SQLGetDescField(desc, 1, SQL_DESC_TYPE, &type, sizeof(SQLSMALLINT), &ind);
171  SQLGetDescField(desc, 1, SQL_DESC_CONCISE_TYPE, &concise_type, sizeof(SQLSMALLINT), &ind);
173  printf("Setted type %s -> [%d (%s), %d (%s), %d]\n",
174  p->name, (int) concise_type, get_type_name(concise_type), (int) type, get_type_name(type), code);
175  check_msg(p->flags & FLAG_SQL, "Type not SQL successed to be set in IPD");
176  } else {
177  fprintf(stderr, "Error setting type %d (%s)\n", (int) p->type, p->name);
178  check_msg(!(p->flags & FLAG_SQL), "Type SQL failed to be set in IPD");
179  }
180  }
181 
182  odbc_disconnect();
183 
184  return result;
185 }
#define int2ptr(i)
Definition: common.h:101
#define SQLLEN
Definition: odbc.h:52
static SQLRETURN odbc_connect(TDS_DBC *dbc, TDSLOGIN *login)
Definition: odbc.c:356
HSTMT odbc_stmt
Definition: common.c:33
int odbc_disconnect(void)
Definition: common.c:290
#define check(s)
Definition: describecol2.c:21
static int type
Definition: getdata.c:31
#define TYPE_SQL(s)
Definition: type.c:16
#define FLAG_SQL
Definition: type.c:13
static const struct type types[]
Definition: type.c:22
int main(int argc, char **argv)
Definition: type.c:97
static const char * get_type_name(SQLSMALLINT type)
Definition: type.c:75
static void check_msg(int check, const char *msg)
Definition: type.c:88
#define TYPE_C(s)
Definition: type.c:15
static int result
Definition: type.c:85
#define FLAG_C
Definition: type.c:12
#define TYPE_BOTH(s, s2)
Definition: type.c:17
#define NULL
Definition: ncbistd.hpp:225
char * buf
static SLJIT_INLINE sljit_ins msg(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)
#define SQL_REAL
Definition: sql.h:173
#define SQL_FLOAT
Definition: sql.h:172
SQLRETURN SQLGetDescField(SQLHDESC DescriptorHandle, SQLSMALLINT RecNumber, SQLSMALLINT FieldIdentifier, SQLPOINTER Value, SQLINTEGER BufferLength, SQLINTEGER *StringLength)
Definition: odbc_export.h:704
#define SQL_CHAR
Definition: sql.h:167
#define SQL_ATTR_APP_PARAM_DESC
Definition: sql.h:84
#define SQL_ATTR_IMP_PARAM_DESC
Definition: sql.h:86
#define SQL_DESC_DATETIME_INTERVAL_CODE
Definition: sql.h:105
#define SQL_TYPE_TIMESTAMP
Definition: sql.h:184
#define SQL_DESC_TYPE
Definition: sql.h:100
#define SQL_SMALLINT
Definition: sql.h:171
#define SQL_NUMERIC
Definition: sql.h:168
#define SQL_TYPE_DATE
Definition: sql.h:182
#define SQL_DECIMAL
Definition: sql.h:169
#define SQL_INTEGER
Definition: sql.h:170
SQLRETURN SQLGetStmtAttr(SQLHSTMT StatementHandle, SQLINTEGER Attribute, SQLPOINTER Value, SQLINTEGER BufferLength, SQLINTEGER *StringLength)
#define SQL_SUCCEEDED(rc)
Definition: sql.h:40
#define SQL_DOUBLE
Definition: sql.h:174
#define SQL_VARCHAR
Definition: sql.h:178
SQLRETURN SQLSetDescField(SQLHDESC DescriptorHandle, SQLSMALLINT RecNumber, SQLSMALLINT FieldIdentifier, SQLPOINTER Value, SQLINTEGER BufferLength)
Definition: odbc_export.h:764
#define SQL_C_INTERVAL_MONTH
Definition: sqlext.h:533
#define SQL_C_INTERVAL_SECOND
Definition: sqlext.h:537
#define SQL_C_INTERVAL_HOUR
Definition: sqlext.h:535
#define SQL_PARAM_INPUT
Definition: sqlext.h:1852
#define SQL_C_INTERVAL_DAY
Definition: sqlext.h:534
#define SQL_C_DOUBLE
Definition: sqlext.h:515
#define SQL_C_TYPE_TIMESTAMP
Definition: sqlext.h:531
SQLRETURN SQLBindParameter(SQLHSTMT hstmt, SQLUSMALLINT ipar, SQLSMALLINT fParamType, SQLSMALLINT fCType, SQLSMALLINT fSqlType, SQLUINTEGER cbColDef, SQLSMALLINT ibScale, SQLPOINTER rgbValue, SQLINTEGER cbValueMax, SQLINTEGER *pcbValue)
#define SQL_C_INTERVAL_MINUTE
Definition: sqlext.h:536
#define SQL_LONGVARBINARY
Definition: sqlext.h:435
#define SQL_LONGVARCHAR
Definition: sqlext.h:432
#define SQL_C_TYPE_DATE
Definition: sqlext.h:529
#define SQL_C_ULONG
Definition: sqlext.h:556
#define SQL_C_TINYINT
Definition: sqlext.h:552
#define SQL_C_SLONG
Definition: sqlext.h:553
#define SQL_C_INTERVAL_HOUR_TO_SECOND
Definition: sqlext.h:543
#define SQL_C_GUID
Definition: sqlext.h:567
#define SQL_C_TYPE_TIME
Definition: sqlext.h:530
#define SQL_C_BINARY
Definition: sqlext.h:546
#define SQL_C_DEFAULT
Definition: sqlext.h:519
#define SQL_C_SHORT
Definition: sqlext.h:513
#define SQL_C_LONG
Definition: sqlext.h:512
#define SQL_C_STINYINT
Definition: sqlext.h:555
#define SQL_C_TIME
Definition: sqlext.h:526
#define SQL_C_FLOAT
Definition: sqlext.h:514
#define SQL_DESC_CONCISE_TYPE
Definition: sqlext.h:393
#define SQL_TINYINT
Definition: sqlext.h:437
#define SQL_BINARY
Definition: sqlext.h:433
#define SQL_C_SSHORT
Definition: sqlext.h:554
#define SQL_VARBINARY
Definition: sqlext.h:434
#define SQL_C_BIT
Definition: sqlext.h:547
#define SQL_C_UBIGINT
Definition: sqlext.h:550
#define SQL_C_INTERVAL_YEAR_TO_MONTH
Definition: sqlext.h:538
#define SQL_C_UTINYINT
Definition: sqlext.h:558
#define SQL_C_INTERVAL_HOUR_TO_MINUTE
Definition: sqlext.h:542
#define SQL_BIT
Definition: sqlext.h:438
#define SQL_C_INTERVAL_YEAR
Definition: sqlext.h:532
#define SQL_C_DATE
Definition: sqlext.h:525
#define SQL_C_INTERVAL_DAY_TO_MINUTE
Definition: sqlext.h:540
#define SQL_C_USHORT
Definition: sqlext.h:557
#define SQL_C_SBIGINT
Definition: sqlext.h:549
#define SQL_C_TIMESTAMP
Definition: sqlext.h:527
#define SQL_C_INTERVAL_DAY_TO_SECOND
Definition: sqlext.h:541
#define SQL_GUID
Definition: sqlext.h:440
#define SQL_C_NUMERIC
Definition: sqlext.h:517
#define SQL_C_INTERVAL_MINUTE_TO_SECOND
Definition: sqlext.h:544
#define SQL_BIGINT
Definition: sqlext.h:436
#define SQL_C_CHAR
Definition: sqlext.h:511
#define SQL_C_INTERVAL_DAY_TO_HOUR
Definition: sqlext.h:539
#define SQL_TIMESTAMP
Definition: sqlext.h:431
unsigned long SQLUINTEGER
Definition: sqltypes.h:177
long SQLINTEGER
Definition: sqltypes.h:176
signed short int SQLSMALLINT
Definition: sqltypes.h:201
SQLHANDLE SQLHDESC
Definition: sqltypes.h:217
Definition: inftrees.h:24
Definition: type.c:6
const char * name
Definition: type.c:8
unsigned flags
Definition: type.c:9
SQLSMALLINT type
Definition: type.c:7
Modified on Fri Sep 20 14:57:09 2024 by modify_doxy.py rev. 669887