From d06893b0d436e5a10b00c85075d582978e4a1463 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 31 Aug 2003 01:21:31 +0000 Subject: [PATCH] *** empty log message *** svn path=/trunk/boinc/; revision=2212 --- db/boinc_db.C | 158 -------------------------------------------------- db/boinc_db.h | 32 +--------- db/db_base.C | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++ db/db_base.h | 30 ++++++++++ 4 files changed, 190 insertions(+), 188 deletions(-) create mode 100644 db/db_base.C create mode 100644 db/db_base.h diff --git a/db/boinc_db.C b/db/boinc_db.C index 58cde6be32..d95d863ac1 100644 --- a/db/boinc_db.C +++ b/db/boinc_db.C @@ -35,164 +35,6 @@ static struct random_init { } } random_init; -DB_BASE::DB_BASE(char *tn) : table_name(tn) { - cursor.active = false; -} - -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); - sprintf(query, "insert into %s set %s", table_name, vals); - return mysql_query(mysql, query); -} - -int DB_BASE::update() { - char vals[MAX_QUERY_LEN], query[MAX_QUERY_LEN]; - db_print(vals); - sprintf(query, "update %s set %s where id=%d", table_name, vals, get_id()); - return mysql_query(mysql, query); -} - -int DB_BASE::lookup(char* clause) { - char query[MAX_QUERY_LEN]; - int retval; - MYSQL_ROW row; - MYSQL_RES* rp; - - sprintf(query, "select * from %s %s", table_name, clause); - retval = mysql_query(mysql, query); - if (retval) return retval; - rp = mysql_store_result(mysql); - if (!rp) return -1; - row = mysql_fetch_row(rp); - if (row) db_parse(row); - mysql_free_result(rp); - return (row == 0); -} - -int DB_BASE::lookup_id(int id) { - char query[MAX_QUERY_LEN]; - int retval; - MYSQL_ROW row; - MYSQL_RES* rp; - - sprintf(query, "select * from %s where id=%d", table_name, id); - retval = mysql_query(mysql, query); - if (retval) return retval; - rp = mysql_store_result(mysql); - if (!rp) return -1; - row = mysql_fetch_row(rp); - if (row) db_parse(row); - mysql_free_result(rp); - return (row == 0); -} - -int DB_BASE::enumerate(char* clause) { - int x; - char query[MAX_QUERY_LEN]; - MYSQL_ROW row; - - if (!cursor.active) { - cursor.active = true; - sprintf(query, "select * from %s %s", table_name, clause); - x = mysql_query(mysql, query); - if (x) return mysql_errno(mysql); - cursor.rp = mysql_store_result(mysql); - if (!cursor.rp) return mysql_errno(mysql); - } - row = mysql_fetch_row(cursor.rp); - if (!row) { - mysql_free_result(cursor.rp); - cursor.active = false; - return 1; - } else { - db_parse(row); - } - return 0; -} - -int DB_BASE::get_integer(char* query, int& n) { - int retval; - MYSQL_ROW row; - MYSQL_RES* resp; - - retval = mysql_query(mysql, query); - if (retval) return retval; - resp = mysql_store_result(mysql); - if (!resp) return -1; - row = mysql_fetch_row(resp); - if (!row) return -1; - if (!row[0]) return -1; - 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; - - retval = mysql_query(mysql, query); - if (retval) return retval; - resp = mysql_store_result(mysql); - if (!resp) return -1; - row = mysql_fetch_row(resp); - if (!row) return -1; - if (!row[0]) return -1; - x = atof(row[0]); - mysql_free_result(resp); - return 0; -} - -int DB_BASE::count(int& n, char* clause) { - char query[MAX_QUERY_LEN]; - sprintf(query, "select count(*) from %s %s", table_name, clause); - return get_integer(query, n); -} - -int DB_BASE::sum(double& x, char* field, char* clause) { - char query[MAX_QUERY_LEN]; - sprintf(query, "select sum(%s) from %s %s", field, table_name, clause); - return get_double(query, x); -} - -static void strcpy2(char* dest, char* src) { - if (!src) *dest = 0; - else strcpy(dest, src); -} - -// convert ' to \' in place -static void escape_single_quotes(char* field) { - char buf[MAX_QUERY_LEN]; - char* q = buf, *p = field; - while (*p) { - if (*p == '\'') { - *q++ = '\\'; - *q++ = '\''; - } else { - *q++ = *p; - } - p++; - } - *q = 0; - strcpy(field, buf); -} - -static void unescape_single_quotes(char* p) { - char* q; - while (1) { - q = strstr(p, "\\'"); - if (!q) break; - strcpy(q, q+1); - } -} - int boinc_db_open(char* dbname, char* password) { mysql = mysql_init(0); if (!mysql) return -1; diff --git a/db/boinc_db.h b/db/boinc_db.h index c277fa8b46..1fcdb87d80 100755 --- a/db/boinc_db.h +++ b/db/boinc_db.h @@ -29,6 +29,8 @@ #include +#include "db_base.h" + // Maximum allowed size for SQL based blobs (Binary Large Object) // #define MAX_BLOB_SIZE 4096 @@ -393,36 +395,6 @@ extern void boinc_db_print_error(char*); extern const char* boinc_db_error_string(); extern int boinc_db_insert_id(); -#include "mysql.h" - -struct CURSOR { - bool active; - MYSQL_RES *rp; -}; - -// Base for derived classes that can access the DB -// Defines various generic operations on DB tables -// -class DB_BASE { -public: - DB_BASE(char *table_name); - int insert(); - int update(); - int lookup_id(int id); - int lookup(char*); - int enumerate(char* clause=""); - int count(int&, char* clause=""); - int sum(double&, char* field, char* clause=""); - int get_double(char* query, double&); - int get_integer(char* query, int&); - - const char *table_name; - CURSOR cursor; - virtual int get_id(); - virtual void db_print(char*); - virtual void db_parse(MYSQL_ROW&); -}; - extern MYSQL *mysql; class DB_PROJECT : public DB_BASE, public PROJECT { diff --git a/db/db_base.C b/db/db_base.C new file mode 100644 index 0000000000..7b418cfbf4 --- /dev/null +++ b/db/db_base.C @@ -0,0 +1,158 @@ +DB_BASE::DB_BASE(char *tn) : table_name(tn) { + cursor.active = false; +} + +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); + sprintf(query, "insert into %s set %s", table_name, vals); + return mysql_query(mysql, query); +} + +int DB_BASE::update() { + char vals[MAX_QUERY_LEN], query[MAX_QUERY_LEN]; + db_print(vals); + sprintf(query, "update %s set %s where id=%d", table_name, vals, get_id()); + return mysql_query(mysql, query); +} + +int DB_BASE::lookup(char* clause) { + char query[MAX_QUERY_LEN]; + int retval; + MYSQL_ROW row; + MYSQL_RES* rp; + + sprintf(query, "select * from %s %s", table_name, clause); + retval = mysql_query(mysql, query); + if (retval) return retval; + rp = mysql_store_result(mysql); + if (!rp) return -1; + row = mysql_fetch_row(rp); + if (row) db_parse(row); + mysql_free_result(rp); + return (row == 0); +} + +int DB_BASE::lookup_id(int id) { + char query[MAX_QUERY_LEN]; + int retval; + MYSQL_ROW row; + MYSQL_RES* rp; + + sprintf(query, "select * from %s where id=%d", table_name, id); + retval = mysql_query(mysql, query); + if (retval) return retval; + rp = mysql_store_result(mysql); + if (!rp) return -1; + row = mysql_fetch_row(rp); + if (row) db_parse(row); + mysql_free_result(rp); + return (row == 0); +} + +int DB_BASE::enumerate(char* clause) { + int x; + char query[MAX_QUERY_LEN]; + MYSQL_ROW row; + + if (!cursor.active) { + cursor.active = true; + sprintf(query, "select * from %s %s", table_name, clause); + x = mysql_query(mysql, query); + if (x) return mysql_errno(mysql); + cursor.rp = mysql_store_result(mysql); + if (!cursor.rp) return mysql_errno(mysql); + } + row = mysql_fetch_row(cursor.rp); + if (!row) { + mysql_free_result(cursor.rp); + cursor.active = false; + return 1; + } else { + db_parse(row); + } + return 0; +} + +int DB_BASE::get_integer(char* query, int& n) { + int retval; + MYSQL_ROW row; + MYSQL_RES* resp; + + retval = mysql_query(mysql, query); + if (retval) return retval; + resp = mysql_store_result(mysql); + if (!resp) return -1; + row = mysql_fetch_row(resp); + if (!row) return -1; + if (!row[0]) return -1; + 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; + + retval = mysql_query(mysql, query); + if (retval) return retval; + resp = mysql_store_result(mysql); + if (!resp) return -1; + row = mysql_fetch_row(resp); + if (!row) return -1; + if (!row[0]) return -1; + x = atof(row[0]); + mysql_free_result(resp); + return 0; +} + +int DB_BASE::count(int& n, char* clause) { + char query[MAX_QUERY_LEN]; + sprintf(query, "select count(*) from %s %s", table_name, clause); + return get_integer(query, n); +} + +int DB_BASE::sum(double& x, char* field, char* clause) { + char query[MAX_QUERY_LEN]; + sprintf(query, "select sum(%s) from %s %s", field, table_name, clause); + return get_double(query, x); +} + +static void strcpy2(char* dest, char* src) { + if (!src) *dest = 0; + else strcpy(dest, src); +} + +// convert ' to \' in place +static void escape_single_quotes(char* field) { + char buf[MAX_QUERY_LEN]; + char* q = buf, *p = field; + while (*p) { + if (*p == '\'') { + *q++ = '\\'; + *q++ = '\''; + } else { + *q++ = *p; + } + p++; + } + *q = 0; + strcpy(field, buf); +} + +static void unescape_single_quotes(char* p) { + char* q; + while (1) { + q = strstr(p, "\\'"); + if (!q) break; + strcpy(q, q+1); + } +} + diff --git a/db/db_base.h b/db/db_base.h new file mode 100644 index 0000000000..77ae3041b9 --- /dev/null +++ b/db/db_base.h @@ -0,0 +1,30 @@ +#include "mysql.h" + +struct CURSOR { + bool active; + MYSQL_RES *rp; +}; + +// Base for derived classes that can access the DB +// Defines various generic operations on DB tables +// +class DB_BASE { +public: + DB_BASE(char *table_name); + int insert(); + int update(); + int lookup_id(int id); + int lookup(char*); + int enumerate(char* clause=""); + int count(int&, char* clause=""); + int sum(double&, char* field, char* clause=""); + int get_double(char* query, double&); + int get_integer(char* query, int&); + + const char *table_name; + CURSOR cursor; + virtual int get_id(); + virtual void db_print(char*); + virtual void db_parse(MYSQL_ROW&); +}; +