diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-11-16 19:19:40 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-11-16 19:19:40 +0100 |
commit | 6a9af570e7288df584504c7167ba4c1f76cafced (patch) | |
tree | a33f85153d96c5d2987d8fbf5f815dc9f74fc697 /pg.pgc | |
parent | 5af90fb735d357a729d02a661c92af6e865b6d3a (diff) |
User lines and minor fixes.
Diffstat (limited to 'pg.pgc')
-rw-r--r-- | pg.pgc | 66 |
1 files changed, 52 insertions, 14 deletions
@@ -204,7 +204,8 @@ void pg_channel_file_set(int channel_id, struct channel_file_t *file, long pos) void pg_user_set(int channel, struct user_t *user) { EXEC SQL BEGIN DECLARE SECTION; const char *nick = user->nick; - int channel_id = channel, + int id = user->id, + channel_id = channel, words = user->words, characters = user->characters, kicks = user->kicks, @@ -224,12 +225,20 @@ void pg_user_set(int channel, struct user_t *user) { monologs = :monologs, seen_first = :seen_first, seen_last = :seen_last - WHERE nick = :nick AND channel_id = :channel_id; + 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], + index = ti; + EXEC SQL END DECLARE SECTION; + EXEC SQL UPDATE users SET lines = :lines WHERE user_id = :id AND time_index = :index; + } } EXEC SQL COMMIT; @@ -237,27 +246,56 @@ void pg_user_set(int channel, struct user_t *user) { if(do_insert) { 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); + 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; } } -void pg_user_get(int channel, struct user_t *user) { +EXEC SQL WHENEVER SQLERROR SQLPRINT; +void pg_users_get(int channel) { EXEC SQL BEGIN DECLARE SECTION; int channel_id = channel; - const char *stmt = "SELECT words, characters, kicks, kicked, monolog_lines, monologs, seen_first, seen_last " - "FROM users WHERE channel_id = ? AND nick = ?"; - const char *nick = user->nick; - int words, characters, kicks, kicked, monolog_lines, monologs, seen_first, seen_last; + char *nick; + int id = 0, words, characters, kicks, kicked, monolog_lines, monologs, seen_first, seen_last; EXEC SQL END DECLARE SECTION; - EXEC SQL PREPARE userget FROM :stmt; - EXEC SQL EXECUTE userget INTO :words, :characters, :kicks, :kicked, :monolog_lines, :monologs, :seen_first, :seen_last - USING :channel_id, :nick; - EXEC SQL DEALLOCATE PREPARE userget; - - if(iserror()) sqlprint(); + EXEC SQL DECLARE usercursor CURSOR FOR SELECT id, words, characters, kicks, kicked, monolog_lines, monologs, seen_first, seen_last + FROM users WHERE channel_id = :channel_id; + + EXEC SQL OPEN usercursor; + + EXEC SQL FETCH NEXT FROM usercursor INTO :id, :nick, :words, :characters, :kicks, :kicked, :monolog_lines, :monologs, :seen_first, :seen_last; + if(id == 0) + goto pg_users_get_close; + + do { + printf("fetched user %s\n", nick); + struct user_t *user = user_get(nick); + user->id = id; + user->words = words; + user->characters = characters; + user->kicks = kicks; + user->kicked = kicked; + user->monolog_lines = monolog_lines; + user->monologs = monologs; + user->seen_first = seen_first; + user->seen_last = seen_last; + EXEC SQL FETCH NEXT FROM usercursor INTO :id, :nick, :words, :characters, :kicks, :kicked, :monolog_lines, :monologs, :seen_first, :seen_last; + } while(id > 0); + +pg_users_get_close: + + EXEC SQL CLOSE usercursor; } /* vim: set syn=c: */ |