~ubuntu-branches/ubuntu/maverick/myodbc/maverick

« back to all changes in this revision

Viewing changes to test/my_relative.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2009-05-10 03:07:47 UTC
  • mfrom: (8.1.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090510030747-pvy1kyun4743gws8
Tags: 3.51.19r646-1
* New upstream release
  - fix build failure with current libmysqlclient.  Closes: #521185.
* Fix lintian warnings about ignoring errors in postinst, config scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
/* Testing SQL_FETCH_RELATIVE with row_set_size as 10 */
26
26
DECLARE_TEST(t_relative)
27
27
{
28
 
    SQLRETURN rc;
29
 
    SQLUINTEGER i, nrows;
30
 
    SQLINTEGER  iarray[15];
31
 
    long      index;
32
 
    char      name[21];
33
 
 
34
 
    SQLExecDirect(hstmt,(SQLCHAR *)"drop table t_relative",SQL_NTS);
35
 
 
36
 
    rc = SQLExecDirect(hstmt,(SQLCHAR *)"create table t_relative(id int,name char(20))",SQL_NTS);
37
 
    mystmt(hstmt,rc);
38
 
 
39
 
    rc = SQLPrepare(hstmt,(SQLCHAR *)"insert into t_relative values(?,?)",SQL_NTS);
40
 
    mystmt(hstmt,rc);
41
 
 
42
 
    rc = SQLBindParameter(hstmt,1,SQL_PARAM_INPUT, SQL_C_ULONG,
43
 
                          SQL_INTEGER,0,0,&i,0,NULL);
44
 
    mystmt(hstmt,rc);
45
 
    rc = SQLBindParameter(hstmt,2,SQL_PARAM_INPUT, SQL_C_CHAR,
46
 
                          SQL_CHAR,20,0,name,20,NULL);
47
 
    mystmt(hstmt,rc);
48
 
 
49
 
    for ( i = 1; i <= 50; i++ )
50
 
    {
51
 
        sprintf(name,"my%d",i);
52
 
        rc = SQLExecute(hstmt);
53
 
        mystmt(hstmt,rc);
54
 
    }
55
 
 
56
 
    SQLFreeStmt(hstmt,SQL_RESET_PARAMS);
57
 
    SQLFreeStmt(hstmt,SQL_CLOSE);
58
 
 
59
 
    rc = SQLEndTran(SQL_HANDLE_DBC,hdbc,SQL_COMMIT);
60
 
    mycon(hdbc,rc);
61
 
 
62
 
    /* set row size as 10 */
63
 
    rc = SQLSetStmtAttr(hstmt,SQL_ATTR_ROW_ARRAY_SIZE,(SQLPOINTER)10,0);
64
 
    mystmt(hstmt,rc);
65
 
 
66
 
    rc = SQLSetStmtAttr(hstmt,SQL_ATTR_ROWS_FETCHED_PTR,&nrows,0);
67
 
    mystmt(hstmt,rc);
68
 
 
69
 
    rc = SQLExecDirect(hstmt,(SQLCHAR *)"select * from t_relative",SQL_NTS);
70
 
    mystmt(hstmt,rc);
71
 
 
72
 
    rc = SQLBindCol(hstmt,1,SQL_C_LONG,&iarray,0,NULL);
73
 
    mystmt(hstmt,rc);
74
 
 
75
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_NEXT,0);/* 1-10 */
76
 
    mystmt(hstmt,rc);
77
 
 
78
 
    printMessage("1-10, total rows:%ld\n",nrows);
79
 
    myassert(nrows == 10);
80
 
 
81
 
    for (index=1; index<=nrows; index++)
82
 
    {
83
 
        printMessage("\n %d",iarray[index-1]);
84
 
        myassert(iarray[index-1] == index);
85
 
    }
86
 
 
87
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_NEXT,0);/* 10-20 */
88
 
    mystmt(hstmt,rc);
89
 
 
90
 
    printMessage("\n10-20, total rows:%d\n",nrows);
91
 
 
92
 
    for (index=1; index<=nrows; index++)
93
 
    {
94
 
        printMessage(" %d ",iarray[index-1]);
95
 
        myassert(iarray[index-1] == index+10);
96
 
    }
97
 
 
98
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_PREV,0);/* 1-10 */
99
 
    mystmt(hstmt,rc);
100
 
 
101
 
    printMessage("\n1-10, total rows:%d\n",nrows);
102
 
 
103
 
    for (index=1; index<=nrows; index++)
104
 
    {
105
 
        printMessage(" %d ",iarray[index-1]);
106
 
        myassert(iarray[index-1] == index);
107
 
    }
108
 
 
109
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,1);/* 2-11 */
110
 
    mystmt(hstmt,rc);
111
 
 
112
 
    printMessage("\n2-12, total rows:%d\n",nrows);
113
 
 
114
 
    for (index=1; index<=nrows; index++)
115
 
    {
116
 
        printMessage(" %d ",iarray[index-1]);
117
 
        myassert(iarray[index-1] == index+1);
118
 
    }
119
 
 
120
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-1);/* 1-10 */
121
 
    mystmt(hstmt,rc);
122
 
 
123
 
    printMessage("\n1-10, total rows:%d\n",nrows);
124
 
 
125
 
    for (index=1; index<=nrows; index++)
126
 
    {
127
 
        printMessage(" %d",iarray[index-1]);
128
 
        myassert(iarray[index-1] == index);
129
 
    }
130
 
 
131
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_FIRST,0);/* 1-10 */
132
 
    mystmt(hstmt,rc);
133
 
 
134
 
    printMessage("\n1-10, total rows:%d\n",nrows);
135
 
 
136
 
    for (index=1; index<=nrows; index++)
137
 
    {
138
 
        printMessage(" %d",iarray[index-1]);
139
 
        myassert(iarray[index-1] == index);
140
 
    }
141
 
 
142
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-1);/* BOF */
143
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
144
 
 
145
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,1);/* 1-10 */
146
 
    mystmt(hstmt,rc);
147
 
 
148
 
    printMessage("\n1-10, total rows:%d\n",nrows);
149
 
 
150
 
    for (index=1; index<=nrows; index++)
151
 
    {
152
 
        printMessage(" %d",iarray[index-1]);
153
 
        myassert(iarray[index-1] == index);
154
 
    }
155
 
 
156
 
    SQLFreeStmt(hstmt,SQL_UNBIND);
157
 
    SQLFreeStmt(hstmt,SQL_CLOSE);
158
 
 
159
 
    rc = SQLSetStmtAttr(hstmt,SQL_ATTR_ROW_ARRAY_SIZE,(SQLPOINTER)1,0);
160
 
    mystmt(hstmt,rc);
 
28
  SQLUINTEGER i, iarray[15];
 
29
  SQLULEN nrows, index;
 
30
  SQLCHAR name[21];
 
31
 
 
32
  ok_stmt(hstmt, SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE,
 
33
                                (SQLPOINTER)SQL_CURSOR_STATIC, 0));
 
34
 
 
35
  ok_sql(hstmt, "DROP TABLE IF EXISTS t_relative");
 
36
  ok_sql(hstmt, "CREATE TABLE t_relative (id INT, name CHAR(20))");
 
37
 
 
38
  ok_stmt(hstmt, SQLPrepare(hstmt,
 
39
                            (SQLCHAR *)"INSERT INTO t_relative VALUES (?,?)",
 
40
                            SQL_NTS));
 
41
 
 
42
  ok_stmt(hstmt, SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_ULONG,
 
43
                                  SQL_INTEGER, 0, 0, &i, 0, NULL));
 
44
  ok_stmt(hstmt, SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
 
45
                                  SQL_CHAR, 20, 0, name, 20, NULL));
 
46
 
 
47
  for (i= 1; i <= 50; i++)
 
48
  {
 
49
    sprintf((char *)name, "my%d", i);
 
50
    ok_stmt(hstmt, SQLExecute(hstmt));
 
51
  }
 
52
 
 
53
  ok_stmt(hstmt, SQLFreeStmt(hstmt, SQL_RESET_PARAMS));
 
54
  ok_stmt(hstmt, SQLFreeStmt(hstmt, SQL_CLOSE));
 
55
 
 
56
  /* set row size as 10 */
 
57
  ok_stmt(hstmt, SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_ARRAY_SIZE,
 
58
                                (SQLPOINTER)10, 0));
 
59
 
 
60
  ok_stmt(hstmt, SQLSetStmtAttr(hstmt, SQL_ATTR_ROWS_FETCHED_PTR, &nrows, 0));
 
61
 
 
62
  ok_sql(hstmt, "SELECT * FROM t_relative");
 
63
 
 
64
  ok_stmt(hstmt, SQLBindCol(hstmt, 1, SQL_C_ULONG, &iarray, 0, NULL));
 
65
 
 
66
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_NEXT, 0)); /* 1-10 */
 
67
  is_num(nrows, 10);
 
68
 
 
69
  for (index= 1; index <= nrows; index++)
 
70
    is_num(iarray[index - 1], index);
 
71
 
 
72
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_NEXT, 0)); /* 10-20 */
 
73
  is_num(nrows, 10);
 
74
 
 
75
  for (index= 1; index <= nrows; index++)
 
76
    is_num(iarray[index - 1], index + 10);
 
77
 
 
78
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_PREV, 0)); /* 1-10 */
 
79
  is_num(nrows, 10);
 
80
 
 
81
  for (index= 1; index <= nrows; index++)
 
82
    is_num(iarray[index - 1], index);
 
83
 
 
84
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 1)); /* 2-11 */
 
85
  is_num(nrows, 10);
 
86
 
 
87
  for (index= 1; index <= nrows; index++)
 
88
    is_num(iarray[index - 1], index + 1);
 
89
 
 
90
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -1)); /* 1-10 */
 
91
  is_num(nrows, 10);
 
92
 
 
93
  for (index= 1; index <= nrows; index++)
 
94
    is_num(iarray[index - 1], index);
 
95
 
 
96
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_FIRST, 0)); /* 1-10 */
 
97
  is_num(nrows, 10);
 
98
 
 
99
  for (index= 1; index <= nrows; index++)
 
100
    is_num(iarray[index - 1], index);
 
101
 
 
102
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -1),
 
103
              SQL_NO_DATA_FOUND); /* BOF */
 
104
 
 
105
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 1)); /* 1-10 */
 
106
  is_num(nrows, 10);
 
107
 
 
108
  for (index= 1; index <= nrows; index++)
 
109
    is_num(iarray[index - 1], index);
 
110
 
 
111
  ok_stmt(hstmt, SQLFreeStmt(hstmt, SQL_UNBIND));
 
112
  ok_stmt(hstmt, SQLFreeStmt(hstmt, SQL_CLOSE));
 
113
 
 
114
  /* reset row size */
 
115
  ok_stmt(hstmt, SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_ARRAY_SIZE,
 
116
                                (SQLPOINTER)1, 0));
 
117
 
 
118
  ok_sql(hstmt, "DROP TABLE IF EXISTS t_relative");
161
119
 
162
120
  return OK;
163
121
}
166
124
/* Testing SQL_FETCH_RELATIVE with row_set_size as 1 */
167
125
DECLARE_TEST(t_relative1)
168
126
{
169
 
    SQLRETURN rc;
170
 
    SQLINTEGER nrows;
171
 
    SQLUINTEGER i;
172
 
    const int max_rows=10;
173
 
 
174
 
    SQLExecDirect(hstmt,(SQLCHAR *)"drop table t_relative1",SQL_NTS);
175
 
 
176
 
    rc = SQLExecDirect(hstmt,(SQLCHAR *)"create table t_relative1(id int)",SQL_NTS);
177
 
    mystmt(hstmt,rc);
178
 
 
179
 
    rc = SQLPrepare(hstmt,(SQLCHAR *)"insert into t_relative1 values(?)",SQL_NTS);
180
 
    mystmt(hstmt,rc);
181
 
 
182
 
    rc = SQLBindParameter(hstmt,1,SQL_PARAM_INPUT, SQL_C_ULONG,
183
 
                          SQL_INTEGER,0,0,&i,0,NULL);
184
 
    mystmt(hstmt,rc);
185
 
 
186
 
    for ( i = 1; i <= max_rows; i++ )
187
 
    {
188
 
        rc = SQLExecute(hstmt);
189
 
        mystmt(hstmt,rc);
190
 
    }
191
 
 
192
 
    SQLFreeStmt(hstmt,SQL_RESET_PARAMS);
193
 
    SQLFreeStmt(hstmt,SQL_CLOSE);
194
 
 
195
 
    rc = SQLEndTran(SQL_HANDLE_DBC,hdbc,SQL_COMMIT);
196
 
    mycon(hdbc,rc);
197
 
 
198
 
    /* set row_size as 1 */
199
 
    rc = SQLSetStmtAttr(hstmt,SQL_ATTR_ROW_ARRAY_SIZE,(SQLPOINTER)1,0);
200
 
    mystmt(hstmt,rc);
201
 
 
202
 
    rc = SQLSetStmtAttr(hstmt,SQL_ATTR_ROWS_FETCHED_PTR,&nrows,0);
203
 
    mystmt(hstmt,rc);
204
 
 
205
 
    rc = SQLExecDirect(hstmt,(SQLCHAR *)"select * from t_relative1",SQL_NTS);
206
 
    mystmt(hstmt,rc);
207
 
 
208
 
    rc = SQLBindCol(hstmt,1,SQL_C_LONG,&i,0,NULL);
209
 
    mystmt(hstmt,rc);
210
 
 
211
 
    /* row 1 */
212
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_NEXT,0);/* 1 */
213
 
    mystmt(hstmt,rc);
214
 
    my_assert(i==1);
215
 
 
216
 
    /* Before start */
217
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-1);/* before start */
218
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
219
 
 
220
 
    /* jump to last row */
221
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,max_rows);/* last row */
222
 
    mystmt(hstmt,rc);
223
 
    my_assert(i==max_rows);
224
 
 
225
 
    /* jump to last row+1 */
226
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,1);/* after last */
227
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
228
 
 
229
 
    /* goto first row */
230
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_FIRST,1);/* 1 */
231
 
    mystmt(hstmt,rc);
232
 
    my_assert(i==1);
233
 
 
234
 
    /* before start */
235
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-1);/* before start */
236
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
237
 
 
238
 
    /* goto fifth  row */
239
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,5);/* 5 */
240
 
    mystmt(hstmt,rc);
241
 
    my_assert(i==5);
242
 
 
243
 
    /* goto after end */
244
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,max_rows);/* after last */
245
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
246
 
 
247
 
    /*
248
 
       the scenarios from ODBC spec
249
 
    */
250
 
 
251
 
    /* CASE 1 */
252
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_FIRST,1);/* 1 */
253
 
    mystmt(hstmt,rc);
254
 
    my_assert(i==1);
255
 
 
256
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-1);/* before start */
257
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
258
 
 
259
 
    /* BeforeStart AND FetchOffset <= 0 */
260
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-20);/* before start */
261
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
262
 
 
263
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-1);/* before start */
264
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
265
 
 
266
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,0);/* before start */
267
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
268
 
 
269
 
    /* case 1: Before start AND FetchOffset > 0 */
270
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,1);/* 1 */
271
 
    mystmt(hstmt,rc);
272
 
    my_assert(i==1);
273
 
 
274
 
    /* CASE 2 */
275
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_LAST,1);/* last row */
276
 
    mystmt(hstmt,rc);
277
 
    my_assert(i==max_rows);
278
 
 
279
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,1);/* after end */
280
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
281
 
 
282
 
    /* After end AND FetchOffset >= 0 */
283
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,10);/* after end */
284
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
285
 
 
286
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,20);/* after end */
287
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
288
 
 
289
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,1);/* after end */
290
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
291
 
 
292
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,0);/* after end */
293
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
294
 
 
295
 
    /* After end AND FetchOffset < 0 */
296
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-1);/* last row */
297
 
    mystmt(hstmt,rc);
298
 
    my_assert(i==max_rows);
299
 
 
300
 
 
301
 
    /* CASE 3 */
302
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_FIRST,1);/* first row */
303
 
    mystmt(hstmt,rc);
304
 
    my_assert(i==1);
305
 
 
306
 
    /* CurrRowsetStart = 1 AND FetchOffset < 0 */
307
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,0);/* first row */
308
 
    mystmt(hstmt,rc);
309
 
    my_assert(i==1);
310
 
 
311
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-1);/* before start */
312
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
313
 
 
314
 
    /* CASE 4 */
315
 
    /* CurrRowsetStart > 1 AND CurrRowsetStart + FetchOffset < 1 AND
316
 
       | FetchOffset | > RowsetSize
317
 
    */
318
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_FIRST,1);/* first row */
319
 
    mystmt(hstmt,rc);
320
 
    my_assert(i==1);
321
 
 
322
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,3);/* fourth row */
323
 
    mystmt(hstmt,rc);
324
 
    my_assert(i==4);
325
 
 
326
 
    /* the following call satisfies 4 > 1 AND (3-4) < 1 AND |-4| > 1 */
327
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-4);/* before start */
328
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
329
 
 
330
 
    /* CASE 5 */
331
 
    /* 1 <= CurrRowsetStart + FetchOffset <= LastResultRow */
332
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_FIRST,1);/* first row */
333
 
    mystmt(hstmt,rc);
334
 
    my_assert(i==1);
335
 
 
336
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,5);/* sixth row */
337
 
    mystmt(hstmt,rc);
338
 
    my_assert(i==6);
339
 
 
340
 
    /* 1 <= 6-2 <= 10 */
341
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-2);/* fourth row */
342
 
    mystmt(hstmt,rc);
343
 
    my_assert(i==4);
344
 
 
345
 
    /* CASE 6 */
346
 
    /*  CurrRowsetStart > 1 AND CurrRowsetStart + FetchOffset < 1 AND
347
 
        | FetchOffset | <= RowsetSize
348
 
     */
349
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_FIRST,1);/* first row */
350
 
    mystmt(hstmt,rc);
351
 
    my_assert(i==1);
352
 
 
353
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,3);/* fourth row */
354
 
    mystmt(hstmt,rc);
355
 
    my_assert(i==4);
356
 
 
357
 
    /* 4 >1 AND 4-4 <1 AND |-4| <=10 */
358
 
    rc = SQLFetchScroll(hstmt,SQL_FETCH_RELATIVE,-4);/* before start */
359
 
    mystmt_err(hstmt,rc==SQL_NO_DATA_FOUND,rc);
360
 
 
361
 
 
362
 
    SQLFreeStmt(hstmt,SQL_UNBIND);
363
 
    SQLFreeStmt(hstmt,SQL_CLOSE);
364
 
 
365
 
    rc = SQLSetStmtAttr(hstmt,SQL_ATTR_ROW_ARRAY_SIZE,(SQLPOINTER)1,0);
366
 
    mystmt(hstmt,rc);
 
127
  SQLULEN nrows;
 
128
  SQLUINTEGER i;
 
129
  const SQLUINTEGER max_rows= 10;
 
130
 
 
131
  ok_stmt(hstmt, SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE,
 
132
                                (SQLPOINTER)SQL_CURSOR_STATIC, 0));
 
133
 
 
134
  ok_sql(hstmt, "DROP TABLE IF EXISTS t_relative1");
 
135
  ok_sql(hstmt, "CREATE TABLE t_relative1 (id INT)");
 
136
 
 
137
  ok_stmt(hstmt, SQLPrepare(hstmt,
 
138
                            (SQLCHAR *)"INSERT INTO t_relative1 VALUES (?)",
 
139
                            SQL_NTS));
 
140
 
 
141
  ok_stmt(hstmt, SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_ULONG,
 
142
                                  SQL_INTEGER, 0, 0, &i, 0, NULL));
 
143
 
 
144
  for (i= 1; i <= max_rows; i++)
 
145
  {
 
146
    ok_stmt(hstmt, SQLExecute(hstmt));
 
147
  }
 
148
 
 
149
  ok_stmt(hstmt, SQLFreeStmt(hstmt, SQL_RESET_PARAMS));
 
150
  ok_stmt(hstmt, SQLFreeStmt(hstmt, SQL_CLOSE));
 
151
 
 
152
  /* set row_size as 1 */
 
153
  ok_stmt(hstmt, SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_ARRAY_SIZE,
 
154
                                (SQLPOINTER)1, 0));
 
155
 
 
156
  ok_stmt(hstmt, SQLSetStmtAttr(hstmt, SQL_ATTR_ROWS_FETCHED_PTR, &nrows, 0));
 
157
 
 
158
  ok_sql(hstmt, "SELECT * FROM t_relative1");
 
159
 
 
160
  ok_stmt(hstmt, SQLBindCol(hstmt, 1, SQL_C_LONG, &i, 0, NULL));
 
161
 
 
162
  /* row 1 */
 
163
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_NEXT, 0));
 
164
  is_num(i, 1);
 
165
 
 
166
  /* Before start */
 
167
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -1),
 
168
              SQL_NO_DATA_FOUND);
 
169
 
 
170
  /* jump to last row */
 
171
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, max_rows));
 
172
  is_num(i, max_rows);
 
173
 
 
174
  /* jump to last row+1 */
 
175
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 1),
 
176
              SQL_NO_DATA_FOUND);
 
177
 
 
178
  /* goto first row */
 
179
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_FIRST, 1));
 
180
  is_num(i, 1);
 
181
 
 
182
  /* before start */
 
183
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -1),
 
184
              SQL_NO_DATA_FOUND);
 
185
 
 
186
  /* goto fifth  row */
 
187
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 5));
 
188
  is_num(i, 5);
 
189
 
 
190
  /* goto after end */
 
191
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, max_rows),
 
192
              SQL_NO_DATA_FOUND);
 
193
 
 
194
  /* the scenarios from ODBC spec */
 
195
 
 
196
  /* CASE 1 */
 
197
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_FIRST, 1));
 
198
  is_num(i, 1);
 
199
 
 
200
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -1),
 
201
              SQL_NO_DATA_FOUND);
 
202
 
 
203
  /* BeforeStart AND FetchOffset <= 0 */
 
204
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -20),
 
205
              SQL_NO_DATA_FOUND);
 
206
 
 
207
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -1),
 
208
              SQL_NO_DATA_FOUND);
 
209
 
 
210
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 0),
 
211
              SQL_NO_DATA_FOUND);
 
212
 
 
213
  /* case 1: Before start AND FetchOffset > 0 */
 
214
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 1));
 
215
  is_num(i, 1);
 
216
 
 
217
  /* CASE 2 */
 
218
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_LAST, 1));
 
219
  is_num(i, max_rows);
 
220
 
 
221
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 1),
 
222
              SQL_NO_DATA_FOUND);
 
223
 
 
224
  /* After end AND FetchOffset >= 0 */
 
225
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 10),
 
226
              SQL_NO_DATA_FOUND);
 
227
 
 
228
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 20),
 
229
              SQL_NO_DATA_FOUND);
 
230
 
 
231
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 1),
 
232
              SQL_NO_DATA_FOUND);
 
233
 
 
234
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 0),
 
235
              SQL_NO_DATA_FOUND);
 
236
 
 
237
  /* After end AND FetchOffset < 0 */
 
238
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -1));
 
239
  is_num(i, max_rows);
 
240
 
 
241
  /* CASE 3 */
 
242
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_FIRST, 1));
 
243
  is_num(i, 1);
 
244
 
 
245
  /* CurrRowsetStart = 1 AND FetchOffset < 0 */
 
246
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 0));
 
247
  is_num(i, 1);
 
248
 
 
249
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -1),
 
250
              SQL_NO_DATA_FOUND);
 
251
 
 
252
  /* CASE 4 */
 
253
  /* CurrRowsetStart > 1 AND CurrRowsetStart + FetchOffset < 1 AND
 
254
     | FetchOffset | > RowsetSize
 
255
  */
 
256
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_FIRST, 1));
 
257
  is_num(i, 1);
 
258
 
 
259
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 3));
 
260
  is_num(i, 4);
 
261
 
 
262
  /* the following call satisfies 4 > 1 AND (3-4) < 1 AND |-4| > 1 */
 
263
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -4),
 
264
              SQL_NO_DATA_FOUND);
 
265
 
 
266
  /* CASE 5 */
 
267
  /* 1 <= CurrRowsetStart + FetchOffset <= LastResultRow */
 
268
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_FIRST, 1));
 
269
  is_num(i, 1);
 
270
 
 
271
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 5));
 
272
  is_num(i, 6);
 
273
 
 
274
  /* 1 <= 6-2 <= 10 */
 
275
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -2));
 
276
  is_num(i, 4);
 
277
 
 
278
  /* CASE 6 */
 
279
  /*  CurrRowsetStart > 1 AND CurrRowsetStart + FetchOffset < 1 AND
 
280
      | FetchOffset | <= RowsetSize
 
281
   */
 
282
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_FIRST, 1));
 
283
  is_num(i, 1);
 
284
 
 
285
  ok_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 3));
 
286
  is_num(i, 4);
 
287
 
 
288
  /* 4 >1 AND 4-4 <1 AND |-4| <=10 */
 
289
  expect_stmt(hstmt, SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, -4),
 
290
              SQL_NO_DATA_FOUND);
 
291
 
 
292
  ok_stmt(hstmt, SQLFreeStmt(hstmt, SQL_UNBIND));
 
293
  ok_stmt(hstmt, SQLFreeStmt(hstmt, SQL_CLOSE));
 
294
 
 
295
  ok_sql(hstmt, "DROP TABLE IF EXISTS t_relative1");
367
296
 
368
297
  return OK;
369
298
}
372
301
/* Testing SQL_FETCH_RELATIVE with row_set_size as 2 */
373
302
DECLARE_TEST(t_relative2)
374
303
{
375
 
    SQLRETURN rc;
376
 
    SQLINTEGER nrows,iarray[15];
377
 
    SQLUINTEGER i;
378
 
    const int max_rows=10;
 
304
  SQLRETURN rc;
 
305
  SQLULEN nrows;
 
306
  SQLUINTEGER i, iarray[15];
 
307
  const SQLUINTEGER max_rows=10;
 
308
 
 
309
    ok_stmt(hstmt, SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE,
 
310
                                  (SQLPOINTER)SQL_CURSOR_STATIC, 0));
379
311
 
380
312
    SQLExecDirect(hstmt,(SQLCHAR *)"drop table t_relative2",SQL_NTS);
381
313
 
616
548
DECLARE_TEST(t_rows_fetched_ptr)
617
549
{
618
550
    SQLRETURN    rc;
619
 
    SQLINTEGER   rowsFetched, rowsSize;
 
551
    SQLULEN rowsFetched, rowsSize;
620
552
    long         i;
621
553
 
622
554
    SQLExecDirect(hstmt,(SQLCHAR *)"drop table t_rows_fetched_ptr",SQL_NTS);
748
680
DECLARE_TEST(t_rows_fetched_ptr1)
749
681
{
750
682
  SQLRETURN   rc;
751
 
  SQLLEN      rowsFetched, rowsSize;
 
683
  SQLULEN     rowsFetched, rowsSize;
752
684
  SQLINTEGER  i;
753
685
 
754
686
    SQLExecDirect(hstmt,(SQLCHAR *)"drop table t_rows_fetched_ptr",SQL_NTS);