mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-02 08:25:55 +00:00
.
This commit is contained in:
23
database/SqliteDatabase.cpp
Normal file
23
database/SqliteDatabase.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "SqliteDatabase.h"
|
||||
|
||||
SqliteDatabase::SqliteDatabase()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* http://www.sqlite.org/lang_expr.html
|
||||
BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character. For example:
|
||||
X'53514C697465'
|
||||
*/
|
||||
void SqliteDatabase::escape(unsigned char* start,int size,std::string& retStr)
|
||||
{
|
||||
retStr.clear();
|
||||
|
||||
char buf[3];
|
||||
retStr.append("X'");
|
||||
for(int n=0; n<size; n++)
|
||||
{
|
||||
retStr.append( itoa(*start,buf,16) );
|
||||
}
|
||||
retStr.push_back('\'');
|
||||
}
|
||||
11
database/SqliteDatabase.h
Normal file
11
database/SqliteDatabase.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "database.h"
|
||||
|
||||
class SqliteDatabase : public Database
|
||||
{
|
||||
public:
|
||||
SqliteDatabase();
|
||||
|
||||
|
||||
void escape(unsigned char* start,int size,std::string& retStr);
|
||||
|
||||
};
|
||||
118
database/database.cpp
Normal file
118
database/database.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
#include "database.h"
|
||||
#include "../utillib/reportingmechanism.h"
|
||||
#include "string/i4string.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
Database::Database(const char* host,const char* user,const char* pass) : mDBPass(pass), mHost(host) ,mUser(user), mNumCol(0)
|
||||
{
|
||||
mColNameTable=NULL;
|
||||
}
|
||||
|
||||
Database::~Database()
|
||||
{
|
||||
delete[] mColNameTable;
|
||||
|
||||
}
|
||||
|
||||
|
||||
char* Database::getStr(const char* colName,std::string& retStr)
|
||||
{
|
||||
int index;
|
||||
if(getColNumber(colName,&index))
|
||||
{
|
||||
return(getStr(index,retStr));
|
||||
}
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
w32 Database::getInt(const char* colName)
|
||||
{
|
||||
int index;
|
||||
if(getColNumber(colName,&index))
|
||||
{
|
||||
return(getInt(index));
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
float Database::getFloat(const char* colName)
|
||||
{
|
||||
int index;
|
||||
if(getColNumber(colName,&index))
|
||||
{
|
||||
return(getFloat(index));
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
bool Database::getBool(const char* colName)
|
||||
{
|
||||
int index;
|
||||
if(getColNumber(colName,&index))
|
||||
{
|
||||
return(getBool(index));
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// returns false if can't find col
|
||||
bool Database::getColNumber(const char* colName,int* retIndex)
|
||||
{
|
||||
for(int n=0; n<mNumCol; n++)
|
||||
{
|
||||
if(i4_stricmp(colName,mColNameTable[n])==0)
|
||||
{
|
||||
*retIndex=n;
|
||||
return(true);
|
||||
}
|
||||
}
|
||||
return(false);
|
||||
}
|
||||
|
||||
int Database::getSingleDBValueInt(const char* sql)
|
||||
{
|
||||
int ret;
|
||||
if( executeSQL(sql) && startIterRows() && getNextRow())
|
||||
{
|
||||
ret=getInt(0);
|
||||
endIterRows();
|
||||
}else
|
||||
{
|
||||
//theUI->statusMsg("ERROR with database: %s",sql);
|
||||
ret=0;
|
||||
}
|
||||
return(ret);
|
||||
}
|
||||
|
||||
float Database::getSingleDBValueFloat(const char* sql)
|
||||
{
|
||||
float ret;
|
||||
if( executeSQL(sql) && startIterRows() && getNextRow())
|
||||
{
|
||||
ret=getFloat(0);
|
||||
endIterRows();
|
||||
}else
|
||||
{
|
||||
//theUI->statusMsg("ERROR with database: %s",sql);
|
||||
ret=0;
|
||||
}
|
||||
return(ret);
|
||||
}
|
||||
|
||||
char* Database::getSingleDBValueStr(const char* sql,std::string& retStr)
|
||||
{
|
||||
char* ret;
|
||||
if( executeSQL(sql) && startIterRows() && getNextRow())
|
||||
{
|
||||
ret=getStr(0,retStr);
|
||||
endIterRows();
|
||||
}else
|
||||
{
|
||||
//theUI->statusMsg("ERROR with database: %s",sql);
|
||||
ret=0;
|
||||
}
|
||||
return(ret);
|
||||
}
|
||||
73
database/database.h
Normal file
73
database/database.h
Normal file
@@ -0,0 +1,73 @@
|
||||
#ifndef __DATABASE__
|
||||
#define __DATABASE__
|
||||
|
||||
#include <string>
|
||||
|
||||
/*
|
||||
this maintains the connection to the database
|
||||
*/
|
||||
class Database
|
||||
{
|
||||
protected:
|
||||
int mNumCol;
|
||||
std:string mUser;
|
||||
std:string mHost;
|
||||
std:string mDBPass;
|
||||
std:string* mColNameTable;
|
||||
|
||||
bool getColNumber(const char* colName, int* retIndex);
|
||||
|
||||
public:
|
||||
Database(const char* host,const char* user,const char* pass);
|
||||
static Database* newMysqlDatabase(const char* host,const char* user,const char* pass);
|
||||
virtual ~Database();
|
||||
|
||||
virtual void connect()=0;
|
||||
virtual void disconnect()=0;
|
||||
|
||||
std::string& getPass(){ return(mDBPass); }
|
||||
|
||||
virtual void escape(unsigned char* start,int size,std::string& retStr)=0;
|
||||
|
||||
// returns true if the query went ok
|
||||
virtual bool executeSQL(const char* sql)=0;
|
||||
|
||||
// tells you how many rows were changed by an update or insert
|
||||
virtual int getNumRowsAffected()=0;
|
||||
|
||||
// returns false if there are no results
|
||||
virtual bool startIterRows()=0;
|
||||
virtual void endIterRows()=0;
|
||||
|
||||
// call this after you executeSQL
|
||||
// will return false if there are no more rows
|
||||
virtual bool getNextRow()=0;
|
||||
|
||||
// get Data from the current row
|
||||
char* getStr(const char* colName,std::string& retStr);
|
||||
int32 getInt(const char* colName);
|
||||
float getFloat(const char* colName);
|
||||
bool getBool(const char* colName);
|
||||
bool getBinary(const char* colName,char* buf,int maxSize);
|
||||
|
||||
virtual char* getStr(int colIndex,std::string& retStr)=0;
|
||||
virtual int32 getInt(int colIndex)=0;
|
||||
virtual float getFloat(int colIndex)=0;
|
||||
virtual bool getBool(int colIndex)=0;
|
||||
virtual bool getBinary(int colIndex,char* buf,int maxSize)=0;
|
||||
|
||||
int getSingleDBValueInt(const char* sql);
|
||||
float getSingleDBValueFloat(const char* sql);
|
||||
char* getSingleDBValueStr(const char* sql, std::string& retStr);
|
||||
|
||||
};
|
||||
|
||||
|
||||
class MsqlDatabase
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
136
database/linux/mysqldatabase.cpp
Normal file
136
database/linux/mysqldatabase.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#include "mysqldatabase.h"
|
||||
#include "reportingmechanism.h"
|
||||
#include "string/i4string.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
Database* Database::newDatabase(const char* host,const char* user,const char* pass)
|
||||
{
|
||||
return(new MySqlDatabase(host,user,pass));
|
||||
}
|
||||
|
||||
MySqlDatabase::MySqlDatabase(const char* host,const char* user,const char* pass) : Database(host,user,pass)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MySqlDatabase::~MySqlDatabase()
|
||||
{
|
||||
}
|
||||
|
||||
void MySqlDatabase::connect()
|
||||
{
|
||||
mysql_init(&mMysql);
|
||||
mysql_options(&mMysql,MYSQL_READ_DEFAULT_GROUP,"i4min");
|
||||
if(!mysql_real_connect(&mMysql,mHost,mUser,mDBPass,NULL,0,NULL,0))
|
||||
{
|
||||
theUI->statusMsg("Failed to connect to database: Error: %s\n", mysql_error(&mMysql));
|
||||
}else theUI->statusMsg("Connection Established to DB");
|
||||
}
|
||||
|
||||
void MySqlDatabase::disconnect()
|
||||
{
|
||||
mysql_close(&mMysql);
|
||||
}
|
||||
|
||||
int MySqlDatabase::getNumRowsAffected()
|
||||
{
|
||||
return( mysql_affected_rows(&mMysql));
|
||||
}
|
||||
|
||||
// returns true if the query went ok
|
||||
bool MySqlDatabase::executeSQL(const char* sql)
|
||||
{
|
||||
int ret=mysql_query(&mMysql, sql);
|
||||
if(ret)
|
||||
{
|
||||
connect();
|
||||
int ret=mysql_query(&mMysql, sql);
|
||||
if(ret)
|
||||
{
|
||||
theUI->statusMsg("ERROR with executeSQL: %d %s",ret,sql);
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool MySqlDatabase::startIterRows()
|
||||
{
|
||||
mResult=mysql_store_result(&mMysql);
|
||||
// get total number of columns from the resultset
|
||||
mNumCol = mysql_num_fields(mResult);
|
||||
if(mNumCol)
|
||||
{
|
||||
delete[](mColNameTable);
|
||||
mColNameTable=new i4_str[mNumCol];
|
||||
|
||||
// fill out the column name table
|
||||
for(int n = 0; n < mNumCol; n++)
|
||||
{
|
||||
MYSQL_FIELD* field=mysql_fetch_field(mResult);
|
||||
mColNameTable[n]= field->name;
|
||||
}
|
||||
return(true);
|
||||
}
|
||||
return(false);
|
||||
}
|
||||
|
||||
// call this after you executeSQL
|
||||
// will return false if there are no more rows
|
||||
bool MySqlDatabase::getNextRow()
|
||||
{
|
||||
mCurRow=mysql_fetch_row(mResult);
|
||||
|
||||
return(mCurRow!=NULL);
|
||||
}
|
||||
|
||||
char* MySqlDatabase::getStr(int colIndex,i4_str* retStr)
|
||||
{
|
||||
if(mCurRow[colIndex])
|
||||
{
|
||||
(*retStr)=mCurRow[colIndex];
|
||||
}else (*retStr)="";
|
||||
|
||||
return(*retStr);
|
||||
}
|
||||
|
||||
w32 MySqlDatabase::getInt(int colIndex)
|
||||
{
|
||||
|
||||
if(mCurRow[colIndex])
|
||||
{
|
||||
w32 ret=atoll(mCurRow[colIndex]);
|
||||
//theUI->statusMsg("getInt: %s,%c,%u",mCurRow[colIndex],mCurRow[colIndex][0],ret);
|
||||
return(ret);
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
float MySqlDatabase::getFloat(int colIndex)
|
||||
{
|
||||
if(mCurRow[colIndex])
|
||||
{
|
||||
float ret=atof(mCurRow[colIndex]);
|
||||
return(ret);
|
||||
}
|
||||
return(0.0);
|
||||
}
|
||||
|
||||
bool MySqlDatabase::getBool(int colIndex)
|
||||
{
|
||||
if(mCurRow[colIndex])
|
||||
{
|
||||
int ret=atoi(mCurRow[colIndex]);
|
||||
return(ret);
|
||||
}
|
||||
return(false);
|
||||
}
|
||||
|
||||
|
||||
void MySqlDatabase::endIterRows()
|
||||
{
|
||||
mysql_free_result(mResult);
|
||||
}
|
||||
|
||||
|
||||
47
database/linux/mysqldatabase.h
Normal file
47
database/linux/mysqldatabase.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef __MYSQLDATABASE__
|
||||
#define __MYSQLDATABASE__
|
||||
|
||||
#include "../database.h"
|
||||
#include "string/i4string.h"
|
||||
#include "mysql.h"
|
||||
|
||||
/*
|
||||
this maintains the connection to the database
|
||||
*/
|
||||
class MySqlDatabase : public Database
|
||||
{
|
||||
MYSQL mMysql;
|
||||
MYSQL_RES* mResult;
|
||||
MYSQL_ROW mCurRow;
|
||||
|
||||
public:
|
||||
MySqlDatabase(const char* host,const char* user,const char* pass);
|
||||
~MySqlDatabase();
|
||||
|
||||
void connect();
|
||||
void disconnect();
|
||||
|
||||
// returns true if the query went ok
|
||||
bool executeSQL(const char* sql);
|
||||
|
||||
int getNumRowsAffected();
|
||||
|
||||
// returns false if there are no results
|
||||
bool startIterRows();
|
||||
void endIterRows();
|
||||
|
||||
// call this after you executeSQL
|
||||
// will return false if there are no more rows
|
||||
bool getNextRow();
|
||||
|
||||
// get Data from the current row
|
||||
|
||||
char* getStr(int colIndex,i4_str* retStr);
|
||||
w32 getInt(int colIndex);
|
||||
float getFloat(int colIndex);
|
||||
bool getBool(int colIndex);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
82
database/win/dbutility.h
Normal file
82
database/win/dbutility.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#ifndef __TMYODBC_UTILITY_H__
|
||||
#define __TMYODBC_UTILITY_H__
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <myconf.h>
|
||||
#endif
|
||||
|
||||
|
||||
/* STANDARD C HEADERS */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
/* ODBC HEADERS */
|
||||
#include <sqlext.h>
|
||||
|
||||
#define MAX_NAME_LEN 95
|
||||
#define MAX_COLUMNS 255
|
||||
#define MAX_ROW_DATA_LEN 255
|
||||
|
||||
|
||||
/* PROTOTYPE */
|
||||
void myerror(SQLRETURN rc,SQLSMALLINT htype, SQLHANDLE handle);
|
||||
|
||||
/* UTILITY MACROS */
|
||||
#define myenv(henv,r) \
|
||||
if ( ((r) != SQL_SUCCESS) ) \
|
||||
myerror(r, 1,henv); \
|
||||
assert( ((r) == SQL_SUCCESS) || ((r) == SQL_SUCCESS_WITH_INFO) )
|
||||
|
||||
#define myenv_err(henv,r,rc) \
|
||||
if ( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO ) \
|
||||
myerror(rc, 1, henv); \
|
||||
assert( r )
|
||||
|
||||
#define mycon(hdbc,r) \
|
||||
if ( ((r) != SQL_SUCCESS) ) \
|
||||
myerror(r, 2, hdbc); \
|
||||
assert( ((r) == SQL_SUCCESS) || ((r) == SQL_SUCCESS_WITH_INFO) )
|
||||
|
||||
#define mycon_err(hdbc,r,rc) \
|
||||
if ( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO ) \
|
||||
myerror(rc, 2, hdbc); \
|
||||
assert( r )
|
||||
|
||||
#define mystmt(hstmt,r) \
|
||||
if ( ((r) != SQL_SUCCESS) ) \
|
||||
myerror(r, 3, hstmt); \
|
||||
assert( ((r) == SQL_SUCCESS) || ((r) == SQL_SUCCESS_WITH_INFO) )
|
||||
|
||||
#define mystmt_err(hstmt,r,rc) \
|
||||
if ( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO ) \
|
||||
myerror(rc, 3, hstmt); \
|
||||
assert( r )
|
||||
|
||||
/********************************************************
|
||||
* MyODBC 3.51 error handler *
|
||||
*********************************************************/
|
||||
void myerror(SQLRETURN rc, SQLSMALLINT htype, SQLHANDLE handle)
|
||||
{
|
||||
SQLRETURN lrc;
|
||||
|
||||
if( rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO )
|
||||
{
|
||||
SQLCHAR szSqlState[6],szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
|
||||
SQLINTEGER pfNativeError;
|
||||
SQLSMALLINT pcbErrorMsg;
|
||||
|
||||
lrc = SQLGetDiagRec(htype, handle,1,
|
||||
(SQLCHAR *)&szSqlState,
|
||||
(SQLINTEGER *)&pfNativeError,
|
||||
(SQLCHAR *)&szErrorMsg,
|
||||
SQL_MAX_MESSAGE_LENGTH-1,
|
||||
(SQLSMALLINT *)&pcbErrorMsg);
|
||||
if(lrc == SQL_SUCCESS || lrc == SQL_SUCCESS_WITH_INFO)
|
||||
printf("\n [%s][%d:%s]\n",szSqlState,pfNativeError,szErrorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif /* __TMYODBC_UTILITY_H__ */
|
||||
|
||||
212
database/win/windatabase.cpp
Normal file
212
database/win/windatabase.cpp
Normal file
@@ -0,0 +1,212 @@
|
||||
#include "windatabase.h"
|
||||
#include "dbutility.h"
|
||||
|
||||
Database* Database::newMysqlDatabase(const char* host,const char* user,const char* pass)
|
||||
{
|
||||
return(new WinDatabase(host,user,pass));
|
||||
}
|
||||
|
||||
WinDatabase::WinDatabase(const char* host,const char* user,const char* pass) : Database(host,user,pass)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
WinDatabase::~WinDatabase()
|
||||
{
|
||||
disconnect();
|
||||
}
|
||||
|
||||
|
||||
void WinDatabase::connect()
|
||||
{
|
||||
SQLRETURN rc;
|
||||
|
||||
rc = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
|
||||
myenv(henv, rc);
|
||||
|
||||
rc = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0);
|
||||
myenv(henv, rc);
|
||||
|
||||
rc = SQLAllocHandle(SQL_HANDLE_DBC,henv, &hdbc);
|
||||
myenv(henv, rc);
|
||||
|
||||
rc = SQLConnect(hdbc, (unsigned char*)(char*) mHost, SQL_NTS, (unsigned char*)(char*) mUser, SQL_NTS, (unsigned char*)(char*) mDBPass, SQL_NTS);
|
||||
mycon(hdbc, rc);
|
||||
|
||||
rc = SQLSetConnectAttr(hdbc,SQL_ATTR_AUTOCOMMIT,(SQLPOINTER)SQL_AUTOCOMMIT_ON,0);
|
||||
mycon(hdbc,rc);
|
||||
|
||||
rc = SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
|
||||
mycon(hdbc,rc);
|
||||
|
||||
//rc = SQLGetInfo(hdbc,SQL_DBMS_NAME,&server_name,40,NULL);
|
||||
//mycon(hdbc, rc);
|
||||
|
||||
theUI->statusMsg("Connection Established to DB");
|
||||
}
|
||||
|
||||
void WinDatabase::disconnect()
|
||||
{
|
||||
SQLRETURN rc;
|
||||
|
||||
rc = SQLFreeStmt(hstmt, SQL_DROP);
|
||||
mystmt(hstmt,rc);
|
||||
|
||||
rc = SQLDisconnect(hdbc);
|
||||
mycon(hdbc, rc);
|
||||
|
||||
rc = SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
|
||||
mycon(hdbc, rc);
|
||||
|
||||
rc = SQLFreeHandle(SQL_HANDLE_ENV, henv);
|
||||
myenv(henv, rc);
|
||||
}
|
||||
|
||||
int WinDatabase::getNumRowsAffected()
|
||||
{
|
||||
// theUI->statusMsg("getNumRowsAffected()");
|
||||
SQLINTEGER ret;
|
||||
SQLRowCount(hstmt,&ret);
|
||||
return(ret);
|
||||
}
|
||||
|
||||
// returns true if the query went ok
|
||||
bool WinDatabase::executeSQL(const char* sql)
|
||||
{
|
||||
SQLRETURN rc = SQLExecDirect(hstmt,(unsigned char*) sql,SQL_NTS);
|
||||
if(rc==SQL_ERROR)
|
||||
{
|
||||
theUI->errorMsg("Trying to recover from DB error");
|
||||
rc = SQLExecDirect(hstmt,(unsigned char*) sql,SQL_NTS);
|
||||
if(rc==SQL_ERROR)
|
||||
{
|
||||
SQLCHAR SqlState[6], /*SQLStmt[100],*/ Msg[SQL_MAX_MESSAGE_LENGTH];
|
||||
SQLINTEGER NativeError;
|
||||
SQLSMALLINT i, MsgLen;
|
||||
SQLRETURN /*rc1,*/ rc2;
|
||||
|
||||
i = 1;
|
||||
while ((rc2 = SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, i, SqlState, &NativeError, Msg, sizeof(Msg), &MsgLen)) != SQL_NO_DATA)
|
||||
{
|
||||
theUI->errorMsg("DB ERROR: %s,%d,%s",SqlState,NativeError,Msg);
|
||||
i++;
|
||||
}
|
||||
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
|
||||
mystmt(hstmt,rc);
|
||||
|
||||
// commit the transaction
|
||||
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
|
||||
mycon(hdbc,rc);
|
||||
|
||||
return(true);
|
||||
|
||||
}
|
||||
|
||||
bool WinDatabase::startIterRows()
|
||||
{
|
||||
SQLUINTEGER pcColDef;
|
||||
SQLCHAR szColName[MAX_NAME_LEN];
|
||||
SQLSMALLINT pfSqlType, pcbScale, pfNullable;
|
||||
SQLSMALLINT numCol;
|
||||
|
||||
/* get total number of columns from the resultset */
|
||||
SQLRETURN rc = SQLNumResultCols(hstmt,&numCol);
|
||||
mystmt(hstmt,rc);
|
||||
mNumCol=(int)numCol;
|
||||
|
||||
if(mNumCol)
|
||||
{
|
||||
delete[](mColNameTable);
|
||||
mColNameTable=new i4_str[mNumCol];
|
||||
|
||||
// fill out the column name table
|
||||
for(int n = 1; n <= mNumCol; n++)
|
||||
{
|
||||
rc = SQLDescribeCol(hstmt,n,szColName, MAX_NAME_LEN, NULL, &pfSqlType,&pcColDef,&pcbScale,&pfNullable);
|
||||
mystmt(hstmt,rc);
|
||||
|
||||
mColNameTable[n-1]= (char*)szColName;
|
||||
}
|
||||
return(true);
|
||||
}
|
||||
return(false);
|
||||
}
|
||||
|
||||
// call this after you executeSQL
|
||||
// will return false if there are no more rows
|
||||
bool WinDatabase::getNextRow()
|
||||
{
|
||||
SQLRETURN rc = SQLFetch(hstmt);
|
||||
return((rc==SQL_SUCCESS) || (rc==SQL_SUCCESS_WITH_INFO));
|
||||
}
|
||||
|
||||
|
||||
|
||||
char* WinDatabase::getStr(int colIndex,i4_str* retStr)
|
||||
{
|
||||
colIndex++;
|
||||
(*retStr)="";
|
||||
char buf[1000];
|
||||
// SQLINTEGER len;
|
||||
buf[0]=0;
|
||||
|
||||
while(SQLGetData(hstmt, colIndex, SQL_C_CHAR, &buf, 1000,NULL)!= SQL_NO_DATA)
|
||||
{
|
||||
(*retStr) += buf;
|
||||
// theUI->statusMsg("Win: %s",buf);
|
||||
}
|
||||
|
||||
//SQLRETURN rc = SQLGetData(hstmt,colIndex,SQL_C_CHAR,&buf,30000,&len);
|
||||
//mystmt(hstmt,rc);
|
||||
//*retStr=buf;
|
||||
|
||||
//theUI->statusMsg("Win: %s",buf);
|
||||
|
||||
return(*retStr);
|
||||
}
|
||||
|
||||
w32 WinDatabase::getInt(int colIndex)
|
||||
{
|
||||
colIndex++;
|
||||
int ret=0;
|
||||
SQLRETURN rc = SQLGetData(hstmt,colIndex,SQL_INTEGER,&ret,sizeof(int),NULL);
|
||||
mystmt(hstmt,rc);
|
||||
return(ret);
|
||||
}
|
||||
|
||||
float WinDatabase::getFloat(int colIndex)
|
||||
{
|
||||
colIndex++;
|
||||
float ret=0;
|
||||
SQLRETURN rc = SQLGetData(hstmt,colIndex,SQL_C_FLOAT,&ret,sizeof(float),NULL);
|
||||
mystmt(hstmt,rc);
|
||||
|
||||
return(ret);
|
||||
}
|
||||
|
||||
bool WinDatabase::getBool(int colIndex)
|
||||
{
|
||||
colIndex++;
|
||||
char buf[1];
|
||||
buf[0]=0;
|
||||
SQLRETURN rc = SQLGetData(hstmt,colIndex,SQL_C_CHAR,&buf,1,NULL);
|
||||
mystmt(hstmt,rc);
|
||||
|
||||
return(buf[0] != 0);
|
||||
}
|
||||
|
||||
|
||||
void WinDatabase::endIterRows()
|
||||
{
|
||||
// free the statement row bind resources
|
||||
SQLRETURN rc = SQLFreeStmt(hstmt, SQL_UNBIND);
|
||||
mystmt(hstmt,rc);
|
||||
|
||||
// free the statement cursor
|
||||
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
|
||||
mystmt(hstmt,rc);
|
||||
}
|
||||
55
database/win/windatabase.h
Normal file
55
database/win/windatabase.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef __WINDATABASE__
|
||||
#define __WINDATABASE__
|
||||
|
||||
#include "../database.h"
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#define _WINSOCKAPI_ // prevent inclusion of winsock.h in windows.h
|
||||
#include <windows.h>
|
||||
#include "sql.h"
|
||||
#endif
|
||||
|
||||
#include "string/i4string.h"
|
||||
|
||||
/*
|
||||
this maintains the connection to the database
|
||||
*/
|
||||
class WinDatabase : public Database
|
||||
{
|
||||
SQLHENV henv;
|
||||
SQLHDBC hdbc;
|
||||
SQLHSTMT hstmt;
|
||||
|
||||
public:
|
||||
WinDatabase(const char* host,const char* user,const char* pass);
|
||||
virtual ~WinDatabase();
|
||||
|
||||
void connect();
|
||||
void disconnect();
|
||||
|
||||
char* getPass(){ return(mDBPass); }
|
||||
|
||||
// returns true if the query went ok
|
||||
bool executeSQL(const char* sql);
|
||||
|
||||
int getNumRowsAffected();
|
||||
|
||||
// returns false if there are no results
|
||||
bool startIterRows();
|
||||
void endIterRows();
|
||||
|
||||
// call this after you executeSQL
|
||||
// will return false if there are no more rows
|
||||
bool getNextRow();
|
||||
|
||||
// get Data from the current row
|
||||
char* getStr(int colIndex,i4_str* retStr);
|
||||
w32 getInt(int colIndex);
|
||||
float getFloat(int colIndex);
|
||||
bool getBool(int colIndex);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user