mdbtools/src/odbc/unittest.c

204 lines
5.3 KiB
C
Raw Normal View History

2001-07-11 06:36:20 +08:00
/* FreeTDS - Library of routines accessing Sybase and Microsoft databases
* Copyright (C) 1998-1999 Brian Bruns
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
2011-08-29 07:53:29 +08:00
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2001-07-11 06:36:20 +08:00
*/
/* #include <windows.h> */
#include <sql.h>
#include <sqlext.h>
2001-07-11 06:36:20 +08:00
#include <stdio.h>
#define SALES_PERSON_LEN 2
#define STATUS_LEN 6
SQLSMALLINT sOrderID;
SQLSMALLINT sCustID;
DATE_STRUCT dsOpenDate;
SQLCHAR szSalesPerson[SALES_PERSON_LEN] = "D";
SQLCHAR szStatus[STATUS_LEN];
SQLINTEGER cbOrderID = 0, cbCustID = 0, cbOpenDate = 0, cbSalesPerson = SQL_NTS,
cbStatus = SQL_NTS;
SQLRETURN retcode;
HENV henv;
HDBC hdbc;
SQLHSTMT hstmt;
static void printStatementError(HSTMT hstmt, char *msg)
{
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER dwNativeError;
2001-07-11 06:36:20 +08:00
SWORD wErrorMsg;
SQLError(SQL_NULL_HENV, SQL_NULL_HDBC, hstmt,
szSqlState, &dwNativeError, szErrorMsg,
SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg);
fprintf(stderr,"%s\n%s\n%s\n",msg,
szSqlState, szErrorMsg);
}
int main()
{
int i;
2001-07-11 06:36:20 +08:00
retcode = SQLAllocEnv(&henv);
if (SQLAllocConnect(henv, &hdbc) != SQL_SUCCESS)
{
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER dwNativeError;
2001-07-11 06:36:20 +08:00
SWORD wErrorMsg;
SQLError(henv, SQL_NULL_HDBC, SQL_NULL_HSTMT,
szSqlState, &dwNativeError, szErrorMsg,
SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg);
fprintf(stderr,"problem with SQLAllocConnect\n%s\n%s\n",
szSqlState, szErrorMsg);
2005-04-29 11:08:12 +08:00
return 1;
2001-07-11 06:36:20 +08:00
}
retcode = SQLSetConnectOption(hdbc, SQL_ACCESS_MODE, SQL_MODE_READ_ONLY);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
{
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER dwNativeError;
2001-07-11 06:36:20 +08:00
SWORD wErrorMsg;
SQLError(SQL_NULL_HENV, hdbc, SQL_NULL_HSTMT,
szSqlState, &dwNativeError, szErrorMsg,
SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg);
fprintf(stderr,"problem with SQLSetConnectOption\n%s\n%s\n",
szSqlState, szErrorMsg);
2005-04-29 11:08:12 +08:00
return 1;
2001-07-11 06:36:20 +08:00
}
retcode = SQLSetConnectOption(hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
{
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER dwNativeError;
2001-07-11 06:36:20 +08:00
SWORD wErrorMsg;
SQLError(SQL_NULL_HENV, hdbc, SQL_NULL_HSTMT,
szSqlState, &dwNativeError, szErrorMsg,
SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg);
fprintf(stderr,"problem with SQLSetConnectOption\n%s\n%s\n",
szSqlState, szErrorMsg);
2005-04-29 11:08:12 +08:00
return 1;
2001-07-11 06:36:20 +08:00
}
retcode = SQLConnect(hdbc,
(UCHAR *)"Northwind", SQL_NTS,
(UCHAR *)"", SQL_NTS,
(UCHAR *)"", SQL_NTS);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
{
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER dwNativeError;
2001-07-11 06:36:20 +08:00
SWORD wErrorMsg;
SQLError(SQL_NULL_HENV, hdbc, SQL_NULL_HSTMT,
szSqlState, &dwNativeError, szErrorMsg,
SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg);
fprintf(stderr,"problem with SQLConnect\n%s\n%s\n",
szSqlState, szErrorMsg);
2005-04-29 11:08:12 +08:00
return 1;
2001-07-11 06:36:20 +08:00
}
if (SQLAllocStmt(hdbc, &hstmt)!= SQL_SUCCESS)
{
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER dwNativeError;
2001-07-11 06:36:20 +08:00
SWORD wErrorMsg;
SQLError(SQL_NULL_HENV, hdbc, SQL_NULL_HSTMT,
szSqlState, &dwNativeError, szErrorMsg,
SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg);
fprintf(stderr,"problem with SQLAllocStmt\n%s\n%s\n",
szSqlState, szErrorMsg);
2005-04-29 11:08:12 +08:00
return 1;
2001-07-11 06:36:20 +08:00
}
/* Prepare the SQL statement with parameter markers. */
retcode = SQLPrepare(hstmt,
(unsigned char *)"select * from Shippers",
2001-07-11 06:36:20 +08:00
SQL_NTS);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
long p1;
long p1Len = sizeof(p1);
long sAge = 1023;
long cbAge = sizeof(long);
UCHAR szCol1[60];
2002-04-09 09:19:26 +08:00
SQLINTEGER length;
2001-07-11 06:36:20 +08:00
printf("excecuting first statement\n");
retcode = SQLExecute(hstmt);
if (retcode != SQL_SUCCESS) {
2001-07-11 06:36:20 +08:00
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER dwNativeError;
2001-07-11 06:36:20 +08:00
SWORD wErrorMsg;
SQLError(SQL_NULL_HENV, SQL_NULL_HDBC, hstmt,
szSqlState, &dwNativeError, szErrorMsg,
SQL_MAX_MESSAGE_LENGTH-1, &wErrorMsg);
fprintf(stderr,"problem with SQLExecute\n%s\n%s\n",
szSqlState, szErrorMsg);
2005-04-29 11:08:12 +08:00
return 1;
2001-07-11 06:36:20 +08:00
}
2002-04-09 09:19:26 +08:00
SQLBindCol(hstmt, 3, SQL_CHAR, szCol1, 60, &length);
//SQLBindCol(hstmt, 1, SQL_CHAR, szCol1, 60, NULL);
/* Execute statement with first row. */
i=0;
2001-07-11 06:36:20 +08:00
while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
{
i++;
2002-04-09 09:19:26 +08:00
printf("%d: szCol1 = %s (%d)\n",i,szCol1, length);
2001-07-11 06:36:20 +08:00
}
if (retcode != SQL_NO_DATA_FOUND)
{
printStatementError(hstmt, "problem with SQLFetch");
2005-04-29 11:08:12 +08:00
return 1;
2001-07-11 06:36:20 +08:00
}
}
printf("Done\n");
return 1;
}