4b903a9b8f97e3f35fcb7242dbc45f5ba57be3e6
1 /*$Id: opensql.c,v 1.6 1999/11/14 21:29:29 lindberg Exp $*/
2 /*$Name: ezmlm-idx-040 $*/
9 static stralloc myp
= {0};
10 static stralloc ers
= {0};
11 static stralloc fn
= {0};
12 static stralloc ourdb
= {0};
13 static char *ourtable
= (char *) 0;
15 char *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.*/
23 char *dbname
; /* database directory */
24 char **table
; /* table root_name */
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;
35 if (!stralloc_copys(&fn
,dbname
)) return ERR_NOMEM
;
36 if (fn
.len
== ourdb
.len
&& !str_diffn(ourdb
.s
,fn
.s
,fn
.len
)) {
38 if (*table
) ourtable
= *table
;
39 else *table
= ourtable
;
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 */
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
;
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; }
60 /* get connection parameters */
61 if (!stralloc_0(&myp
)) return ERR_NOMEM
;
63 if (myp
.s
[j
= str_chr(myp
.s
,':')]) {
67 if (myp
.s
[j
+= str_chr(myp
.s
+j
,':')]) {
70 if (myp
.s
[j
+= str_chr(myp
.s
+j
,':')]) {
73 if (myp
.s
[j
+= str_chr(myp
.s
+j
,':')]) {
76 if (myp
.s
[j
+= str_chr(myp
.s
+j
,':')]) {
77 ourtable
= myp
.s
+ j
++;
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;
90 if (*table
) ourtable
= *table
;
91 else *table
= ourtable
;
92 if (!*table
) return ERR_NO_TABLE
;
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
);
105 /* close connection to SQL server, if open */
107 if (psql
) mysql_close((MYSQL
*) psql
);
108 psql
= (void *) 0; /* destroy pointer */
109 ourdb
.len
= 0; /* destroy cache */