File: odbctest.C

package info (click to toggle)
sqlrelay 1%3A0.37.1-3.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 13,084 kB
  • ctags: 6,691
  • sloc: cpp: 48,136; python: 10,118; ansic: 9,673; java: 9,195; php: 8,839; perl: 8,827; sh: 8,554; ruby: 8,516; tcl: 5,039; makefile: 3,665
file content (139 lines) | stat: -rw-r--r-- 2,911 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
// Copyright (c) 2000-2001  David Muse
// See the file COPYING for more information

#include "../../config.h"

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

#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>

#define FETCH_AT_ONCE		10
#define MAX_SELECT_LIST_SIZE	256
#define	MAX_ITEM_BUFFER_SIZE	4096

#ifdef HAVE_UNIXODBC
char		field[MAX_SELECT_LIST_SIZE]
			[FETCH_AT_ONCE][MAX_ITEM_BUFFER_SIZE];
SQLINTEGER	indicator[MAX_SELECT_LIST_SIZE][FETCH_AT_ONCE];
#else
char		field[MAX_SELECT_LIST_SIZE][MAX_ITEM_BUFFER_SIZE];
SQLINTEGER	indicator[MAX_SELECT_LIST_SIZE];
#endif

int main(int argc, char **argv) {

	if (argc<4) {
		printf("usage: odbctest dsn query iterations\n");
		exit(0);
	}

	char	*dsn=argv[1];
	char	*query=argv[2];
	int	iterations=atoi(argv[3]);
	int	length=strlen(query);

	SQLHENV		env;
	SQLHDBC		dbc;
	SQLHSTMT	stmt;
	SQLSMALLINT	ncols;

	// init the timer
	time_t	starttime=time(NULL);
	clock();

	for (int count=0; count<iterations; count++) {

#ifdef HAVE_UNIXODBC
		SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env);
		SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc);
#else
		SQLAllocEnv(&env);
		SQLAllocConnect(env,&dbc);
#endif
		SQLConnect(dbc,(SQLCHAR *)dsn,SQL_NTS,
				(SQLCHAR *)"",SQL_NTS,
				(SQLCHAR *)"",SQL_NTS);

		// execute the query
#ifdef HAVE_UNIXODBC
		SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt);
		SQLSetStmtAttr(stmt,SQL_ATTR_ROW_ARRAY_SIZE,
				(SQLPOINTER)FETCH_AT_ONCE,0);
#else
		SQLAllocStmt(dbc,&stmt);
#endif
		SQLPrepare(stmt,(SQLCHAR *)query,length);
		SQLExecute(stmt);
		SQLNumResultCols(stmt,&ncols);

		for (int i=0; i<ncols; i++) {
			SQLBindCol(stmt,i+1,SQL_C_CHAR,
					field[i],MAX_ITEM_BUFFER_SIZE,
					(SQLINTEGER *)indicator[i]);
		}

#ifdef HAVE_UNIXODBC
		// run through the rows
		int	oldrow=0;
		int	rownumber;
		for (;;) {
			SQLFetchScroll(stmt,SQL_FETCH_NEXT,0);
			SQLGetStmtAttr(stmt,SQL_ATTR_ROW_NUMBER,
					(SQLPOINTER)&rownumber,0,NULL);
			if (rownumber==oldrow) {
				break;
			}

			for (int row=oldrow; row<rownumber; row++) {
				for (int col=0; col<ncols; col++) {

					if (indicator[col][row]==
							SQL_NULL_DATA) {
						//printf("NULL,");
					} else {
						//printf("%s,",field[col][row]);
					}
				}
				//printf("\n");
			}
			oldrow=rownumber;
		}
#else
		SQLFetch(stmt);
		for (int col=0; col<ncols; col++) {

			if (indicator[col]==SQL_NULL_DATA) {
				printf("NULL,");
			} else {
				printf("%s,",field[col]);
			}
		}
		printf("\n");
#endif

		// free the result set
#ifdef HAVE_UNIXODBC
		SQLFreeHandle(SQL_HANDLE_STMT,stmt);
#else
		SQLFreeStmt(stmt,SQL_DROP);
#endif

		// log off
		SQLDisconnect(dbc);
#ifdef HAVE_UNIXODBC
		SQLFreeHandle(SQL_HANDLE_DBC,dbc);
		SQLFreeHandle(SQL_HANDLE_ENV,env);
#else
		SQLFreeConnect(&dbc);
		SQLFreeEnv(&env);
#endif
	}

	printf("total system time used: %ld\n",clock());
	printf("total real time: %ld\n",time(NULL)-starttime);
}