Moved cpp code to src/cpp and js code to src/js.

This commit is contained in:
Stefan Thomas
2012-11-06 12:02:59 -08:00
parent 3c880b8301
commit fa3fab5816
214 changed files with 62 additions and 57 deletions

View File

@@ -0,0 +1,201 @@
#include "SqliteDatabase.h"
#include "sqlite3.h"
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
SqliteDatabase::SqliteDatabase(const char* host) : Database(host,"","")
{
mConnection=NULL;
mCurrentStmt=NULL;
}
void SqliteDatabase::connect()
{
int rc = sqlite3_open(mHost.c_str(), &mConnection);
if( rc )
{
cout << "Can't open database: " << mHost << " " << rc << endl;
sqlite3_close(mConnection);
}
}
void SqliteDatabase::disconnect()
{
sqlite3_finalize(mCurrentStmt);
sqlite3_close(mConnection);
}
// returns true if the query went ok
bool SqliteDatabase::executeSQL(const char* sql, bool fail_ok)
{
sqlite3_finalize(mCurrentStmt);
int rc = sqlite3_prepare_v2(mConnection, sql, -1, &mCurrentStmt, NULL);
if (rc != SQLITE_OK )
{
if (!fail_ok)
{
#ifdef DEBUG
cout << "SQL Perror:" << rc << endl;
cout << "Statement: " << sql << endl;
cout << "Error: " << sqlite3_errmsg(mConnection) << endl;
#endif
}
return false;
}
rc = sqlite3_step(mCurrentStmt);
if (rc == SQLITE_ROW)
{
mMoreRows = true;
}
else if (rc == SQLITE_DONE)
{
mMoreRows = false;
}
else
{
mMoreRows = false;
if (!fail_ok)
{
#ifdef DEBUG
cout << "SQL Serror:" << rc << endl;
cout << "Statement: " << sql << endl;
cout << "Error: " << sqlite3_errmsg(mConnection) << endl;
#endif
}
return false;
}
return true;
}
// tells you how many rows were changed by an update or insert
int SqliteDatabase::getNumRowsAffected()
{
// TODO: SqliteDatabase::getNumRowsAffected()
return(0);
}
int SqliteDatabase::getLastInsertID()
{
return(sqlite3_last_insert_rowid(mConnection));
}
// returns false if there are no results
bool SqliteDatabase::startIterRows()
{
mColNameTable.clear();
mColNameTable.resize(sqlite3_column_count(mCurrentStmt));
for(unsigned n=0; n<mColNameTable.size(); n++)
{
mColNameTable[n]=sqlite3_column_name(mCurrentStmt,n);
}
return(mMoreRows);
}
void SqliteDatabase::endIterRows()
{
sqlite3_finalize(mCurrentStmt);
mCurrentStmt=NULL;
}
// call this after you executeSQL
// will return false if there are no more rows
bool SqliteDatabase::getNextRow()
{
if(!mMoreRows) return(false);
int rc=sqlite3_step(mCurrentStmt);
if(rc==SQLITE_ROW)
{
return(true);
}else if(rc==SQLITE_DONE)
{
return(false);
}else
{
cout << "SQL Rerror:" << rc << endl;
return(false);
}
}
bool SqliteDatabase::getNull(int colIndex)
{
return(SQLITE_NULL == sqlite3_column_type(mCurrentStmt, colIndex));
}
char* SqliteDatabase::getStr(int colIndex,std::string& retStr)
{
retStr=(char*)sqlite3_column_text(mCurrentStmt, colIndex);
return((char*)retStr.c_str());
}
int32 SqliteDatabase::getInt(int colIndex)
{
return(sqlite3_column_int(mCurrentStmt, colIndex));
}
float SqliteDatabase::getFloat(int colIndex)
{
return(sqlite3_column_double(mCurrentStmt, colIndex));
}
bool SqliteDatabase::getBool(int colIndex)
{
return(sqlite3_column_int(mCurrentStmt, colIndex));
}
int SqliteDatabase::getBinary(int colIndex,unsigned char* buf,int maxSize)
{
const void* blob=sqlite3_column_blob(mCurrentStmt, colIndex);
int size=sqlite3_column_bytes(mCurrentStmt, colIndex);
if(size<maxSize) maxSize=size;
memcpy(buf,blob,maxSize);
return(size);
}
std::vector<unsigned char> SqliteDatabase::getBinary(int colIndex)
{
const unsigned char* blob = reinterpret_cast<const unsigned char*>(sqlite3_column_blob(mCurrentStmt, colIndex));
size_t iSize = sqlite3_column_bytes(mCurrentStmt, colIndex);
std::vector<unsigned char> vucResult;
vucResult.resize(iSize);
std::copy(blob, blob+iSize, vucResult.begin());
return vucResult;
}
uint64 SqliteDatabase::getBigInt(int colIndex)
{
return(sqlite3_column_int64(mCurrentStmt, colIndex));
}
/* 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(const unsigned char* start, int size, std::string& retStr)
{
static const char toHex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
retStr.resize(3 + (size * 2));
int pos = 0;
retStr[pos++] = 'X';
retStr[pos++] = '\'';
for (int n = 0; n < size; ++n)
{
retStr[pos++] = toHex[start[n] >> 4];
retStr[pos++] = toHex[start[n] & 0x0f];
}
retStr[pos] = '\'';
}
// vim:ts=4

View File

@@ -0,0 +1,45 @@
#include "database.h"
struct sqlite3;
struct sqlite3_stmt;
class SqliteDatabase : public Database
{
sqlite3* mConnection;
sqlite3_stmt* mCurrentStmt;
bool mMoreRows;
public:
SqliteDatabase(const char* host);
void connect();
void disconnect();
// returns true if the query went ok
bool executeSQL(const char* sql, bool fail_okay);
// tells you how many rows were changed by an update or insert
int getNumRowsAffected();
int getLastInsertID();
// 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();
bool getNull(int colIndex);
char* getStr(int colIndex,std::string& retStr);
int32 getInt(int colIndex);
float getFloat(int colIndex);
bool getBool(int colIndex);
// returns amount stored in buf
int getBinary(int colIndex,unsigned char* buf,int maxSize);
std::vector<unsigned char> getBinary(int colIndex);
uint64 getBigInt(int colIndex);
void escape(const unsigned char* start,int size,std::string& retStr);
};
// vim:ts=4

View File

@@ -0,0 +1,196 @@
#include "database.h"
#include <stdlib.h>
#include <string.h>
Database::Database(const char* host,const char* user,const char* pass) : mNumCol(0)
{
mDBPass = pass;
mHost = host;
mUser = user;
}
Database::~Database()
{
}
bool Database::getNull(const char* colName)
{
int index;
if (getColNumber(colName,&index))
{
return getNull(index);
}
return true;
}
char* Database::getStr(const char* colName,std::string& retStr)
{
int index;
if (getColNumber(colName,&index))
{
return getStr(index,retStr);
}
return NULL;
}
int32 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;
}
int Database::getBinary(const char* colName,unsigned char* buf,int maxSize)
{
int index;
if (getColNumber(colName,&index))
{
return(getBinary(index,buf,maxSize));
}
return(0);
}
std::vector<unsigned char> Database::getBinary(const std::string& strColName)
{
int index;
if (getColNumber(strColName.c_str(), &index))
{
return getBinary(index);
}
return std::vector<unsigned char>();
}
std::string Database::getStrBinary(const std::string& strColName)
{
// YYY Could eliminate a copy if getStrBinary was a template.
return strCopy(getBinary(strColName.c_str()));
}
uint64 Database::getBigInt(const char* colName)
{
int index;
if (getColNumber(colName,&index))
{
return getBigInt(index);
}
return 0;
}
// returns false if can't find col
bool Database::getColNumber(const char* colName,int* retIndex)
{
for (unsigned int n=0; n<mColNameTable.size(); n++)
{
if (strcmp(colName,mColNameTable[n].c_str())==0)
{
*retIndex=n;
return(true);
}
}
return false;
}
#if 0
int Database::getSingleDBValueInt(const char* sql)
{
int ret;
if ( executeSQL(sql) && startIterRows()
{
ret=getInt(0);
endIterRows();
}
else
{
//theUI->statusMsg("ERROR with database: %s",sql);
ret=0;
}
return(ret);
}
#endif
#if 0
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);
}
#endif
#if 0
char* Database::getSingleDBValueStr(const char* sql,std::string& retStr)
{
char* ret;
if (executeSQL(sql) && startIterRows())
{
ret=getStr(0,retStr);
endIterRows();
}
else
{
//theUI->statusMsg("ERROR with database: %s",sql);
ret=0;
}
return(ret);
}
#endif
std::string Database::escape(const std::string strValue)
{
std::string strReturn;
escape(reinterpret_cast<const unsigned char*>(strValue.c_str()), strValue.size(), strReturn);
return strReturn;
}
// vim:ts=4

View File

@@ -0,0 +1,92 @@
#ifndef __DATABASE__
#define __DATABASE__
#include <string>
#include <vector>
#include "../ripple/types.h"
#include "../ripple/utils.h"
#define SQL_FOREACH(_db, _strQuery) \
if ((_db)->executeSQL(_strQuery)) \
for (bool _bMore = (_db)->startIterRows(); _bMore; _bMore = (_db)->getNextRow())
#define SQL_EXISTS(_db, _strQuery) \
((_db)->executeSQL(_strQuery) && (_db)->startIterRows())
/*
this maintains the connection to the database
*/
class Database
{
protected:
int mNumCol;
std::string mUser;
std::string mHost;
std::string mDBPass;
std::vector<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(const unsigned char* start,int size,std::string& retStr)=0;
std::string escape(const std::string strValue);
// returns true if the query went ok
virtual bool executeSQL(const char* sql, bool fail_okay=false)=0;
bool executeSQL(std::string strSql, bool fail_okay=false) {
return executeSQL(strSql.c_str(), fail_okay);
}
// tells you how many rows were changed by an update or insert
virtual int getNumRowsAffected()=0;
virtual int getLastInsertID()=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
bool getNull(const char* colName);
char* getStr(const char* colName,std::string& retStr);
std::string getStrBinary(const std::string& strColName);
int32 getInt(const char* colName);
float getFloat(const char* colName);
bool getBool(const char* colName);
// returns amount stored in buf
int getBinary(const char* colName, unsigned char* buf, int maxSize);
std::vector<unsigned char> getBinary(const std::string& strColName);
uint64 getBigInt(const char* colName);
virtual bool getNull(int colIndex)=0;
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 int getBinary(int colIndex,unsigned char* buf,int maxSize)=0;
virtual uint64 getBigInt(int colIndex)=0;
virtual std::vector<unsigned char> getBinary(int colIndex)=0;
// int getSingleDBValueInt(const char* sql);
// float getSingleDBValueFloat(const char* sql);
// char* getSingleDBValueStr(const char* sql, std::string& retStr);
};
#endif
// vim:ts=4

View 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);
}

View File

@@ -0,0 +1,47 @@
#ifndef __MYSQLDATABASE__
#define __MYSQLDATABASE__
#include "../database.h"
#include "string/i4string.h"
#include "mysql/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

134161
src/cpp/database/sqlite3.c Normal file

File diff suppressed because it is too large Load Diff

6999
src/cpp/database/sqlite3.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,447 @@
/*
** 2006 June 7
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This header file defines the SQLite interface for use by
** shared libraries that want to be imported as extensions into
** an SQLite instance. Shared libraries that intend to be loaded
** as extensions by SQLite should #include this file instead of
** sqlite3.h.
*/
#ifndef _SQLITE3EXT_H_
#define _SQLITE3EXT_H_
#include "sqlite3.h"
typedef struct sqlite3_api_routines sqlite3_api_routines;
/*
** The following structure holds pointers to all of the SQLite API
** routines.
**
** WARNING: In order to maintain backwards compatibility, add new
** interfaces to the end of this structure only. If you insert new
** interfaces in the middle of this structure, then older different
** versions of SQLite will not be able to load each others' shared
** libraries!
*/
struct sqlite3_api_routines {
void * (*aggregate_context)(sqlite3_context*,int nBytes);
int (*aggregate_count)(sqlite3_context*);
int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
int (*bind_double)(sqlite3_stmt*,int,double);
int (*bind_int)(sqlite3_stmt*,int,int);
int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
int (*bind_null)(sqlite3_stmt*,int);
int (*bind_parameter_count)(sqlite3_stmt*);
int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
const char * (*bind_parameter_name)(sqlite3_stmt*,int);
int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
int (*busy_timeout)(sqlite3*,int ms);
int (*changes)(sqlite3*);
int (*close)(sqlite3*);
int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
int eTextRep,const char*));
int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
int eTextRep,const void*));
const void * (*column_blob)(sqlite3_stmt*,int iCol);
int (*column_bytes)(sqlite3_stmt*,int iCol);
int (*column_bytes16)(sqlite3_stmt*,int iCol);
int (*column_count)(sqlite3_stmt*pStmt);
const char * (*column_database_name)(sqlite3_stmt*,int);
const void * (*column_database_name16)(sqlite3_stmt*,int);
const char * (*column_decltype)(sqlite3_stmt*,int i);
const void * (*column_decltype16)(sqlite3_stmt*,int);
double (*column_double)(sqlite3_stmt*,int iCol);
int (*column_int)(sqlite3_stmt*,int iCol);
sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
const char * (*column_name)(sqlite3_stmt*,int);
const void * (*column_name16)(sqlite3_stmt*,int);
const char * (*column_origin_name)(sqlite3_stmt*,int);
const void * (*column_origin_name16)(sqlite3_stmt*,int);
const char * (*column_table_name)(sqlite3_stmt*,int);
const void * (*column_table_name16)(sqlite3_stmt*,int);
const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
const void * (*column_text16)(sqlite3_stmt*,int iCol);
int (*column_type)(sqlite3_stmt*,int iCol);
sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
int (*complete)(const char*sql);
int (*complete16)(const void*sql);
int (*create_collation)(sqlite3*,const char*,int,void*,
int(*)(void*,int,const void*,int,const void*));
int (*create_collation16)(sqlite3*,const void*,int,void*,
int(*)(void*,int,const void*,int,const void*));
int (*create_function)(sqlite3*,const char*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*));
int (*create_function16)(sqlite3*,const void*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*));
int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
int (*data_count)(sqlite3_stmt*pStmt);
sqlite3 * (*db_handle)(sqlite3_stmt*);
int (*declare_vtab)(sqlite3*,const char*);
int (*enable_shared_cache)(int);
int (*errcode)(sqlite3*db);
const char * (*errmsg)(sqlite3*);
const void * (*errmsg16)(sqlite3*);
int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
int (*expired)(sqlite3_stmt*);
int (*finalize)(sqlite3_stmt*pStmt);
void (*free)(void*);
void (*free_table)(char**result);
int (*get_autocommit)(sqlite3*);
void * (*get_auxdata)(sqlite3_context*,int);
int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
int (*global_recover)(void);
void (*interruptx)(sqlite3*);
sqlite_int64 (*last_insert_rowid)(sqlite3*);
const char * (*libversion)(void);
int (*libversion_number)(void);
void *(*malloc)(int);
char * (*mprintf)(const char*,...);
int (*open)(const char*,sqlite3**);
int (*open16)(const void*,sqlite3**);
int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
void *(*realloc)(void*,int);
int (*reset)(sqlite3_stmt*pStmt);
void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_double)(sqlite3_context*,double);
void (*result_error)(sqlite3_context*,const char*,int);
void (*result_error16)(sqlite3_context*,const void*,int);
void (*result_int)(sqlite3_context*,int);
void (*result_int64)(sqlite3_context*,sqlite_int64);
void (*result_null)(sqlite3_context*);
void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_value)(sqlite3_context*,sqlite3_value*);
void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
const char*,const char*),void*);
void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
char * (*snprintf)(int,char*,const char*,...);
int (*step)(sqlite3_stmt*);
int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
char const**,char const**,int*,int*,int*);
void (*thread_cleanup)(void);
int (*total_changes)(sqlite3*);
void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
sqlite_int64),void*);
void * (*user_data)(sqlite3_context*);
const void * (*value_blob)(sqlite3_value*);
int (*value_bytes)(sqlite3_value*);
int (*value_bytes16)(sqlite3_value*);
double (*value_double)(sqlite3_value*);
int (*value_int)(sqlite3_value*);
sqlite_int64 (*value_int64)(sqlite3_value*);
int (*value_numeric_type)(sqlite3_value*);
const unsigned char * (*value_text)(sqlite3_value*);
const void * (*value_text16)(sqlite3_value*);
const void * (*value_text16be)(sqlite3_value*);
const void * (*value_text16le)(sqlite3_value*);
int (*value_type)(sqlite3_value*);
char *(*vmprintf)(const char*,va_list);
/* Added ??? */
int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
/* Added by 3.3.13 */
int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
int (*clear_bindings)(sqlite3_stmt*);
/* Added by 3.4.1 */
int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
void (*xDestroy)(void *));
/* Added by 3.5.0 */
int (*bind_zeroblob)(sqlite3_stmt*,int,int);
int (*blob_bytes)(sqlite3_blob*);
int (*blob_close)(sqlite3_blob*);
int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
int,sqlite3_blob**);
int (*blob_read)(sqlite3_blob*,void*,int,int);
int (*blob_write)(sqlite3_blob*,const void*,int,int);
int (*create_collation_v2)(sqlite3*,const char*,int,void*,
int(*)(void*,int,const void*,int,const void*),
void(*)(void*));
int (*file_control)(sqlite3*,const char*,int,void*);
sqlite3_int64 (*memory_highwater)(int);
sqlite3_int64 (*memory_used)(void);
sqlite3_mutex *(*mutex_alloc)(int);
void (*mutex_enter)(sqlite3_mutex*);
void (*mutex_free)(sqlite3_mutex*);
void (*mutex_leave)(sqlite3_mutex*);
int (*mutex_try)(sqlite3_mutex*);
int (*open_v2)(const char*,sqlite3**,int,const char*);
int (*release_memory)(int);
void (*result_error_nomem)(sqlite3_context*);
void (*result_error_toobig)(sqlite3_context*);
int (*sleep)(int);
void (*soft_heap_limit)(int);
sqlite3_vfs *(*vfs_find)(const char*);
int (*vfs_register)(sqlite3_vfs*,int);
int (*vfs_unregister)(sqlite3_vfs*);
int (*xthreadsafe)(void);
void (*result_zeroblob)(sqlite3_context*,int);
void (*result_error_code)(sqlite3_context*,int);
int (*test_control)(int, ...);
void (*randomness)(int,void*);
sqlite3 *(*context_db_handle)(sqlite3_context*);
int (*extended_result_codes)(sqlite3*,int);
int (*limit)(sqlite3*,int,int);
sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
const char *(*sql)(sqlite3_stmt*);
int (*status)(int,int*,int*,int);
int (*backup_finish)(sqlite3_backup*);
sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
int (*backup_pagecount)(sqlite3_backup*);
int (*backup_remaining)(sqlite3_backup*);
int (*backup_step)(sqlite3_backup*,int);
const char *(*compileoption_get)(int);
int (*compileoption_used)(const char*);
int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*),
void(*xDestroy)(void*));
int (*db_config)(sqlite3*,int,...);
sqlite3_mutex *(*db_mutex)(sqlite3*);
int (*db_status)(sqlite3*,int,int*,int*,int);
int (*extended_errcode)(sqlite3*);
void (*log)(int,const char*,...);
sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
const char *(*sourceid)(void);
int (*stmt_status)(sqlite3_stmt*,int,int);
int (*strnicmp)(const char*,const char*,int);
int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
int (*wal_autocheckpoint)(sqlite3*,int);
int (*wal_checkpoint)(sqlite3*,const char*);
void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
int (*vtab_config)(sqlite3*,int op,...);
int (*vtab_on_conflict)(sqlite3*);
};
/*
** The following macros redefine the API routines so that they are
** redirected throught the global sqlite3_api structure.
**
** This header file is also used by the loadext.c source file
** (part of the main SQLite library - not an extension) so that
** it can get access to the sqlite3_api_routines structure
** definition. But the main library does not want to redefine
** the API. So the redefinition macros are only valid if the
** SQLITE_CORE macros is undefined.
*/
#ifndef SQLITE_CORE
#define sqlite3_aggregate_context sqlite3_api->aggregate_context
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_aggregate_count sqlite3_api->aggregate_count
#endif
#define sqlite3_bind_blob sqlite3_api->bind_blob
#define sqlite3_bind_double sqlite3_api->bind_double
#define sqlite3_bind_int sqlite3_api->bind_int
#define sqlite3_bind_int64 sqlite3_api->bind_int64
#define sqlite3_bind_null sqlite3_api->bind_null
#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
#define sqlite3_bind_text sqlite3_api->bind_text
#define sqlite3_bind_text16 sqlite3_api->bind_text16
#define sqlite3_bind_value sqlite3_api->bind_value
#define sqlite3_busy_handler sqlite3_api->busy_handler
#define sqlite3_busy_timeout sqlite3_api->busy_timeout
#define sqlite3_changes sqlite3_api->changes
#define sqlite3_close sqlite3_api->close
#define sqlite3_collation_needed sqlite3_api->collation_needed
#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
#define sqlite3_column_blob sqlite3_api->column_blob
#define sqlite3_column_bytes sqlite3_api->column_bytes
#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
#define sqlite3_column_count sqlite3_api->column_count
#define sqlite3_column_database_name sqlite3_api->column_database_name
#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
#define sqlite3_column_decltype sqlite3_api->column_decltype
#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
#define sqlite3_column_double sqlite3_api->column_double
#define sqlite3_column_int sqlite3_api->column_int
#define sqlite3_column_int64 sqlite3_api->column_int64
#define sqlite3_column_name sqlite3_api->column_name
#define sqlite3_column_name16 sqlite3_api->column_name16
#define sqlite3_column_origin_name sqlite3_api->column_origin_name
#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
#define sqlite3_column_table_name sqlite3_api->column_table_name
#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
#define sqlite3_column_text sqlite3_api->column_text
#define sqlite3_column_text16 sqlite3_api->column_text16
#define sqlite3_column_type sqlite3_api->column_type
#define sqlite3_column_value sqlite3_api->column_value
#define sqlite3_commit_hook sqlite3_api->commit_hook
#define sqlite3_complete sqlite3_api->complete
#define sqlite3_complete16 sqlite3_api->complete16
#define sqlite3_create_collation sqlite3_api->create_collation
#define sqlite3_create_collation16 sqlite3_api->create_collation16
#define sqlite3_create_function sqlite3_api->create_function
#define sqlite3_create_function16 sqlite3_api->create_function16
#define sqlite3_create_module sqlite3_api->create_module
#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
#define sqlite3_data_count sqlite3_api->data_count
#define sqlite3_db_handle sqlite3_api->db_handle
#define sqlite3_declare_vtab sqlite3_api->declare_vtab
#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
#define sqlite3_errcode sqlite3_api->errcode
#define sqlite3_errmsg sqlite3_api->errmsg
#define sqlite3_errmsg16 sqlite3_api->errmsg16
#define sqlite3_exec sqlite3_api->exec
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_expired sqlite3_api->expired
#endif
#define sqlite3_finalize sqlite3_api->finalize
#define sqlite3_free sqlite3_api->free
#define sqlite3_free_table sqlite3_api->free_table
#define sqlite3_get_autocommit sqlite3_api->get_autocommit
#define sqlite3_get_auxdata sqlite3_api->get_auxdata
#define sqlite3_get_table sqlite3_api->get_table
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_global_recover sqlite3_api->global_recover
#endif
#define sqlite3_interrupt sqlite3_api->interruptx
#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
#define sqlite3_libversion sqlite3_api->libversion
#define sqlite3_libversion_number sqlite3_api->libversion_number
#define sqlite3_malloc sqlite3_api->malloc
#define sqlite3_mprintf sqlite3_api->mprintf
#define sqlite3_open sqlite3_api->open
#define sqlite3_open16 sqlite3_api->open16
#define sqlite3_prepare sqlite3_api->prepare
#define sqlite3_prepare16 sqlite3_api->prepare16
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
#define sqlite3_profile sqlite3_api->profile
#define sqlite3_progress_handler sqlite3_api->progress_handler
#define sqlite3_realloc sqlite3_api->realloc
#define sqlite3_reset sqlite3_api->reset
#define sqlite3_result_blob sqlite3_api->result_blob
#define sqlite3_result_double sqlite3_api->result_double
#define sqlite3_result_error sqlite3_api->result_error
#define sqlite3_result_error16 sqlite3_api->result_error16
#define sqlite3_result_int sqlite3_api->result_int
#define sqlite3_result_int64 sqlite3_api->result_int64
#define sqlite3_result_null sqlite3_api->result_null
#define sqlite3_result_text sqlite3_api->result_text
#define sqlite3_result_text16 sqlite3_api->result_text16
#define sqlite3_result_text16be sqlite3_api->result_text16be
#define sqlite3_result_text16le sqlite3_api->result_text16le
#define sqlite3_result_value sqlite3_api->result_value
#define sqlite3_rollback_hook sqlite3_api->rollback_hook
#define sqlite3_set_authorizer sqlite3_api->set_authorizer
#define sqlite3_set_auxdata sqlite3_api->set_auxdata
#define sqlite3_snprintf sqlite3_api->snprintf
#define sqlite3_step sqlite3_api->step
#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
#define sqlite3_total_changes sqlite3_api->total_changes
#define sqlite3_trace sqlite3_api->trace
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
#endif
#define sqlite3_update_hook sqlite3_api->update_hook
#define sqlite3_user_data sqlite3_api->user_data
#define sqlite3_value_blob sqlite3_api->value_blob
#define sqlite3_value_bytes sqlite3_api->value_bytes
#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
#define sqlite3_value_double sqlite3_api->value_double
#define sqlite3_value_int sqlite3_api->value_int
#define sqlite3_value_int64 sqlite3_api->value_int64
#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
#define sqlite3_value_text sqlite3_api->value_text
#define sqlite3_value_text16 sqlite3_api->value_text16
#define sqlite3_value_text16be sqlite3_api->value_text16be
#define sqlite3_value_text16le sqlite3_api->value_text16le
#define sqlite3_value_type sqlite3_api->value_type
#define sqlite3_vmprintf sqlite3_api->vmprintf
#define sqlite3_overload_function sqlite3_api->overload_function
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
#define sqlite3_clear_bindings sqlite3_api->clear_bindings
#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
#define sqlite3_blob_bytes sqlite3_api->blob_bytes
#define sqlite3_blob_close sqlite3_api->blob_close
#define sqlite3_blob_open sqlite3_api->blob_open
#define sqlite3_blob_read sqlite3_api->blob_read
#define sqlite3_blob_write sqlite3_api->blob_write
#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
#define sqlite3_file_control sqlite3_api->file_control
#define sqlite3_memory_highwater sqlite3_api->memory_highwater
#define sqlite3_memory_used sqlite3_api->memory_used
#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
#define sqlite3_mutex_enter sqlite3_api->mutex_enter
#define sqlite3_mutex_free sqlite3_api->mutex_free
#define sqlite3_mutex_leave sqlite3_api->mutex_leave
#define sqlite3_mutex_try sqlite3_api->mutex_try
#define sqlite3_open_v2 sqlite3_api->open_v2
#define sqlite3_release_memory sqlite3_api->release_memory
#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
#define sqlite3_sleep sqlite3_api->sleep
#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
#define sqlite3_vfs_find sqlite3_api->vfs_find
#define sqlite3_vfs_register sqlite3_api->vfs_register
#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
#define sqlite3_threadsafe sqlite3_api->xthreadsafe
#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
#define sqlite3_result_error_code sqlite3_api->result_error_code
#define sqlite3_test_control sqlite3_api->test_control
#define sqlite3_randomness sqlite3_api->randomness
#define sqlite3_context_db_handle sqlite3_api->context_db_handle
#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
#define sqlite3_limit sqlite3_api->limit
#define sqlite3_next_stmt sqlite3_api->next_stmt
#define sqlite3_sql sqlite3_api->sql
#define sqlite3_status sqlite3_api->status
#define sqlite3_backup_finish sqlite3_api->backup_finish
#define sqlite3_backup_init sqlite3_api->backup_init
#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
#define sqlite3_backup_remaining sqlite3_api->backup_remaining
#define sqlite3_backup_step sqlite3_api->backup_step
#define sqlite3_compileoption_get sqlite3_api->compileoption_get
#define sqlite3_compileoption_used sqlite3_api->compileoption_used
#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
#define sqlite3_db_config sqlite3_api->db_config
#define sqlite3_db_mutex sqlite3_api->db_mutex
#define sqlite3_db_status sqlite3_api->db_status
#define sqlite3_extended_errcode sqlite3_api->extended_errcode
#define sqlite3_log sqlite3_api->log
#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
#define sqlite3_sourceid sqlite3_api->sourceid
#define sqlite3_stmt_status sqlite3_api->stmt_status
#define sqlite3_strnicmp sqlite3_api->strnicmp
#define sqlite3_unlock_notify sqlite3_api->unlock_notify
#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
#define sqlite3_wal_hook sqlite3_api->wal_hook
#define sqlite3_blob_reopen sqlite3_api->blob_reopen
#define sqlite3_vtab_config sqlite3_api->vtab_config
#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
#endif /* SQLITE_CORE */
#define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api = 0;
#define SQLITE_EXTENSION_INIT2(v) sqlite3_api = v;
#endif /* _SQLITE3EXT_H_ */

View 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__ */

View File

@@ -0,0 +1,246 @@
#include "windatabase.h"
#include "dbutility.h"
using namespace std;
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.c_str(), SQL_NTS, (unsigned char*)(char*) mUser.c_str(), SQL_NTS, (unsigned char*)(char*) mDBPass.c_str(), 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, bool fail_okay)
{
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)
{
mColNameTable.resize(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,string& 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((char*)retStr.c_str());
}
int32 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);
}
// TODO
void WinDatabase::escape(const unsigned char* start,int size,std::string& retStr)
{
retStr=(char*)start;
}
// TODO
int WinDatabase::getLastInsertID()
{
return(0);
}
uint64 WinDatabase::getBigInt(int colIndex)
{
colIndex++;
uint64 ret=0;
SQLRETURN rc = SQLGetData(hstmt,colIndex,SQL_INTEGER,&ret,sizeof(uint64),NULL);
mystmt(hstmt,rc);
return(ret);
}
// TODO:
int WinDatabase::getBinary(int colIndex,unsigned char* buf,int maxSize)
{
return(0);
}
std::vector<unsigned char> WinDatabase::getBinary(int colIndex)
{
// TODO:
std::vector<unsigned char> vucResult;
return vucResult;
}

View File

@@ -0,0 +1,61 @@
#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
/*
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((char*)mDBPass.c_str()); }
// returns true if the query went ok
bool executeSQL(const char* sql, bool fail_okay=false);
int getNumRowsAffected();
int getLastInsertID();
// 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,std::string& retStr);
int32 getInt(int colIndex);
float getFloat(int colIndex);
bool getBool(int colIndex);
uint64 getBigInt(int colIndex);
int getBinary(int colIndex,unsigned char* buf,int maxSize);
std::vector<unsigned char> getBinary(int colIndex);
bool getNull(int colIndex){ return(true); }
void escape(const unsigned char* start,int size,std::string& retStr);
};
#endif