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

Go to the SVN repository for this file.

1 #include "common.h"
2 #include <freetds/macros.h>
3 
4 #include <freetds/test_assert.h>
5 
6 /* test conversion of Hebrew characters (which have shift sequences) */
7 
8 static const char * const column_names[] = {
9  "hebrew",
10  "cn"
11 };
12 
13 typedef struct {
14  /* number of column */
15  int num;
16  /* hex representation, used during insert */
17  const char *hex;
18  /* output */
19  const char *out;
20 } column_t;
21 
22 static const column_t columns[] = {
23  { 0, "0xde05d905d305e205", "\xd7\x9e\xd7\x99\xd7\x93\xd7\xa2" },
24  { 0, "0x69006e0066006f00", "info", },
25  { 0, "0xd805e705e105d805", "\xd7\x98\xd7\xa7\xd7\xa1\xd7\x98", },
26  { 0, "0xd005d105db05", "\xd7\x90\xd7\x91\xd7\x9b", },
27  { 1, "0xf78b7353d153278d3a00228c228c56e02000",
28  "\xe8\xaf\xb7\xe5\x8d\xb3\xe5\x8f\x91\xe8\xb4\xa7\x3a\xe8\xb0\xa2\xe8\xb0\xa2\xee\x81\x96\x20", },
29  { 0, NULL, NULL },
30 };
31 
32 int
33 main(int argc, char *argv[])
34 {
35  char tmp[1024];
36  char out[TDS_VECTOR_SIZE(column_names)][32];
38  const column_t *p;
39  int n;
40 
42  odbc_conn_additional_params = "ClientCharset=UTF-8;";
43 
44  odbc_connect();
45  if (!odbc_driver_is_freetds()) {
47  printf("Driver is not FreeTDS, exiting\n");
49  return 0;
50  }
51 
52  if (!odbc_db_is_microsoft() || odbc_db_version_int() < 0x08000000u || odbc_tds_version() < 0x701) {
54  /* protocol till 7.1 does not support telling encoding so we
55  * cannot understand how the string is encoded
56  */
57  printf("Test for MSSQL only using protocol 7.1\n");
59  return 0;
60  }
61 
62  CHKAllocStmt(&odbc_stmt, "S");
63 
64  /* create test table */
65  odbc_command("CREATE TABLE #tmp (i INT"
66  ", hebrew VARCHAR(20) COLLATE Hebrew_CI_AI NULL"
67  ", cn VARCHAR(20) COLLATE Chinese_PRC_CI_AS NULL"
68  ")");
69 
70  /* insert with INSERT statements */
71  for (n = 0, p = columns; p[n].hex; ++n) {
72  sprintf(tmp, "INSERT INTO #tmp(i, %s) VALUES(%d, CAST(%s AS NVARCHAR(20)))",
73  column_names[p[n].num], n+1, p[n].hex);
75  }
76 
77  /* test conversions in libTDS */
78  odbc_command("SELECT hebrew, cn FROM #tmp ORDER BY i");
79 
80  /* insert with SQLPrepare/SQLBindParameter/SQLExecute */
81  for (n = 0; n < TDS_VECTOR_SIZE(column_names); ++n)
82  CHKBindCol(n+1, SQL_C_CHAR, out[n], sizeof(out[0]), &n_len[n], "S");
83  for (n = 0, p = columns; p[n].hex; ++n) {
84  memset(out, 0, sizeof(out));
85  CHKFetch("S");
86  if (n_len[p[n].num] != strlen(p[n].out) || strcmp(p[n].out, out[p[n].num]) != 0) {
87  fprintf(stderr, "Wrong row %d %s\n", n+1, out[p[n].num]);
89  return 1;
90  }
91  }
92 
94  printf("Done.\n");
95  return 0;
96 }
97 
#define CHKAllocStmt(a, res)
Definition: common.h:90
#define odbc_command(cmd)
Definition: common.h:179
#define CHKBindCol(a, b, c, d, e, res)
Definition: common.h:94
#define CHKFetch(res)
Definition: common.h:118
std::ofstream out("events_result.xml")
main entry point for tests
#define SQLLEN
Definition: odbc.h:52
#define TDS_VECTOR_SIZE(x)
Definition: tds.h:360
static SQLRETURN odbc_connect(TDS_DBC *dbc, TDSLOGIN *login)
Definition: odbc.c:356
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
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
static char tmp[3200]
Definition: utf8.c:42
int main(int argc, char *argv[])
Definition: utf8_2.c:35
const char * odbc_conn_additional_params
Definition: common.c:37
static const column_t columns[]
Definition: utf8_2.c:22
static const char *const column_names[]
Definition: utf8_2.c:8
#define NULL
Definition: ncbistd.hpp:225
yy_size_t n
static void hex(unsigned char c)
Definition: mdb_dump.c:56
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
#define SQL_C_CHAR
Definition: sqlext.h:511
const char * out
Definition: utf8_2.c:19
const char * hex
Definition: utf8_2.c:17
int num
Definition: utf8_2.c:15
Modified on Sat May 18 11:35:59 2024 by modify_doxy.py rev. 669887