File: cursor.c

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (353 lines) | stat: -rw-r--r-- 9,410 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 *  $Id: cursor.c,v 1.2.2.1 2009/04/20 21:18:31 source Exp $
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2009 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#ifdef UNIX
#include <sys/time.h>
#endif

#include "timeacct.h"
#include <stdio.h>
#include <string.h>
#include <memory.h>

#include "odbcinc.h"

#include "odbcuti.h"

#include <stdlib.h>



int messages_off = 0;
int quiet = 0;



HDBC hdbc;
HENV henv;
HSTMT read_stmt;
HSTMT del_stmt;

timer_account_t del_ta;

#ifdef UDBC

#define ROWSET_SZ 20

UDWORD crow = 1;

void del_3 (char * cursor_text, int n_rows)
{
  long row_no [ROWSET_SZ];
  long fi2 [ROWSET_SZ];
  UWORD rowstat [ROWSET_SZ];
  int rc;
  int ctr = 0;
  printf ("%s\n", cursor_text);
  ta_init (& del_ta, cursor_text);
  ta_enter (& del_ta);
  SQLSetStmtOption (read_stmt, SQL_BIND_TYPE, SQL_BIND_BY_COLUMN);
  SQLSetStmtOption (read_stmt, SQL_KEYSET_SIZE, 2);
  SQLSetStmtOption (read_stmt, SQL_CONCURRENCY, SQL_CONCUR_VALUES);
  SQLSetCursorName (read_stmt, "CR", SQL_NTS);
  IF_ERR_GO (read_stmt, end, SQLExecDirect (read_stmt, cursor_text, SQL_NTS));

  while (1) {
    rc = SQLExtendedFetch (read_stmt, SQL_FETCH_NEXT, 1, & crow, rowstat);
    if (rc == SQL_NO_DATA_FOUND) break;
    IF_ERR_GO (read_stmt, end, rc);
    if (ctr % 3 == 0) {
      IF_ERR_GO (read_stmt, end, SQLSetPos (read_stmt, 1, SQL_DELETE, SQL_LOCK_NO_CHANGE));

      printf ("Delete OK\n");
    }
    ctr ++;
    if (ctr > n_rows)
      break;
  }
 end:
  SQLFreeStmt (read_stmt, SQL_CLOSE);
  SQLTransact (SQL_NULL_HENV, hdbc, SQL_COMMIT);
  ta_leave (& del_ta);
  ta_print_out (stdout, & del_ta);
}


long account [ROWSET_SZ];
long branch [ROWSET_SZ];
UWORD rowstat [ROWSET_SZ];
SDWORD len_array [ROWSET_SZ];


void acct_3 (char * cursor_text, int n_rows)
{
  UDWORD crow = 1;
  int rc;
  int ctr = 0;
  printf ("%s\n", cursor_text);
  ta_init (& del_ta, cursor_text);
  ta_enter (& del_ta);
  IF_ERR_GO (read_stmt, end, SQLSetStmtOption (read_stmt, SQL_BIND_TYPE, SQL_BIND_BY_COLUMN));
  SQLSetStmtOption (read_stmt, SQL_KEYSET_SIZE, 100);
  SQLSetStmtOption (read_stmt, SQL_ROWSET_SIZE, 4);

  IF_ERR_GO (read_stmt, end, SQLSetStmtOption (read_stmt, SQL_CONCURRENCY, SQL_CONCUR_TIMESTAMP));
  SQLSetCursorName (read_stmt, "CR", SQL_NTS);
  IF_ERR_GO (read_stmt, end, SQLExecDirect (read_stmt, cursor_text, SQL_NTS));


  SQLBindCol (read_stmt, 1, SQL_C_LONG, (void*) & account, sizeof (long), len_array);
  SQLBindCol (read_stmt, 2, SQL_C_LONG, (void*) & branch [0], sizeof (long), len_array);

  while (1) {
    rc = SQLExtendedFetch (read_stmt, SQL_FETCH_NEXT, 2, & crow, rowstat);
    if (rc == SQL_NO_DATA_FOUND) break;
    IF_ERR_GO (read_stmt, end, rc);

    branch [0] = 900;
    printf ("Update OK.\n");
    IF_ERR_GO (read_stmt, end, SQLSetPos (read_stmt, 1, SQL_UPDATE, SQL_LOCK_NO_CHANGE));
    branch [0] = -1;
    IF_ERR_GO (read_stmt, end, SQLSetPos (read_stmt, 1, SQL_REFRESH, SQL_LOCK_NO_CHANGE));
    if (branch [0] == 900) printf ("Refresh OK\n");
    else printf ("*** Refreshed to %ld.\n", branch [0]);
    IF_ERR_GO (read_stmt, end, SQLSetPos (read_stmt, 2, SQL_DELETE, SQL_LOCK_NO_CHANGE));
    printf ("Delete OK.\n");
    account [0] += 2000;
    IF_ERR_GO (read_stmt, end, SQLSetPos (read_stmt, 1, SQL_ADD, SQL_LOCK_NO_CHANGE));
    printf ("Add OK.\n");
    IF_ERR_GO (read_stmt, end, SQLSetPos (read_stmt, 1, SQL_POSITION, SQL_LOCK_NO_CHANGE));
    printf ("Position OK.\n");
    /* IF_ERR_GO (del_stmt, end, SQLExecDirect (del_stmt, "DELETE FROM ACCOUNT WHERE CURRENT OF CR", SQL_NTS)); */


    ctr ++;
    if (ctr > n_rows)
      break;
  }
 end:
  SQLFreeStmt (read_stmt, SQL_CLOSE);
  SQLTransact (SQL_NULL_HENV, hdbc, SQL_COMMIT);
  ta_leave (& del_ta);
  ta_print_out (stdout, & del_ta);
}


#else

void del_3 (char * cursor_text, int n_rows)
{
  int ctr = 0;
  printf ("%s\n", cursor_text);
  ta_init (& del_ta, cursor_text);
  ta_enter (& del_ta);
  IF_ERR_GO (read_stmt, end, SQLExecDirect (read_stmt, cursor_text, SQL_NTS));
  while (SQL_SUCCESS == SQLFetch (read_stmt)) {
    if (ctr % 3 == 0) {
      IF_ERR_GO (del_stmt, end, SQLExecute (del_stmt));
    }
    ctr ++;
    if (ctr > n_rows)
      break;
  }
 end:
  SQLFreeStmt (read_stmt, SQL_CLOSE);
  SQLTransact (SQL_NULL_HENV, hdbc, SQL_COMMIT);
  ta_leave (& del_ta);
  ta_print_out (stdout, & del_ta);
}

#endif

















void print_error (HSTMT e1, HSTMT e2, HSTMT e3)
{
  int len;
  char state [10];
  char message [1000];
  SQLError (e1, e2, e3, (UCHAR *) state, NULL,
	    (UCHAR *) & message, sizeof (message),
	    (SWORD *) & len);
  printf ("\n*** Error %s: %s\n", state, message);
}


#define ROWSET_SZ 20

void close_test (int autocommit, int batch, int read_n, char * text)
{
  int inx;
  HSTMT stmt;

  printf ("close test %d %d %d %s\n", autocommit, batch, read_n, text);
  SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, autocommit);
  SQLAllocStmt (hdbc, & stmt);
  if (batch > 10)
    SQLSetStmtOption (stmt, SQL_ROWSET_SIZE, 3);
  IF_ERR_GO (stmt, err, SQLPrepare (stmt, text, SQL_NTS));
  IF_ERR_GO (stmt, err, SQLExecute (stmt));
  for (inx = 0; inx < read_n; inx++) {
    IF_ERR_GO (stmt, err, SQLFetch (stmt));
  }
  SQLFreeStmt (stmt, SQL_DROP);
 err: ;
  SQLTransact (SQL_NULL_HENV, hdbc, SQL_COMMIT);
}

void close_sync_test (int autocommit, int batch, int read_n, char * text)
{
  int inx, cnt;
  HSTMT stmt;

  printf ("close sync test  %s\n", text);
  SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, autocommit);
  SQLAllocStmt (hdbc, & stmt);
  for (cnt = 0; cnt < 10; cnt ++) {

    if (batch > 10)
      SQLSetStmtOption (stmt, SQL_ROWSET_SIZE, batch);
    IF_ERR_GO (stmt, err, SQLPrepare (stmt, text, SQL_NTS));
    IF_ERR_GO (stmt, err, SQLExecute (stmt));
    for (inx = 0; inx < read_n; inx++) {
      IF_ERR_GO (stmt, err, SQLFetch (stmt));
    }
    SQLFreeStmt (stmt, SQL_CLOSE);
  }
 err: ;
  SQLFreeStmt (stmt, SQL_DROP);
  SQLTransact (SQL_NULL_HENV, hdbc, SQL_COMMIT);
}



char * inc_text = "create procedure inc (inout x integer) { x := x + 1; }";


void in_out_test ()
{

  long io = 1, iol = 4;
  HSTMT stmt;
  SQLAllocStmt (hdbc, & stmt);
  IF_ERR_GO (stmt, err, SQLExecDirect (stmt, inc_text, SQL_NTS));

  SQLBindParameter (stmt, 1, SQL_PARAM_INPUT_OUTPUT, SQL_C_LONG, SQL_INTEGER,
		    4, 0, & io, 4, & iol);
  IF_ERR_GO (stmt, err, SQLExecDirect (stmt, "inc (?)", SQL_NTS));
  printf ("Input output param set to %ld\n", io);
err: ;
}






int main (int argc, char ** argv)
{
  char cr_ret [16];
  SWORD cr_ret_len;
  char* uid = "dba", * pwd = "dba";
  long  times;

  if (argc < 2) {
    printf ("Usage: cursor dsn delete-n [user] [password]\n");
    exit (1);
  }
  times  = atoi (argv [2]);

  if (argc > 3) {
	  uid = argv [3]; pwd = argv [4];
  }

  SQLAllocEnv (& henv);
  SQLAllocConnect (henv, &hdbc);

  if (SQL_ERROR == SQLConnect (hdbc, (UCHAR *) argv [1], SQL_NTS,
				 (UCHAR *) uid, SQL_NTS,
				 (UCHAR *) pwd, SQL_NTS)) {
    print_error (SQL_NULL_HENV, hdbc, SQL_NULL_HSTMT);
    exit (1);
  }
  in_out_test ();

  INIT_STMT (hdbc, read_stmt, "select ROW_NO from T1");
  INIT_STMT (hdbc, del_stmt, "delete from T1 where current of CR");
  SQLSetStmtOption (read_stmt, SQL_CURSOR_TYPE, SQL_CURSOR_KEYSET_DRIVEN);
  SQLSetCursorName (read_stmt, "CR", SQL_NTS);
  SQLGetCursorName (read_stmt, cr_ret, sizeof (cr_ret), & cr_ret_len);
  if (cr_ret_len != 2 || 0 != strcmp ("CR", cr_ret))
    printf ("*** bad cu name %s, %d chars returned.\n", cr_ret, cr_ret_len);



  SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, 0);



#ifdef UDBC
  acct_3 ("select account, branch  from account where account > 10", times);
  acct_3 ("select ROW_NO, FI2 from T1", times);

  SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, 1);

  acct_3 ("select account, branch  from account where account > 10", times);

  exit (0);
#else
  del_3 ("select FS1 from T1 where ROW_NO > 1", times);
  del_3 ("select FS1 from T1 where ROW_NO = 1111", times);
  del_3 ("select FS1 from T1 where STRING1 > '1'", times);
  del_3 ("select ROW_NO from T1 where STRING1 > '2'", times);
  del_3 ("select ROW_NO from T1", times);
#endif

  close_test (0, 1000, 0, "select * from T1 where FS1 like 'qqq'");
  close_test (1, 1000, 0, "select * from T1 where FS1 like 'qqq'");
  close_test (0, 1000, 10, "select * from T1");
  close_test (1, 1000, 1002, "select * from T1");
  close_test (0, 20, 20, "select * from T1");
  close_test (1, 20, 20, "select * from T1");

  close_sync_test (0, 100, 0, "select ROW_NO from T1 where FS1 like 'xx'");
  close_sync_test (1, 100, 0, "select ROW_NO from T1 where FS1 like 'xx'");
  close_sync_test (0, 1000, 1, "select ROW_NO from T1");
  close_sync_test (1, 1000, 1, "select ROW_NO from T1");
  return 0;
}