Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F91925400
sql_connection_manager.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Nov 15, 19:03
Size
2 KB
Mime Type
text/x-c
Expires
Sun, Nov 17, 19:03 (2 d)
Engine
blob
Format
Raw Data
Handle
22348433
Attached To
R3127 blackdynamite
sql_connection_manager.hh
View Options
/*
author : Nicolas RICHART <nicolas.richart@epfl.ch>
*/
#ifndef __BLACKDYNAMITE_SQL_CONNECTION_MANAGER_HH__
#define __BLACKDYNAMITE_SQL_CONNECTION_MANAGER_HH__
#include "common_types.hh"
#include <pqxx/pqxx>
namespace
BlackDynamite
{
class
SQLConnectionManager
{
public
:
SQLConnectionManager
(
const
std
::
string
&
dbname
,
const
std
::
string
&
user
,
const
std
::
string
&
host
,
const
std
::
string
&
schema
)
{
if
(
this
->
connection_counter
==
0
)
{
std
::
string
options
(
""
);
if
(
this
->
sql_connection
==
NULL
)
{
options
+=
request_increment
(
"dbname"
,
dbname
);
options
+=
request_increment
(
"user"
,
user
);
if
(
host
!=
"localhost"
&&
host
!=
""
)
{
options
+=
request_increment
(
"host"
,
host
);
}
try
{
this
->
sql_connection
=
new
pqxx
::
connection
(
options
);
this
->
sql_connection
->
activate
();
}
catch
(
std
::
runtime_error
&
e
)
{
FATAL
(
"Database connection failed : "
<<
e
.
what
());
}
catch
(
std
::
exception
&
e
)
{
FATAL
(
e
.
what
());
}
catch
(...)
{
FATAL
(
"Unknown exception during connection to database"
);
}
}
}
this
->
connection_counter
++
;
this
->
sql_mode
=
false
;
}
virtual
~
SQLConnectionManager
()
{
this
->
connection_counter
--
;
if
(
this
->
connection_counter
==
0
)
{
delete
this
->
sql_connection
;
this
->
sql_connection
=
NULL
;
}
}
pqxx
::
connection
&
getConnection
()
{
return
*
sql_connection
;
}
UInt
getInstanceCounter
()
{
return
connection_counter
;
}
private
:
//! simple function to concatenate the keyword and parameter for forging a request
inline
std
::
string
request_increment
(
std
::
string
keyword
,
std
::
string
parameter
)
{
if
(
!
keyword
.
empty
())
return
keyword
+
"="
+
parameter
+
" "
;
return
""
;
}
public
:
static
pqxx
::
connection
*
sql_connection
;
static
unsigned
int
connection_counter
;
bool
sql_mode
;
};
}
#endif
/* __BLACKDYNAMITE_SQL_CONNECTION_MANAGER_HH__ */
Event Timeline
Log In to Comment