2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2004-01-15 23:53:13 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC 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 3 of the License, or (at your option) any later version.
|
2004-01-15 23:53:13 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2003-08-31 05:33:59 +00:00
|
|
|
|
|
|
|
#ifndef _DB_BASE_
|
|
|
|
#define _DB_BASE_
|
|
|
|
|
2007-12-21 21:09:40 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <string>
|
2005-09-29 12:03:23 +00:00
|
|
|
#include <mysql.h>
|
2003-08-31 01:21:31 +00:00
|
|
|
|
2010-04-03 00:02:38 +00:00
|
|
|
extern bool g_print_queries;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-02-02 17:01:57 +00:00
|
|
|
inline double 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)); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2011-11-06 06:25:48 +00:00
|
|
|
#define MAX_QUERY_LEN 262144
|
2004-07-01 20:24:00 +00:00
|
|
|
// 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
|
|
|
};
|
|
|
|
|
2007-02-13 21:41:07 +00:00
|
|
|
enum ISOLATION_LEVEL {
|
|
|
|
READ_UNCOMMITTED,
|
|
|
|
READ_COMMITTED,
|
|
|
|
REPEATABLE_READ,
|
|
|
|
SERIALIZABLE
|
|
|
|
};
|
|
|
|
|
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);
|
2007-02-13 21:41:07 +00:00
|
|
|
int set_isolation_level(ISOLATION_LEVEL);
|
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();
|
2008-10-02 19:03:52 +00:00
|
|
|
int ping();
|
2004-07-21 21:50:25 +00:00
|
|
|
int start_transaction();
|
2008-03-07 21:13:01 +00:00
|
|
|
int rollback_transaction();
|
2004-07-21 21:50:25 +00:00
|
|
|
int commit_transaction();
|
2008-10-02 19:03:52 +00:00
|
|
|
|
|
|
|
MYSQL* mysql;
|
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*);
|
2006-12-30 00:26:22 +00:00
|
|
|
virtual ~DB_BASE(){}
|
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();
|
2008-02-08 17:20:09 +00:00
|
|
|
int update_field(const char*, const char* where_clause=NULL);
|
2010-03-29 22:28:20 +00:00
|
|
|
int update_fields_noid(char* set_clause, char* where_clause);
|
2004-09-12 00:49:38 +00:00
|
|
|
int delete_from_db();
|
2011-10-26 07:15:22 +00:00
|
|
|
int get_field_ints(const char*, int, int*);
|
2007-02-06 21:50:48 +00:00
|
|
|
int get_field_str(const char*, 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&);
|
2008-03-07 21:13:01 +00:00
|
|
|
int affected_rows();
|
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
|