chiark / gitweb /
Import ezmlm-idx 0.40
[ezmlm] / sub_pgsql / logmsg.c
1 /*$Id: logmsg.c,v 1.3 1999/12/23 02:40:57 lindberg Exp $*/
2 /*$Name: ezmlm-idx-040 $*/
3 #include "stralloc.h"
4 #include "fmt.h"
5 #include "subscribe.h"
6 #include "errtxt.h"
7 #include <unistd.h>
8 #include <libpq-fe.h>
9
10 static stralloc logline = {0};
11 static strnum[FMT_ULONG];
12
13 char *logmsg(dir,num,listno,subs,done)
14 /* creates an entry for message num and the list listno and code "done". */
15 /* Returns NULL on success, "" if dir/sql was not found, and the error   */
16 /* string on error.   NOTE: This routine does nothing for non-sql lists! */
17 char *dir;
18 unsigned long num;
19 unsigned long listno;
20 unsigned long subs;
21 int done;
22 {
23   char *table = (char *) 0;
24   char *ret;
25
26   PGresult *result;
27   PGresult *result2;
28
29   if ((ret = opensql(dir,&table))) {
30     if (*ret)
31       return ret;
32     else
33       return (char *) 0;        /* no SQL => success */
34   }
35   if (!stralloc_copys(&logline,"INSERT INTO ")) return ERR_NOMEM;
36   if (!stralloc_cats(&logline,table)) return ERR_NOMEM;
37   if (!stralloc_cats(&logline,"_mlog (msgnum,listno,subs,done) VALUES ("))
38         return ERR_NOMEM;
39   if (!stralloc_catb(&logline,strnum,fmt_ulong(strnum,num))) return ERR_NOMEM;
40   if (!stralloc_cats(&logline,",")) return ERR_NOMEM;
41   if (!stralloc_catb(&logline,strnum,fmt_ulong(strnum,listno)))
42         return ERR_NOMEM;
43   if (!stralloc_cats(&logline,",")) return ERR_NOMEM;
44   if (!stralloc_catb(&logline,strnum,fmt_ulong(strnum,subs))) return ERR_NOMEM;
45   if (!stralloc_cats(&logline,",")) return ERR_NOMEM;
46   if (done < 0) {
47     done = - done;
48     if (!stralloc_append(&logline,"-")) return ERR_NOMEM;
49   }
50   if (!stralloc_catb(&logline,strnum,fmt_uint(strnum,done))) return ERR_NOMEM;
51   if (!stralloc_append(&logline,")")) return ERR_NOMEM;
52
53   if (!stralloc_0(&logline)) return ERR_NOMEM;
54   result = PQexec(psql,logline.s);
55   if(result==NULL)
56     return (PQerrorMessage(psql));
57   if(PQresultStatus(result) != PGRES_COMMAND_OK) { /* Check if duplicate */
58     if (!stralloc_copys(&logline,"SELECT msgnum FROM ")) return ERR_NOMEM;
59     if (!stralloc_cats(&logline,table)) return ERR_NOMEM;
60     if (!stralloc_cats(&logline,"_mlog WHERE msgnum = ")) return ERR_NOMEM;
61     if (!stralloc_catb(&logline,strnum,fmt_ulong(strnum,num)))
62       return ERR_NOMEM;
63     if (!stralloc_cats(&logline," AND listno = ")) return ERR_NOMEM;
64     if (!stralloc_catb(&logline,strnum,fmt_ulong(strnum,listno)))
65       return ERR_NOMEM;
66     if (!stralloc_cats(&logline," AND done = ")) return ERR_NOMEM;
67     if (!stralloc_catb(&logline,strnum,fmt_ulong(strnum,done)))
68       return ERR_NOMEM;
69     /* Query */
70     if (!stralloc_0(&logline)) return ERR_NOMEM;
71     result2 = PQexec(psql,logline.s);
72     if (result2 == NULL)
73       return (PQerrorMessage(psql));
74     if (PQresultStatus(result2) != PGRES_TUPLES_OK)
75       return (char *) (PQresultErrorMessage(result2));
76     /* No duplicate, return ERROR from first query */
77     if (PQntuples(result2)<1)
78       return (char *) (PQresultErrorMessage(result));
79     PQclear(result2);
80   }
81   PQclear(result);
82   return (char *) 0;
83 }