2003-08-31 05:33:59 +00:00
|
|
|
// The contents of this file are subject to the BOINC 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://boinc.berkeley.edu/license_1.0.txt
|
2004-01-15 23:53:13 +00:00
|
|
|
//
|
2003-08-31 05:33:59 +00:00
|
|
|
// 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
|
2004-01-15 23:53:13 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2003-08-31 05:33:59 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2004-01-15 23:53:13 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2003-08-31 05:33:59 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef _DB_BASE_
|
|
|
|
#define _DB_BASE_
|
|
|
|
|
2003-08-31 01:21:31 +00:00
|
|
|
#include "mysql.h"
|
|
|
|
|
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) \
|
|
|
|
{ \
|
|
|
|
char* z = y; \
|
|
|
|
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);
|
2004-01-14 20:24:24 +00:00
|
|
|
int do_query(char*);
|
2003-09-05 21:26:21 +00:00
|
|
|
void close();
|
|
|
|
int insert_id();
|
|
|
|
void print_error(char*);
|
|
|
|
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:
|
2004-10-04 23:23:57 +00:00
|
|
|
DB_BASE(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();
|
2004-01-14 20:24:24 +00:00
|
|
|
int update_field(char*);
|
2004-09-12 00:49:38 +00:00
|
|
|
int delete_from_db();
|
2004-08-21 00:40:01 +00:00
|
|
|
int get_field_int(char*, int&);
|
2003-08-31 01:21:31 +00:00
|
|
|
int lookup_id(int id);
|
|
|
|
int lookup(char*);
|
2004-09-24 20:48:07 +00:00
|
|
|
int enumerate(char* clause="", bool use_use_result=false);
|
2004-05-18 18:33:01 +00:00
|
|
|
int end_enumerate();
|
2003-08-31 01:21:31 +00:00
|
|
|
int count(int&, char* clause="");
|
|
|
|
int sum(double&, char* field, char* clause="");
|
|
|
|
int get_double(char* query, double&);
|
|
|
|
int get_integer(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);
|
2003-08-31 05:33:59 +00:00
|
|
|
|
|
|
|
#endif
|