summaryrefslogtreecommitdiff
path: root/pg.pgc
diff options
context:
space:
mode:
Diffstat (limited to 'pg.pgc')
-rw-r--r--pg.pgc67
1 files changed, 60 insertions, 7 deletions
diff --git a/pg.pgc b/pg.pgc
index 2eb2a37..2ca3dfd 100644
--- a/pg.pgc
+++ b/pg.pgc
@@ -90,9 +90,11 @@ int pg_init() {
EXEC SQL CREATE TABLE word (
id serial,
+ channel_id integer,
name varchar,
count integer,
- PRIMARY KEY (id)
+ PRIMARY KEY (id),
+ FOREIGN KEY (channel_id) REFERENCES channel (id)
);
EXEC SQL COMMIT;
@@ -265,22 +267,21 @@ EXEC SQL WHENEVER SQLERROR SQLPRINT;
void pg_users_get(int channel) {
EXEC SQL BEGIN DECLARE SECTION;
int channel_id = channel;
- char *nick;
+ VARCHAR nick[0x100];
int id = 0, words, characters, kicks, kicked, monolog_lines, monologs, seen_first, seen_last;
EXEC SQL END DECLARE SECTION;
- EXEC SQL DECLARE usercursor CURSOR FOR SELECT id, words, characters, kicks, kicked, monolog_lines, monologs, seen_first, seen_last
+ EXEC SQL DECLARE usercursor CURSOR FOR SELECT id, nick, 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)
+ if(iserror())
goto pg_users_get_close;
do {
- printf("fetched user %s\n", nick);
- struct user_t *user = user_get(nick);
+ struct user_t *user = user_get(nick.arr);
user->id = id;
user->words = words;
user->characters = characters;
@@ -291,11 +292,63 @@ void pg_users_get(int channel) {
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);
+ } while(!iserror());
pg_users_get_close:
EXEC SQL CLOSE usercursor;
}
+EXEC SQL WHENEVER SQLERROR CONTINUE;
+void pg_word_set(int channel, struct word_t *word) {
+ EXEC SQL BEGIN DECLARE SECTION;
+ int channel_id = channel,
+ id = word->id,
+ count = word->count;
+ 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();
+ }
+
+ 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) {
+ EXEC SQL BEGIN DECLARE SECTION;
+ int channel_id = channel, id = 0, count;
+ VARCHAR name[0x400];
+ EXEC SQL END DECLARE SECTION;
+
+ EXEC SQL DECLARE wordcursor CURSOR FOR SELECT id, name, count FROM word WHERE channel_id = :channel_id;
+
+ EXEC SQL OPEN wordcursor;
+
+ EXEC SQL FETCH NEXT FROM wordcursor INTO :id, :name, :count;
+ if(iserror())
+ goto pg_word_get_close;
+
+ do {
+ struct word_t *word = word_get(name.arr);
+ word->id = id;
+ word->count = count;
+ EXEC SQL FETCH NEXT FROM wordcursor INTO :id, :name, :count;
+ } while(iserror());
+
+pg_word_get_close:
+
+ EXEC SQL CLOSE wordcursor;
+}
+
/* vim: set syn=c: */