Import ezmlm-idx 0.40
[ezmlm] / sub_mysql / opensql.c
CommitLineData
f8beb284
MW
1/*$Id: opensql.c,v 1.6 1999/11/14 21:29:29 lindberg Exp $*/
2/*$Name: ezmlm-idx-040 $*/
3#include "stralloc.h"
4#include "strerr.h"
5#include "errtxt.h"
6#include "subscribe.h"
7#include <mysql.h>
8
9static stralloc myp = {0};
10static stralloc ers = {0};
11static stralloc fn = {0};
12static stralloc ourdb = {0};
13static char *ourtable = (char *) 0;
14
15char *opensql(dbname,table)
16/* reads the file dbname/sql, and if the file exists, parses it into the */
17/* components. The string should be host:port:user:pw:db:table. If */
18/* the file does not exists, returns "". On success returns NULL. On error */
19/* returns error string for temporary error. If table is NULL it is */
20/* left alone. If *table is not null, it overrides the table in the sql */
21/* file. If we already opended dbname the cached info is used, rather than */
22/* rereading the file. Note that myp is static and all pointers point to it.*/
23char *dbname; /* database directory */
24char **table; /* table root_name */
25
26{
27 char *host = (char *) 0;
28 unsigned long port = 0L;
29 char *db = "ezmlm"; /* default */
30 char *user = (char *) 0;
31 char *pw = (char *) 0;
32 unsigned int j;
33 char *cp;
34
35 if (!stralloc_copys(&fn,dbname)) return ERR_NOMEM;
36 if (fn.len == ourdb.len && !str_diffn(ourdb.s,fn.s,fn.len)) {
37 if (table) {
38 if (*table) ourtable = *table;
39 else *table = ourtable;
40 }
41 return 0;
42 }
43 if (!stralloc_cats(&fn,"/sql")) return ERR_NOMEM;
44 if (!stralloc_0(&fn)) return ERR_NOMEM;
45 /* host:port:db:table:user:pw:name */
46
47 myp.len = 0;
48 switch (slurp(fn.s,&myp,128)) {
49 case -1: if (!stralloc_copys(&ers,ERR_READ)) return ERR_NOMEM;
50 if (!stralloc_cat(&ers,&fn)) return ERR_NOMEM;
51 if (!stralloc_0(&ers)) return ERR_NOMEM;
52 return ers.s;
53 case 0: return "";
54 }
55 if (!stralloc_copy(&ourdb,&fn)) return ERR_NOMEM;
56 if (!stralloc_append(&myp,"\n")) return ERR_NOMEM;
57 for (j=0; j< myp.len; ++j) {
58 if (myp.s[j] == '\n') { myp.s[j] = '\0'; break; }
59 }
60 /* get connection parameters */
61 if (!stralloc_0(&myp)) return ERR_NOMEM;
62 host = myp.s;
63 if (myp.s[j = str_chr(myp.s,':')]) {
64 cp = myp.s + j++;
65 *(cp++) = '\0';
66 scan_ulong(cp,&port);
67 if (myp.s[j += str_chr(myp.s+j,':')]) {
68 j++;
69 user = myp.s + j;
70 if (myp.s[j += str_chr(myp.s+j,':')]) {
71 pw = myp.s + j++;
72 *(pw++) = '\0';
73 if (myp.s[j += str_chr(myp.s+j,':')]) {
74 db = myp.s + j++;
75 *(db++) = '\0';
76 if (myp.s[j += str_chr(myp.s+j,':')]) {
77 ourtable = myp.s + j++;
78 *(ourtable++) = '\0';
79 }
80 }
81 }
82 }
83 }
84 if (host && !*host) host = (char *) 0;
85 if (user && !*user) user = (char *) 0;
86 if (pw && !*pw) pw = (char *) 0;
87 if (db && !*db) db = (char *) 0;
88 if (ourtable && !*ourtable) ourtable = (char *) 0;
89 if (table) {
90 if (*table) ourtable = *table;
91 else *table = ourtable;
92 if (!*table) return ERR_NO_TABLE;
93 }
94 if (!psql) {
95 if (!((MYSQL *) psql = mysql_init((MYSQL *) 0)))
96 return ERR_NOMEM; /* init */
97 if (!(mysql_real_connect((MYSQL *) psql, host, user, pw, db,
98 (unsigned int) port, 0, CLIENT_COMPRESS))) /* conn */
99 return mysql_error((MYSQL *) psql);
100 }
101 return (char *) 0;
102}
103
104void closesql()
105/* close connection to SQL server, if open */
106{
107 if (psql) mysql_close((MYSQL *) psql);
108 psql = (void *) 0; /* destroy pointer */
109 ourdb.len = 0; /* destroy cache */
110 return;
111}
112