2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-01-15 23:53:13 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2004-01-15 23:53:13 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2003-08-31 05:33:59 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2003-08-31 05:33:59 +00:00
|
|
|
|
|
|
|
#ifndef _DB_BASE_
|
|
|
|
#define _DB_BASE_
|
|
|
|
|
2005-09-29 12:03:23 +00:00
|
|
|
#include <mysql.h>
|
2003-08-31 01:21:31 +00:00
|
|
|
|
2004-07-01 18:43:36 +00:00
|
|
|
// if SQL columns are not 'not null', you must use these safe_atoi, safe_atof
|
|
|
|
// instead of atoi, atof, since the strings returned by MySQL may be NULL.
|
|
|
|
//
|
2004-07-01 20:24:00 +00:00
|
|
|
|
2004-07-01 18:43:36 +00:00
|
|
|
inline int safe_atoi(const char* s) {
|
2004-10-04 22:37:08 +00:00
|
|
|
if (!s) return 0;
|
|
|
|
return atoi(s);
|
2004-07-01 18:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline float safe_atof(const char* s) {
|
2004-10-04 22:37:08 +00:00
|
|
|
if (!s) return 0;
|
|
|
|
return atof(s);
|
2004-07-01 18:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define strcpy2(x, y) \
|
|
|
|
{ \
|
2005-02-16 23:17:43 +00:00
|
|
|
const char* z = y; \
|
2004-07-01 18:43:36 +00:00
|
|
|
if (!z) { \
|
|
|
|
x[0]=0; \
|
|
|
|
} else { \
|
|
|
|
strlcpy(x, z, sizeof(x)); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2004-07-01 20:24:00 +00:00
|
|
|
#define MAX_QUERY_LEN 256000
|
|
|
|
// TODO: use string for queries, get rid of this
|
|
|
|
|
2003-08-31 01:21:31 +00:00
|
|
|
struct CURSOR {
|
|
|
|
bool active;
|
|
|
|
MYSQL_RES *rp;
|
2004-07-09 14:11:17 +00:00
|
|
|
CURSOR() { active = false; rp = NULL; }
|
2003-08-31 01:21:31 +00:00
|
|
|
};
|
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
// represents a connection to a database
|
|
|
|
//
|
2003-09-27 23:20:40 +00:00
|
|
|
class DB_CONN {
|
2003-09-05 21:26:21 +00:00
|
|
|
public:
|
|
|
|
DB_CONN();
|
2004-01-15 23:53:13 +00:00
|
|
|
int open(char* name, char* host, char* user, char* passwd);
|
2005-02-16 23:17:43 +00:00
|
|
|
int do_query(const char*);
|
2006-03-17 04:47:51 +00:00
|
|
|
int affected_rows();
|
2003-09-05 21:26:21 +00:00
|
|
|
void close();
|
|
|
|
int insert_id();
|
2005-02-16 23:17:43 +00:00
|
|
|
void print_error(const char*);
|
2003-09-05 21:26:21 +00:00
|
|
|
const char* error_string();
|
|
|
|
|
|
|
|
MYSQL* mysql;
|
2004-07-21 21:50:25 +00:00
|
|
|
int start_transaction();
|
|
|
|
int commit_transaction();
|
2003-09-05 21:26:21 +00:00
|
|
|
};
|
|
|
|
|
2003-08-31 01:21:31 +00:00
|
|
|
// Base for derived classes that can access the DB
|
|
|
|
// Defines various generic operations on DB tables
|
|
|
|
//
|
|
|
|
class DB_BASE {
|
|
|
|
public:
|
2005-02-16 23:17:43 +00:00
|
|
|
DB_BASE(const char *table_name, DB_CONN*);
|
2003-08-31 01:21:31 +00:00
|
|
|
int insert();
|
2005-01-10 00:00:42 +00:00
|
|
|
int insert_batch(std::string&);
|
2003-08-31 01:21:31 +00:00
|
|
|
int update();
|
2005-02-16 23:17:43 +00:00
|
|
|
int update_field(const char*);
|
2004-09-12 00:49:38 +00:00
|
|
|
int delete_from_db();
|
2005-02-16 23:17:43 +00:00
|
|
|
int get_field_int(const char*, int&);
|
2003-08-31 01:21:31 +00:00
|
|
|
int lookup_id(int id);
|
2005-02-16 23:17:43 +00:00
|
|
|
int lookup(const char*);
|
|
|
|
int enumerate(const char* clause="", bool use_use_result=false);
|
2004-05-18 18:33:01 +00:00
|
|
|
int end_enumerate();
|
2005-02-16 23:17:43 +00:00
|
|
|
int count(int&, const char* clause="");
|
|
|
|
int max_id(int&, const char* clause="");
|
|
|
|
int sum(double&, const char* field, const char* clause="");
|
|
|
|
int get_double(const char* query, double&);
|
|
|
|
int get_integer(const char* query, int&);
|
2004-06-30 18:25:14 +00:00
|
|
|
bool is_high_priority;
|
2003-08-31 01:21:31 +00:00
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
DB_CONN* db;
|
2003-08-31 01:21:31 +00:00
|
|
|
const char *table_name;
|
|
|
|
CURSOR cursor;
|
|
|
|
virtual int get_id();
|
|
|
|
virtual void db_print(char*);
|
|
|
|
virtual void db_parse(MYSQL_ROW&);
|
|
|
|
};
|
|
|
|
|
2004-07-01 20:24:00 +00:00
|
|
|
// Base for derived classes that can get special-purpose data,
|
|
|
|
// perhaps spanning multiple tables
|
2004-07-01 18:43:36 +00:00
|
|
|
//
|
2004-07-01 20:24:00 +00:00
|
|
|
class DB_BASE_SPECIAL {
|
2004-07-01 18:43:36 +00:00
|
|
|
public:
|
2004-10-04 23:23:57 +00:00
|
|
|
DB_BASE_SPECIAL(DB_CONN*);
|
2004-07-01 18:43:36 +00:00
|
|
|
|
|
|
|
DB_CONN* db;
|
|
|
|
CURSOR cursor;
|
|
|
|
};
|
2003-12-26 06:03:03 +00:00
|
|
|
|
2004-04-09 23:33:50 +00:00
|
|
|
void escape_string(char* field, int len);
|
|
|
|
void unescape_string(char* p, int len);
|
2005-06-28 05:43:22 +00:00
|
|
|
void escape_mysql_like_pattern(const char* in, char* out);
|
|
|
|
// if you're going to use a "like X" clause,
|
|
|
|
// call this function to escape the non-wildcard part of X.
|
|
|
|
// If it contains wildcard chars (%, _) this will put
|
2005-06-28 05:48:41 +00:00
|
|
|
// two (2) backslashes before each one,
|
2005-06-28 05:43:22 +00:00
|
|
|
// so that they don't get treated as wildcards
|
2003-08-31 05:33:59 +00:00
|
|
|
|
|
|
|
#endif
|