From 6a9af570e7288df584504c7167ba4c1f76cafced Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 16 Nov 2010 19:19:40 +0100 Subject: User lines and minor fixes. --- parsing.c | 6 ++++-- pg.h | 2 +- pg.pgc | 66 +++++++++++++++++++++++++++++++++++++++++++++++++-------------- user.c | 1 + user.h | 1 + 5 files changed, 59 insertions(+), 17 deletions(-) diff --git a/parsing.c b/parsing.c index f0e4fb5..6759104 100644 --- a/parsing.c +++ b/parsing.c @@ -9,6 +9,7 @@ #include "user.h" #include "word.h" #include "config.h" +#include "pg.h" #define NICK_BUFFER_SIZE 0x100 #define TEXT_BUFFER_SIZE 0x400 @@ -321,7 +322,7 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * } } -static void save_users(struct channel_t *channel) { +static void save_users(int channel_id) { for(int i = 0; i < USERS_MAX; i++) { struct user_t *user = &users[i]; if(!user->nick) @@ -329,7 +330,7 @@ static void save_users(struct channel_t *channel) { while(user) { if(!user->real_user) - pg_user_set(channel, user); + pg_user_set(channel_id, user); user = user->next; } } @@ -342,6 +343,7 @@ void process() { word_init(); struct channel_t *channel = channel_get(chan_i); int channel_id = pg_channel_get(channel); + pg_users_get(channel_id); printf("Channel %s\n", channel->name); struct channel_file_t *file = channel->files; while(file) { diff --git a/pg.h b/pg.h index 26fbe54..568ab6d 100644 --- a/pg.h +++ b/pg.h @@ -13,6 +13,6 @@ int pg_channel_get(struct channel_t *channel); long pg_channel_file_get(int channel_id, struct channel_file_t *file); void pg_channel_file_set(int channel_id, struct channel_file_t *file, long pos); void pg_user_set(int channel_id, struct user_t *user); -void pg_user_get(int channel_id, struct user_t *user); +void pg_users_get(int channel_id); #endif diff --git a/pg.pgc b/pg.pgc index fdf223e..2eb2a37 100644 --- a/pg.pgc +++ b/pg.pgc @@ -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: */ diff --git a/user.c b/user.c index d6171a5..faf2245 100644 --- a/user.c +++ b/user.c @@ -32,6 +32,7 @@ struct user_t *user_get(char *nick) { user->hash = hash; user->nick = strdup(nick); memset(user->lines, 0, 24*4 * sizeof(unsigned long)); + user->id = 0; user->words = user->characters = user->kicks = user->kicked = user->monolog_lines = user->monologs = 0; user->seen_first = user->seen_last = 0; user->next = NULL; diff --git a/user.h b/user.h index 469c6e7..0b43fb7 100644 --- a/user.h +++ b/user.h @@ -8,6 +8,7 @@ struct user_t { unsigned long hash; char *nick; + int id; unsigned long lines[24*4]; unsigned long long words, characters, kicks, kicked, monolog_lines, monologs; time_t seen_first, seen_last; -- cgit v1.2.3