Line data Source code
1 : #include "common.h"
2 :
3 : #if HAVE_SYS_STAT_H
4 : #include <sys/stat.h>
5 : #endif /* HAVE_SYS_STAT_H */
6 :
7 : #include <bkpublic.h>
8 : #include "blk_in.h"
9 :
10 : static CS_RETCODE
11 : do_bind(CS_BLKDESC * blkdesc, int colnum, CS_INT host_format, CS_INT host_type, CS_INT host_maxlen,
12 : void *var_addr,
13 : CS_INT *var_len_addr,
14 : CS_SMALLINT *var_ind_addr );
15 :
16 : /*
17 : * Static data for insertion
18 : */
19 : static int not_null_bit = 1;
20 : static CS_INT l_not_null_bit = 4;
21 : static CS_SMALLINT i_not_null_bit = 0;
22 :
23 : static char not_null_char[] = "a char";
24 : static CS_INT l_not_null_char = 6;
25 : static CS_SMALLINT i_not_null_char = 0;
26 :
27 : static char not_null_varchar[] = "a varchar";
28 : static CS_INT l_not_null_varchar = 9;
29 : static CS_SMALLINT i_not_null_varchar = 0;
30 :
31 : static char not_null_datetime[] = "Dec 17 2003 3:44PM";
32 : static CS_INT l_not_null_datetime = 19;
33 : static CS_SMALLINT i_not_null_datetime = 0;
34 :
35 : static char not_null_smalldatetime[] = "Dec 17 2003 3:44PM";
36 : static CS_INT l_not_null_smalldatetime = 19;
37 : static CS_SMALLINT i_not_null_smalldatetime = 0;
38 :
39 : static char not_null_money[] = "12.34";
40 : static CS_INT l_not_null_money = 5;
41 : static CS_SMALLINT i_not_null_money = 0;
42 :
43 : static char not_null_smallmoney[] = "12.34";
44 : static CS_INT l_not_null_smallmoney = 5;
45 : static CS_SMALLINT i_not_null_smallmoney = 0;
46 :
47 : static char not_null_float[] = "12.34";
48 : static CS_INT l_not_null_float = 5;
49 : static CS_SMALLINT i_not_null_float = 0;
50 :
51 : static char not_null_real[] = "12.34";
52 : static CS_INT l_not_null_real = 5;
53 : static CS_SMALLINT i_not_null_real = 0;
54 :
55 : static char not_null_decimal[] = "12.34";
56 : static CS_INT l_not_null_decimal = 5;
57 : static CS_SMALLINT i_not_null_decimal = 0;
58 :
59 : static char not_null_numeric[] = "12.34";
60 : static CS_INT l_not_null_numeric = 5;
61 : static CS_SMALLINT i_not_null_numeric = 0;
62 :
63 : static int not_null_int = 1234;
64 : static CS_INT l_not_null_int = 4;
65 : static CS_SMALLINT i_not_null_int = 0;
66 :
67 : static int not_null_smallint = 1234;
68 : static CS_INT l_not_null_smallint = 4;
69 : static CS_SMALLINT i_not_null_smallint = 0;
70 :
71 : static int not_null_tinyint = 123;
72 : static CS_INT l_not_null_tinyint = 4;
73 : static CS_SMALLINT i_not_null_tinyint = 0;
74 :
75 : static CS_INT l_null_char = 0;
76 : static CS_SMALLINT i_null_char = -1;
77 :
78 : static CS_INT l_null_varchar = 0;
79 : static CS_SMALLINT i_null_varchar = -1;
80 :
81 : static CS_INT l_null_datetime = 0;
82 : static CS_SMALLINT i_null_datetime = -1;
83 :
84 : static CS_INT l_null_smalldatetime = 0;
85 : static CS_SMALLINT i_null_smalldatetime = -1;
86 :
87 : static CS_INT l_null_money = 0;
88 : static CS_SMALLINT i_null_money = -1;
89 :
90 : static CS_INT l_null_smallmoney = 0;
91 : static CS_SMALLINT i_null_smallmoney = -1;
92 :
93 : static CS_INT l_null_float = 0;
94 : static CS_SMALLINT i_null_float = -1;
95 :
96 : static CS_INT l_null_real = 0;
97 : static CS_SMALLINT i_null_real = -1;
98 :
99 : static CS_INT l_null_decimal = 0;
100 : static CS_SMALLINT i_null_decimal = -1;
101 :
102 : static CS_INT l_null_numeric = 0;
103 : static CS_SMALLINT i_null_numeric = -1;
104 :
105 : static CS_INT l_null_int = 0;
106 : static CS_SMALLINT i_null_int = -1;
107 :
108 : static CS_INT l_null_smallint = 0;
109 : static CS_SMALLINT i_null_smallint = -1;
110 :
111 : static CS_INT l_null_tinyint = 0;
112 : static CS_SMALLINT i_null_tinyint = -1;
113 :
114 : static void
115 8 : do_binds(CS_BLKDESC * blkdesc)
116 : {
117 : /* non nulls */
118 :
119 8 : do_bind(blkdesc, 1, CS_FMT_UNUSED, CS_INT_TYPE, 4, ¬_null_bit, &l_not_null_bit, &i_not_null_bit);
120 8 : do_bind(blkdesc, 2, CS_FMT_NULLTERM, CS_CHAR_TYPE, 7, not_null_char, &l_not_null_char, &i_not_null_char);
121 8 : do_bind(blkdesc, 3, CS_FMT_NULLTERM, CS_CHAR_TYPE, 10, not_null_varchar, &l_not_null_varchar, &i_not_null_varchar);
122 8 : do_bind(blkdesc, 4, CS_FMT_NULLTERM, CS_CHAR_TYPE, 20, not_null_datetime, &l_not_null_datetime, &i_not_null_datetime);
123 8 : do_bind(blkdesc, 5, CS_FMT_NULLTERM, CS_CHAR_TYPE, 20, not_null_smalldatetime, &l_not_null_smalldatetime, &i_not_null_smalldatetime);
124 8 : do_bind(blkdesc, 6, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_money, &l_not_null_money, &i_not_null_money);
125 8 : do_bind(blkdesc, 7, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_smallmoney, &l_not_null_smallmoney, &i_not_null_smallmoney);
126 8 : do_bind(blkdesc, 8, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_float, &l_not_null_float, &i_not_null_float);
127 8 : do_bind(blkdesc, 9, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_real, &l_not_null_real, &i_not_null_real);
128 8 : do_bind(blkdesc, 10, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_decimal, &l_not_null_decimal, &i_not_null_decimal);
129 8 : do_bind(blkdesc, 11, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_numeric, &l_not_null_numeric, &i_not_null_numeric);
130 8 : do_bind(blkdesc, 12, CS_FMT_UNUSED, CS_INT_TYPE, 4, ¬_null_int, &l_not_null_int, &i_not_null_int);
131 8 : do_bind(blkdesc, 13, CS_FMT_UNUSED, CS_INT_TYPE, 4, ¬_null_smallint, &l_not_null_smallint, &i_not_null_smallint);
132 8 : do_bind(blkdesc, 14, CS_FMT_UNUSED, CS_INT_TYPE, 4, ¬_null_tinyint, &l_not_null_tinyint, &i_not_null_tinyint);
133 :
134 : /* nulls */
135 :
136 8 : do_bind(blkdesc, 15, CS_FMT_NULLTERM, CS_CHAR_TYPE, 7, not_null_char, &l_null_char, &i_null_char);
137 8 : do_bind(blkdesc, 16, CS_FMT_NULLTERM, CS_CHAR_TYPE, 10, not_null_varchar, &l_null_varchar, &i_null_varchar);
138 8 : do_bind(blkdesc, 17, CS_FMT_NULLTERM, CS_CHAR_TYPE, 20, not_null_datetime, &l_null_datetime, &i_null_datetime);
139 8 : do_bind(blkdesc, 18, CS_FMT_NULLTERM, CS_CHAR_TYPE, 20, not_null_smalldatetime, &l_null_smalldatetime, &i_null_smalldatetime);
140 8 : do_bind(blkdesc, 19, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_money, &l_null_money, &i_null_money);
141 8 : do_bind(blkdesc, 20, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_smallmoney, &l_null_smallmoney, &i_null_smallmoney);
142 8 : do_bind(blkdesc, 21, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_float, &l_null_float, &i_null_float);
143 8 : do_bind(blkdesc, 22, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_real, &l_null_real, &i_null_real);
144 8 : do_bind(blkdesc, 23, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_decimal, &l_null_decimal, &i_null_decimal);
145 8 : do_bind(blkdesc, 24, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_numeric, &l_null_numeric, &i_null_numeric);
146 8 : do_bind(blkdesc, 25, CS_FMT_UNUSED, CS_INT_TYPE, 4, ¬_null_int, &l_null_int, &i_null_int);
147 8 : do_bind(blkdesc, 26, CS_FMT_UNUSED, CS_INT_TYPE, 4, ¬_null_smallint, &l_null_smallint, &i_null_smallint);
148 8 : do_bind(blkdesc, 27, CS_FMT_UNUSED, CS_INT_TYPE, 4, ¬_null_tinyint, &l_null_tinyint, &i_null_tinyint);
149 8 : }
150 :
151 : static CS_RETCODE
152 216 : do_bind(CS_BLKDESC * blkdesc, int colnum, CS_INT host_format, CS_INT host_type, CS_INT host_maxlen,
153 : void *var_addr,
154 : CS_INT *var_len_addr,
155 : CS_SMALLINT *var_ind_addr )
156 : {
157 : CS_DATAFMT datafmt;
158 : CS_RETCODE ret;
159 :
160 216 : ret = blk_describe(blkdesc, colnum, &datafmt);
161 216 : if (ret != CS_SUCCEED) {
162 0 : fprintf(stderr, "blk_describe(%d) failed", colnum);
163 0 : return ret;
164 : }
165 :
166 216 : datafmt.format = host_format;
167 216 : datafmt.datatype = host_type;
168 216 : datafmt.maxlength = host_maxlen;
169 216 : datafmt.count = 1;
170 :
171 216 : ret = blk_bind(blkdesc, colnum, &datafmt, var_addr, var_len_addr, var_ind_addr );
172 216 : if (ret != CS_SUCCEED) {
173 0 : fprintf(stderr, "blk_bind() failed\n");
174 0 : return ret;
175 : }
176 : return ret;
177 : }
178 :
179 :
180 : int
181 8 : main(void)
182 : {
183 : CS_CONTEXT *ctx;
184 : CS_CONNECTION *conn;
185 : CS_COMMAND *cmd;
186 : CS_BLKDESC *blkdesc;
187 8 : int verbose = 0;
188 8 : int count = 0;
189 : int i;
190 : char command[512];
191 :
192 : static const char table_name[] = "all_types_bcp_unittest";
193 :
194 8 : printf("%s: Retrieve data using array binding \n", __FILE__);
195 : if (verbose) {
196 : printf("Trying login\n");
197 : }
198 8 : check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
199 :
200 8 : sprintf(command,"if exists (select 1 from sysobjects where type = 'U' and name = '%s') drop table %s",
201 : table_name, table_name);
202 :
203 8 : check_call(run_command, (cmd, command));
204 :
205 8 : check_call(run_command, (cmd, create_table_sql));
206 :
207 8 : check_call(blk_alloc, (conn, BLK_VERSION_100, &blkdesc));
208 :
209 8 : check_call(blk_init, (blkdesc, CS_BLK_IN, (char *) table_name, CS_NULLTERM));
210 :
211 8 : do_binds(blkdesc);
212 :
213 8 : printf("Sending same row 10 times... \n");
214 88 : for (i=0; i<10; i++) {
215 80 : check_call(blk_rowxfer, (blkdesc));
216 : }
217 :
218 8 : check_call(blk_done, (blkdesc, CS_BLK_ALL, &count));
219 :
220 8 : blk_drop(blkdesc);
221 :
222 8 : printf("%d rows copied.\n", count);
223 :
224 8 : printf("done\n");
225 :
226 8 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
227 :
228 : return 0;
229 : }
|