📄 db2connection.c
字号:
// Copyright (c) 1999-2001 David Muse// See the file COPYING for more information#include <db2connection.h>#include <datatypes.h>#include <config.h>#include <stdlib.h>uint16_t db2connection::getNumberOfConnectStringVars() { return NUM_CONNECT_STRING_VARS;}void db2connection::handleConnectString() { // override legacy "server" parameter with modern "db" parameter server=connectStringValue("server"); const char *tmp=connectStringValue("db"); if (tmp && tmp[0]) { server=tmp; } setUser(connectStringValue("user")); setPassword(connectStringValue("password")); const char *autocom=connectStringValue("autocommit"); setAutoCommitBehavior((autocom && !charstring::compareIgnoringCase(autocom,"yes")));}bool db2connection::logIn(bool printerrors) { // allocate environment handle 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; } // allocate connection handle erg=SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) { SQLFreeHandle(SQL_HANDLE_DBC,dbc); SQLFreeHandle(SQL_HANDLE_ENV,env); return false; } // set the connect timeout SQLSetConnectAttr(dbc,SQL_LOGIN_TIMEOUT,(SQLPOINTER *)5,0); // connect to the database erg=SQLConnect(dbc,(SQLCHAR *)server,SQL_NTS, (SQLCHAR *)getUser(),SQL_NTS, (SQLCHAR *)getPassword(),SQL_NTS); if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) { SQLFreeHandle(SQL_HANDLE_DBC,dbc); SQLFreeHandle(SQL_HANDLE_ENV,env); return false; } return true;}sqlrcursor_svr *db2connection::initCursor() { return (sqlrcursor_svr *)new db2cursor((sqlrconnection_svr *)this);}void db2connection::deleteCursor(sqlrcursor_svr *curs) { delete (db2cursor *)curs;}void db2connection::logOut() { SQLDisconnect(dbc); SQLFreeHandle(SQL_HANDLE_DBC,dbc); SQLFreeHandle(SQL_HANDLE_ENV,env);}int16_t db2connection::nullBindValue() { return SQL_NULL_DATA;}bool db2connection::bindValueIsNull(int16_t isnull) { if (isnull==SQL_NULL_DATA) { return true; } return false;}bool db2connection::autoCommitOn() { return (SQLSetConnectAttr(dbc,SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, sizeof(SQLINTEGER))==SQL_SUCCESS);}bool db2connection::autoCommitOff() { return (SQLSetConnectAttr(dbc,SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_OFF, sizeof(SQLINTEGER))==SQL_SUCCESS);}bool db2connection::commit() { return (SQLEndTran(SQL_HANDLE_ENV,env,SQL_COMMIT)==SQL_SUCCESS);}bool db2connection::rollback() { return (SQLEndTran(SQL_HANDLE_ENV,env,SQL_ROLLBACK)==SQL_SUCCESS);}const char *db2connection::pingQuery() { return "values 1";}const char *db2connection::identify() { return "db2";}const char *db2connection::dbVersion() { SQLSMALLINT dbversionlen; SQLGetInfo(dbc,SQL_DBMS_VER, (SQLPOINTER)dbversion, (SQLSMALLINT)sizeof(dbversion), &dbversionlen); return dbversion;}const char *db2connection::bindFormat() { return "?";}db2cursor::db2cursor(sqlrconnection_svr *conn) : sqlrcursor_svr(conn) { db2conn=(db2connection *)conn; errormsg=NULL; stmt=0;}db2cursor::~db2cursor() { if (errormsg) { delete errormsg; }}bool db2cursor::prepareQuery(const char *query, uint32_t length) { if (stmt) { SQLFreeHandle(SQL_HANDLE_STMT,stmt); } // allocate the cursor erg=SQLAllocHandle(SQL_HANDLE_STMT,db2conn->dbc,&stmt); if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) { return false; } // 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; }#if (DB2VERSION>7) // set the row status ptr erg=SQLSetStmtAttr(stmt,SQL_ATTR_ROW_STATUS_PTR, (SQLPOINTER)rowstat,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 db2cursor::inputBindString(const char *variable, uint16_t variablesize, const char *value, uint16_t valuesize, int16_t *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, (SQLINTEGER *)isnull); } else { erg=SQLBindParameter(stmt, charstring::toInteger(variable+1), SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, (SQLPOINTER)value, valuesize, (SQLINTEGER *)NULL); } if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) { return false; } return true;}bool db2cursor::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), (SQLINTEGER *)NULL); if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) { return false; } return true;}bool db2cursor::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_DOUBLE, precision, scale, value, sizeof(double), (SQLINTEGER *)NULL); if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) { return false; } return true;}bool db2cursor::outputBindString(const char *variable, uint16_t variablesize, char *value, uint16_t valuesize, int16_t *isnull) { erg=SQLBindParameter(stmt, charstring::toInteger(variable+1), SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, value, valuesize, (SQLINTEGER *)isnull); if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) { return false; } return true;}bool db2cursor::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), (SQLINTEGER *)isnull); if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) { return false; } return true;}bool db2cursor::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), (SQLINTEGER *)isnull); if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) { return false; } return true;}bool db2cursor::executeQuery(const char *query, uint32_t length, bool execute) { // initialize counts ncols=0; rowgroupindex=0; totalinrowgroup=0; totalrows=0; // execute the query erg=SQLExecute(stmt); if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO && erg!=SQL_NO_DATA) { return false; } checkForTempTable(query,length); // get the column count erg=SQLNumResultCols(stmt,&ncols); if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -