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

Go to the SVN repository for this file.

1 #include "common.h"
2 
3 #include <common/test_assert.h>
4 
5 /* Test compute results */
6 
7 /*
8  * This it's quite important cause it test different result types
9  * mssql odbc have also some extension not supported by FreeTDS
10  * and declared in odbcss.h
11  */
12 
13 static char col1[256], col2[256];
14 static SQLLEN ind1, ind2;
15 
16 static int main_line;
17 
18 static void
19 TestName(SQLSMALLINT index, const char *expected_name)
20 {
21  SQLTCHAR name[128];
22  char buf[256];
24 
25 #define NAME_TEST \
26  do { \
27  if (strcmp(C(name), expected_name) != 0) \
28  { \
29  sprintf(buf, "line %d: wrong name in column %d expected '%s' got '%s'", \
30  main_line, index, expected_name, C(name)); \
31  ODBC_REPORT_ERROR(buf); \
32  } \
33  } while(0)
34 
35  /* retrieve with SQLDescribeCol */
36  CHKDescribeCol(index, name, ODBC_VECTOR_SIZE(name), &len, &type, NULL, NULL, NULL, "S");
37  NAME_TEST;
38 
39  /* retrieve with SQLColAttribute */
40  CHKColAttribute(index, SQL_DESC_NAME, name, ODBC_VECTOR_SIZE(name), &len, NULL, "S");
42  NAME_TEST;
43  CHKColAttribute(index, SQL_DESC_LABEL, name, ODBC_VECTOR_SIZE(name), &len, NULL, "S");
44  NAME_TEST;
45 }
46 
47 static void
48 CheckFetch(const char *c1name, const char *c1, const char *c2)
49 {
50  int error = 0;
51 
52  TestName(1, c1name);
53 
54  CHKFetch("S");
55 
56  if (strlen(c1) != ind1 || strcmp(c1, col1) != 0) {
57  fprintf(stderr, "%s:%d: Column 1 error '%s' (%d) expected '%s' (%d)\n", __FILE__, main_line, col1, (int) ind1, c1,
58  (int) strlen(c1));
59  error = 1;
60  }
61 
62  if (strlen(c2) != ind2 || strcmp(c2, col2) != 0) {
63  fprintf(stderr, "%s:%d: Column 2 error '%s' (%d) expected '%s' (%d)\n", __FILE__, main_line, col2, (int) ind2, c2,
64  (int) strlen(c2));
65  error = 1;
66  }
67 
68  if (error)
69  exit(1);
70 }
71 
72 #define CheckFetch main_line = __LINE__; CheckFetch
73 
74 int
75 main(int argc, char *argv[])
76 {
77  odbc_connect();
78 
79  /* MSSQL 2012+, compute not supported */
80  if (odbc_db_is_microsoft() && odbc_db_version_int() >= 0x0b000000u) {
82  return 0;
83  }
84 
85  odbc_command("create table #tmp1 (c varchar(20), i int)");
86  odbc_command("insert into #tmp1 values('pippo', 12)");
87  odbc_command("insert into #tmp1 values('pippo', 34)");
88  odbc_command("insert into #tmp1 values('pluto', 1)");
89  odbc_command("insert into #tmp1 values('pluto', 2)");
90  odbc_command("insert into #tmp1 values('pluto', 3)");
91 
92 
93  /*
94  * TODO skip rows/column on compute (compute.c)
95  * TODO check rows/column after moreresults after compute
96  */
97 
98 
99  /* select * from #tmp1 compute sum(i) */
100  SQLBindCol(odbc_stmt, 1, SQL_C_CHAR, col1, sizeof(col1), &ind1);
101  SQLBindCol(odbc_stmt, 2, SQL_C_CHAR, col2, sizeof(col2), &ind2);
102  odbc_command("select * from #tmp1 order by c, i compute sum(i)");
103  CheckFetch("c", "pippo", "12");
104  CheckFetch("c", "pippo", "34");
105  CheckFetch("c", "pluto", "1");
106  CheckFetch("c", "pluto", "2");
107  CheckFetch("c", "pluto", "3");
108  CHKFetch("No");
109  CHKMoreResults("S");
110 
111  /* why I need to rebind ?? ms bug of feature ?? */
112  SQLBindCol(odbc_stmt, 1, SQL_C_CHAR, col1, sizeof(col1), &ind1);
113  SQLBindCol(odbc_stmt, 2, SQL_C_CHAR, col2, sizeof(col2), &ind2);
114  col2[0] = '@';
115  CheckFetch("sum", "52", "@");
116  CHKFetch("No");
117  CHKMoreResults("No");
118 
119 
120 
121 
122  /* select * from #tmp1 order by c compute sum(i) by c */
123  SQLBindCol(odbc_stmt, 1, SQL_C_CHAR, col1, sizeof(col1), &ind1);
124  SQLBindCol(odbc_stmt, 2, SQL_C_CHAR, col2, sizeof(col2), &ind2);
125  odbc_command("select c as mao, i from #tmp1 order by c, i compute sum(i) by c compute max(i)");
126  CheckFetch("mao", "pippo", "12");
127  CheckFetch("mao", "pippo", "34");
128  CHKFetch("No");
129  CHKMoreResults("S");
130 
131  SQLBindCol(odbc_stmt, 1, SQL_C_CHAR, col1, sizeof(col1), &ind1);
132  SQLBindCol(odbc_stmt, 2, SQL_C_CHAR, col2, sizeof(col2), &ind2);
133  strcpy(col2, "##");
134  CheckFetch("sum", "46", "##");
135  CHKFetch("No");
136  CHKMoreResults("S");
137 
138  SQLBindCol(odbc_stmt, 1, SQL_C_CHAR, col1, sizeof(col1), &ind1);
139  SQLBindCol(odbc_stmt, 2, SQL_C_CHAR, col2, sizeof(col2), &ind2);
140  CheckFetch("mao", "pluto", "1");
141  CheckFetch("mao", "pluto", "2");
142  CheckFetch("mao", "pluto", "3");
143  CHKFetch("No");
144  CHKMoreResults("S");
145 
146  SQLBindCol(odbc_stmt, 1, SQL_C_CHAR, col1, sizeof(col1), &ind1);
147  SQLBindCol(odbc_stmt, 2, SQL_C_CHAR, col2, sizeof(col2), &ind2);
148  strcpy(col2, "%");
149  CheckFetch("sum", "6", "%");
150  CHKFetch("No");
151  CHKMoreResults("S");
152 
153  SQLBindCol(odbc_stmt, 1, SQL_C_CHAR, col1, sizeof(col1), &ind1);
154  SQLBindCol(odbc_stmt, 2, SQL_C_CHAR, col2, sizeof(col2), &ind2);
155  strcpy(col2, "&");
156  CheckFetch("max", "34", "&");
157  CHKFetch("No");
158  CHKMoreResults("No");
159 
160 
161 
162  /* test skip recordset with computed rows */
163 
164  /* select * from #tmp1 where i = 2 compute min(i) */
165  SQLBindCol(odbc_stmt, 1, SQL_C_CHAR, col1, sizeof(col1), &ind1);
166  SQLBindCol(odbc_stmt, 2, SQL_C_CHAR, col2, sizeof(col2), &ind2);
167  odbc_command("select * from #tmp1 where i = 2 or i = 34 order by c, i compute min(i) by c");
168  CheckFetch("c", "pippo", "34");
169  CHKFetch("No");
170  CHKMoreResults("S");
171 
172  /* here just skip results, before a row */
173  CHKMoreResults("S");
174 
175  SQLBindCol(odbc_stmt, 1, SQL_C_CHAR, col1, sizeof(col1), &ind1);
176  SQLBindCol(odbc_stmt, 2, SQL_C_CHAR, col2, sizeof(col2), &ind2);
177  CheckFetch("c", "pluto", "2");
178  CHKFetch("No");
179  CHKMoreResults("S");
180 
181  /* here just skip results, before done */
182  CHKMoreResults("No");
183 
184 
185  odbc_disconnect();
186  return 0;
187 }
int main(int argc, char *argv[])
Definition: compute.c:75
static void TestName(SQLSMALLINT index, const char *expected_name)
Definition: compute.c:19
#define NAME_TEST
static char col1[256]
Definition: compute.c:13
static SQLINTEGER ind1
Definition: compute.c:14
static char col2[256]
Definition: compute.c:13
static int main_line
Definition: compute.c:16
#define CheckFetch
Definition: compute.c:72
static SQLINTEGER ind2
Definition: compute.c:14
#define CHKMoreResults(res)
Definition: common.h:138
#define odbc_command(cmd)
Definition: common.h:179
#define CHKColAttribute(a, b, c, d, e, f, res)
Definition: common.h:102
#define CHKDescribeCol(a, b, c, d, e, f, g, h, res)
Definition: common.h:104
#define ODBC_VECTOR_SIZE(x)
Definition: common.h:188
#define CHKFetch(res)
Definition: common.h:118
static int type
Definition: getdata.c:31
#define NULL
Definition: ncbistd.hpp:225
exit(2)
char * buf
int len
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
HSTMT odbc_stmt
Definition: common.c:33
unsigned int odbc_db_version_int(void)
Definition: common.c:483
int odbc_disconnect(void)
Definition: common.c:290
int odbc_db_is_microsoft(void)
Definition: common.c:325
static SQLRETURN odbc_connect(TDS_DBC *dbc, TDSLOGIN *login)
Definition: odbc.c:356
#define SQLLEN
Definition: odbc.h:52
#define SQL_DESC_NAME
Definition: sql.h:109
SQLRETURN SQLBindCol(SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLPOINTER TargetValue, SQLINTEGER BufferLength, SQLINTEGER *StrLen_or_Ind)
#define SQL_DESC_LABEL
Definition: sqlext.h:397
#define SQL_C_CHAR
Definition: sqlext.h:511
SQLCHAR SQLTCHAR
Definition: sqltypes.h:463
signed short int SQLSMALLINT
Definition: sqltypes.h:201
Definition: type.c:6
Modified on Sat Dec 09 04:44:22 2023 by modify_doxy.py rev. 669887