Line data Source code
1 : #include "common.h"
2 : #include <assert.h>
3 :
4 : /* test conversion using SQLGetData */
5 :
6 10 : TEST_MAIN()
7 : {
8 : SQLLEN len;
9 : unsigned char buf[30];
10 : static const char expected[] = "\xf0\x9f\x8e\x84";
11 : int i;
12 :
13 10 : odbc_use_version3 = 1;
14 10 : odbc_conn_additional_params = "ClientCharset=UTF-8;";
15 :
16 10 : odbc_connect();
17 10 : if (!odbc_driver_is_freetds()) {
18 0 : odbc_disconnect();
19 0 : printf("Driver is not FreeTDS, exiting\n");
20 0 : odbc_test_skipped();
21 0 : return 0;
22 : }
23 :
24 10 : if (!odbc_db_is_microsoft() || odbc_tds_version() < 0x700) {
25 2 : odbc_disconnect();
26 : /* we need NVARCHAR */
27 2 : printf("Test for MSSQL only using protocol 7.0\n");
28 2 : odbc_test_skipped();
29 0 : return 0;
30 : }
31 :
32 8 : CHKAllocStmt(&odbc_stmt, "S");
33 :
34 : /* a Christmas tree */
35 8 : odbc_command("SELECT CONVERT(NVARCHAR(10), CONVERT(VARBINARY(20), 0x3CD884DF))");
36 :
37 8 : CHKFetch("S");
38 :
39 : /* read one byte at a time and test it */
40 40 : for (i = 0; i < 4; ++i) {
41 32 : memset(buf, '-', sizeof(buf));
42 32 : CHKGetData(1, SQL_C_CHAR, buf, 2, &len, i < 3 ? "I" : "S");
43 32 : printf("res %ld buf { 0x%02x, 0x%02x }\n", (long int) len, buf[0], buf[1]);
44 32 : assert(len == SQL_NO_TOTAL || len == 4 - i);
45 32 : assert(buf[0] == (unsigned char) expected[i]);
46 32 : assert(buf[1] == 0);
47 : }
48 8 : CHKGetData(1, SQL_C_CHAR, buf, 2, &len, "No");
49 :
50 8 : odbc_reset_statement();
51 :
52 : #define CN_STRING \
53 : "202020202052656974657261746520486f6c642028324829207261a174696e6720a15820" \
54 : "a14ea16fa172a173a174a161a172a1207265706f72746564206120736574206f6620756e" \
55 : "6578636974696e6720726573756c74732077697468206d6f646573742067726f77746820" \
56 : "696e20726576656e756520616e6420626f74746f6d206c696e652e20457870616e73696f" \
57 : "6e20696e746f2074686520646f6d6573"
58 :
59 : /* insert does not change as much as CONVERT so insert first into a new table */
60 8 : odbc_command("CREATE TABLE #tmp1(c VARCHAR(200) COLLATE Chinese_PRC_CI_AS NULL)");
61 8 : odbc_command("INSERT INTO #tmp1 VALUES(CONVERT(VARBINARY(200), 0x" CN_STRING "))");
62 8 : odbc_command("SELECT c FROM #tmp1");
63 8 : CHKFetch("S");
64 48 : for (i = 0; i < 5; ++i) {
65 40 : memset(buf, 0, sizeof(buf));
66 40 : CHKGetData(1, SQL_C_CHAR, buf, sizeof(buf), &len, "I");
67 40 : printf("loop %d output '%s'\n", i, buf);
68 40 : assert(strlen((char *) buf) == sizeof(buf) - 1);
69 : }
70 8 : CHKGetData(1, SQL_C_CHAR, buf, sizeof(buf), &len, "S");
71 :
72 8 : odbc_disconnect();
73 8 : return 0;
74 : }
75 :
|