1 #ifndef CARMA_DBMS_DBCONNECTION_H
2 #define CARMA_DBMS_DBCONNECTION_H
19 #include "carma/util/Logger.h"
24 #include <unixODBC/sqlext.h>
49 friend class DBConnectionTest;
93 inline std::string
rdbmsName()
const {
return rdbmsName_;}
123 const std::string& tag,
124 const std::string *
const location=NULL)
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;
155 (
const file2TableMap& file2Table,
const bool& useTransactions,
156 const int& linesToIgnore=0,
const std::string& columnDelimiter=
"\t")
172 const std::string& table,
173 const std::string&
function,
181 virtual long getRowCount(
const std::string& tableName)
const;
194 const std::string& resultTableName=
"RESULT TABLE")
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")
315 const std::string *matchString=NULL)
const;
330 bool tableExists(
const std::string& table,
const std::string& database)
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=
"");
368 (
const std::string& joinTable,
const std::string& onClause);
377 (
const std::string& tableName,
const std::vector<std::string> columns,
378 const std::vector<std::string> values);
390 (
const std::string& tableName,
const std::vector<std::string> columns,
391 const std::string values);
426 std::string rdbmsName_;
427 std::string dataSource_;
429 std::string odbcConnectString_;
435 std::string passwordFile_;
436 log4cpp::Category& logger_;
438 std::vector<SQLHSTMT> activeStatements;
502 void directSQLExec_(
const SQLHSTMT hstmt,
const std::string& statement)
576 const int lineNum = __LINE__)
580 const char* fileName = __FILE__,
581 const int lineNum = __LINE__)
593 SQLException (
const char* msg,
const char* fileName = __FILE__,
594 const int lineNum = __LINE__)
598 const char* fileName = __FILE__,
599 const int lineNum = __LINE__)
606 InsertDeniedException (
const char* msg,
const char* fileName = __FILE__,
607 const int lineNum = __LINE__)
608 : carma::util::ErrorException (msg, fileName, lineNum) {};
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) {};
620 #endif // CARMA_DBMS_DBCONNECTION_H
Class to mimic a db table The Table class is meant to mimic a database table.
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
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.
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.
SQLHDBC hdbc_
connection and environment handles for ODBC interface
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
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...
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
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'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.
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
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.
ColumnType
describes the type of data a column holds.
std::string odbcini_
odbc config file
creating a database connection via the static createConnection() method
std::string dataSource() const
is this object using the production database?
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