2002-04-30 22:22:54 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
// A collection of functions to facilitate MySQL access.
|
|
|
|
// Should be independent of the actual database
|
|
|
|
// (other than the assumption that ID is always field zero)
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "mysql.h"
|
|
|
|
#include "mysql_util.h"
|
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
#define MAX_QUERY_LEN 8192
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_open(char* dbname, char* password) {
|
2002-04-30 22:22:54 +00:00
|
|
|
mp = mysql_init(0);
|
|
|
|
if (!mp) return -1;
|
2002-08-14 05:43:05 +00:00
|
|
|
mp = mysql_real_connect(mp, 0, 0, password, dbname, 0, 0, 0);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!mp) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_close() {
|
2002-04-30 22:22:54 +00:00
|
|
|
mysql_close(mp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-02 22:39:34 +00:00
|
|
|
const char* MYSQL_DB::db_error_string() {
|
|
|
|
return mp?mysql_error(mp):"Not connected";
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
void MYSQL_DB::db_print_error(char* p) {
|
2002-04-30 22:22:54 +00:00
|
|
|
if (mp) {
|
2002-10-03 18:33:46 +00:00
|
|
|
fprintf(stderr, "%s: Database error: %s\n", p, mysql_error(mp));
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert ' to \' in place
|
2003-03-02 22:39:34 +00:00
|
|
|
void escape_single_quotes(char* field) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[MAX_QUERY_LEN];
|
|
|
|
char* q = buf, *p = field;
|
|
|
|
while (*p) {
|
|
|
|
if (*p == '\'') {
|
2003-01-31 07:19:20 +00:00
|
|
|
*q++ = '\\';
|
2002-04-30 22:22:54 +00:00
|
|
|
*q++ = '\'';
|
|
|
|
} else {
|
|
|
|
*q++ = *p;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*q = 0;
|
|
|
|
strcpy(field, buf);
|
|
|
|
}
|
|
|
|
|
2003-03-02 22:39:34 +00:00
|
|
|
void unescape_single_quotes(char* p) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char* q;
|
|
|
|
while (1) {
|
|
|
|
q = strstr(p, "\\'");
|
|
|
|
if (!q) break;
|
|
|
|
strcpy(q, q+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assumes ID is first
|
|
|
|
static int* id(void* vp, int type) {
|
|
|
|
return (int*) vp;
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_new(void* vp, int type) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[MAX_QUERY_LEN], sbuf[MAX_QUERY_LEN];
|
|
|
|
struct_to_str(vp, sbuf, type);
|
|
|
|
sprintf(buf, "insert into %s set %s", table_name[type], sbuf);
|
|
|
|
return mysql_query(mp, buf);
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_insert_id() {
|
2003-03-08 23:51:10 +00:00
|
|
|
int retval;
|
|
|
|
retval = mysql_query(mp, "select LAST_INSERT_ID()");
|
|
|
|
if (retval) return mysql_errno(mp);
|
2002-04-30 22:22:54 +00:00
|
|
|
rp = mysql_store_result(mp);
|
|
|
|
row = mysql_fetch_row(rp);
|
|
|
|
return atoi(row[0]);
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_delete(int id, int type) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[MAX_QUERY_LEN];
|
|
|
|
|
|
|
|
sprintf(buf, "delete from %s where id=%d", table_name[type], id);
|
|
|
|
return mysql_query(mp, buf);
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_lookup_id(int i, void* vp, int type) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[MAX_QUERY_LEN];
|
2003-03-08 23:51:10 +00:00
|
|
|
int retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
sprintf(buf, "select * from %s where id=%d", table_name[type], i);
|
2003-03-08 23:51:10 +00:00
|
|
|
retval = mysql_query(mp, buf);
|
|
|
|
if (retval) return mysql_errno(mp);
|
2002-04-30 22:22:54 +00:00
|
|
|
rp = mysql_store_result(mp);
|
2003-03-08 23:51:10 +00:00
|
|
|
if (!rp) return mysql_errno(mp);
|
2002-04-30 22:22:54 +00:00
|
|
|
row = mysql_fetch_row(rp);
|
|
|
|
if (row) row_to_struct(row, vp, type);
|
|
|
|
mysql_free_result(rp);
|
|
|
|
return (row == 0);
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_lookup(void* vp, int type, char* clause) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[MAX_QUERY_LEN];
|
2003-03-08 23:51:10 +00:00
|
|
|
int retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
sprintf(buf, "select * from %s where %s", table_name[type], clause);
|
2003-03-08 23:51:10 +00:00
|
|
|
retval = mysql_query(mp, buf);
|
|
|
|
if (retval) return mysql_errno(mp);
|
2002-04-30 22:22:54 +00:00
|
|
|
rp = mysql_store_result(mp);
|
2003-03-08 23:51:10 +00:00
|
|
|
if (!rp) return mysql_errno(mp);
|
2002-04-30 22:22:54 +00:00
|
|
|
row = mysql_fetch_row(rp);
|
|
|
|
if (row) row_to_struct(row, vp, type);
|
|
|
|
mysql_free_result(rp);
|
|
|
|
return (row == 0);
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_update(void* vp, int type) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[MAX_QUERY_LEN], sbuf[MAX_QUERY_LEN];
|
|
|
|
struct_to_str(vp, sbuf, type);
|
|
|
|
sprintf(
|
2003-01-09 21:48:33 +00:00
|
|
|
buf,
|
|
|
|
"update %s set %s where id=%d",
|
|
|
|
table_name[type], sbuf, *id(vp, type)
|
2002-04-30 22:22:54 +00:00
|
|
|
);
|
|
|
|
return mysql_query(mp, buf);
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_enum(ENUM& e, void* p, int type, char* clause, int limit) {
|
2002-07-11 01:09:53 +00:00
|
|
|
char buf[MAX_QUERY_LEN], buf2[256];
|
2003-03-08 23:51:10 +00:00
|
|
|
int retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!e.active) {
|
2003-01-09 21:48:33 +00:00
|
|
|
e.active = 1;
|
|
|
|
sprintf(buf, "select * from %s %s", table_name[type], clause?clause:"");
|
2002-04-30 22:22:54 +00:00
|
|
|
if (limit) {
|
|
|
|
sprintf(buf2, " limit %d", limit);
|
|
|
|
strcat(buf, buf2);
|
|
|
|
}
|
2003-03-08 23:51:10 +00:00
|
|
|
retval = mysql_query(mp, buf);
|
|
|
|
if (retval) return mysql_errno(mp);
|
2003-01-09 21:48:33 +00:00
|
|
|
e.rp = mysql_store_result(mp);
|
2003-03-08 23:51:10 +00:00
|
|
|
if (!e.rp) return mysql_errno(mp);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
row = mysql_fetch_row(e.rp);
|
|
|
|
if (!row) {
|
2003-01-09 21:48:33 +00:00
|
|
|
mysql_free_result(e.rp);
|
|
|
|
e.active = 0;
|
|
|
|
return 1;
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
2003-01-09 21:48:33 +00:00
|
|
|
row_to_struct(row, p, type);
|
|
|
|
return 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_enum_field(ENUM& e, int type, char* field, char* clause) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[MAX_QUERY_LEN];
|
2003-03-08 23:51:10 +00:00
|
|
|
int retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!e.active) {
|
2003-01-09 21:48:33 +00:00
|
|
|
e.active = 1;
|
|
|
|
sprintf(buf, "select %s from %s %s", field, table_name[type], clause);
|
2003-03-08 23:51:10 +00:00
|
|
|
retval = mysql_query(mp, buf);
|
|
|
|
if (retval) return mysql_errno(mp);
|
2003-01-09 21:48:33 +00:00
|
|
|
e.rp = mysql_store_result(mp);
|
2003-03-08 23:51:10 +00:00
|
|
|
if (!e.rp) return mysql_errno(mp);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
row = mysql_fetch_row(e.rp);
|
|
|
|
if (!row) {
|
2003-01-09 21:48:33 +00:00
|
|
|
mysql_free_result(e.rp);
|
|
|
|
e.active = 0;
|
|
|
|
return 1;
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
2003-01-09 21:48:33 +00:00
|
|
|
return 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_query_int(int* ip, char* query) {
|
2003-03-08 23:51:10 +00:00
|
|
|
int retval;
|
|
|
|
retval = mysql_query(mp, query);
|
|
|
|
if (retval) return mysql_errno(mp);
|
2002-04-30 22:22:54 +00:00
|
|
|
rp = mysql_store_result(mp);
|
2003-03-08 23:51:10 +00:00
|
|
|
if (!rp) return mysql_errno(mp);
|
2002-04-30 22:22:54 +00:00
|
|
|
row = mysql_fetch_row(rp);
|
2003-03-08 23:51:10 +00:00
|
|
|
if (!row || !row[0]) return mysql_errno(mp);
|
2002-04-30 22:22:54 +00:00
|
|
|
*ip = atoi(row[0]);
|
|
|
|
mysql_free_result(rp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
int MYSQL_DB::db_query_double(double* dp, char* query) {
|
2003-03-08 23:51:10 +00:00
|
|
|
int retval;
|
|
|
|
retval = mysql_query(mp, query);
|
|
|
|
if (retval) return mysql_errno(mp);
|
2003-03-08 00:09:40 +00:00
|
|
|
rp = mysql_store_result(mp);
|
2003-03-08 23:51:10 +00:00
|
|
|
if (!rp) return mysql_errno(mp);
|
2003-03-08 00:09:40 +00:00
|
|
|
row = mysql_fetch_row(rp);
|
2003-03-08 23:51:10 +00:00
|
|
|
if (!row || !row[0]) return mysql_errno(mp);
|
2003-03-08 00:09:40 +00:00
|
|
|
*dp = atof(row[0]);
|
|
|
|
mysql_free_result(rp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_count(int* np, char* what, int type, char* clause) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[MAX_QUERY_LEN];
|
2003-01-30 23:03:52 +00:00
|
|
|
sprintf(buf, "select count(%s) from %s ", what, table_name[type]);
|
|
|
|
if (clause) strcat(buf, clause);
|
2002-04-30 22:22:54 +00:00
|
|
|
return db_query_int(np, buf);
|
|
|
|
}
|
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
int MYSQL_DB::db_sum(double* np, char* what, int type, char* clause) {
|
|
|
|
char buf[MAX_QUERY_LEN];
|
|
|
|
sprintf(buf, "select sum(%s) from %s ", what, table_name[type]);
|
|
|
|
if (clause) strcat(buf, clause);
|
|
|
|
return db_query_double(np, buf);
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
int MYSQL_DB::db_query(char* p) {
|
2002-10-16 01:21:00 +00:00
|
|
|
return mysql_query(mp, p);
|
|
|
|
}
|