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