[1cff17]: src / org / esb / sql / Connection.cpp  Maximize  Restore  History

Download this file

93 lines (78 with data), 2.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "org/esb/util/StringTokenizer.h"
#include "org/esb/util/Properties.h"
#include "Connection.h"
#include "Statement.h"
#include "PreparedStatement.h"
using namespace org::esb::sql;
using namespace org::esb::util;
Connection::Connection(){
}
Connection::Connection(const char * con){
std::string constr=con;
parseConnectionString(constr);
mysql_init(&mysql);
if (!mysql_real_connect(&mysql,_host.c_str(),_username.c_str(),_passwd.c_str(),_db.c_str(),0,NULL,0))
{
throw SqlException(string("Failed to connect to database: Error:").append(mysql_error(&mysql)));
}
}
Connection::Connection(const char * host, const char * db, const char * user, const char *pass){
_username=user;
_passwd=pass;
_host=host;
_db=db;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql,_host.c_str(),_username.c_str(),_passwd.c_str(),_db.c_str(),0,NULL,0))
{
throw SqlException(string("Failed to connect to database: Error:").append(mysql_error(&mysql)));
}
}
Connection::~Connection(){
mysql_close(&mysql);
}
Statement Connection::createStatement(const char * sql){
return Statement(mysql, sql);
}
PreparedStatement Connection::prepareStatement(const char * sql){
// PreparedStatement *stmt=new PreparedStatement(*this, sql);
return PreparedStatement(mysql, sql);
}
//Statement & Connection::createStatement(){return;}
void Connection::close(){
// mysql_close(mysql);
// sqlite3_connection::close();
}
void Connection::executeNonQuery(const char * sql){
// sqlite3_connection::close();
}
long Connection::lastInsertId(){
return mysql_insert_id(&mysql);
// return tntcon.selectValue("select last_insert_id()").getInt();
// sqlite3_connection::close();
}
void Connection::parseConnectionString(std::string & constr){
StringTokenizer tok(constr,":");
if(tok.countTokens()==2){
tok.nextToken();
StringTokenizer data(tok.nextToken(),";");
while(data.hasMoreTokens()){
StringTokenizer line(data.nextToken(),"=");
string key=line.nextToken();
string val=line.nextToken();
if(key.compare("user")==0){
_username=val;
}else
if(key.compare("passwd")==0){
_passwd=val;
}else
if(key.compare("host")==0){
_host=val;
}else
if(key.compare("db")==0){
_db=val;
}else{
/*nothing*/
}
}
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks