Browse Source

Removed MySQL names - first step to general db library

master
Artyom Beilis 16 years ago
parent
commit
26297d3395
6 changed files with 63 additions and 63 deletions
  1. +4
    -4
      cppblog.prj
  2. +24
    -24
      db_wrapper.cpp
  3. +24
    -24
      db_wrapper.h
  4. +6
    -6
      main_thread.cpp
  5. +4
    -4
      main_thread.h
  6. +1
    -1
      makefile

+ 4
- 4
cppblog.prj View File

@@ -70,12 +70,12 @@ module.include.files=\
worker_thread.h\
textstream.h\
main_thread.h\
mysql_db.h\
thread_pool.h\
global_config.h\
http_error.h\
templates.h\
url.h
url.h\
db_wrapper.h

module.source.name=.
module.source.type=
@@ -84,12 +84,12 @@ module.source.files=\
worker_thread.cpp\
textstream.cpp\
main_thread.cpp\
mysql_db.cpp\
thread_pool.cpp\
makefile\
global_config.cpp\
templates.cpp\
url.cpp
url.cpp\
db_wrapper.cpp

module.pixmap.name=.
module.pixmap.type=


mysql_db.cpp → db_wrapper.cpp View File

@@ -1,19 +1,19 @@
#include "mysql_db.h"
#include "db_wrapper.h"
#include <boost/scoped_array.hpp>
#include <iostream>

#include "global_config.h"

namespace mysql_wrapper {
namespace db_wrapper {

MySQL_DB_Escape::MySQL_DB_Escape(char const *format)
DB_Escape::DB_Escape(char const *format)
{
query=NULL;
this->format=format;
parameters.reserve(16);
}

MySQL_DB_Escape &MySQL_DB_Escape::operator<<(long val)
DB_Escape &DB_Escape::operator<<(long val)
{
element e;
e.data.long_val=val;
@@ -23,7 +23,7 @@ MySQL_DB_Escape &MySQL_DB_Escape::operator<<(long val)
return *this;
}

MySQL_DB_Escape &MySQL_DB_Escape::operator<<(double val)
DB_Escape &DB_Escape::operator<<(double val)
{
element e;
e.data.double_val=val;
@@ -33,7 +33,7 @@ MySQL_DB_Escape &MySQL_DB_Escape::operator<<(double val)
return *this;
}

MySQL_DB_Escape &MySQL_DB_Escape::operator<<(char const *str)
DB_Escape &DB_Escape::operator<<(char const *str)
{
element e;
e.data.str_val=str;
@@ -43,7 +43,7 @@ MySQL_DB_Escape &MySQL_DB_Escape::operator<<(char const *str)
return *this;
}

char *MySQL_DB_Escape::get(MYSQL *conn)
char *DB_Escape::get(MYSQL *conn)
{
int overall_len=calc_len();
query=new char [overall_len+1];
@@ -51,7 +51,7 @@ char *MySQL_DB_Escape::get(MYSQL *conn)
return query;
}

int MySQL_DB_Escape::calc_len()
int DB_Escape::calc_len()
{
int id,len=0,i,j;
for(i=0;format[i];i++) {
@@ -62,7 +62,7 @@ int MySQL_DB_Escape::calc_len()
id=id*10+format[j]-'0';
}
else {
throw MySQL_DB_Err("Invalid format");
throw DB_Err("Invalid format");
}
}
i=j;
@@ -78,7 +78,7 @@ int MySQL_DB_Escape::calc_len()
return i+len;
}

void MySQL_DB_Escape::escape_str(MYSQL *conn)
void DB_Escape::escape_str(MYSQL *conn)
{
char *q=query;
int i,j,id;
@@ -90,7 +90,7 @@ void MySQL_DB_Escape::escape_str(MYSQL *conn)
id=id*10+format[j]-'0';
}
else {
throw MySQL_DB_Err("Invalid format");
throw DB_Err("Invalid format");
}
}
i=j;
@@ -126,11 +126,11 @@ void MySQL_DB_Escape::escape_str(MYSQL *conn)
*q=0;
}

void MySQL_DB::connect()
void Data_Base::connect()
{
conn=mysql_init(NULL);
if(!conn){
throw MySQL_DB_Err("No memory");
throw DB_Err("No memory");
}
if(!mysql_real_connect( conn,
host.c_str(),
@@ -140,11 +140,11 @@ void MySQL_DB::connect()
0,NULL,0))
{
close();
throw MySQL_DB_Err("Failed to connect to database");
throw DB_Err("Failed to connect to database");
}
}

void MySQL_DB::open(string const &h,string const &u,string const &p,string const &d)
void Data_Base::open(string const &h,string const &u,string const &p,string const &d)
{
host=h;
username=u;
@@ -154,7 +154,7 @@ void MySQL_DB::open(string const &h,string const &u,string const &p,string const
connect();
}

void MySQL_DB::open()
void Data_Base::open()
{
open( global_config.sval("mysql.host"),
global_config.sval("mysql.username"),
@@ -162,10 +162,10 @@ void MySQL_DB::open()
global_config.sval("mysql.database"));
}
void MySQL_DB::exec_query(char const *q)
void Data_Base::exec_query(char const *q)
{
bool not_try_once_more=false;
if(!setup) throw MySQL_DB_Err("Date base must be open first");
if(!setup) throw DB_Err("Date base must be open first");
if(!conn){
connect();
not_try_once_more=true;
@@ -175,31 +175,31 @@ void MySQL_DB::exec_query(char const *q)
close();
connect();
if(mysql_query(conn,q)) {
throw MySQL_DB_Err(string("Failed to exectue the query:") + q);
throw DB_Err(string("Failed to exectue the query:") + q);
}
}
}

void MySQL_DB::exec(char const *q)
void Data_Base::exec(char const *q)
{
exec_query(q);
MYSQL_RES *res=mysql_store_result(conn);
if(res) {
mysql_free_result(res);
throw MySQL_DB_Err("You must not use query for operation that "
throw DB_Err("You must not use query for operation that "
"returns result");
}
if(mysql_errno(conn)) {
throw MySQL_DB_Err(string("Error executing query:")+q);
throw DB_Err(string("Error executing query:")+q);
}
}

MYSQL_RES *MySQL_DB::query(char const *q)
MYSQL_RES *Data_Base::query(char const *q)
{
exec_query(q);
MYSQL_RES *res=mysql_store_result(conn);
if(!res || mysql_errno(conn)) {
throw MySQL_DB_Err(string("Error executing query:")+q);
throw DB_Err(string("Error executing query:")+q);
}
return res;
}

mysql_db.h → db_wrapper.h View File

@@ -10,15 +10,15 @@
#include <vector>
#include <iostream>

namespace mysql_wrapper {
namespace db_wrapper {
using namespace std;

#define DB_ERR_MAX_LEN 128

typedef MYSQL_ROW MySQL_DB_Row;
typedef char **DB_Row;

class MySQL_DB_Escape {
class DB_Escape {
typedef enum { LONG, DOUBLE, STRING } types;
struct element {
types type;
@@ -36,59 +36,59 @@ class MySQL_DB_Escape {
int calc_len();
void escape_str(MYSQL *conn);
public:
MySQL_DB_Escape &operator<<(char const *str);
MySQL_DB_Escape &operator<<(string const &str)
DB_Escape &operator<<(char const *str);
DB_Escape &operator<<(string const &str)
{
return *this<<str.c_str();
};
MySQL_DB_Escape &operator<<(int v)
DB_Escape &operator<<(int v)
{
return *this<<(long)v;
};
MySQL_DB_Escape &operator<<(long);
MySQL_DB_Escape &operator<<(double);
DB_Escape &operator<<(long);
DB_Escape &operator<<(double);
char *get(MYSQL *conn);
MySQL_DB_Escape(char const *format);
~MySQL_DB_Escape() { delete [] query; };
DB_Escape(char const *format);
~DB_Escape() { delete [] query; };
};

typedef MySQL_DB_Escape escape;
typedef DB_Escape escape;


class MySQL_DB_Err {
class DB_Err {
char message[DB_ERR_MAX_LEN];
public:
char const *get() { return message; };
MySQL_DB_Err(char const *text)
DB_Err(char const *text)
{
strncpy(message,text,DB_ERR_MAX_LEN);
message[DB_ERR_MAX_LEN-1]=0;
};
MySQL_DB_Err(string const &str)
DB_Err(string const &str)
{
strncpy(message,str.c_str(),DB_ERR_MAX_LEN);
message[DB_ERR_MAX_LEN-1]=0;
};
};

class MySQL_DB_Res {
class DB_Res {
MYSQL_RES *res;
public:
MySQL_DB_Res() { res=NULL; };
MySQL_DB_Res(MYSQL_RES *result) {
DB_Res() { res=NULL; };
DB_Res(MYSQL_RES *result) {
res=result;
};
void free() { if(res) mysql_free_result(res); };
~MySQL_DB_Res() { free(); };
MySQL_DB_Row next() { return mysql_fetch_row(res); };
~DB_Res() { free(); };
DB_Row next() { return mysql_fetch_row(res); };
void operator=(MYSQL_RES *result) { free(); res=result; };
int cols() { return mysql_num_fields(res); };
int rows() { return mysql_num_rows(res); };
};


class MySQL_DB {
class Data_Base {
bool setup;
MYSQL *conn;
string host;
@@ -99,11 +99,11 @@ class MySQL_DB {
void exec_query(char const *q);
public:
MYSQL_RES *query(char const *);
MYSQL_RES *query(MySQL_DB_Escape &e) { return query(e.get(conn)); };
MYSQL_RES *query(DB_Escape &e) { return query(e.get(conn)); };
MYSQL_RES *query(string const &s) { return query(s.c_str()); };

void exec(char const *);
void exec(MySQL_DB_Escape &e) { exec(e.get(conn)); };
void exec(DB_Escape &e) { exec(e.get(conn)); };
void exec(string const &s) { exec(s.c_str()); };

@@ -111,8 +111,8 @@ public:
void open(string const &h,string const &u,string const &p,string const &d);
void open();
MySQL_DB() { conn=NULL; setup=false; };
~MySQL_DB() { if(conn) mysql_close(conn); };
Data_Base() { conn=NULL; setup=false; };
~Data_Base() { if(conn) mysql_close(conn); };
};


+ 6
- 6
main_thread.cpp View File

@@ -24,7 +24,7 @@ void Main_Thread::init()
try {
db.open();
}
catch(MySQL_DB_Err &e)
catch(DB_Err &e)
{
throw HTTP_Error(e.get());
}
@@ -68,11 +68,11 @@ void Main_Thread::load_cookies()

void Main_Thread::check_athentication_by_name(string name,string password)
{
MySQL_DB_Res res=db.query(
DB_Res res=db.query(
escape( "SELECT password,id from cp_users "
"WHERE username='%1%' LIMIT 1")<<name);
MySQL_DB_Row row;
DB_Row row;
if((row=res.next())!=NULL){
if(password==row[0]) {
@@ -192,7 +192,7 @@ void Main_Thread::show_main_page(string from)
c[TV_username]=username;
}
MySQL_DB_Res res;
DB_Res res;
if(from=="end") {
res = db.query(
@@ -213,7 +213,7 @@ void Main_Thread::show_main_page(string from)
res = db.query(escape(q)<<from_id);
}
MySQL_DB_Row row;
DB_Row row;

int id;
string content;
@@ -274,7 +274,7 @@ void Main_Thread::main()
try {
show_page();
}
catch (MySQL_DB_Err &mysql_err) {
catch (DB_Err &mysql_err) {
throw HTTP_Error(mysql_err.get());
}
}

+ 4
- 4
main_thread.h View File

@@ -3,18 +3,18 @@


#include "worker_thread.h"
#include "mysql_db.h"
#include <mysql/mysql.h>
#include "db_wrapper.h"
#include <cgicc/HTTPStatusHeader.h>
#include <cgicc/HTTPRedirectHeader.h>

using namespace mysql_wrapper;
using namespace db_wrapper;
using namespace cgicc;

class Main_Thread : public Worker_Thread {
URL_Parser url;
// Internal Data
MySQL_DB db;
Data_Base db;
// User Data
int user_id;
string username;


+ 1
- 1
makefile View File

@@ -1,5 +1,5 @@
TR=test.fcgi
SRC=main.cpp textstream.cpp worker_thread.cpp FCgiIO.cpp main_thread.cpp mysql_db.cpp thread_pool.cpp \
SRC=main.cpp textstream.cpp worker_thread.cpp FCgiIO.cpp main_thread.cpp db_wrapper.cpp thread_pool.cpp \
global_config.cpp url.cpp templates.cpp

LSRC=textstream.cpp worker_thread.cpp FCgiIO.cpp mysql_db.cpp thread_pool.cpp global_config.cpp templates.cpp


Loading…
Cancel
Save