![]() |
Diana Software
|
base class for db interface More...
Public Types | |
| typedef std::vector< QVdt > | column |
| typedef std::map< std::string, column > | QDbTable |
Public Member Functions | |
| virtual | ~QDb () |
| double | DoQueryDouble (const std::string &query) |
| int | DoQueryInt (const std::string &query) |
| bool | DoQueryBool (const std::string &query) |
| std::string | DoQueryString (const std::string &query) |
| std::vector< int > | DoQueryVectorInt (const std::string &query) |
| std::vector< double > | DoQueryVectorDouble (const std::string &query) |
| std::vector< std::string > | DoQueryVectorString (const std::string &query) |
| QError | DoQuery (const std::string &query, QDbTable &table) |
| QDbTable | DoQuery (const std::string &query) |
| QError | DoSQLNoReturn (const std::string &query) |
| int | DoExec (const std::string &Query) |
| Execute an INSERT, UPDATE, DELETE, FETCH, or MOVE statement. More... | |
| int | Insert (const std::string &tableName, const column &fields, const column &values) |
| void | SetConnectAttemptTimeoutSec (int timeout) |
| void | SetConnectRandomDelaySec (double minDelaySec, double maxDelaySec) |
| void | SetDisconnectTimeoutSec (double timeout) |
| bool | Disconnect () |
Protected Member Functions | |
| QDb (const std::string &dbHost, const std::string &dbPort, const std::string &dbUsr, const std::string &dbPwd, const std::string &dbName) | |
| bool | IsConnected () const |
| const char * | DataBase () const |
Private Member Functions | |
| QError | Connect () |
| bool | UnlockedDisconnect () |
| QError | Query (const std::string &query, PGresult **result) |
| QError | BasicQuery (const std::string &query, PGresult **result) const |
| QError | CheckSingleResult (const QDbTable &table, const std::string &query, const std::string &funcName, const std::string &file, int line) const |
| QError | ConnectBase () |
| void | RandomWaitBeforeConnect () const |
| QError | InitWatchdogThread () |
| QError | UpdateWatchdogTimer () |
Private Attributes | |
| PGconn * | fConnection |
| const std::string | fDbHost |
| const std::string | fDbPort |
| const std::string | fDbUser |
| const std::string | fDbPwd |
| const std::string | fDbName |
| int | fConnectAttemptTimeoutSec |
| double | fDisconnectTimeoutSec |
| QDbWatchdogThreadHandler * | fThreadHandler |
| QWatchdogTimer * | fWatchdogTimer |
| boost::mutex * | fMutex |
| double | fMinConnectDelaySec |
| double | fMaxConnectDelaySec |
Friends | |
| class | QDbWatchdogThread |
| typedef std::vector<QVdt> QDb::column |
| typedef std::map<std::string, column> QDb::QDbTable |
|
virtual |
Definition at line 51 of file QDb.cc.
References Disconnect(), fMutex, fThreadHandler, fWatchdogTimer, and IsConnected().
|
protected |
|
private |
Perform a query without checking the connection status, essentially just run PQexec() and check the error status. This is the only method of this class where PQexec is called.
Definition at line 376 of file QDb.cc.
References fConnection, QERR_DB_QUERY_FAILED, and QERR_SUCCESS.
Referenced by Query().
|
private |
check that table contains a single result (one field, one row)
Definition at line 456 of file QDb.cc.
References QERR_DB_MAXLINES_EXCEEDED, QERR_DB_NULL_RESULT, QERR_SUCCESS, and QERR_UNDEFINED_STATUS.
Referenced by DoQueryBool(), DoQueryDouble(), DoQueryInt(), DoQueryString(), DoQueryVectorDouble(), DoQueryVectorInt(), and DoQueryVectorString().
|
private |
Open a database connection if it is not already open.
This method also initializes and updates the watchdog timer (if this feature is enabled, see SetDisconnectTimeoutSec()). For the watchdog timer mechanism to work properly, this method assumes that the Query() method include a call to Connect().
Definition at line 73 of file QDb.cc.
References ConnectBase(), err, fConnectAttemptTimeoutSec, InitWatchdogThread(), IsConnected(), QERR_SUCCESS, RandomWaitBeforeConnect(), and UpdateWatchdogTimer().
Referenced by Query().
|
private |
Definition at line 115 of file QDb.cc.
References fConnection, fDbHost, fDbName, fDbPort, fDbPwd, fDbUser, IsConnected(), QERR_DB_CONN_FAILED, and QERR_SUCCESS.
Referenced by Connect().
|
inlineprotected |
| bool QDb::Disconnect | ( | ) |
Definition at line 151 of file QDb.cc.
References fMutex, and UnlockedDisconnect().
Referenced by QDbWatchdogThread::operator()(), QDianaDb::Reset(), and ~QDb().
| int QDb::DoExec | ( | const std::string & | Query | ) |
Execute an INSERT, UPDATE, DELETE, FETCH, or MOVE statement.
Definition at line 305 of file QDb.cc.
References err, QERR_SUCCESS, and Query().
Referenced by Insert(), QCalibrationParametersHandle::StoreOnDB(), QCOFParametersHandle::StoreOnDB(), QEnergyRangesHandle::StoreOnDB(), and QJitterHandle::StoreOnDB().
| QDb::QDbTable QDb::DoQuery | ( | const std::string & | query | ) |
Definition at line 169 of file QDb.cc.
References DoQuery(), err, and QERR_SUCCESS.
Definition at line 409 of file QDb.cc.
References err, QERR_DB_QUERY_FAILED, QERR_SUCCESS, and Query().
Referenced by DoQuery(), DoQueryBool(), DoQueryDouble(), DoQueryInt(), DoQueryString(), DoQueryVectorDouble(), DoQueryVectorInt(), DoQueryVectorString(), QCalibrationParametersHandle::FillFromDB(), QJitterHandle::FillFromDB(), QChannelRunDataHandle::FillFromDB(), and QCOFParametersHandle::StoreOnDB().
| bool QDb::DoQueryBool | ( | const std::string & | query | ) |
expect a single result of type bool
Definition at line 213 of file QDb.cc.
References CheckSingleResult(), DoQuery(), err, and QERR_SUCCESS.
| double QDb::DoQueryDouble | ( | const std::string & | query | ) |
expect a single result of type double
Definition at line 181 of file QDb.cc.
References CheckSingleResult(), DoQuery(), err, and QERR_SUCCESS.
Referenced by QCalibrationParametersHandle::FillFromDB(), QEnergyRangesHandle::FillFromDB(), QJitterHandle::FillFromDB(), QRunDataHandle::FillFromDB(), and QEnergyRangesHandle::StoreOnDB().
| int QDb::DoQueryInt | ( | const std::string & | query | ) |
expect a single result of type int
Definition at line 197 of file QDb.cc.
References CheckSingleResult(), DoQuery(), err, and QERR_SUCCESS.
Referenced by QAverageNoiseHandle::FillFromDB(), QAveragePulseHandle::FillFromDB(), QCalibrationParametersHandle::FillFromDB(), and QJitterHandle::FillFromDB().
| std::string QDb::DoQueryString | ( | const std::string & | query | ) |
expect a single result of type string
Definition at line 229 of file QDb.cc.
References CheckSingleResult(), DoQuery(), err, and QERR_SUCCESS.
Referenced by QCalibrationParametersHandle::FillFromDB(), QRunDataHandle::FillFromDB(), QCalibrationParametersHandle::StoreOnDB(), and QJitterHandle::StoreOnDB().
| std::vector< double > QDb::DoQueryVectorDouble | ( | const std::string & | query | ) |
expect a single result of type "vector of double"
Definition at line 260 of file QDb.cc.
References CheckSingleResult(), DoQuery(), err, and QERR_SUCCESS.
Referenced by QAverageNoiseHandle::FillFromDB(), QAveragePulseHandle::FillFromDB(), and QCalibrationParametersHandle::FillFromDB().
| std::vector< int > QDb::DoQueryVectorInt | ( | const std::string & | query | ) |
expect a single result of type "vector of int"
Definition at line 245 of file QDb.cc.
References CheckSingleResult(), DoQuery(), err, and QERR_SUCCESS.
Referenced by QAverageNoiseHandle::FillFromDB(), QAveragePulseHandle::FillFromDB(), and QCalibrationParametersHandle::FillFromDB().
| std::vector< std::string > QDb::DoQueryVectorString | ( | const std::string & | query | ) |
expect a single result of type "vector of string"
Definition at line 276 of file QDb.cc.
References CheckSingleResult(), DoQuery(), err, and QERR_SUCCESS.
| QError QDb::DoSQLNoReturn | ( | const std::string & | query | ) |
make query and expect no output
Definition at line 292 of file QDb.cc.
References err, QERR_SUCCESS, and Query().
|
private |
Definition at line 523 of file QDb.cc.
References err, fDisconnectTimeoutSec, fThreadHandler, fWatchdogTimer, QERR_SUCCESS, QDbWatchdogThreadHandler::SetDatabase(), QWatchdogTimer::SetTimeoutSec(), QDbWatchdogThreadHandler::SetWatchdogTimer(), and QDbWatchdogThreadHandler::Start().
Referenced by Connect().
Definition at line 320 of file QDb.cc.
References DoExec(), err, and QERR_SIZE_NOT_MATCH.
Referenced by QAverageNoiseHandle::StoreOnDB(), QAveragePulseHandle::StoreOnDB(), QCalibrationParametersHandle::StoreOnDB(), QCOFParametersHandle::StoreOnDB(), QEnergyRangesHandle::StoreOnDB(), and QJitterHandle::StoreOnDB().
|
protected |
Definition at line 66 of file QDb.cc.
References fConnection.
Referenced by Connect(), ConnectBase(), QDbWatchdogThread::operator()(), QDianaDb::Reset(), and ~QDb().
|
private |
Possibly open the connection and perform the query using BasicQuery(). Disconnect(), Connect() and call again BasicQuery() if the first attempt failed. The second attempt is needed for the connection failover mechanism to work properly. All the queries are supposed to pass through this method, which in turns calls BasicQuery().
Definition at line 352 of file QDb.cc.
References BasicQuery(), Connect(), err, fMutex, QERR_SUCCESS, and UnlockedDisconnect().
Referenced by DoExec(), DoQuery(), and DoSQLNoReturn().
|
private |
Definition at line 498 of file QDb.cc.
References fMaxConnectDelaySec, and fMinConnectDelaySec.
Referenced by Connect().
|
inline |
Set timeout in seconds for attempting db connection. Any value smaller than 1 will result in no timeout. The default is no timeout.
Definition at line 82 of file QDb.hh.
References fConnectAttemptTimeoutSec.
|
inline |
Definition at line 101 of file QDb.hh.
References fMaxConnectDelaySec, and fMinConnectDelaySec.
|
inline |
Set a timeout for disconnecting from the server after the connection is unused for a certain time. A negative or zero value means that the connection will remain open until this object is deleted.
Definition at line 110 of file QDb.hh.
References fDisconnectTimeoutSec.
|
private |
Definition at line 158 of file QDb.cc.
References fConnection.
Referenced by Disconnect(), and Query().
|
private |
Definition at line 551 of file QDb.cc.
References QBaseThreadHandler::Check(), err, fThreadHandler, fWatchdogTimer, QERR_SUCCESS, QERR_UNDEFINED_STATUS, and QWatchdogTimer::Start().
Referenced by Connect().
|
friend |
|
private |
Definition at line 141 of file QDb.hh.
Referenced by Connect(), and SetConnectAttemptTimeoutSec().
|
private |
Definition at line 132 of file QDb.hh.
Referenced by BasicQuery(), ConnectBase(), IsConnected(), and UnlockedDisconnect().
|
private |
Definition at line 134 of file QDb.hh.
Referenced by ConnectBase().
|
private |
Definition at line 138 of file QDb.hh.
Referenced by ConnectBase(), and DataBase().
|
private |
Definition at line 135 of file QDb.hh.
Referenced by ConnectBase().
|
private |
Definition at line 137 of file QDb.hh.
Referenced by ConnectBase().
|
private |
Definition at line 136 of file QDb.hh.
Referenced by ConnectBase().
|
private |
Definition at line 154 of file QDb.hh.
Referenced by InitWatchdogThread(), and SetDisconnectTimeoutSec().
|
private |
Definition at line 150 of file QDb.hh.
Referenced by RandomWaitBeforeConnect(), and SetConnectRandomDelaySec().
|
private |
Definition at line 149 of file QDb.hh.
Referenced by RandomWaitBeforeConnect(), and SetConnectRandomDelaySec().
|
private |
|
private |
Definition at line 156 of file QDb.hh.
Referenced by InitWatchdogThread(), UpdateWatchdogTimer(), and ~QDb().
|
private |
Definition at line 157 of file QDb.hh.
Referenced by InitWatchdogThread(), UpdateWatchdogTimer(), and ~QDb().