Line data Source code
1 : #include "common.h"
2 :
3 : #include <bkpublic.h>
4 :
5 : /* Testing: array binding of result set */
6 10 : TEST_MAIN()
7 : {
8 : CS_CONTEXT *ctx;
9 : CS_CONNECTION *conn;
10 : CS_COMMAND *cmd;
11 : CS_BLKDESC *blkdesc;
12 10 : bool verbose = false;
13 :
14 : CS_RETCODE ret;
15 :
16 : CS_DATAFMT datafmt;
17 10 : CS_INT count = 0, total = 0;
18 :
19 : CS_INT col1[2];
20 : CS_CHAR col2[2][5];
21 : CS_CHAR col3[2][32];
22 : CS_INT lencol1[2];
23 : CS_SMALLINT indcol1[2];
24 : CS_INT lencol2[2];
25 : CS_SMALLINT indcol2[2];
26 : CS_INT lencol3[2];
27 : CS_SMALLINT indcol3[2];
28 :
29 : int i;
30 :
31 :
32 10 : printf("%s: Retrieve data using array binding \n", __FILE__);
33 : if (verbose) {
34 : printf("Trying login\n");
35 : }
36 10 : check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
37 :
38 : /* do not test error */
39 10 : run_command(cmd, "IF OBJECT_ID('tempdb..#ctlibarray') IS NOT NULL DROP TABLE #ctlibarray");
40 :
41 10 : check_call(run_command, (cmd, "CREATE TABLE #ctlibarray (col1 int null, col2 char(4) not null, col3 datetime not null)"));
42 10 : check_call(run_command, (cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan 1 2002 10:00:00AM')"));
43 10 : check_call(run_command, (cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan 2 2002 10:00:00AM')"));
44 10 : check_call(run_command, (cmd, "insert into #ctlibarray values (3, 'CCCC', 'Jan 3 2002 10:00:00AM')"));
45 10 : check_call(run_command, (cmd, "insert into #ctlibarray values (8, 'DDDD', 'Jan 4 2002 10:00:00AM')"));
46 10 : check_call(run_command, (cmd, "insert into #ctlibarray values (NULL, 'EEEE', 'Jan 5 2002 10:00:00AM')"));
47 :
48 10 : check_call(blk_alloc, (conn, BLK_VERSION_150, &blkdesc));
49 :
50 10 : check_call(blk_init, (blkdesc, CS_BLK_OUT, "#ctlibarray", CS_NULLTERM));
51 :
52 10 : check_call(blk_describe, (blkdesc, 1, &datafmt));
53 :
54 10 : datafmt.format = CS_FMT_UNUSED;
55 10 : if (datafmt.maxlength > 1024) {
56 0 : datafmt.maxlength = 1024;
57 : }
58 :
59 10 : datafmt.count = 2;
60 :
61 10 : check_call(blk_bind, (blkdesc, 1, &datafmt, &col1[0], &lencol1[0], &indcol1[0]));
62 :
63 10 : check_call(blk_describe, (blkdesc, 2, &datafmt));
64 :
65 10 : datafmt.format = CS_FMT_NULLTERM;
66 10 : datafmt.maxlength = 5;
67 10 : datafmt.count = 2;
68 :
69 10 : check_call(blk_bind, (blkdesc, 2, &datafmt, col2[0], &lencol2[0], &indcol2[0]));
70 :
71 10 : check_call(blk_describe, (blkdesc, 3, &datafmt));
72 :
73 10 : datafmt.datatype = CS_CHAR_TYPE;
74 10 : datafmt.format = CS_FMT_NULLTERM;
75 10 : datafmt.maxlength = 32;
76 10 : datafmt.count = 2;
77 :
78 10 : check_call(blk_bind, (blkdesc, 3, &datafmt, col3[0], &lencol3[0], &indcol3[0]));
79 :
80 50 : while ((ret = blk_rowxfer_mult(blkdesc, &count)) == CS_SUCCEED) {
81 30 : if (count < 1 || count > 2) {
82 0 : fprintf(stderr, "blk_rowxfer_mult() returned %d rows\n", (int) count);
83 0 : return 1;
84 : }
85 50 : for (i = 0; i < count; i++) {
86 150 : printf("retrieved %d (%d,%d) %s (%d,%d) %s (%d,%d)\n",
87 50 : col1[i], lencol1[i], indcol1[i],
88 100 : col2[i], lencol2[i], indcol2[i],
89 100 : col3[i], lencol3[i], indcol3[i] );
90 : }
91 30 : total += count;
92 :
93 30 : if (total == 2) {
94 : /* batches are not supported for copy out */
95 10 : check_fail(blk_done, (blkdesc, CS_BLK_BATCH, &count));
96 10 : check_last_message(CTMSG_CLIENT, 0x01010110, "CS_BLK_ALL");
97 :
98 : /* rows still to retrieve, copy can continue */
99 10 : check_fail(blk_done, (blkdesc, CS_BLK_ALL, &count));
100 10 : check_last_message(CTMSG_CLIENT, 0x010101a3, "completely processed");
101 : }
102 : }
103 10 : if (ret != CS_END_DATA) {
104 0 : fprintf(stderr, "blk_rowxfer_mult() failed\n");
105 0 : return 1;
106 : }
107 : /* rows are returned with CS_SUCCEED, last call returns no rows */
108 10 : if (count != 0 || total != 5) {
109 0 : fprintf(stderr, "wrong rows, last count %d total %d\n", (int) count, (int) total);
110 0 : return 1;
111 : }
112 :
113 10 : count = -1;
114 10 : check_call(blk_done, (blkdesc, CS_BLK_ALL, &count));
115 10 : if (count != 5) {
116 0 : fprintf(stderr, "blk_done() returned %d rows\n", (int) count);
117 0 : return 1;
118 : }
119 :
120 10 : blk_drop(blkdesc);
121 :
122 : /* connection can be used */
123 10 : check_call(run_command, (cmd, "DROP TABLE #ctlibarray"));
124 :
125 10 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
126 :
127 10 : return 0;
128 : }
|