⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 odbcconnection.c.save

📁 适合于Unix/Linux下的一个持久数据库连接池
💻 SAVE
📖 第 1 页 / 共 2 页
字号:
// Copyright (c) 1999-2001  David Muse// See the file COPYING for more information#include <odbcconnection.h>#include <config.h>#include <datatypes.h>#include <stdlib.h>uint16_t odbcconnection::getNumberOfConnectStringVars() {	return NUM_CONNECT_STRING_VARS;}void odbcconnection::handleConnectString() {	dsn=connectStringValue("dsn");	setUser(connectStringValue("user"));	setPassword(connectStringValue("password"));	const char	*autocom=connectStringValue("autocommit");	setAutoCommitBehavior((autocom &&		!charstring::compareIgnoringCase(autocom,"yes")));}bool odbcconnection::logIn() {	// allocate environment handle#if (ODBCVER >= 0x0300)	erg=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		SQLFreeHandle(SQL_HANDLE_ENV,env);		return false;	}	erg=SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,				(void *)SQL_OV_ODBC3,0);#else	erg=SQLAllocEnv(&env);#endif	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {#if (ODBCVER >= 0x0300)		SQLFreeHandle(SQL_HANDLE_ENV,env);#else		SQLFreeEnv(env);#endif		return false;	}	// allocate connection handle#if (ODBCVER >= 0x0300)	erg=SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc);#else	erg=SQLAllocConnect(env,&dbc);#endif	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {#if (ODBCVER >= 0x0300)		SQLFreeHandle(SQL_HANDLE_ENV,env);		SQLFreeHandle(SQL_HANDLE_DBC,dbc);#else		SQLFreeConnect(dbc);		SQLFreeEnv(env);#endif		return false;	}	// set the connect timeout#if (ODBCVER >= 0x0300)	SQLSetConnectAttr(dbc,SQL_LOGIN_TIMEOUT,(SQLPOINTER *)5,0);#endif	// connect to the database	erg=SQLConnect(dbc,(SQLCHAR *)dsn,SQL_NTS,				(SQLCHAR *)getUser(),SQL_NTS,				(SQLCHAR *)getPassword(),SQL_NTS);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {#if (ODBCVER >= 0x0300)		SQLFreeHandle(SQL_HANDLE_ENV,env);		SQLFreeHandle(SQL_HANDLE_DBC,dbc);#else		SQLFreeConnect(dbc);		SQLFreeEnv(env);#endif		return false;	}	return true;}sqlrcursor_svr *odbcconnection::initCursor() {	return (sqlrcursor_svr *)new odbccursor((sqlrconnection_svr *)this);}void odbcconnection::deleteCursor(sqlrcursor_svr *curs) {	delete (odbccursor *)curs;}void odbcconnection::logOut() {	SQLDisconnect(dbc);#if (ODBCVER >= 0x0300)	SQLFreeHandle(SQL_HANDLE_DBC,dbc);	SQLFreeHandle(SQL_HANDLE_ENV,env);#else	SQLFreeConnect(dbc);	SQLFreeEnv(env);#endif}bool odbcconnection::ping() {	return true;}const char *odbcconnection::identify() {	return "odbc";}#if (ODBCVER >= 0x0300)bool odbcconnection::autoCommitOn() {	return (SQLSetConnectAttr(dbc,SQL_ATTR_AUTOCOMMIT,				(SQLPOINTER)SQL_AUTOCOMMIT_ON,				sizeof(SQLINTEGER))==SQL_SUCCESS);}bool odbcconnection::autoCommitOff() {	return (SQLSetConnectAttr(dbc,SQL_ATTR_AUTOCOMMIT,				(SQLPOINTER)SQL_AUTOCOMMIT_OFF,				sizeof(SQLINTEGER))==SQL_SUCCESS);}bool odbcconnection::commit() {	return (SQLEndTran(SQL_HANDLE_ENV,env,SQL_COMMIT)==SQL_SUCCESS);}bool odbcconnection::rollback() {	return (SQLEndTran(SQL_HANDLE_ENV,env,SQL_ROLLBACK)==SQL_SUCCESS);}#endifodbccursor::odbccursor(sqlrconnection_svr *conn) : sqlrcursor_svr(conn) {	errormsg=NULL;	odbcconn=(odbcconnection *)conn;	stmt=NULL;}odbccursor::~odbccursor() {	if (errormsg) {		delete errormsg;	}}bool odbccursor::prepareQuery(const char *query, uint32_t length) {	if (stmt) {#if (ODBCVER >= 0x0300)		SQLFreeHandle(SQL_HANDLE_STMT,stmt);#else		SQLFreeStmt(stmt,SQL_DROP);#endif	}	// allocate the cursor#if (ODBCVER >= 0x0300)	erg=SQLAllocHandle(SQL_HANDLE_STMT,odbcconn->dbc,&stmt);#else	erg=SQLAllocStmt(odbcconn->dbc,&stmt);#endif	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}// this code is here in case unixodbc or iodbc ever // successfully support array fetches/*#if (ODBCVER >= 0x0300)	// set the row array size	erg=SQLSetStmtAttr(stmt,SQL_ATTR_ROW_ARRAY_SIZE,				(SQLPOINTER)FETCH_AT_ONCE,0);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}#endif*/	// prepare the query	erg=SQLPrepare(stmt,(SQLCHAR *)query,length);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	return true;}bool odbccursor::inputBindString(const char *variable,					uint16_t variablesize,					const char *value,					uint16_t valuesize,					short *isnull) {	if (*isnull==SQL_NULL_DATA) {		erg=SQLBindParameter(stmt,				charstring::toInteger(variable+1),				SQL_PARAM_INPUT,				SQL_C_CHAR,				SQL_CHAR,				0,				0,				(SQLPOINTER)value,				valuesize,				#ifdef SQLBINDPARAMETER_SQLLEN				(SQLLEN *)isnull				#else				(SQLINTEGER *)isnull				#endif				);	} else {		erg=SQLBindParameter(stmt,				charstring::toInteger(variable+1),				SQL_PARAM_INPUT,				SQL_C_CHAR,				SQL_CHAR,				0,				0,				(SQLPOINTER)value,				valuesize,				#ifdef SQLBINDPARAMETER_SQLLEN				(SQLLEN *)NULL				#else				(SQLINTEGER *)NULL				#endif				);	}	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	return true;}bool odbccursor::inputBindInteger(const char *variable,					uint16_t variablesize,					int64_t *value) {	erg=SQLBindParameter(stmt,				charstring::toInteger(variable+1),				SQL_PARAM_INPUT,				SQL_C_LONG,				SQL_INTEGER,				0,				0,				value,				sizeof(int64_t),				#ifdef SQLBINDPARAMETER_SQLLEN				(SQLLEN *)NULL				#else				(SQLINTEGER *)NULL				#endif				);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	return true;}bool odbccursor::inputBindDouble(const char *variable,					uint16_t variablesize,					double *value,					uint32_t precision,					uint32_t scale) {	erg=SQLBindParameter(stmt,				charstring::toInteger(variable+1),				SQL_PARAM_INPUT,				SQL_C_DOUBLE,				SQL_DECIMAL,				precision,				scale,				value,				sizeof(double),				#ifdef SQLBINDPARAMETER_SQLLEN				(SQLLEN *)NULL				#else				(SQLINTEGER *)NULL				#endif				);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	return true;}bool odbccursor::outputBindString(const char *variable, 					uint16_t variablesize,					const char *value, 					uint16_t valuesize, 					short *isnull) {	erg=SQLBindParameter(stmt,				charstring::toInteger(variable+1),				SQL_PARAM_OUTPUT,				SQL_C_CHAR,				SQL_CHAR,				0,				0,				(SQLPOINTER)value,				valuesize,				#ifdef SQLBINDPARAMETER_SQLLEN				(SQLLEN *)isnull				#else				(SQLINTEGER *)isnull				#endif				);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	return true;}bool odbccursor::outputBindInteger(const char *variable,						uint16_t variablesize,						int64_t *value,						int16_t *isnull) {	erg=SQLBindParameter(stmt,				charstring::toInteger(variable+1),				SQL_PARAM_OUTPUT,				SQL_C_LONG,				SQL_INTEGER,				0,				0,				value,				sizeof(int64_t),				#ifdef SQLBINDPARAMETER_SQLLEN				(SQLLEN *)isnull				#else				(SQLINTEGER *)isnull				#endif				);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	return true;}bool odbccursor::outputBindDouble(const char *variable,						uint16_t variablesize,						double *value,						uint32_t *precision,						uint32_t *scale,						int16_t *isnull) {	erg=SQLBindParameter(stmt,				charstring::toInteger(variable+1),				SQL_PARAM_OUTPUT,				SQL_C_DOUBLE,				SQL_DOUBLE,				0,				0,				value,				sizeof(double),				#ifdef SQLBINDPARAMETER_SQLLEN				(SQLLEN *)isnull				#else				(SQLINTEGER *)isnull				#endif				);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	return true;}short odbccursor::nonNullBindValue() {	return 0;}short odbccursor::nullBindValue() {	return SQL_NULL_DATA;}bool odbccursor::bindValueIsNull(short isnull) {	return (isnull==SQL_NULL_DATA);}bool odbccursor::executeQuery(const char *query, uint32_t length,							bool execute) {	// initialize counts	ncols=0;	row=0;	maxrow=0;	totalrows=0;	// execute the query	erg=SQLExecute(stmt);	if (erg!=SQL_SUCCESS &&		erg!=SQL_SUCCESS_WITH_INFO#if defined(SQL_NO_DATA)		&& erg!=SQL_NO_DATA#elif defined(SQL_NO_DATA_FOUND)		&& erg!=SQL_NO_DATA_FOUND#endif		) {		return false;	}	checkForTempTable(query,length);	// get the column count	erg=SQLNumResultCols(stmt,&ncols);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	if (ncols>MAX_SELECT_LIST_SIZE) {		ncols=MAX_SELECT_LIST_SIZE;	}	// run through the columns	for (SQLSMALLINT i=0; i<ncols; i++) {		if (conn->sendColumnInfo()) {#if (ODBCVER >= 0x0300)			// column name

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -