CARMA C++
DBConnection.h
Go to the documentation of this file.
1 #ifndef CARMA_DBMS_DBCONNECTION_H
2 #define CARMA_DBMS_DBCONNECTION_H
3 
17 #include "carma/dbms/Table.h"
19 #include "carma/util/Logger.h"
20 #include "carma/util/Time.h"
21 #include <map>
22 #include <string>
23 #include <vector>
24 #include <unixODBC/sqlext.h>
25 
26 namespace carma {
27 namespace dbms {
28 
29  class DBConfigurator;
30 
46 class DBConnection {
47 
48 public:
49  friend class DBConnectionTest;
50  friend class LogDatabase;
51  friend class MonitorConfigurationDatabase;
52  friend class MonitorDataDatabase;
53  friend class MonitorDataQueryManager;
54 
58  typedef std::map<std::string, std::string> file2TableMap;
59 
60 
64  virtual ~DBConnection();
65 
66  // -------------------- Database status/info methods --------------------
75  static bool isUp(const DBConfigurator *dbconf = NULL);
76 
77 
82  virtual bool isDBUp();
83 
87  void odbcInfo() const;
88 
93  inline std::string rdbmsName() const {return rdbmsName_;}
94 
99  //inline bool usingProductionDB() const {return useProductionDB_;}
100 
105  inline std::string dataSource() const {return dataSource_;}
106 
107 
108  //------------------ Monitor data tables access --------------------
109 
120  virtual std::string createMonitorDataTable(
121  const MonitorAverageType& averageType,
122  const MonitorAggregateType& aggType,
123  const std::string& tag,
124  const std::string *const location=NULL)
125  const = 0;
126 
127 
139  virtual void loadDataFromFile
140  (const std::string& filename, const std::string& table,
141  const bool& useTransactions, const int& linesToIgnore=0,
142  const std::string& columnDelimiter="\t") const = 0;
143 
154  virtual void loadDataFromFiles
155  (const file2TableMap& file2Table, const bool& useTransactions,
156  const int& linesToIgnore=0, const std::string& columnDelimiter="\t")
157  const = 0;
158 
171  virtual void* getAggregate(const std::string& column,
172  const std::string& table,
173  const std::string& function,
174  carma::dbms::Table::ColumnType& type) const;
175 
181  virtual long getRowCount(const std::string& tableName) const;
182 
183 
184  //---------------------- General table access ------------------------
185 
193  virtual carma::dbms::Table execSQLSelect(const std::string& statement,
194  const std::string& resultTableName="RESULT TABLE")
195  const;
196 
197 
212  (const std::vector<std::string>& columnNames,
213  const std::string& tableName,
214  const std::string& whereClause="",
215  const std::string& orderByClause="",
216  const std::string& groupByClause="",
217  const std::string& havingClause="",
218  const std::string& resultTableName="RESULT TABLE")
219  const;
220 
227  virtual void execSQLDropTable(const std::string& tableName) const;
228 
235  virtual void deleteFromMonitorIndexTable(const std::string& whereClause)
236  const;
237 
243  virtual void monitorDataTableHasBeenPopulated(const std::string& tableName,
244  const MonitorAverageType& averageType)
245  const;
246 
251  virtual void dropScratchTable(const std::string& table) const;
252 
253  //------------------- Transaction support -------------------------
258  virtual void beginTransaction() const;
259 
264  virtual void rollBackTransaction() const;
265 
270  virtual void commitTransaction() const;
271 
277  virtual bool inTransaction() const;
278 
279 
280  //------------------- Log table related methods ---------------------
281 
289  void insertLogMessage(const std::string& message,
290  const carma::util::frameType& frameCount
292 
293 
313  const carma::util::frameType& end
315  const std::string *matchString=NULL) const;
316 
320  void closeConnection();
321 
322  //--------------------------- Database info ----------------------
330  bool tableExists(const std::string& table, const std::string& database)
331  const;
332 
338  carma::dbms::Table databaseInfo(const std::string& database="") const;
339 
340  // --------------------------SQL Helpers -------------------------
355  static std::string createJoinClause
356  (const std::string& leftTable, const std::string& rightTable,
357  const std::string& leftColumn, const std::string& rightColumn="",
358  const std::string& rightTableAlias="",
359  const std::string& indexCol="");
360 
367  static std::string createJoinClause
368  (const std::string& joinTable, const std::string& onClause);
369 
376  static std::string createInsertStatement
377  (const std::string& tableName, const std::vector<std::string> columns,
378  const std::vector<std::string> values);
379 
389  static std::string createInsertStatement
390  (const std::string& tableName, const std::vector<std::string> columns,
391  const std::string values);
392 
393 
394  //-------------------------- RDBMS limits --------------------------
400  virtual unsigned maxTablesPerJoin() const = 0;
401 
406  virtual unsigned maxColumnsPerTable() const = 0;
407 
412  std::string virtual caseSensitiveSearchModifier() const { return " "; }
413 
414  //----------------------- table repair ---------------------
415 
420  virtual void repairMonitorDataTable(const std::string& tableName)
421  const = 0;
422 
423 
424 
425 protected:
426  std::string rdbmsName_;
427  std::string dataSource_;
428  std::string dbuser_;
429  std::string odbcConnectString_;
430  std::string socket_;
431  unsigned port_;
432 
433  // dbname is needed for methods which use native rdbms api calls
434  std::string dbname_;
435  std::string passwordFile_;
436  log4cpp::Category& logger_;
437  bool inTxn_;
438  std::vector<SQLHSTMT> activeStatements;
439  //bool useProductionDB_;
443  std::string odbcini_;
444 
445 
449  SQLHDBC hdbc_;
450  SQLHENV henv_;
451 
462 
463  //----------------------- Database access methods -----------------------
469  void openODBCConnection_(SQLHENV *henv, SQLHDBC *hdbc);
470  void openODBCConnection_();
471 
476  void closeODBCConnection_();
477  void closeODBCConnection_(SQLHENV henv, SQLHDBC hdbc) const;
478 
483  void freeSQLStatement_(const SQLHSTMT& hstmt) const;
484 
485 
486  //----------------------- Database query methods ------------------
487 
494  void directSQLInsert_(const std::string& statement) const;
495 
502  void directSQLExec_(const SQLHSTMT hstmt, const std::string& statement)
503  const;
504 
510  carma::dbms::Table odbcResultSetToTable_(const SQLHSTMT& hstmt) const;
511 
517  void directSQLExec_(const std::string& statement) const;
518 
519  //---------------- query construction methods ---------------------
529  std::string readPassword_() const;
530 
531 };
532 
540  public:
561  static DBConnection *createConnection(const DBConfigurator *dbconf=NULL);
562 
563  };
564 
565 
566 
567 // DBConnectionException definition
568 
574 public:
575  DBConnectionException (const char* msg, const char* fileName = __FILE__,
576  const int lineNum = __LINE__)
577  : carma::util::ErrorException (msg, fileName, lineNum) {};
578 
579  DBConnectionException (const std::string msg,
580  const char* fileName = __FILE__,
581  const int lineNum = __LINE__)
582  : carma::util::ErrorException (msg.c_str(), fileName, lineNum) {};
583 
584 };
585 
592 public:
593  SQLException (const char* msg, const char* fileName = __FILE__,
594  const int lineNum = __LINE__)
595  : carma::util::ErrorException (msg, fileName, lineNum) {};
596 
597  SQLException (const std::string msg,
598  const char* fileName = __FILE__,
599  const int lineNum = __LINE__)
600  : carma::util::ErrorException (msg.c_str(), fileName, lineNum) {};
601 
602 };
603 
604 class InsertDeniedException : public carma::util::ErrorException {
605 public:
606  InsertDeniedException (const char* msg, const char* fileName = __FILE__,
607  const int lineNum = __LINE__)
608  : carma::util::ErrorException (msg, fileName, lineNum) {};
609 
610  InsertDeniedException (const std::string msg,
611  const char* fileName = __FILE__,
612  const int lineNum = __LINE__)
613  : carma::util::ErrorException (msg.c_str(), fileName, lineNum) {};
614 
615 };
616 
617 }}
618 
619 
620 #endif // CARMA_DBMS_DBCONNECTION_H
621 
Class to mimic a db table The Table class is meant to mimic a database table.
Definition: Table.h:34
virtual void repairMonitorDataTable(const std::string &tableName) const =0
attempt to repair a corrupt monitor data table
virtual void dropScratchTable(const std::string &table) const
drop a table from the scratch database
virtual void deleteFromMonitorIndexTable(const std::string &whereClause) const
delete rows from the MONITOR_INDEX_TABLE
Class used for configuring a DBConnection.
Table getLogMessages(const carma::util::frameType &start, const carma::util::frameType &end=carma::util::Time::computeCurrentFrame(), const std::string *matchString=NULL) const
get the log messages in the specified frameCount range.
virtual unsigned maxTablesPerJoin() const =0
the maximum number of tables the RDBMS allows in a join
std::string rdbmsName() const
get the name of the RDBMS associated with this object
Definition: DBConnection.h:93
virtual void loadDataFromFile(const std::string &filename, const std::string &table, const bool &useTransactions, const int &linesToIgnore=0, const std::string &columnDelimiter="\t") const =0
load data into a table from a disk file.
virtual void * getAggregate(const std::string &column, const std::string &table, const std::string &function, carma::dbms::Table::ColumnType &type) const
get the specified aggregate value of the specified column from the specified table.
Common time functions.
void closeConnection()
close the ODBC database connection
virtual carma::dbms::Table execSQLSelect(const std::string &statement, const std::string &resultTableName="RESULT TABLE") const
execute a SELECT query
carma::dbms::Table odbcResultSetToTable_(const SQLHSTMT &hstmt) const
convert an ODBC result set to a carma::dbms::Table object
virtual void rollBackTransaction() const
rollback a transaction
MonitorAggregateType
aggregate data type of the monitor[point,data file, table]
Exception class for errors.
bool tableExists(const std::string &table, const std::string &database) const
does the specified table exist in the specified database?
void freeSQLStatement_(const SQLHSTMT &hstmt) const
free an ODBC statment handle
virtual ~DBConnection()
destructor, derived classes may want to override
virtual void loadDataFromFiles(const file2TableMap &file2Table, const bool &useTransactions, const int &linesToIgnore=0, const std::string &columnDelimiter="\t") const =0
load data into tables from disk files.
virtual bool inTransaction() const
is a transaction being executed?
unsigned int frameType
Half second frames since Jan 1, 2000.
Definition: types.h:29
SQLHDBC hdbc_
connection and environment handles for ODBC interface
Definition: DBConnection.h:449
This class contains methods for accessing monitor configuration information.
std::map< std::string, std::string > file2TableMap
type to map data file names to tables they should be loaded into
Definition: DBConnection.h:58
carma::dbms::Table databaseInfo(const std::string &database="") const
get info on the database
void directSQLInsert_(const std::string &statement) const
perform a single sql insert statement directly (ie, no prepared statements
This class contains methods for accessing monitor configuration information.
an exception indicating there is a problem executing an SQL statement (more often than not indicating...
Definition: DBConnection.h:591
relationships between the monitor and dbms systems
Class to compose and execute a query of the monitor database.
static frameType computeCurrentFrame()
Get integral half-second frame since 2000, rounded down.
virtual void commitTransaction() const
commit a transaction
virtual std::string caseSensitiveSearchModifier() const
get the modifier necessary for a case-sensitive text search
Definition: DBConnection.h:412
static std::string createJoinClause(const std::string &leftTable, const std::string &rightTable, const std::string &leftColumn, const std::string &rightColumn="", const std::string &rightTableAlias="", const std::string &indexCol="")
create a (inner) join clause
void insertLogMessage(const std::string &message, const carma::util::frameType &frameCount=carma::util::Time::computeCurrentFrame()) const
add a log message to the Log table
std::string readPassword_() const
read the database user&#39;s password so that it can be sent to the ODBC driver as part of the connect st...
This class contains methods for accessing the log table in the database.
Definition: LogDatabase.h:24
virtual long getRowCount(const std::string &tableName) const
get the number of rows in the specified table
MonitorAverageType
average type of the monitor[point,data file,table]
static bool isUp(const DBConfigurator *dbconf=NULL)
is the database specified by the dbconf object up and accepting connections?
DBConnection(const carma::dbms::DBConfigurator *dbconf)
create a DBConnection object.
an exception indicating there is a problem communicating with the database
Definition: DBConnection.h:573
Exception class for errors The exception comes with a text string that can be printed or logged...
virtual void execSQLDropTable(const std::string &tableName) const
drop a table.
virtual bool isDBUp()
is the RDBMS in question up and accepting connections
void odbcInfo() const
print various details about ODBC support
void directSQLExec_(const SQLHSTMT hstmt, const std::string &statement) const
execute a single SQL statement
virtual unsigned maxColumnsPerTable() const =0
the maximum number of columns the RDBMS allows in a table
virtual void beginTransaction() const
begin a transaction
void openODBCConnection_(SQLHENV *henv, SQLHDBC *hdbc)
open a database connection using ODBC, henv_ and hdbc_ are initialized here
virtual void monitorDataTableHasBeenPopulated(const std::string &tableName, const MonitorAverageType &averageType) const
performs tasks after a monitor data table has been completely populated.
void closeODBCConnection_()
close a database connection using ODBC, henv_ and hdbc_ are freed here
an abstract class from which database queries may be launched.
Definition: DBConnection.h:46
ColumnType
describes the type of data a column holds.
Definition: Table.h:42
std::string odbcini_
odbc config file
Definition: DBConnection.h:443
creating a database connection via the static createConnection() method
Definition: DBConnection.h:539
std::string dataSource() const
is this object using the production database?
Definition: DBConnection.h:105
Table class.
virtual std::string createMonitorDataTable(const MonitorAverageType &averageType, const MonitorAggregateType &aggType, const std::string &tag, const std::string *const location=NULL) const =0
create a monitor data table
static DBConnection * createConnection(const DBConfigurator *dbconf=NULL)
connect to the carma database.
static std::string createInsertStatement(const std::string &tableName, const std::vector< std::string > columns, const std::vector< std::string > values)
create an SQL insert statement