Changeset - b9fc585e2628
[Not reviewed]
0 2 0
HanzZ - 14 years ago 2011-08-12 18:32:53
hanzz.k@gmail.com
print mysql errors
2 files changed with 5 insertions and 0 deletions:
0 comments (0 inline, 0 general)
include/transport/mysqlbackend.h
Show inline comments
 
@@ -102,28 +102,30 @@ class MySQLBackend : public StorageBackend
 

	
 
				// Pushes new data used as input for the statement.
 
				template <typename T>
 
				Statement& operator << (const T& t);
 

	
 
				Statement& operator << (const std::string& str);
 

	
 
				// Pulls fetched data by previous execute(); call.
 
				template <typename T>
 
				Statement& operator >> (T& t);
 
			private:
 
				MYSQL_STMT *m_stmt;
 
				MYSQL *m_conn;
 
				std::vector<MYSQL_BIND> m_params;
 
				int m_resultOffset;
 
				int m_offset;
 
				int m_error;
 
				std::string m_string;
 
		};
 

	
 
		MYSQL m_conn;
 
		Config *m_config;
 
		std::string m_prefix;
 

	
 
		// statements
 
// 		MYSQL_STMT *m_setUser;
 
		Statement * m_setUser;
 
		MYSQL_STMT *m_getUser;
 
		MYSQL_STMT *m_getUserSetting;
 
		MYSQL_STMT *m_setUserSetting;
src/mysqlbackend.cpp
Show inline comments
 
@@ -69,25 +69,27 @@ using namespace log4cxx;
 
	if (mysql_stmt_execute(STATEMENT)) { \
 
		LOG4CXX_ERROR(logger, NAME << " " << mysql_error(&m_conn)); \
 
	}
 

	
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
static LoggerPtr logger = Logger::getLogger("MySQLBackend");
 

	
 
MySQLBackend::Statement::Statement(MYSQL *conn, const std::string &format, const std::string &statement) {
 
	m_resultOffset = -1;
 
	m_conn = conn;
 
	m_offset = 0;
 
	m_string = statement;
 
	m_stmt = mysql_stmt_init(conn);
 
	if (mysql_stmt_prepare(m_stmt, statement.c_str(), statement.size())) {
 
		LOG4CXX_ERROR(logger, statement << " " << mysql_error(conn));
 
		return;
 
	}
 

	
 
	for (int i = 0; i < format.length(); i++) {
 
		switch (format.at(i)) {
 
			case 's':
 
				m_params.resize(m_params.size() + 1);
 
				memset(&m_params.back(), 0, sizeof(MYSQL_BIND));
 

	
 
@@ -139,24 +141,25 @@ MySQLBackend::Statement::~Statement() {
 
	}
 
	FINALIZE_STMT(m_stmt);
 
}
 

	
 
bool MySQLBackend::Statement::execute() {
 
	// If statement has some input and doesn't have any output, we have
 
	// to clear the offset now, because operator>> will not be called.
 
	if (m_resultOffset != 0 && m_offset + 1 == m_params.size()) {
 
		m_offset = 0;
 
	}
 

	
 
	if (mysql_stmt_execute(m_stmt)) {
 
		LOG4CXX_ERROR(logger, m_string << " " << mysql_error(m_conn));
 
		return false;
 
	}
 
	return true;
 
}
 

	
 
template <typename T>
 
MySQLBackend::Statement& MySQLBackend::Statement::operator << (const T& t) {
 
	if (m_offset >= m_resultOffset)
 
		return *this;
 
	T *data = (T *) m_params[m_offset].buffer;
 
	*data = t;
 

	
0 comments (0 inline, 0 general)