1
/***************************************************************************
5
copyright : (C) 2001 by Venu, MySQL AB
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
18
#ifndef __TMYODBC__TEST__H
19
#define __TMYODBC__TEST__H
33
/* STANDARD C HEADERS */
47
#define ushort unsigned short
51
#define bool unsigned char
63
#define my_assert assert
67
#define myassert assert
75
#define myprintf printf
80
#define MAX_NAME_LEN 95
81
#define MAX_COLUMNS 255
82
#define MAX_ROW_DATA_LEN 255
84
SQLCHAR *mydsn = "test";
85
SQLCHAR *myuid = "root";
89
void myerror(SQLRETURN rc,SQLHENV henv,SQLHDBC hdbc,SQLHSTMT hstmt);
90
SQLUINTEGER myresult(SQLHSTMT hstmt);
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"); }
106
void myerror(SQLRETURN rc, SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt)
110
if( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO )
112
SQLCHAR szSqlState[6],szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
113
SQLINTEGER pfNativeError;
114
SQLSMALLINT pcbErrorMsg;
116
lrc = SQLError(henv,hdbc,hstmt,
117
(SQLCHAR *)&szSqlState,
118
(SQLINTEGER *)&pfNativeError,
119
(SQLCHAR *)&szErrorMsg,
120
SQL_MAX_MESSAGE_LENGTH-1,
121
(SQLSMALLINT *)&pcbErrorMsg);
123
if(lrc == SQL_SUCCESS || lrc == SQL_SUCCESS_WITH_INFO)
124
myprintf("\n%d,[%s][%d:%s]\n",rc,szSqlState,pfNativeError,szErrorMsg);
126
myprintf("SQLError returned :%d, but rc = %d\n",lrc,rc);
132
void myconnect(SQLHENV *henv,SQLHDBC *hdbc, SQLHSTMT *hstmt)
136
rc = SQLAllocEnv(henv);
139
rc = SQLAllocConnect(*henv, hdbc);
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);
146
rc = SQLSetConnectOption(*hdbc,SQL_AUTOCOMMIT,SQL_AUTOCOMMIT_ON);
149
rc = SQLAllocStmt(*hdbc, hstmt);
152
rc = SQLSetStmtOption(*hstmt,SQL_CURSOR_TYPE,SQL_CURSOR_STATIC);
155
/* MS SQL Server to work...*/
157
SQLSetStmtAttr(hstmt, SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_ROWVER, 0);
158
SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_KEYSET_DRIVEN, 0);
164
void mydisconnect(SQLHENV *henv,SQLHDBC *hdbc, SQLHSTMT *hstmt)
168
rc = SQLFreeStmt(*hstmt, SQL_DROP);
171
rc = SQLDisconnect(*hdbc);
174
rc = SQLFreeConnect(*hdbc);
177
rc = SQLFreeEnv(*henv);
183
SQLUINTEGER myresult(SQLHSTMT hstmt)
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;
191
rc = SQLNumResultCols(hstmt,&ncol);
196
for(nIndex = 1; nIndex <= ncol; nIndex++)
198
rc = SQLDescribeCol(hstmt,nIndex,szColName, MAX_NAME_LEN+1, NULL,
199
&pfSqlType,&pcColDef,&pcbScale,&pfNullable);
202
myprintf("%s\t",szColName);
204
rc = SQLBindCol(hstmt,nIndex, SQL_C_CHAR, szData[nIndex-1],
205
MAX_ROW_DATA_LEN+1,NULL);
209
myprintf("\n------------------------------------------------------\n");
211
rc = SQLFetch(hstmt);
212
while(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
215
for(nIndex=0; nIndex< ncol; nIndex++)
216
myprintf("%s\t",szData[nIndex]);
219
rc = SQLFetch(hstmt);
221
SQLFreeStmt(hstmt,SQL_UNBIND);
223
myprintf("\n total rows fetched:%d\n",nRowCount);
229
SQLRETURN tmysql_exec(SQLHSTMT hstmt, SQLCHAR *sql_stmt)
231
return(SQLExecDirect(hstmt,sql_stmt,SQL_NTS));
236
SQLRETURN tmysql_prepare(SQLHSTMT hstmt, SQLCHAR *sql_stmt)
238
return(SQLPrepare(hstmt,sql_stmt,SQL_NTS));
241
return integer data by fetching it
243
SQLINTEGER my_fetch_int(SQLHSTMT hstmt, SQLUSMALLINT irow)
247
SQLGetData(hstmt,irow,SQL_INTEGER,&nData,0,NULL);
248
myprintf("my_fetch_int:%d\n",nData);
252
return string data, by fetching it
254
const char *my_fetch_str(SQLHSTMT hstmt, SQLCHAR *szData,SQLUSMALLINT irow)
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);
263
#endif /* __TMYODBC__TEST__H */