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

« back to all changes in this revision

Viewing changes to test/mytest.h

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2005-06-28 16:07:14 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050628160714-bbrx5607325j9rqt
Tags: 3.51.11-4
Doesn't build cleanly with REAL_64_BIT_MODE: fix some conflicting
types in MyODBC so that they're 64-bittable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          mytest.h  -  description
 
3
                             -------------------
 
4
    begin                : Wed Aug 8 2001
 
5
    copyright            : (C) 2001 by Venu, MySQL AB
 
6
    email                : venu@mysql.com
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
#ifndef __TMYODBC__TEST__H
 
19
#define __TMYODBC__TEST__H
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include <myconf.h>
 
23
#endif
 
24
 
 
25
#ifdef WIN32
 
26
#include <windows.h>
 
27
#endif 
 
28
 
 
29
#ifndef MY_DEBUG
 
30
#define MY_DEBUG
 
31
#endif
 
32
 
 
33
/* STANDARD C HEADERS */
 
34
#include <stdio.h>
 
35
#include <stdlib.h>
 
36
#include <assert.h>
 
37
 
 
38
/* ODBC HEADERS */
 
39
#include <sql.h>
 
40
#include <sqlext.h>
 
41
 
 
42
#ifndef NULL
 
43
#define NULL 0
 
44
#endif
 
45
 
 
46
#ifndef ushort
 
47
#define ushort unsigned short
 
48
#endif
 
49
 
 
50
#ifndef bool
 
51
#define bool unsigned char
 
52
#endif
 
53
 
 
54
#ifndef true
 
55
#define true 1
 
56
#endif
 
57
 
 
58
#ifndef false
 
59
#define false 0
 
60
#endif
 
61
 
 
62
#ifndef my_assert
 
63
#define my_assert assert
 
64
#endif
 
65
 
 
66
#ifndef myassert
 
67
#define myassert assert
 
68
#endif
 
69
 
 
70
#ifndef Sleep
 
71
#define Sleep _sleep
 
72
#endif
 
73
 
 
74
#ifdef MY_DEBUG
 
75
#define myprintf printf
 
76
#else
 
77
#define myprintf
 
78
#endif
 
79
 
 
80
#define MAX_NAME_LEN 95
 
81
#define MAX_COLUMNS 255
 
82
#define MAX_ROW_DATA_LEN 255
 
83
 
 
84
SQLCHAR *mydsn = "test";
 
85
SQLCHAR *myuid = "root";
 
86
SQLCHAR *mypwd = "";
 
87
 
 
88
/* PROTOTYPE */
 
89
void myerror(SQLRETURN rc,SQLHENV henv,SQLHDBC hdbc,SQLHSTMT hstmt);
 
90
SQLUINTEGER myresult(SQLHSTMT hstmt);
 
91
 
 
92
/* UTILITY MACROS */
 
93
#define myenv(henv,r) if ( ((r) != SQL_SUCCESS) ) myerror(r, henv, NULL, NULL); my_assert( ((r) == SQL_SUCCESS) || ((r) == SQL_SUCCESS_WITH_INFO) )
 
94
#define myenv_err(henv,r,rc) if ( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO ) myerror(rc, henv, NULL, NULL); my_assert( r )
 
95
#define myenv_r(henv,rc) if ( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO ) myerror(rc, henv, NULL, NULL); my_assert( rc == SQL_ERROR)
 
96
#define mycon(hdbc,r)  if ( ((r) != SQL_SUCCESS) ) myerror(r, NULL, hdbc, NULL); my_assert( ((r) == SQL_SUCCESS) || ((r) == SQL_SUCCESS_WITH_INFO) )
 
97
#define mycon_err(hdbc,r,rc)  if ( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO ) myerror(rc, NULL, hdbc, NULL); my_assert( r )
 
98
#define mycon_r(hdbc,rc) if ( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO ) myerror(rc, NULL, hdbc, NULL); my_assert( rc == SQL_ERROR)
 
99
#define mystmt(hstmt,r) if ( ((r) != SQL_SUCCESS) ) myerror(r, NULL, NULL, hstmt); my_assert( ((r) == SQL_SUCCESS) || ((r) == SQL_SUCCESS_WITH_INFO) )
 
100
#define mystmt_err(hstmt,r,rc)  if ( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO ) myerror(rc, NULL, NULL, hstmt); my_assert( r )
 
101
#define mystmt_r(hstmt,rc) if ( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO ) myerror(rc, NULL, NULL, hstmt); my_assert( rc == SQL_ERROR)
 
102
#define myheader(str) { myprintf("\n######################################\n"); myprintf("%s",str); myprintf("\n######################################\n"); }
 
103
/**
 
104
ERROR HANDLER
 
105
*/
 
106
void myerror(SQLRETURN rc, SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt)
 
107
{
 
108
  RETCODE lrc;
 
109
 
 
110
  if( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO ) 
 
111
  {
 
112
    SQLCHAR      szSqlState[6],szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
 
113
    SQLINTEGER   pfNativeError;
 
114
    SQLSMALLINT pcbErrorMsg;
 
115
 
 
116
    lrc = SQLError(henv,hdbc,hstmt,
 
117
        (SQLCHAR *)&szSqlState,
 
118
        (SQLINTEGER *)&pfNativeError,
 
119
        (SQLCHAR *)&szErrorMsg,
 
120
         SQL_MAX_MESSAGE_LENGTH-1,
 
121
        (SQLSMALLINT *)&pcbErrorMsg);
 
122
 
 
123
    if(lrc == SQL_SUCCESS || lrc == SQL_SUCCESS_WITH_INFO)
 
124
      myprintf("\n%d,[%s][%d:%s]\n",rc,szSqlState,pfNativeError,szErrorMsg);
 
125
    else 
 
126
      myprintf("SQLError returned :%d, but rc = %d\n",lrc,rc);
 
127
  }
 
128
}
 
129
/**
 
130
  CONNECTION
 
131
*/
 
132
void myconnect(SQLHENV *henv,SQLHDBC *hdbc, SQLHSTMT *hstmt)
 
133
{
 
134
  SQLRETURN rc;
 
135
 
 
136
    rc = SQLAllocEnv(henv);
 
137
    myenv(*henv,rc);    
 
138
 
 
139
    rc = SQLAllocConnect(*henv, hdbc);
 
140
    myenv(*henv,rc);    
 
141
 
 
142
    myprintf("connecting to '%s' with user name '%s'...\n",mydsn,myuid);
 
143
    rc = SQLConnect(*hdbc, mydsn, SQL_NTS, myuid, SQL_NTS,  mypwd, SQL_NTS);
 
144
    mycon(*hdbc,rc);
 
145
 
 
146
    rc = SQLSetConnectOption(*hdbc,SQL_AUTOCOMMIT,SQL_AUTOCOMMIT_ON);
 
147
    mycon(*hdbc,rc);
 
148
 
 
149
    rc = SQLAllocStmt(*hdbc, hstmt);
 
150
    mycon(*hdbc,rc);
 
151
 
 
152
    rc = SQLSetStmtOption(*hstmt,SQL_CURSOR_TYPE,SQL_CURSOR_STATIC);
 
153
    mystmt(*hstmt,rc);
 
154
 
 
155
  /* MS SQL Server to work...*/
 
156
#if 0
 
157
  SQLSetStmtAttr(hstmt, SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_ROWVER, 0);
 
158
  SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_KEYSET_DRIVEN, 0);
 
159
#endif
 
160
}
 
161
/**
 
162
  DISCONNECT
 
163
*/
 
164
void mydisconnect(SQLHENV *henv,SQLHDBC *hdbc, SQLHSTMT *hstmt)
 
165
{
 
166
  SQLRETURN rc;
 
167
 
 
168
    rc = SQLFreeStmt(*hstmt, SQL_DROP);
 
169
    mystmt(*hstmt,rc);
 
170
 
 
171
    rc = SQLDisconnect(*hdbc);
 
172
    mycon(*hdbc,rc);
 
173
 
 
174
    rc = SQLFreeConnect(*hdbc);
 
175
    mycon(*hdbc,rc);
 
176
 
 
177
    rc = SQLFreeEnv(*henv);
 
178
    myenv(*henv,rc);
 
179
}
 
180
/**
 
181
  RESULT SET
 
182
*/
 
183
SQLUINTEGER myresult(SQLHSTMT hstmt)
 
184
{
 
185
  SQLRETURN   rc;
 
186
  SQLUINTEGER nRowCount=0, pcColDef;
 
187
  SQLCHAR     szColName[MAX_NAME_LEN];
 
188
  SQLCHAR     szData[MAX_COLUMNS][MAX_ROW_DATA_LEN]={0};
 
189
  SQLSMALLINT nIndex,ncol,pfSqlType, pcbScale, pfNullable;
 
190
 
 
191
  rc = SQLNumResultCols(hstmt,&ncol);
 
192
  mystmt(hstmt,rc);
 
193
 
 
194
  myprintf("\n");
 
195
 
 
196
  for(nIndex = 1; nIndex <= ncol; nIndex++)
 
197
  {
 
198
    rc = SQLDescribeCol(hstmt,nIndex,szColName, MAX_NAME_LEN+1, NULL,
 
199
                        &pfSqlType,&pcColDef,&pcbScale,&pfNullable);
 
200
    mystmt(hstmt,rc);
 
201
 
 
202
    myprintf("%s\t",szColName);
 
203
 
 
204
    rc = SQLBindCol(hstmt,nIndex, SQL_C_CHAR, szData[nIndex-1],
 
205
                    MAX_ROW_DATA_LEN+1,NULL);
 
206
    mystmt(hstmt,rc);
 
207
  }
 
208
 
 
209
  myprintf("\n------------------------------------------------------\n");
 
210
 
 
211
  rc = SQLFetch(hstmt);
 
212
  while(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
 
213
  {
 
214
     nRowCount++;
 
215
     for(nIndex=0; nIndex< ncol; nIndex++)
 
216
       myprintf("%s\t",szData[nIndex]);
 
217
     
 
218
     myprintf("\n");
 
219
     rc = SQLFetch(hstmt);
 
220
   }
 
221
   SQLFreeStmt(hstmt,SQL_UNBIND);
 
222
 
 
223
   myprintf("\n total rows fetched:%d\n",nRowCount);
 
224
   return(nRowCount);
 
225
}
 
226
/**
 
227
  SQLExecDirect
 
228
*/
 
229
SQLRETURN tmysql_exec(SQLHSTMT hstmt, SQLCHAR *sql_stmt)
 
230
{
 
231
  return(SQLExecDirect(hstmt,sql_stmt,SQL_NTS));
 
232
}
 
233
/**
 
234
  SQLPrepare
 
235
*/
 
236
SQLRETURN tmysql_prepare(SQLHSTMT hstmt, SQLCHAR *sql_stmt)
 
237
{
 
238
  return(SQLPrepare(hstmt,sql_stmt,SQL_NTS));
 
239
}
 
240
/**
 
241
  return integer data by fetching it
 
242
*/
 
243
SQLINTEGER my_fetch_int(SQLHSTMT hstmt, SQLUSMALLINT irow)
 
244
{
 
245
  SQLINTEGER nData;
 
246
 
 
247
  SQLGetData(hstmt,irow,SQL_INTEGER,&nData,0,NULL);
 
248
  myprintf("my_fetch_int:%d\n",nData);
 
249
  return(nData);  
 
250
}
 
251
/**
 
252
  return string data, by fetching it
 
253
*/
 
254
const char *my_fetch_str(SQLHSTMT hstmt, SQLCHAR *szData,SQLUSMALLINT irow)
 
255
{
 
256
  SQLINTEGER nLen=0;
 
257
 
 
258
  SQLGetData(hstmt,irow,SQL_CHAR,szData,MAX_ROW_DATA_LEN+1,&nLen);
 
259
  myprintf("my_fetch_str:%s(%d)\n",szData,nLen);
 
260
  return((const char *)szData);  
 
261
}
 
262
 
 
263
#endif /* __TMYODBC__TEST__H */
 
264
 
 
265