really slow database-updates

This commit is contained in:
Enno Rehling 2009-07-29 22:22:43 +00:00
parent 627b87790b
commit 98d1836d95

View file

@ -2,6 +2,7 @@
#include <kernel/eressea.h> #include <kernel/eressea.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <util/unicode.h> #include <util/unicode.h>
#include <util/base36.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <md5.h> #include <md5.h>
#include <assert.h> #include <assert.h>
@ -17,20 +18,22 @@ faction * get_faction_by_id(int uid)
return NULL; return NULL;
} }
#define SQL_EXPECT(res, val) if (res!=val) { return val; }
int int
db_update_factions(sqlite3 * db) db_update_factions(sqlite3 * db)
{ {
int game_id = 6; int game_id = 6;
// const char * sql = "INSERT OR REPLACE INTO email (md5, email) VALUES(?, ?)"; // const char * sql = "INSERT OR REPLACE INTO email (md5, email) VALUES(?, ?)";
const char * sql_select = const char sql_select[] =
"SELECT faction.id, faction.email_id, faction.no, email.email FROM email, faction" "SELECT faction.id, faction.email_id, faction.code, email.email FROM email, faction"
" WHERE email.id=faction.email_id AND faction.game_id=?"; " WHERE email.id=faction.email_id AND faction.game_id=?";
const char * sql_insert_email = const char sql_insert_email[] =
"INSERT OR IGNORE email (email,md5) VALUES (?,?)"; "INSERT OR FAIL INTO email (email,md5) VALUES (?,?)";
const char * sql_select_email = const char sql_select_email[] =
"SELECT id FROM email WHERE md5=?"; "SELECT id FROM email WHERE md5=?";
const char sql_update_faction[] = const char sql_update_faction[] =
"UPDATE faction SET email_id=?, lastturn=?, no=? WHERE id=?"; "UPDATE faction SET email_id=?, lastturn=?, code=?, name=? WHERE id=?";
faction * f; faction * f;
sqlite3_stmt *stmt_insert_email; sqlite3_stmt *stmt_insert_email;
@ -39,11 +42,11 @@ db_update_factions(sqlite3 * db)
sqlite3_stmt *stmt_select; sqlite3_stmt *stmt_select;
int res; int res;
res = sqlite3_prepare_v2(db, sql_select_email, (int)strlen(sql_select_email)+1, &stmt_select_email, NULL); res = sqlite3_prepare_v2(db, sql_select_email, (int)sizeof(sql_select_email), &stmt_select_email, NULL);
if (res!=SQLITE_OK) return res; if (res!=SQLITE_OK) return res;
res = sqlite3_prepare_v2(db, sql_insert_email, (int)strlen(sql_insert_email)+1, &stmt_insert_email, NULL); res = sqlite3_prepare_v2(db, sql_insert_email, (int)sizeof(sql_insert_email), &stmt_insert_email, NULL);
if (res!=SQLITE_OK) return res; if (res!=SQLITE_OK) return res;
res = sqlite3_prepare_v2(db, sql_select, (int)strlen(sql_select)+1, &stmt_select, NULL); res = sqlite3_prepare_v2(db, sql_select, (int)sizeof(sql_select), &stmt_select, NULL);
if (res!=SQLITE_OK) return res; if (res!=SQLITE_OK) return res;
res = sqlite3_prepare_v2(db, sql_update_faction, sizeof(sql_update_faction), &stmt_update_faction, NULL); res = sqlite3_prepare_v2(db, sql_update_faction, sizeof(sql_update_faction), &stmt_update_faction, NULL);
if (res!=SQLITE_OK) return res; if (res!=SQLITE_OK) return res;
@ -62,10 +65,12 @@ db_update_factions(sqlite3 * db)
length = sqlite3_column_bytes(stmt_select, 3); length = sqlite3_column_bytes(stmt_select, 3);
assert(length<sizeof(email)); assert(length<sizeof(email));
memcpy(email, sqlite3_column_text(stmt_select, 3), length); memcpy(email, sqlite3_column_text(stmt_select, 3), length);
email[length] = 0;
f = get_faction_by_id((int)idfaction); f = get_faction_by_id((int)idfaction);
if (f==NULL) { if (f==NULL) {
// update status? // update status?
} else { } else {
const char * code = itoa36(f->no);
if (strcmp(f->email, email)!=0) { if (strcmp(f->email, email)!=0) {
char lower[64]; char lower[64];
unicode_utf8_tolower(lower, sizeof(lower), f->email); unicode_utf8_tolower(lower, sizeof(lower), f->email);
@ -84,7 +89,7 @@ db_update_factions(sqlite3 * db)
res = sqlite3_bind_text(stmt_insert_email, 1, lower, -1, SQLITE_TRANSIENT); res = sqlite3_bind_text(stmt_insert_email, 1, lower, -1, SQLITE_TRANSIENT);
res = sqlite3_bind_text(stmt_insert_email, 2, md5hash, -1, SQLITE_TRANSIENT); res = sqlite3_bind_text(stmt_insert_email, 2, md5hash, -1, SQLITE_TRANSIENT);
res = sqlite3_step(stmt_insert_email); res = sqlite3_step(stmt_insert_email);
if (res==SQLITE_OK) { if (res==SQLITE_DONE) {
idemail = sqlite3_last_insert_rowid(db); idemail = sqlite3_last_insert_rowid(db);
} else { } else {
res = sqlite3_bind_text(stmt_select_email, 1, md5hash, -1, SQLITE_TRANSIENT); res = sqlite3_bind_text(stmt_select_email, 1, md5hash, -1, SQLITE_TRANSIENT);
@ -92,18 +97,26 @@ db_update_factions(sqlite3 * db)
if (res==SQLITE_ROW) { if (res==SQLITE_ROW) {
idemail = sqlite3_column_int64(stmt_select_email, 0); idemail = sqlite3_column_int64(stmt_select_email, 0);
} }
res = sqlite3_reset(stmt_select_email);
SQL_EXPECT(res, SQLITE_OK);
} }
// INSERT OR IGNORE email res = sqlite3_reset(stmt_insert_email);
// if (OK) idemail = lat_rowid
// else SELECT email.id
// UPDATE faction SET email_id=?, lastturn=?, no=?
} }
} }
res = sqlite3_bind_int64(stmt_update_faction, 1, idemail); res = sqlite3_bind_int64(stmt_update_faction, 1, idemail);
res = sqlite3_bind_int(stmt_update_faction, 2, turn); SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_bind_int(stmt_update_faction, 3, f->no); res = sqlite3_bind_int(stmt_update_faction, 2, f->lastorders);
res = sqlite3_bind_int64(stmt_update_faction, 4, idfaction); SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_bind_text(stmt_update_faction, 3, code, -1, SQLITE_TRANSIENT);
SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_bind_text(stmt_update_faction, 4, f->name, -1, SQLITE_TRANSIENT);
SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_bind_int64(stmt_update_faction, 5, idfaction);
SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_step(stmt_update_faction); res = sqlite3_step(stmt_update_faction);
SQL_EXPECT(res, SQLITE_DONE);
res = sqlite3_reset(stmt_update_faction);
SQL_EXPECT(res, SQLITE_OK);
} }
} }
sqlite3_finalize(stmt_select); sqlite3_finalize(stmt_select);