ezmlmx 0.68
ezmlmx
Loading...
Searching...
No Matches
opensql.c
Go to the documentation of this file.
1#include "str.h"
2#include "slurp.h"
3#include "stralloc.h"
4#include "logmsg.h"
5#include "errtxt.h"
6#include "subscribe.h"
7#include <unistd.h>
8#include <libpq-fe.h>
9
15
16static stralloc myp = {0};
17static stralloc ers = {0};
18static stralloc fn = {0};
19static stralloc ourdb = {0};
20static const char *ourtable = (char *) 0;
21
22PGconn *pgsql = 0;
23
37
38const char *opensql(const char *dbname,const char **table)
39{
40 const char *host = (char *) 0;
41 const char *port = (char *) 0;
42 const char *db = "ezmlm"; /* default */
43 const char *user = (char *) 0;
44 const char *pw = (char *) 0;
45 unsigned int j;
46
47 if (!stralloc_copys(&fn,dbname)) return ERR_NOMEM;
48 if (fn.len == ourdb.len && !str_diffn(ourdb.s,fn.s,fn.len)) {
49 if (table) {
50 if (*table) ourtable = *table;
51 else *table = ourtable;
52 }
53 return 0;
54 }
55 if (!stralloc_cats(&fn,"/sql")) return ERR_NOMEM;
56 if (!stralloc_0(&fn)) return ERR_NOMEM;
57
58 /* host:port:db:table:user:pw:name */
59
60 myp.len = 0;
61 switch (openreadlclose(fn.s,&myp,128)) {
62 case -1: if (!stralloc_copys(&ers,ERR_READ)) return ERR_NOMEM;
63 if (!stralloc_cat(&ers,&fn)) return ERR_NOMEM;
64 if (!stralloc_0(&ers)) return ERR_NOMEM;
65 return ers.s;
66 case 0: return "";
67 }
68
69 if (!stralloc_copy(&ourdb,&fn)) return ERR_NOMEM;
70 if (!stralloc_append(&myp,"\n")) return ERR_NOMEM;
71
72 for (j = 0; j< myp.len; ++j) {
73 if (myp.s[j] == '\n') { myp.s[j] = '\0'; break; }
74 }
75
76 /* get connection parameters */
77
78 if (!stralloc_0(&myp)) return ERR_NOMEM;
79 host = myp.s;
80 if (myp.s[j = str_chr(myp.s,':')]) {
81 port = myp.s + j;
82 myp.s[j++] = '\0';
83 if (myp.s[j += str_chr(myp.s+j,':')]) {
84 user = myp.s + j;
85 myp.s[j++] = '\0';
86 if (myp.s[j += str_chr(myp.s+j,':')]) {
87 pw = myp.s + j;
88 myp.s[j++] = '\0';
89 if (myp.s[j += str_chr(myp.s+j,':')]) {
90 db = myp.s + j;
91 myp.s[j++] = '\0';
92 if (myp.s[j += str_chr(myp.s+j,':')]) {
93 ourtable = myp.s + j;
94 myp.s[j++] = '\0';
95 }
96 }
97 }
98 }
99 }
100
101 if (host && !*host) host = (char *) 0;
102 if (user && !*user) user = (char *) 0;
103 if (pw && !*pw) pw = (char *) 0;
104 if (db && !*db) db = (char *) 0;
105 if (ourtable && !*ourtable) ourtable = (char *) 0;
106 if (table) {
107 if (*table) ourtable = *table;
108 else *table = ourtable;
109 if (!*table) return ERR_NO_TABLE;
110 }
111 if (!pgsql) { /* Make connection to database */
112 pgsql = PQsetdbLogin(host,port,NULL,NULL,db,user,pw);
113
114 /* Check to see that the backend connection was successfully made */
115
116 if (PQstatus(pgsql) == CONNECTION_BAD)
117 return PQerrorMessage(pgsql);
118 }
119 return (char *) 0;
120}
121
122/* close connection to SQL server, if open */
123
124void closesql(void)
125{
126 if (pgsql)
127 PQfinish(pgsql);
128 pgsql = 0; /* Destroy pointer */
129 return;
130}
Error messages. If you translate these, I would urge you to keep the English version as well....
#define ERR_NOMEM
Definition errtxt.h:14
#define ERR_NO_TABLE
Definition errtxt.h:158
#define ERR_READ
Definition errtxt.h:18
const char * opensql(const char *dbname, const char **table)
Definition opensql.c:13
void closesql(void)
close connection to SQL server, if open
Definition opensql.c:21
stralloc fn
stralloc user
Definition ezmlm-cron.c:46
char * host
Definition ezmlm-cgi.c:107
PGconn * pgsql
Definition opensql.c:22