SqlStatement Database::CreateStatement(SqlStatementID& index, const char* fmt) { int nId = -1; //check if statement ID is initialized if (!index.initialized()) { //convert to lower register std::string szFmt(fmt); //count input parameters int nParams = std::count(szFmt.begin(), szFmt.end(), '?'); //find existing or add a new record in registry LOCK_GUARD _guard(m_stmtGuard); PreparedStmtRegistry::const_iterator iter = m_stmtRegistry.find(szFmt); if (iter == m_stmtRegistry.end()) { nId = ++m_iStmtIndex; m_stmtRegistry[szFmt] = nId; } else nId = iter->second; //save initialized statement index info index.init(nId, nParams); } return SqlStatement(index, *this); }
bool Database::DirectExecuteStmt(const SqlStatementID& id, SqlStmtParameters* params) { MANGOS_ASSERT(params); std::auto_ptr<SqlStmtParameters> p(params); //execute statement SqlConnection::Lock _guard(getAsyncConnection()); return _guard->ExecuteStmt(id.ID(), *params); }
unique_ptr<SqlStatement> ConcreteDatabase::makeStatement( SqlStatementID& index, std::string sqlText ) { //initialize the statement if its not (or missing in the registry) if(!index.isInitialized()) { //count input parameters size_t nParams = std::count(sqlText.begin(), sqlText.end(), '?'); UInt32 nId = _prepStmtRegistry.getStmtId(std::move(sqlText)); //save initialized statement index info index.init(nId, nParams); } else if (!_prepStmtRegistry.idDefined(index.getId())) _prepStmtRegistry.insertStmt(index.getId(),std::move(sqlText)); return unique_ptr<SqlStatement>(new SqlStatementImpl(index, *this)); }
bool Database::ExecuteStmt(const SqlStatementID& id, SqlStmtParameters* params) { if (!m_pAsyncConn) return false; SqlTransaction* pTrans = m_TransStorage->get(); if (pTrans) { //add SQL request to trans queue pTrans->DelayExecute(new SqlPreparedRequest(id.ID(), params)); } else { //if async execution is not available if (!m_bAllowAsyncTransactions) return DirectExecuteStmt(id, params); // Simple sql statement m_threadBody->Delay(new SqlPreparedRequest(id.ID(), params)); } return true; }