From 83a55b1e5db986a2e39e2d2ba9358300fe566063 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 17 Nov 2010 19:51:18 +0100 Subject: Use id for users and words to decide whether to insert or update. --- pg.pgc | 54 +++++++++++++++++++----------------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/pg.pgc b/pg.pgc index 2ca3dfd..18410ca 100644 --- a/pg.pgc +++ b/pg.pgc @@ -218,22 +218,18 @@ void pg_user_set(int channel, struct user_t *user) { seen_last = user->seen_last; EXEC SQL END DECLARE SECTION; - EXEC SQL UPDATE users SET - words = :words, - characters = :characters, - kicks = :kicks, - kicked = :kicked, - monolog_lines = :monolog_lines, - monologs = :monologs, - seen_first = :seen_first, - seen_last = :seen_last - WHERE id = :id AND channel_id = :channel_id; + if(id) { + EXEC SQL UPDATE users SET + words = :words, + characters = :characters, + kicks = :kicks, + kicked = :kicked, + monolog_lines = :monolog_lines, + monologs = :monologs, + seen_first = :seen_first, + seen_last = :seen_last + WHERE id = :id AND channel_id = :channel_id; - int do_insert = iserror() && testerror("02000"); - if(iserror() && !do_insert) { - printerror(); - sqlprint(); - } else { for(int ti = 0; ti < 24*4; ti++) { EXEC SQL BEGIN DECLARE SECTION; int lines = user->lines[ti], @@ -241,26 +237,22 @@ void pg_user_set(int channel, struct user_t *user) { EXEC SQL END DECLARE SECTION; EXEC SQL UPDATE users SET lines = :lines WHERE user_id = :id AND time_index = :index; } - } - - EXEC SQL COMMIT; - - if(do_insert) { + } else { EXEC SQL INSERT INTO users (channel_id, nick, words, characters, kicks, kicked, monolog_lines, monologs, seen_first, seen_last) VALUES (:channel_id, :nick, :words, :characters, :kicks, :kicked, :monolog_lines, :monologs, :seen_first, :seen_last) RETURNING id INTO :id; + for(int ti = 0; ti < 24*4; ti++) { EXEC SQL BEGIN DECLARE SECTION; int index = ti, lines = user->lines[ti]; EXEC SQL END DECLARE SECTION; EXEC SQL INSERT INTO user_hours (user_id, time_index, lines) VALUES (:id, :index, :lines); - if(iserror()) sqlprint(); } - if(iserror()) sqlprint(); - EXEC SQL COMMIT; } + + EXEC SQL COMMIT; } EXEC SQL WHENEVER SQLERROR SQLPRINT; @@ -308,21 +300,13 @@ void pg_word_set(int channel, struct word_t *word) { const char *name = word->name; EXEC SQL END DECLARE SECTION; - EXEC SQL UPDATE word SET count = :count WHERE channel_id = :channel_id AND id = :id; - - int do_insert = iserror() && testerror("02000"); - if(iserror() && !do_insert) { - printerror(); - sqlprint(); + if(id) { + EXEC SQL UPDATE word SET count = :count WHERE channel_id = :channel_id AND id = :id; + } else { + EXEC SQL INSERT INTO word (channel_id, name, count) VALUES (:channel_id, :name, :count); } EXEC SQL COMMIT; - - if(do_insert) { - EXEC SQL INSERT INTO word (channel_id, name, count) VALUES (:channel_id, :name, :count); - if(iserror()) sqlprint(); - EXEC SQL COMMIT; - } } void pg_word_get(int channel) { -- cgit v1.2.3