Line data Source code
1 : #include "common.h"
2 :
3 : /* Test for data format returned from SQLPrepare */
4 :
5 : static char software_version[] = "$Id: prepare_results.c,v 1.15 2011-07-12 10:16:59 freddy77 Exp $";
6 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
7 :
8 : static void
9 16 : Test(int use_ird)
10 : {
11 : SQLSMALLINT count, namelen, type, digits, nullable;
12 : SQLULEN size;
13 : SQLHDESC desc;
14 : SQLINTEGER ind;
15 : SQLTCHAR name[128];
16 16 : char *cname = NULL;
17 :
18 : /* test query returns column information */
19 16 : CHKPrepare(T("select * from #odbctestdata select * from #odbctestdata"), SQL_NTS, "S");
20 :
21 16 : SQLNumParams(odbc_stmt, &count);
22 16 : if (count != 0) {
23 0 : fprintf(stderr, "Wrong number of params returned. Got %d expected 0\n", (int) count);
24 0 : exit(1);
25 : }
26 :
27 16 : if (use_ird) {
28 : /* get IRD */
29 8 : CHKGetStmtAttr(SQL_ATTR_IMP_ROW_DESC, &desc, sizeof(desc), &ind, "S");
30 :
31 8 : CHKGetDescField(desc, 0, SQL_DESC_COUNT, &count, sizeof(count), &ind, "S");
32 : } else {
33 8 : CHKNumResultCols(&count, "S");
34 : }
35 :
36 16 : if (count != 3) {
37 0 : fprintf(stderr, "Wrong number of columns returned. Got %d expected 3\n", (int) count);
38 0 : exit(1);
39 : }
40 :
41 16 : CHKDescribeCol(1, name, TDS_VECTOR_SIZE(name), &namelen, &type, &size, &digits, &nullable, "S");
42 :
43 16 : cname = (char*) C(name);
44 16 : if (type != SQL_INTEGER || strcmp(cname, "i") != 0) {
45 0 : fprintf(stderr, "wrong column 1 informations (type %d name '%s' size %d)\n", (int) type, cname, (int) size);
46 0 : exit(1);
47 : }
48 :
49 16 : CHKDescribeCol(2, name, TDS_VECTOR_SIZE(name), &namelen, &type, &size, &digits, &nullable, "S");
50 :
51 16 : cname = (char*) C(name);
52 16 : if (type != SQL_CHAR || strcmp(cname, "c") != 0 || (size != 20 && (odbc_db_is_microsoft() || size != 40))) {
53 0 : fprintf(stderr, "wrong column 2 informations (type %d name '%s' size %d)\n", (int) type, cname, (int) size);
54 0 : exit(1);
55 : }
56 :
57 16 : CHKDescribeCol(3, name, TDS_VECTOR_SIZE(name), &namelen, &type, &size, &digits, &nullable, "S");
58 :
59 16 : cname = (char*) C(name);
60 16 : if (type != SQL_NUMERIC || strcmp(cname, "n") != 0 || size != 34 || digits != 12) {
61 0 : fprintf(stderr, "wrong column 3 informations (type %d name '%s' size %d)\n", (int) type, cname, (int) size);
62 0 : exit(1);
63 : }
64 16 : ODBC_FREE();
65 16 : }
66 :
67 : int
68 8 : main(int argc, char *argv[])
69 : {
70 : SQLSMALLINT count;
71 :
72 8 : odbc_connect();
73 :
74 8 : odbc_command("create table #odbctestdata (i int, c char(20), n numeric(34,12) )");
75 :
76 : /* reset state */
77 8 : odbc_command("select * from #odbctestdata");
78 8 : SQLFetch(odbc_stmt);
79 8 : SQLMoreResults(odbc_stmt);
80 :
81 : /* test query returns column information for select without param */
82 8 : odbc_reset_statement();
83 8 : CHKPrepare(T("select * from #odbctestdata"), SQL_NTS, "S");
84 :
85 8 : count = -1;
86 8 : CHKNumResultCols(&count, "S");
87 :
88 8 : if (count != 3) {
89 0 : fprintf(stderr, "Wrong number of columns returned. Got %d expected 3\n", (int) count);
90 0 : exit(1);
91 : }
92 :
93 : /* test query returns column information for select with param */
94 8 : odbc_reset_statement();
95 8 : CHKPrepare(T("select c,n from #odbctestdata where i=?"), SQL_NTS, "S");
96 :
97 8 : count = -1;
98 8 : CHKNumResultCols(&count, "S");
99 :
100 8 : if (count != 2) {
101 0 : fprintf(stderr, "Wrong number of columns returned. Got %d expected 2\n", (int) count);
102 0 : exit(1);
103 : }
104 :
105 : /* test query returns column information for update */
106 8 : odbc_reset_statement();
107 8 : CHKPrepare(T("update #odbctestdata set i = 20"), SQL_NTS, "S");
108 :
109 8 : count = -1;
110 8 : CHKNumResultCols(&count, "S");
111 :
112 8 : if (count != 0) {
113 0 : fprintf(stderr, "Wrong number of columns returned. Got %d expected 0\n", (int) count);
114 0 : exit(1);
115 : }
116 :
117 8 : if (odbc_driver_is_freetds()) {
118 8 : if (odbc_db_is_microsoft() && odbc_tds_version() >= 0x704) {
119 0 : odbc_disconnect();
120 0 : putenv("TDSVER=7.3");
121 0 : odbc_connect();
122 0 : odbc_command("create table #odbctestdata (i int, c char(20), n numeric(34,12) )");
123 : }
124 8 : if (!odbc_db_is_microsoft() || odbc_tds_version() < 0x704) {
125 8 : Test(0);
126 8 : odbc_reset_statement();
127 8 : Test(1);
128 : }
129 : }
130 :
131 : /* TODO test SQLDescribeParam (when implemented) */
132 8 : odbc_command("drop table #odbctestdata");
133 :
134 8 : odbc_disconnect();
135 :
136 8 : printf("Done.\n");
137 : return 0;
138 : }
|