File: b3078.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 (407 lines) | stat: -rw-r--r-- 9,352 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 *
 *  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
 *
 */

#include <stdio.h>
#include <string.h>

#include "odbcinc.h"

SQLHENV  henv = 0;
SQLHDBC  hdbc = 0;
SQLHSTMT hstmt = 0;

#define MAXCOLS  25
#define ARRAY_SIZE 3

int
ODBC_Connect (char *dsn, char *usr, char *pwd)
{
  SQLRETURN  retcode;

  retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
    {
      /* Set the ODBC version environment attribute */
      retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);

      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
	{
	  /* Allocate connection handle */
	  retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);

	  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
	    {

	      /* Connect to data source */
	      retcode = SQLConnect(hdbc, dsn, SQL_NTS, usr, SQL_NTS, pwd, SQL_NTS);

	      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
		{
		  /* Allocate statement handle */
		  retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);

		  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
		    return 0;
		  SQLDisconnect(hdbc);
		}
	      SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
	    }
	}
      SQLFreeHandle(SQL_HANDLE_ENV, henv);
    }
  return -1;
}


int
ODBC_Disconnect (void)
{
  if (hstmt)
    SQLFreeStmt (hstmt, SQL_DROP);

  if (hdbc)
    SQLDisconnect (hdbc);

  if (hdbc)
    SQLFreeHandle (SQL_HANDLE_DBC, hdbc);

  if (henv)
    SQLFreeHandle (SQL_HANDLE_ENV, henv);

  return 0;
}


int
ODBC_Errors (char *where)
{
  unsigned char buf[250];
  unsigned char sqlstate[15];

  /*
   *  Get statement errors
   */
  while (SQLError (henv, hdbc, hstmt, sqlstate, NULL,
	buf, sizeof(buf), NULL) == SQL_SUCCESS)
    {
      fprintf (stdout, "%s ||%s, SQLSTATE=%s\n", where, buf, sqlstate);
    }

  /*
   *  Get connection errors
   */
  while (SQLError (henv, hdbc, SQL_NULL_HSTMT, sqlstate, NULL,
	buf, sizeof(buf), NULL) == SQL_SUCCESS)
    {
      fprintf (stdout, "%s ||%s, SQLSTATE=%s\n", where, buf, sqlstate);
    }

  /*
   *  Get environmental errors
   */
  while (SQLError (henv, SQL_NULL_HDBC, SQL_NULL_HSTMT, sqlstate, NULL,
	buf, sizeof(buf), NULL) == SQL_SUCCESS)
    {
      fprintf (stdout, "%s ||%s, SQLSTATE=%s\n", where, buf, sqlstate);
    }

  return -1;
}


int
ODBC_PrintResult()
{
  char fetchBuffer[1000];
  short displayWidths[MAXCOLS];
  short displayWidth;
  short numCols;
  short colNum;
  char colName[50];
  short colType;
  UDWORD colPrecision;
  SDWORD colIndicator;
  short colScale;
  short colNullable;
  UDWORD totalRows;
  UDWORD totalSets;
  int i;

  totalSets = 0;
  do
    {

      /*
       *  Get the number of result columns for this cursor.
       *  If it is 0, then the statement was probably a select
       */
      if (SQLNumResultCols (hstmt, &numCols) != SQL_SUCCESS)
	{
	  ODBC_Errors ("SQLNumResultCols");
	  goto endCursor;
	}
      if (numCols == 0)
	{
	  printf ("Statement executed.\n");
	  goto endCursor;
	}

      if (numCols > MAXCOLS)
	numCols = MAXCOLS;

      /*
       *  Get the names for the columns
       */
      putchar ('\n');
      for (colNum = 1; colNum <= numCols; colNum++)
	{
	  /*
	   *  Get the name and other type information
	   */
	  if (SQLDescribeCol (hstmt, colNum, (UCHAR *) colName,
		sizeof (colName), NULL, &colType, &colPrecision,
		&colScale, &colNullable) != SQL_SUCCESS)
	    {
	      ODBC_Errors ("SQLDescribeCol");
	      goto endCursor;
	    }
	  /*
	   *  Calculate the display width for the column
	   */
	  switch (colType)
	    {
	      case SQL_VARCHAR:
	      case SQL_CHAR:
		  displayWidth = (short) colPrecision;
		  break;
	      case SQL_BIT:
		  displayWidth = 1;
		  break;
	      case SQL_TINYINT:
	      case SQL_SMALLINT:
	      case SQL_INTEGER:
	      case SQL_BIGINT:
		  displayWidth = colPrecision + 1;	/* sign */
		  break;
	      case SQL_DOUBLE:
	      case SQL_DECIMAL:
	      case SQL_NUMERIC:
	      case SQL_FLOAT:
	      case SQL_REAL:
		  displayWidth = colPrecision + 2;	/* sign, comma */
		  break;
	      case SQL_DATE:
		  displayWidth = 10;
		  break;
	      case SQL_TIME:
		  displayWidth = 8;
		  break;
	      case SQL_TIMESTAMP:
		  displayWidth = 19;
		  break;
	      default:
		  displayWidths[colNum - 1] = 0;	/* skip other data types */
		  continue;
	    }

	  if (displayWidth < strlen (colName))
	    displayWidth = strlen (colName);
	  if (displayWidth > sizeof (fetchBuffer) - 1)
	    displayWidth = sizeof (fetchBuffer) - 1;

	  displayWidths[colNum - 1] = displayWidth;

	  /*
	   *  Print header field
	   */
	  printf ("%-*.*s", displayWidth, displayWidth, colName);
	  if (colNum < numCols)
	    putchar ('|');
	}
      putchar ('\n');

      /*
       *  Print second line
       */
      for (colNum = 1; colNum <= numCols; colNum++)
	{
	  for (i = 0; i < displayWidths[colNum - 1]; i++)
	    putchar ('-');
	  if (colNum < numCols)
	    putchar ('+');
	}
      putchar ('\n');

      /*
       *  Print all the fields
       */
      totalRows = 0;
      while (1)
	{
	  int sts = SQLFetch (hstmt);

	  if (sts == SQL_NO_DATA_FOUND)
	    break;

	  if (sts != SQL_SUCCESS)
	    {
	      ODBC_Errors ("Fetch");
	      break;
	    }
	  for (colNum = 1; colNum <= numCols; colNum++)
	    {
	      /*
	       *  Fetch this column as character
	       */
	      if (SQLGetData (hstmt, colNum, SQL_CHAR, fetchBuffer,
		    sizeof (fetchBuffer), &colIndicator) != SQL_SUCCESS)
		{
		  ODBC_Errors ("SQLGetData");
		  goto endCursor;
		}

	      /*
	       *  Show NULL fields as ****
	       */
	      if (colIndicator == SQL_NULL_DATA)
		{
		  for (i = 0; i < displayWidths[colNum - 1]; i++)
		    fetchBuffer[i] = '*';
		  fetchBuffer[i] = '\0';
		}

	      printf ("%-*.*s", displayWidths[colNum - 1],
		  displayWidths[colNum - 1], fetchBuffer);
	      if (colNum < numCols)
		putchar ('|');
	    }
	  putchar ('\n');
	  totalRows++;
	}

      printf ("\n result set %lu returned %lu rows.\n\n",
	  totalSets, totalRows);
      totalSets++;
    }
  while (SQLMoreResults (hstmt) == SQL_SUCCESS);

endCursor:
  printf ("\n");
  fprintf (stderr, "\n");
  if (totalSets == ARRAY_SIZE)
    {
      printf ("PASSED: ");
      fprintf (stderr, "PASSED: ");
    }
  else
    {
      printf ("***FAILED: ");
      fprintf (stderr, "***FAILED: ");
    }
  printf ("B3078 : array parameters selects returned %lu resultsets\n", totalSets);
  fprintf (stderr, "B3078 : array parameters selects returned %lu resultsets\n", totalSets);
  SQLFreeStmt (hstmt, SQL_CLOSE);

  return 0;
}


int
ODBC_Execute()
{

  SQLCHAR *      Statement = "select * from BTEST where id > ?";
  SQLUINTEGER    IDArray[ARRAY_SIZE];

  SQLINTEGER     IDIndArray[ARRAY_SIZE];

  SQLUSMALLINT   i, ParamStatusArray[ARRAY_SIZE];
  SQLUINTEGER    ParamsProcessed;

  if (SQLParamOptions(hstmt, ARRAY_SIZE, &ParamsProcessed) != SQL_SUCCESS)
    {
      ODBC_Errors ("ODBC_Execute");
      return -1;
    }

  IDArray[0] = 3;  IDIndArray[0] = 0;
  IDArray[1] = 2;  IDIndArray[1] = 0;
  IDArray[2] = 1;  IDIndArray[2] = 0;

  /*    Bind the parameters in column-wise fashion. */
  if (SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 5, 0, IDArray, 0, IDIndArray) != SQL_SUCCESS)
    {
      ODBC_Errors ("ODBC_Execute");
      return -1;
    }


  /*   Execute the statement. */
  if (SQLExecDirect(hstmt, Statement, SQL_NTS) != SQL_SUCCESS)
    {
      ODBC_Errors ("ODBC_Execute");
      return -1;
    }

  /*    Check to see which sets of parameters were processed successfully. */
  printf("Parameter Sets Processed = %d\n",ParamsProcessed);
  printf("--------------------------------------\n");
  return 0;
}


int
main(int argc, char *argv[])
{
  /*   if (ODBC_Connect ("O_Sql2k", "sa", "") != 0) */
  if (ODBC_Connect (argv[1], argv[2], argv[3]) != 0)
    {
      ODBC_Errors ("ODBC_Connect");
    }
  else if (ODBC_Execute () != 0)
    {
      ODBC_Errors ("ODBC_Test");
    }
  else if (ODBC_PrintResult () != 0)
    {
      ODBC_Errors ("ODBC_Test");
    }
  /*
   *  End the connection
   */
  ODBC_Disconnect ();

  exit(0);
}


/*******************************************************************
The table for a test:

create table BTEST(id integer, name varchar(20));
insert into BTEST values(1, '11111111111');
insert into BTEST values(2, '22222222222');
insert into BTEST values(3, '33333333333');
insert into BTEST values(4, '44444444444');

*********************************************************************/