NCBI C++ ToolKit
utf8.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 binding with UTF-8 encoding */
6 
7 #ifndef _WIN32
8 static void init_connect(void);
9 
10 static void
12 {
13  CHKAllocEnv(&odbc_env, "S");
16 }
17 
18 /* test table name, it contains two japanese characters */
19 static const char table_name[] = "mytab\xe7\x8e\x8b\xe9\xb4\xbb";
20 
21 static const char * const strings[] = {
22  /* ascii */
23  "aaa", "aaa",
24  /* latin 1*/
25  "abc\xc3\xa9\xc3\xa1\xc3\xb4", "abc\xc3\xa9\xc3\xae\xc3\xb4",
26  /* Japanese... */
27  "abc\xe7\x8e\x8b\xe9\xb4\xbb", "abc\xe7\x8e\x8b\xe9\xb4\xbb\xe5\x82\x91\xe7\x8e\x8b\xe9\xb4\xbb\xe5\x82\x91",
28  NULL, NULL
29 };
30 
31 /* same strings in hex */
32 static const char * const strings_hex[] = {
33  /* ascii */
34  "0x610061006100", "0x610061006100",
35  /* latin 1*/
36  "0x610062006300e900e100f400", "0x610062006300e900ee00f400",
37  /* Japanese... */
38  "0x6100620063008b733b9d", "0x6100620063008b733b9d91508b733b9d9150",
39  NULL, NULL
40 };
41 
42 static char tmp[3200];
43 
44 static void
46 {
47  const char * const*p;
48  SQLINTEGER n;
49  SQLLEN n_len;
50 
51  sprintf(tmp, "DELETE FROM %s", table_name);
53 
54  /* insert with SQLPrepare/SQLBindParameter/SQLExecute */
55  sprintf(tmp, "INSERT INTO %s VALUES(?,?,?)", table_name);
56  CHKPrepare(T(tmp), SQL_NTS, "S");
58  SQL_INTEGER, 0, 0, &n, 0, &n_len, "S");
59  n_len = sizeof(n);
60 
61  for (n = 1, p = strings; p[0] && p[1]; p += 2, ++n) {
62  SQLLEN s1_len, s2_len;
63  unsigned int len;
64 
65  len = minimum ? ((int) strlen(strings_hex[p-strings]) - 2) / 4
66  : 40;
68  SQL_WCHAR, len, 0, (void *) p[0], 0, &s1_len, "S");
69  len = minimum ? ((int)strlen(strings_hex[p+1-strings]) - 2) / 4
70  : 40;
71  /* FIXME this with SQL_VARCHAR produce wrong protocol data */
73  SQL_WVARCHAR, len, 0, (void *) p[1], 0, &s2_len, "S");
74  s1_len = strlen(p[0]);
75  s2_len = strlen(p[1]);
76  printf("insert #%d\n", (int) n);
77  CHKExecute("S");
78  }
79 
80  /* check rows */
81  for (n = 1, p = strings_hex; p[0] && p[1]; p += 2, ++n) {
82  sprintf(tmp, "IF NOT EXISTS(SELECT * FROM %s WHERE k = %d AND c = %s AND vc = %s) SELECT 1", table_name, (int) n, p[0], p[1]);
84  }
85 
87 }
88 
89 int
90 main(int argc, char *argv[])
91 {
93  const char * const*p;
94  SQLINTEGER n;
95 
97  exit(1);
98 
99  /* connect string using DSN */
100  init_connect();
101  sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;DATABASE=%s;ClientCharset=UTF-8;", odbc_server, odbc_user, odbc_password, odbc_database);
102  CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp)/sizeof(SQLTCHAR), &len, SQL_DRIVER_NOPROMPT, "SI");
103  if (!odbc_driver_is_freetds()) {
104  odbc_disconnect();
105  printf("Driver is not FreeTDS, exiting\n");
107  return 0;
108  }
109 
110  if (!odbc_db_is_microsoft() || odbc_db_version_int() < 0x08000000u) {
111  odbc_disconnect();
112  printf("Test for MSSQL only\n");
114  return 0;
115  }
116 
117  CHKAllocStmt(&odbc_stmt, "S");
118 
119  /* create test table */
120  sprintf(tmp, "IF OBJECT_ID(N'%s') IS NOT NULL DROP TABLE %s", table_name, table_name);
121  odbc_command(tmp);
122  sprintf(tmp, "CREATE TABLE %s (k int, c NCHAR(10), vc NVARCHAR(10))", table_name);
123  odbc_command(tmp);
124 
125  /* insert with INSERT statements */
126  for (n = 1, p = strings; p[0] && p[1]; p += 2, ++n) {
127  sprintf(tmp, "INSERT INTO %s VALUES (%d,N'%s',N'%s')", table_name, (int) n, p[0], p[1]);
128  odbc_command(tmp);
129  }
130 
131  /* check rows */
132  for (n = 1, p = strings_hex; p[0] && p[1]; p += 2, ++n) {
133  sprintf(tmp, "IF NOT EXISTS(SELECT * FROM %s WHERE k = %d AND c = %s AND vc = %s) SELECT 1", table_name, (int) n, p[0], p[1]);
135  }
136 
137  TestBinding(0);
138 
139  TestBinding(1);
140 
141  /* cleanup */
142  sprintf(tmp, "IF OBJECT_ID(N'%s') IS NOT NULL DROP TABLE %s", table_name, table_name);
143  odbc_command(tmp);
144 
145  odbc_disconnect();
146  printf("Done.\n");
147  return 0;
148 }
149 
150 #else
151 
152 int
153 main(void)
154 {
155  /* on Windows SQLExecDirect is always converted to SQLExecDirectW by the DM */
156  printf("Not possible for this platform.\n");
158  return 0;
159 }
160 #endif
#define CHKBindParameter(a, b, c, d, e, f, g, h, i, res)
Definition: common.h:96
#define CHKPrepare(a, b, res)
Definition: common.h:146
#define odbc_reset_statement()
Definition: common.h:71
#define CHKAllocStmt(a, res)
Definition: common.h:90
#define odbc_command(cmd)
Definition: common.h:179
#define CHKExecute(res)
Definition: common.h:114
#define CHKAllocConnect(a, res)
Definition: common.h:86
#define T(s)
Definition: common.h:230
#define CHKAllocEnv(a, res)
Definition: common.h:88
#define CHKDriverConnect(a, b, c, d, e, f, g, res)
Definition: common.h:108
#define SQLLEN
Definition: odbc.h:52
HSTMT odbc_stmt
Definition: common.c:33
unsigned int odbc_db_version_int(void)
Definition: common.c:483
void odbc_test_skipped(void)
Definition: common.c:539
char odbc_database[512]
Definition: common.c:40
int odbc_disconnect(void)
Definition: common.c:290
int odbc_read_login_info(void)
Definition: common.c:88
HENV odbc_env
Definition: common.c:31
char odbc_server[512]
Definition: common.c:38
int odbc_db_is_microsoft(void)
Definition: common.c:325
char odbc_password[512]
Definition: common.c:39
void odbc_check_no_row(const char *query)
Definition: common.c:883
HDBC odbc_conn
Definition: common.c:32
int odbc_driver_is_freetds(void)
Definition: common.c:345
char odbc_user[512]
Definition: common.c:37
static const char *const strings[]
Definition: utf8.c:21
int main(int argc, char *argv[])
Definition: utf8.c:90
static const char table_name[]
Definition: utf8.c:19
static char tmp[3200]
Definition: utf8.c:42
static void init_connect(void)
Definition: utf8.c:11
static void TestBinding(int minimum)
Definition: utf8.c:45
static const char *const strings_hex[]
Definition: utf8.c:32
#define NULL
Definition: ncbistd.hpp:225
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
exit(2)
yy_size_t n
int len
Int minimum(Int i, Int j)
Definition: njn_integer.hpp:51
SQLRETURN SQLSetEnvAttr(SQLHENV EnvironmentHandle, SQLINTEGER Attribute, SQLPOINTER Value, SQLINTEGER StringLength)
#define SQL_INTEGER
Definition: sql.h:170
#define SQL_NTS
Definition: sql.h:49
#define SQL_OV_ODBC3
Definition: sqlext.h:98
#define SQL_PARAM_INPUT
Definition: sqlext.h:1852
#define SQL_C_LONG
Definition: sqlext.h:512
#define SQL_IS_UINTEGER
Definition: sqlext.h:306
#define SQL_ATTR_ODBC_VERSION
Definition: sqlext.h:74
#define SQL_C_CHAR
Definition: sqlext.h:511
#define SQL_DRIVER_NOPROMPT
Definition: sqlext.h:1785
void * SQLPOINTER
Definition: sqltypes.h:195
SQLCHAR SQLTCHAR
Definition: sqltypes.h:463
long SQLINTEGER
Definition: sqltypes.h:176
signed short int SQLSMALLINT
Definition: sqltypes.h:201
#define SQL_WCHAR
Definition: sqlucode.h:14
#define SQL_WVARCHAR
Definition: sqlucode.h:15
Modified on Mon Apr 29 05:16:59 2024 by modify_doxy.py rev. 669887