Line data Source code
1 : #include "common.h"
2 :
3 : /* Test SQLFetchScroll with a non-unitary rowset, using bottom-up direction */
4 :
5 : static char software_version[] = "$Id: cursor7.c,v 1.10 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 6 : Test(void)
10 : {
11 : enum { ROWS=5 };
12 : struct data_t {
13 : SQLINTEGER i;
14 : SQLLEN ind_i;
15 : char c[20];
16 : SQLLEN ind_c;
17 : } data[ROWS];
18 : SQLUSMALLINT statuses[ROWS];
19 : SQLULEN num_row;
20 :
21 : int i;
22 : SQLRETURN RetCode;
23 :
24 6 : odbc_reset_statement();
25 :
26 6 : CHKSetStmtAttr(SQL_ATTR_CONCURRENCY, TDS_INT2PTR(SQL_CONCUR_READ_ONLY), 0, "S");
27 6 : CHKSetStmtAttr(SQL_ATTR_CURSOR_TYPE, TDS_INT2PTR(SQL_CURSOR_STATIC), 0, "S");
28 :
29 6 : CHKPrepare(T("SELECT c, i FROM #cursor7_test"), SQL_NTS, "S");
30 6 : CHKExecute("S");
31 6 : CHKSetStmtAttr(SQL_ATTR_ROW_BIND_TYPE, TDS_INT2PTR(sizeof(data[0])), 0, "S");
32 6 : CHKSetStmtAttr(SQL_ATTR_ROW_ARRAY_SIZE, TDS_INT2PTR(ROWS), 0, "S");
33 6 : CHKSetStmtAttr(SQL_ATTR_ROW_STATUS_PTR, statuses, 0, "S");
34 6 : CHKSetStmtAttr(SQL_ATTR_ROWS_FETCHED_PTR, &num_row, 0, "S");
35 :
36 6 : CHKBindCol(1, SQL_C_CHAR, &data[0].c, sizeof(data[0].c), &data[0].ind_c, "S");
37 6 : CHKBindCol(2, SQL_C_LONG, &data[0].i, sizeof(data[0].i), &data[0].ind_i, "S");
38 :
39 : /* Read records from last to first */
40 6 : printf("\n\nReading records from last to first:\n");
41 6 : RetCode = CHKFetchScroll(SQL_FETCH_LAST, -ROWS, "SINo");
42 30 : while (RetCode != SQL_NO_DATA) {
43 : SQLULEN RowNumber;
44 :
45 : /* Print this set of rows */
46 90 : for (i = ROWS - 1; i >= 0; i--) {
47 90 : if (statuses[i] != SQL_ROW_NOROW)
48 72 : printf("\t %d, %s\n", (int) data[i].i, data[i].c);
49 : }
50 18 : printf("\n");
51 :
52 18 : CHKGetStmtAttr(SQL_ROW_NUMBER, (SQLPOINTER)(&RowNumber), sizeof(RowNumber), NULL, "S");
53 18 : printf("---> We are in record No: %u\n", (unsigned int) RowNumber);
54 :
55 : /* Read next rowset */
56 18 : if ( (RowNumber>1) && (RowNumber<ROWS) ) {
57 6 : RetCode = CHKFetchScroll(SQL_FETCH_RELATIVE, 1-RowNumber, "SINo");
58 24 : for (i=RowNumber-1; i<ROWS; ++i)
59 18 : statuses[i] = SQL_ROW_NOROW;
60 : } else {
61 12 : RetCode = CHKFetchScroll(SQL_FETCH_RELATIVE, -ROWS, "SINo");
62 : }
63 : }
64 6 : }
65 :
66 : static void
67 6 : Init(void)
68 : {
69 : int i;
70 : char sql[128];
71 :
72 6 : printf("\n\nCreating table #cursor7_test with 12 records.\n");
73 :
74 6 : odbc_command("\tCREATE TABLE #cursor7_test (i INT, c VARCHAR(20))");
75 78 : for (i = 1; i <= 12; ++i) {
76 72 : sprintf(sql, "\tINSERT INTO #cursor7_test(i,c) VALUES(%d, 'a%db%dc%d')", i, i, i, i);
77 72 : odbc_command(sql);
78 : }
79 :
80 6 : }
81 :
82 : int
83 8 : main(int argc, char *argv[])
84 : {
85 8 : odbc_use_version3 = 1;
86 8 : odbc_connect();
87 :
88 8 : odbc_check_cursor();
89 :
90 6 : Init();
91 :
92 6 : Test();
93 :
94 6 : odbc_disconnect();
95 :
96 : return 0;
97 : }
|