2004-11-20 07:32:32 +00:00
|
|
|
static volatile const char *BOINCrcsid="$Id$";
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
2003-08-31 05:33:59 +00:00
|
|
|
|
2003-10-17 17:22:49 +00:00
|
|
|
#include "error_numbers.h"
|
2004-04-09 23:33:50 +00:00
|
|
|
#include "util.h"
|
2003-08-31 05:33:59 +00:00
|
|
|
#include "db_base.h"
|
|
|
|
|
2004-04-30 23:18:56 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
#include "fcgi_stdio.h"
|
|
|
|
#endif
|
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
DB_CONN::DB_CONN() {
|
|
|
|
mysql = 0;
|
|
|
|
}
|
|
|
|
|
2004-01-15 23:53:13 +00:00
|
|
|
int DB_CONN::open(char* db_name, char* db_host, char* db_user, char* dbpassword) {
|
2003-09-05 21:26:21 +00:00
|
|
|
mysql = mysql_init(0);
|
2003-12-18 00:22:25 +00:00
|
|
|
if (!mysql) return ERR_DB_CANT_INIT;
|
2004-01-15 23:53:13 +00:00
|
|
|
mysql = mysql_real_connect(mysql, db_host, db_user, dbpassword, db_name, 0, 0, 0);
|
2003-10-17 17:22:49 +00:00
|
|
|
if (mysql == 0) return ERR_DB_CANT_CONNECT;
|
|
|
|
return 0;
|
2003-09-05 21:26:21 +00:00
|
|
|
}
|
2003-08-31 05:33:59 +00:00
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
void DB_CONN::close() {
|
|
|
|
if (mysql) mysql_close(mysql);
|
|
|
|
}
|
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
int DB_CONN::do_query(char* p) {
|
2004-01-19 01:12:53 +00:00
|
|
|
int retval;
|
|
|
|
retval = mysql_query(mysql, p);
|
|
|
|
if (retval) {
|
2004-07-15 18:54:17 +00:00
|
|
|
fprintf(stderr, "Database error: %s\nquery=%s\n", error_string(), p);
|
2004-01-19 01:12:53 +00:00
|
|
|
}
|
|
|
|
return retval;
|
2004-01-14 20:24:24 +00:00
|
|
|
}
|
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
int DB_CONN::insert_id() {
|
|
|
|
int retval;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_RES* rp;
|
2004-06-29 06:21:05 +00:00
|
|
|
|
2004-06-30 18:25:14 +00:00
|
|
|
if (mysql) {
|
2004-07-06 18:30:22 +00:00
|
|
|
retval = do_query("select HIGH_PRIORITY LAST_INSERT_ID()");
|
2004-06-29 06:21:05 +00:00
|
|
|
} else {
|
2004-07-06 18:30:22 +00:00
|
|
|
retval = do_query("select LAST_INSERT_ID()");
|
2004-06-29 06:21:05 +00:00
|
|
|
}
|
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
rp = mysql_store_result(mysql);
|
|
|
|
row = mysql_fetch_row(rp);
|
|
|
|
return atoi(row[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DB_CONN::print_error(char* p) {
|
2004-07-15 18:54:17 +00:00
|
|
|
fprintf(stderr, "%s: Database error: %s\n", p, error_string());
|
2003-09-05 21:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* DB_CONN::error_string() {
|
|
|
|
return mysql?mysql_error(mysql):"Not connected";
|
|
|
|
}
|
|
|
|
|
2004-07-21 21:50:25 +00:00
|
|
|
int DB_CONN::start_transaction() {
|
|
|
|
return do_query("START TRANSACTION");
|
|
|
|
}
|
|
|
|
|
|
|
|
int DB_CONN::commit_transaction() {
|
|
|
|
return do_query("COMMIT");
|
|
|
|
}
|
|
|
|
|
2004-10-04 23:23:57 +00:00
|
|
|
DB_BASE::DB_BASE(char *tn, DB_CONN* p) : db(p), table_name(tn) {
|
2004-06-30 18:25:14 +00:00
|
|
|
is_high_priority = false;
|
2003-08-31 01:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DB_BASE::get_id() { return 0;}
|
|
|
|
void DB_BASE::db_print(char*) {}
|
|
|
|
|
|
|
|
void DB_BASE::db_parse(MYSQL_ROW&) {}
|
|
|
|
|
|
|
|
int DB_BASE::insert() {
|
|
|
|
char vals[MAX_QUERY_LEN], query[MAX_QUERY_LEN];
|
|
|
|
db_print(vals);
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "insert into %s set %s", table_name, vals);
|
2004-01-14 20:24:24 +00:00
|
|
|
return db->do_query(query);
|
2003-08-31 01:21:31 +00:00
|
|
|
}
|
|
|
|
|
2004-07-21 21:50:25 +00:00
|
|
|
int DB_BASE::insert_batch(const char* values) {
|
|
|
|
char query[MAX_QUERY_LEN];
|
|
|
|
sprintf(query, "insert into %s values %s", table_name, values);
|
|
|
|
return db->do_query(query);
|
|
|
|
}
|
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
// update an entire record
|
|
|
|
//
|
2003-08-31 01:21:31 +00:00
|
|
|
int DB_BASE::update() {
|
|
|
|
char vals[MAX_QUERY_LEN], query[MAX_QUERY_LEN];
|
|
|
|
db_print(vals);
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "update %s set %s where id=%d", table_name, vals, get_id());
|
2004-01-14 20:24:24 +00:00
|
|
|
return db->do_query(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update one or more fields
|
|
|
|
// "clause" is something like "foo=5, blah='xxx'" or "foo=foo+5"
|
|
|
|
//
|
|
|
|
int DB_BASE::update_field(char* clause) {
|
|
|
|
char query[MAX_QUERY_LEN];
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "update %s set %s where id=%d", table_name, clause, get_id());
|
2004-01-14 20:24:24 +00:00
|
|
|
return db->do_query(query);
|
2003-08-31 01:21:31 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 00:49:38 +00:00
|
|
|
// delete record
|
|
|
|
//
|
|
|
|
int DB_BASE::delete_from_db() {
|
|
|
|
char vals[MAX_QUERY_LEN], query[MAX_QUERY_LEN];
|
|
|
|
db_print(vals);
|
|
|
|
sprintf(query, "delete from %s where id=%d", table_name, get_id());
|
|
|
|
return db->do_query(query);
|
|
|
|
}
|
|
|
|
|
2004-08-21 00:40:01 +00:00
|
|
|
int DB_BASE::get_field_int(char* field, int& val) {
|
|
|
|
char query[MAX_QUERY_LEN];
|
|
|
|
int retval;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_RES* rp;
|
|
|
|
|
|
|
|
sprintf(query, "select %s from %s where id=%d", field, table_name, get_id());
|
|
|
|
retval = db->do_query(query);
|
|
|
|
if (retval) return retval;
|
|
|
|
rp = mysql_store_result(db->mysql);
|
|
|
|
if (!rp) return -1;
|
|
|
|
row = mysql_fetch_row(rp);
|
|
|
|
if (row) val = atoi(row[0]);
|
|
|
|
mysql_free_result(rp);
|
|
|
|
if (row == 0) return ERR_DB_NOT_FOUND;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-08-31 01:21:31 +00:00
|
|
|
int DB_BASE::lookup(char* clause) {
|
|
|
|
char query[MAX_QUERY_LEN];
|
|
|
|
int retval;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_RES* rp;
|
|
|
|
|
2004-08-21 00:40:01 +00:00
|
|
|
if (is_high_priority) {
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "select HIGH_PRIORITY * from %s %s", table_name, clause);
|
2004-06-29 06:21:05 +00:00
|
|
|
} else {
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "select * from %s %s", table_name, clause);
|
2004-06-29 06:21:05 +00:00
|
|
|
}
|
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
retval = db->do_query(query);
|
2003-08-31 01:21:31 +00:00
|
|
|
if (retval) return retval;
|
2003-09-05 21:26:21 +00:00
|
|
|
rp = mysql_store_result(db->mysql);
|
2003-08-31 01:21:31 +00:00
|
|
|
if (!rp) return -1;
|
|
|
|
row = mysql_fetch_row(rp);
|
|
|
|
if (row) db_parse(row);
|
|
|
|
mysql_free_result(rp);
|
2003-10-17 17:22:49 +00:00
|
|
|
if (row == 0) return ERR_DB_NOT_FOUND;
|
|
|
|
return 0;
|
2003-08-31 01:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DB_BASE::lookup_id(int id) {
|
|
|
|
char query[MAX_QUERY_LEN];
|
|
|
|
int retval;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_RES* rp;
|
|
|
|
|
2004-08-21 00:40:01 +00:00
|
|
|
if (is_high_priority) {
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "select HIGH_PRIORITY * from %s where id=%d", table_name, id);
|
2004-06-29 06:21:05 +00:00
|
|
|
} else {
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "select * from %s where id=%d", table_name, id);
|
2004-06-29 06:21:05 +00:00
|
|
|
}
|
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
retval = db->do_query(query);
|
2003-08-31 01:21:31 +00:00
|
|
|
if (retval) return retval;
|
2003-09-05 21:26:21 +00:00
|
|
|
rp = mysql_store_result(db->mysql);
|
2003-08-31 01:21:31 +00:00
|
|
|
if (!rp) return -1;
|
|
|
|
row = mysql_fetch_row(rp);
|
|
|
|
if (row) db_parse(row);
|
|
|
|
mysql_free_result(rp);
|
2003-10-17 17:22:49 +00:00
|
|
|
if (row == 0) return ERR_DB_NOT_FOUND;
|
|
|
|
|
|
|
|
// don't bother checking for uniqueness here
|
|
|
|
return 0;
|
2003-08-31 01:21:31 +00:00
|
|
|
}
|
|
|
|
|
2004-09-24 20:48:07 +00:00
|
|
|
int DB_BASE::enumerate(char* clause, bool use_use_result) {
|
2003-08-31 01:21:31 +00:00
|
|
|
int x;
|
|
|
|
char query[MAX_QUERY_LEN];
|
|
|
|
MYSQL_ROW row;
|
|
|
|
|
|
|
|
if (!cursor.active) {
|
|
|
|
cursor.active = true;
|
2004-06-29 06:21:05 +00:00
|
|
|
|
2004-08-21 00:40:01 +00:00
|
|
|
if (is_high_priority) {
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "select HIGH_PRIORITY * from %s %s", table_name, clause);
|
2004-06-29 06:21:05 +00:00
|
|
|
} else {
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "select * from %s %s", table_name, clause);
|
2004-06-29 06:21:05 +00:00
|
|
|
}
|
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
x = db->do_query(query);
|
2003-09-05 21:26:21 +00:00
|
|
|
if (x) return mysql_errno(db->mysql);
|
2004-07-15 18:54:17 +00:00
|
|
|
|
2004-09-24 20:48:07 +00:00
|
|
|
// if you use mysql_use_result() here,
|
2004-07-15 18:54:17 +00:00
|
|
|
// any other transactions will fail
|
|
|
|
//
|
2004-09-24 20:48:07 +00:00
|
|
|
if (use_use_result) {
|
|
|
|
cursor.rp = mysql_use_result(db->mysql);
|
|
|
|
} else {
|
|
|
|
cursor.rp = mysql_store_result(db->mysql);
|
|
|
|
}
|
2003-09-05 21:26:21 +00:00
|
|
|
if (!cursor.rp) return mysql_errno(db->mysql);
|
2003-08-31 01:21:31 +00:00
|
|
|
}
|
|
|
|
row = mysql_fetch_row(cursor.rp);
|
|
|
|
if (!row) {
|
|
|
|
mysql_free_result(cursor.rp);
|
|
|
|
cursor.active = false;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
db_parse(row);
|
2004-05-18 18:33:01 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// call this to end an enumeration before reaching end
|
|
|
|
//
|
|
|
|
int DB_BASE::end_enumerate() {
|
|
|
|
if (cursor.active) {
|
|
|
|
mysql_free_result(cursor.rp);
|
|
|
|
cursor.active = false;
|
2003-08-31 01:21:31 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DB_BASE::get_integer(char* query, int& n) {
|
|
|
|
int retval;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_RES* resp;
|
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
retval = db->do_query(query);
|
2003-08-31 01:21:31 +00:00
|
|
|
if (retval) return retval;
|
2003-09-05 21:26:21 +00:00
|
|
|
resp = mysql_store_result(db->mysql);
|
2003-10-17 17:22:49 +00:00
|
|
|
if (!resp) return ERR_DB_NOT_FOUND;
|
2003-08-31 01:21:31 +00:00
|
|
|
row = mysql_fetch_row(resp);
|
2003-10-17 17:22:49 +00:00
|
|
|
if (!row) return ERR_DB_NOT_FOUND;
|
|
|
|
if (!row[0]) return ERR_DB_NOT_FOUND;
|
2003-08-31 01:21:31 +00:00
|
|
|
n = atoi(row[0]);
|
|
|
|
mysql_free_result(resp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DB_BASE::get_double(char* query, double& x) {
|
|
|
|
int retval;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_RES* resp;
|
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
retval = db->do_query(query);
|
2003-08-31 01:21:31 +00:00
|
|
|
if (retval) return retval;
|
2003-09-05 21:26:21 +00:00
|
|
|
resp = mysql_store_result(db->mysql);
|
2003-10-17 17:22:49 +00:00
|
|
|
if (!resp) return ERR_DB_NOT_FOUND;
|
2003-08-31 01:21:31 +00:00
|
|
|
row = mysql_fetch_row(resp);
|
2003-10-17 17:22:49 +00:00
|
|
|
if (!row) return ERR_DB_NOT_FOUND;
|
|
|
|
if (!row[0]) return ERR_DB_NOT_FOUND;
|
2003-08-31 01:21:31 +00:00
|
|
|
x = atof(row[0]);
|
|
|
|
mysql_free_result(resp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DB_BASE::count(int& n, char* clause) {
|
|
|
|
char query[MAX_QUERY_LEN];
|
2004-06-29 06:21:05 +00:00
|
|
|
|
2004-08-21 00:40:01 +00:00
|
|
|
if (is_high_priority) {
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "select HIGH_PRIORITY count(*) from %s %s", table_name, clause);
|
2004-06-29 06:21:05 +00:00
|
|
|
} else {
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "select count(*) from %s %s", table_name, clause);
|
2004-06-29 06:21:05 +00:00
|
|
|
}
|
|
|
|
|
2003-08-31 01:21:31 +00:00
|
|
|
return get_integer(query, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DB_BASE::sum(double& x, char* field, char* clause) {
|
|
|
|
char query[MAX_QUERY_LEN];
|
2004-06-29 06:21:05 +00:00
|
|
|
|
2004-08-21 00:40:01 +00:00
|
|
|
if (is_high_priority) {
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "select HIGH_PRIORITY sum(%s) from %s %s", field, table_name, clause);
|
2004-06-29 06:21:05 +00:00
|
|
|
} else {
|
2004-07-06 18:30:22 +00:00
|
|
|
sprintf(query, "select sum(%s) from %s %s", field, table_name, clause);
|
2004-06-29 06:21:05 +00:00
|
|
|
}
|
|
|
|
|
2003-08-31 01:21:31 +00:00
|
|
|
return get_double(query, x);
|
|
|
|
}
|
|
|
|
|
2004-07-01 18:43:36 +00:00
|
|
|
|
2004-10-04 23:23:57 +00:00
|
|
|
DB_BASE_SPECIAL::DB_BASE_SPECIAL(DB_CONN* p) : db(p) {
|
2004-07-01 18:43:36 +00:00
|
|
|
}
|
|
|
|
|
2003-12-26 06:03:03 +00:00
|
|
|
// convert a string into a form that allows it to be used
|
2004-04-09 23:33:50 +00:00
|
|
|
// in SQL queries delimited by single quotes:
|
2004-07-21 21:50:25 +00:00
|
|
|
// replace ' with \', '\' with '\\'
|
2003-12-26 06:03:03 +00:00
|
|
|
//
|
2004-04-09 23:33:50 +00:00
|
|
|
void escape_string(char* field, int len) {
|
2003-08-31 01:21:31 +00:00
|
|
|
char buf[MAX_QUERY_LEN];
|
|
|
|
char* q = buf, *p = field;
|
|
|
|
while (*p) {
|
|
|
|
if (*p == '\'') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = '\'';
|
2003-12-26 06:03:03 +00:00
|
|
|
} else if (*p == '\\') {
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = '\\';
|
2003-08-31 01:21:31 +00:00
|
|
|
} else {
|
|
|
|
*q++ = *p;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*q = 0;
|
2004-04-09 23:33:50 +00:00
|
|
|
safe_strncpy(field, buf, len);
|
2003-08-31 01:21:31 +00:00
|
|
|
}
|
|
|
|
|
2004-04-09 23:33:50 +00:00
|
|
|
// undo the above process
|
|
|
|
// (len not used because this doesn't expand the string)
|
|
|
|
//
|
|
|
|
void unescape_string(char* p, int /*len*/) {
|
2003-12-26 06:03:03 +00:00
|
|
|
while (*p) {
|
|
|
|
if (*p == '\\') {
|
|
|
|
strcpy(p, p+1);
|
|
|
|
}
|
|
|
|
p++;
|
2003-08-31 01:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|