1 #ifndef CARMA_DBMS_TABLE_H
2 #define CARMA_DBMS_TABLE_H
59 Table(
const std::string& name);
70 inline std::string
getName()
const {
return name_;}
76 void setName(
const std::string& name) {name_ = name;}
82 inline unsigned columnCount()
const {
return indicesVector_.size();}
88 inline unsigned rowCount()
const {
return nRows_;}
219 (
const std::string& columnName)
const;
332 (
const std::string& columnName)
const;
354 (
const std::string& columnName)
const;
362 return colTypesVector_;
388 (
const std::vector<std::string>& colNames,
389 const std::string& tableName=
"subtable")
const;
394 std::vector<carma::dbms::Column<long long> > bigIntColumns_;
395 std::vector<carma::dbms::Column<double> > doubleColumns_;
396 std::vector<carma::dbms::Column<float> > floatColumns_;
397 std::vector<carma::dbms::Column<int> > intColumns_;
398 std::vector<carma::dbms::Column<std::string> > stringColumns_;
400 std::vector<carma::dbms::Column<std::string> > decimalColumns_;
401 std::vector<carma::dbms::Column<short> > shortColumns_;
403 std::vector<int> indicesVector_;
404 std::vector< ColumnType > colTypesVector_;
405 std::map<std::string, unsigned> colNames2Indices_;
406 std::vector<std::string> colNames_;
415 const std::string& columnName);
423 void getColumn_(
const std::string& columnName)
const;
433 int addElementsToPrivateColumns(
const Column<T> &col);
442 Column<T> getPrivateColumn(
const unsigned &index)
const;
447 template <
typename T >
449 Table::addElementsToPrivateColumns(
const Column< T > & col ) {
453 struct we_should_not_be_here;
455 if (
sizeof( we_should_not_be_here ) != 3 )
464 inline int Table::addElementsToPrivateColumns<int>(
const Column<int> &col) {
465 intColumns_.push_back(col);
466 colTypesVector_.push_back(Table::INT_COLUMN_TYPE);
467 return intColumns_.size();
471 inline int Table::addElementsToPrivateColumns<float>(
const Column<float> &col) {
472 floatColumns_.push_back(col);
473 colTypesVector_.push_back(Table::FLOAT_COLUMN_TYPE);
474 return floatColumns_.size();
478 inline int Table::addElementsToPrivateColumns<long long>(
const Column<long long> &col) {
479 bigIntColumns_.push_back(col);
480 colTypesVector_.push_back(Table::BIGINT_COLUMN_TYPE);
481 return bigIntColumns_.size();
485 inline int Table::addElementsToPrivateColumns<double>(
const Column<double> &col) {
486 doubleColumns_.push_back(col);
487 colTypesVector_.push_back(Table::DOUBLE_COLUMN_TYPE);
488 return doubleColumns_.size();
492 inline int Table::addElementsToPrivateColumns<short>(
const Column<short> &col) {
493 shortColumns_.push_back(col);
494 colTypesVector_.push_back(Table::SHORT_COLUMN_TYPE);
495 return shortColumns_.size();
499 inline int Table::addElementsToPrivateColumns<std::string>(
const Column<std::string> &col) {
500 stringColumns_.push_back(col);
501 colTypesVector_.push_back(Table::STRING_COLUMN_TYPE);
502 return stringColumns_.size();
512 int nElements = addElementsToPrivateColumns<T>(col);
513 indicesVector_.push_back(nElements-1);
514 colNames2Indices_[col.
getName()] = indicesVector_.size()-1;
515 colNames_.push_back(col.
getName());
522 template <
typename T >
524 Table::getPrivateColumn(
const unsigned & index )
const {
528 struct we_should_not_be_here;
530 if (
sizeof( we_should_not_be_here ) != 3 )
539 inline Column<long long>
540 Table::getPrivateColumn<long long>(
const unsigned &index)
const {
541 getColumn_(index, Table::BIGINT_COLUMN_TYPE);
542 return bigIntColumns_[indicesVector_[index]];
546 inline Column<double>
547 Table::getPrivateColumn<double>(
const unsigned &index)
const {
548 getColumn_(index, Table::DOUBLE_COLUMN_TYPE);
549 return doubleColumns_[indicesVector_[index]];
554 Table::getPrivateColumn<float>(
const unsigned &index)
const {
555 getColumn_(index, Table::FLOAT_COLUMN_TYPE);
556 return floatColumns_[indicesVector_[index]];
561 Table::getPrivateColumn<int>(
const unsigned &index)
const {
562 getColumn_(index, Table::INT_COLUMN_TYPE);
563 return intColumns_[indicesVector_[index]];
568 Table::getPrivateColumn<short>(
const unsigned &index)
const {
569 getColumn_(index, Table::SHORT_COLUMN_TYPE);
570 return shortColumns_[indicesVector_[index]];
574 inline Column<std::string>
575 Table::getPrivateColumn<std::string>(
const unsigned &index)
const {
576 getColumn_(index, Table::STRING_COLUMN_TYPE);
577 return stringColumns_[indicesVector_[index]];
587 return getColumn<T>((colNames2Indices_.find(columnName))->second);
594 return getPrivateColumn<T>(index);
613 std::ostream &
operator<<( ::std::ostream & os,
616 #endif // CARMA_DBMS_TABLE_H
Class to mimic a db table The Table class is meant to mimic a database table.
void addBigIntColumn(const Column< long long > &col)
add a big (64 bit) int column to the table
template to mimic a db column.
~Table()
Table destructor.
std::ostream & operator<<(::std::ostream &os, const carma::dbms::Table &table)
std::string getName() const
get the table name
carma::dbms::Table createSubTable(const std::vector< std::string > &colNames, const std::string &tableName="subtable") const
create a table from a subset of columns in this table
void addStringColumn(const Column< std::string > &col)
add a string column to the table
carma::dbms::Column< long long > getBigIntColumn(const unsigned &index) const
get a column containing big (64 bit) int values
void addShortColumn(const Column< short > &col)
add a short column to the table
void addIntColumn(const Column< int > &col)
add an int column to the table
void addNewColumn_(const unsigned int &columnSize, const std::string &columnName)
check that the new column has the correct size and set the number in the table
std::vector< std::string > getColumnNames() const
get the column names in this table in the order in which the columns were added
carma::dbms::Column< T > getColumn(const unsigned &index) const
template function for getting columns for generic type
carma::dbms::Column< int > getIntColumn(const unsigned &index) const
get a column containing integer values
void setName(const std::string &name)
set the table name
void addDecimalColumn(const Column< std::string > &col)
add a decimal column to the table
void addDoubleColumn(const Column< double > &col)
add a double column to the table
unsigned rowCount() const
get the number of rows in the table
std::vector< ColumnType > getColumnTypes() const
get the column types in this table in the order in which the columns were added
carma::dbms::Column< float > getFloatColumn(const unsigned &index) const
get a column containing float values
carma::dbms::Column< double > getDoubleColumn(const unsigned &index) const
get a column containing double values
static std::string columnType2String(const ColumnType &ctype)
get a string representation of the column type
carma::dbms::Column< std::string > getStringColumn(const unsigned &index) const
get a column containing string values
void getColumn_(const unsigned &index, const ColumnType &colType) const
checks to run when get*Column is called
void addColumn(const Column< T > &col)
template function for adding columns to tables
ColumnType
describes the type of data a column holds.
unsigned columnCount() const
get the number of columns
void addFloatColumn(const Column< float > &col)
add a float column to the table
std::string toString() const
get a string representation of the table
carma::dbms::Column< short > getShortColumn(const unsigned &index) const
get a column containing short values
std::string getName() const
get the column name
carma::dbms::Column< std::string > getDecimalColumn(const unsigned &index) const
get a column containing decimal values