diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-11-17 19:51:18 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-11-17 19:51:18 +0100 |
commit | 83a55b1e5db986a2e39e2d2ba9358300fe566063 (patch) | |
tree | a905dab25ffb903cdf243a54f7dce298455933ff | |
parent | 517600329eaaa144d3c26b02bab75cd30e49dac9 (diff) |
Use id for users and words to decide whether to insert or update.
-rw-r--r-- | pg.pgc | 54 |
1 files changed, 19 insertions, 35 deletions
@@ -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) { |