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 "scan.h"
4#include "stralloc.h"
5#include "errtxt.h"
6#include "subscribe.h"
7#include <mysql.h>
8
9#define WHO "opensql"
10
16
17static stralloc myp = {0};
18static stralloc ers = {0};
19static stralloc fn = {0};
20static stralloc ourdb = {0};
21static const char *ourtable = (char *) 0;
22
23MYSQL *mysql = 0;
24
39
40const char *opensql(const char *dbname,const char **table)
41{
42 const char *host = (char *) 0;
43 unsigned long port = 0L;
44 const char *db = (char*)"ezmlm"; /* default */
45 const char *user = (char *) 0;
46 const char *pw = (char *) 0;
47 unsigned int j;
48 char *cp;
49
50 if (!stralloc_copys(&fn,dbname)) return ERR_NOMEM;
51 if (fn.len == ourdb.len && !str_diffn(ourdb.s,fn.s,fn.len)) {
52 if (table) {
53 if (*table) ourtable = *table;
54 else *table = ourtable;
55 }
56 return 0;
57 }
58 if (!stralloc_cats(&fn,"/sql")) return ERR_NOMEM;
59 if (!stralloc_0(&fn)) return ERR_NOMEM; /* host:port:db:table:user:pw:name */
60
61 myp.len = 0;
62 switch (openreadclose(fn.s,&myp,128)) {
63 case -1: if (!stralloc_copys(&ers,ERR_READ)) return ERR_NOMEM;
64 if (!stralloc_cat(&ers,&fn)) return ERR_NOMEM;
65 if (!stralloc_0(&ers)) return ERR_NOMEM;
66 return ers.s;
67 case 0: return "";
68 }
69
70 if (!stralloc_copy(&ourdb,&fn)) return ERR_NOMEM;
71 if (!stralloc_append(&myp,"\n")) return ERR_NOMEM;
72
73 for (j=0; j < myp.len; ++j) {
74 if (myp.s[j] == '\n') { myp.s[j] = '\0'; break; }
75 } /* get connection parameters */
76 if (!stralloc_0(&myp)) return ERR_NOMEM;
77
78 host = myp.s; /* parameter parsing ;-) */
79 if (myp.s[j = str_chr(myp.s,':')]) {
80 cp = myp.s + j++;
81 *(cp++) = '\0';
82 scan_ulong(cp,&port);
83 if (myp.s[j += str_chr(myp.s+j,':')]) {
84 j++;
85 user = myp.s + j;
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 if (host && !*host) host = (char *) 0;
101 if (user && !*user) user = (char *) 0;
102 if (pw && !*pw) pw = (char *) 0;
103 if (db && !*db) db = (char *) 0;
104 if (ourtable && !*ourtable) ourtable = (char *) 0;
105
106 if (table) {
107 if (*table) ourtable = *table;
108 else *table = ourtable;
109 if (!*table) return ERR_NO_TABLE;
110 }
111
112 if (!mysql) {
113 if (!(mysql = mysql_init((MYSQL *) 0)))
114 return ERR_NOMEM; /* init */
115 if (!(mysql_real_connect(mysql, host, user, pw, db,
116 (unsigned int) port, 0, CLIENT_COMPRESS))) /* conn */
117 return mysql_error(mysql);
118 }
119
120 return (char *) 0;
121}
122
127
128void closesql(void)
129{
130 if (mysql)
131 mysql_close(mysql);
132 mysql = 0; /* destroy pointer */
133 ourdb.len = 0; /* destroy cache */
134
135 return;
136}
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
const char * cp
Definition ezmlm-cron.c:76
stralloc user
Definition ezmlm-cron.c:46
char * host
Definition ezmlm-cgi.c:107
MYSQL * mysql
Definition opensql.c:23