// SQLConnection.cpp: implementation of the CSQLConnection class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SQLConnection.h"
#include "SQLResult.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNAMIC(CSQLConnection, CSQLBase)
/******************************************************************************
Function: CSQLConnection::CSQLConnection
Purpose: Constructor
Remarks: Call the Open function to establish the connection before
every other database operations.
******************************************************************************/
CSQLConnection::CSQLConnection()
: CSQLBase(NULL)
{
}
/******************************************************************************
Function: CSQLConnection::~CSQLConnection
Purpose: Destructor. Closes the database connection.
******************************************************************************/
CSQLConnection::~CSQLConnection()
{
Close(); // close the database connection and frees the connection handle
}
/******************************************************************************
Function: CSQLConnection::Open
Purpose: Connect to database
Parameters: pszHost - Host name
pszDatabase - Database name
pszUser - user name
nPort - port number; default 3306
Exceptions: - mysql_init fails
- mysql_real_connect fails
******************************************************************************/
void CSQLConnection::Open(LPCTSTR pszHost, LPCTSTR pszDatabase,
LPCTSTR pszUser, LPCTSTR pszPassword,
UINT nPort /*=3306*/)
{
Close();
m_hConnect = mysql_init(NULL);
ThrowExceptionIfNotOpen();
HCONNECT hConnect = mysql_real_connect(m_hConnect, pszHost,
pszUser, pszPassword,
pszDatabase,
nPort, NULL, 0 );
// if mysql_real_connect failed then returns NULL
// but first argument, m_hConnect, which was initialized by
// mysql_init can be further used to get the error
if(NULL == hConnect)
{
ThrowException(); // m_hConnect is used to get the error
}
}
/******************************************************************************
Function: CSQLConnection::Close
Purpose: Closes the database connection and free the connection handle
******************************************************************************/
void CSQLConnection::Close()
{
if(NULL != m_hConnect)
{
// close the database connection
// mysql_close also frees the connection handle
mysql_close(m_hConnect);
m_hConnect = NULL;
}
}
/******************************************************************************
Function: CSQLConnection::IsOpen
Purpose: checks if database connetion is open
******************************************************************************/
bool CSQLConnection::IsOpen()
{
return (NULL != m_hConnect);
}
/******************************************************************************
Function: CSQLConnection::TransBegin
Purpose: Begin transcation
******************************************************************************/
void CSQLConnection::TransBegin()
{
ExecuteSQL("START TRANSACTION");
}
/******************************************************************************
Function: CSQLConnection::TransCancel
Purpose: Rollback transcation
******************************************************************************/
void CSQLConnection::TransCancel()
{
ExecuteSQL("ROLLBACK");
}
/******************************************************************************
Function: CSQLConnection::TransCommit
Purpose: Commit transcation
******************************************************************************/
void CSQLConnection::TransCommit()
{
ExecuteSQL("COMMIT");
}
/******************************************************************************
Function: CSQLConnection::ExecuteSQL
Parameters: pszSQL - the query statement
Purpose: Executes SQL statements that do not require result-set
Exceptions: connection is not opened
mysql_query fails
******************************************************************************/
void CSQLConnection::ExecuteSQL(LPCTSTR pszSQL)
{
ThrowExceptionIfNotOpen();
if(0 != mysql_query(m_hConnect, pszSQL))
{
ThrowException();
}
}
/******************************************************************************
Function: CSQLConnection::ExecuteSQLV
Parameters: pszFormat - the format control string
... - variable list of arguments
Purpose: Executes SQL statements those do not require result-set
The SQL statement is built according to the format control string
and the variable list of arguments.
Exceptions: Thrown by ExecuteSQL
******************************************************************************/
void CSQLConnection::ExecuteSQLV(LPCTSTR pszFormat, ...)
{
CString strSQL;
// formats the string them pass it to ExecuteSQL function
va_list args;
va_start(args, pszFormat);
strSQL.FormatV(pszFormat, args);
ExecuteSQL(strSQL);
}
/******************************************************************************
Function: CSQLConnection::GetOneRow
Parameters: pszSelect - SELECT statement
arrValues - array of CSQLValues to retrieve
Purpose: Get a single row from a recordset
Exceptions: Thrown by CSQLResult::Query and CSQLResult::Fetch
******************************************************************************/
bool CSQLConnection::GetOneRow(LPCTSTR pszSelect, SQLValueArray& arrValues)
{
CSQLResult res(*this);
res.Query(pszSelect);
return res.Fetch(arrValues);
}
/******************************************************************************
Function: CSQLConnection::GetOneRowV
Parameters: arrValues - array of CSQLValues to retrieve
pszFormat - the format control string
... - variable list of arguments
Purpose: Get a single row from a recordset
The SQL statement is built according to the format control string
and the variable list of arguments.
Exceptions: Thrown by CSQLResult::Query and CSQLResult::Fetch
******************************************************************************/
bool CSQLConnection::GetOneRowV(SQLValueArray& arrValues, LPCTSTR pszFormat, ... )
{
CString strSelect;
// formats the string them pass it to GetOneRow function
va_list args;
va_start(args, pszFormat);
strSelect.FormatV(pszFormat, args);
return GetOneRow(strSelect, arrValues);
}
/******************************************************************************
Function: CSQLConnection::UpdateBLOBField
Parameters: value - contains value to update
pszTable - table name
pszField - BLOB field name
pszWhere - WHERE condition; default NULL
Purpose: Update a BLOB field
Exceptions: connection is not opened
mysql_real_query fails
******************************************************************************/
void CSQLConnection::UpdateBLOBField(const CSQLValue& value,
LPCTSTR pszTable,
LPCTSTR pszField,
LPCTSTR pszWhere /*=NULL*/)
{
_ASSERTE(true == value.IsBLOB()); // please, call this function only for BLOB type
ThrowExceptionIfNotOpen();
CString strQueryBegin, strQueryEnd;
strQueryBegin.Format(_T("UPDATE %s SET %s='"), pszTable, pszField);
if(NULL == pszWhere)
{
strQueryEnd = _T("'");
}
else
{
strQueryEnd.Format(_T("' WHERE %
- 1
- 2
前往页