Core/DBLayer:

- A few prepared statement implementations in authsocket as example.
- Add an ASSERT in MySQLConnection::Execute(PreparedStatement*) to catch faulty created statements

--HG--
branch : trunk
This commit is contained in:
Machiavelli
2010-09-03 01:00:49 +02:00
parent 2c1c391597
commit 7982cc0f7d
4 changed files with 26 additions and 3 deletions
+15 -2
View File
@@ -294,7 +294,13 @@ void AuthSocket::_SetVSFields(const std::string& rI)
const char *v_hex, *s_hex;
v_hex = v.AsHexStr();
s_hex = s.AsHexStr();
LoginDatabase.PExecute("UPDATE account SET v = '%s', s = '%s' WHERE username = '%s'", v_hex, s_hex, _safelogin.c_str());
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SET_VS);
stmt->setString(0, v_hex);
stmt->setString(1, s_hex);
stmt->setString(2, _safelogin);
LoginDatabase.Execute(stmt);
OPENSSL_free((void*)v_hex);
OPENSSL_free((void*)s_hex);
}
@@ -607,7 +613,14 @@ bool AuthSocket::_HandleLogonProof()
///- Update the sessionkey, last_ip, last login time and reset number of failed logins in the account table for this account
// No SQL injection (escaped user name) and IP address as received by socket
const char* K_hex = K.AsHexStr();
LoginDatabase.PExecute("UPDATE account SET sessionkey = '%s', last_ip = '%s', last_login = NOW(), locale = '%u', failed_logins = 0 WHERE username = '%s'", K_hex, socket().get_remote_address().c_str(), GetLocaleByName(_localizationName), _safelogin.c_str());
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SET_LOGONPROOF);
stmt->setString(0, K_hex);
stmt->setString(1, socket().get_remote_address().c_str());
stmt->setUInt32(2, GetLocaleByName(_localizationName));
stmt->setString(3, _safelogin);
LoginDatabase.Execute(stmt);
OPENSSL_free((void*)K_hex);
///- Finish SRP6 and send the final result to the client
@@ -20,11 +20,18 @@
bool LoginDatabaseConnection::Open(const std::string& infoString)
{
if (!MySQLConnection::Open(infoString))
return false;
m_stmts.resize(MAX_LOGINDATABASE_STATEMENTS);
/*
##################################
LOAD YOUR PREPARED STATEMENTS HERE
##################################
*/
PrepareStatement(LOGIN_SET_VS, "UPDATE account SET v = ?, s = ? WHERE username = ?");
PrepareStatement(LOGIN_SET_LOGONPROOF, "UPDATE account SET sessionkey = ?, last_ip = ?, last_login = NOW(), locale = ?, failed_logins = 0 WHERE username = ?");
return MySQLConnection::Open(infoString);
return true;
}
@@ -37,6 +37,8 @@ typedef DatabaseWorkerPool<LoginDatabaseConnection> LoginDatabaseWorkerPool;
enum LoginDatabaseStatements
{
LOGIN_SET_VS,
LOGIN_SET_LOGONPROOF,
MAX_LOGINDATABASE_STATEMENTS,
};
@@ -191,6 +191,7 @@ bool MySQLConnection::Execute(PreparedStatement* stmt)
uint32 index = stmt->m_index;
MySQLPreparedStatement* m_mStmt = GetPreparedStatement(index);
ASSERT(m_mStmt); // Can only be null if preparation failed, server side error or bad query
m_mStmt->m_stmt = stmt; // Cross reference them for debug output
stmt->m_stmt = m_mStmt;