CARMA C++
Table.h
Go to the documentation of this file.
1 #ifndef CARMA_DBMS_TABLE_H
2 #define CARMA_DBMS_TABLE_H
3 
15 #include "carma/dbms/Column.h"
16 #include <map>
17 #include <string>
18 #include <vector>
19 
20 namespace carma {
21 namespace dbms {
22 
34 class Table {
35 
36 public:
42  typedef enum {
43  INT_COLUMN_TYPE,
44  SHORT_COLUMN_TYPE,
45  FLOAT_COLUMN_TYPE,
46  DOUBLE_COLUMN_TYPE,
47  STRING_COLUMN_TYPE,
48  BIGINT_COLUMN_TYPE,
49  DECIMAL_COLUMN_TYPE
50  } ColumnType;
51 
52 
53  Table();
54 
59  Table(const std::string& name);
60 
64  ~Table();
65 
70  inline std::string getName() const {return name_;}
71 
76  void setName(const std::string& name) {name_ = name;}
77 
82  inline unsigned columnCount() const {return indicesVector_.size();}
83 
88  inline unsigned rowCount() const {return nRows_;}
89 
99  template<typename T>
100  void addColumn(const Column<T> &col);
101 
111  void addBigIntColumn(const Column<long long>& col);
112 
122  void addDoubleColumn(const Column<double>& col);
123 
133  void addFloatColumn(const Column<float>& col);
134 
144  void addIntColumn(const Column<int>& col);
145 
155  void addShortColumn(const Column<short>& col);
156 
166  void addStringColumn(const Column<std::string>& col);
167 
177  void addDecimalColumn(const Column<std::string>& col);
178 
185  template<typename T>
186  carma::dbms::Column<T> getColumn(const unsigned &index) const;
187 
196  template<typename T>
197  carma::dbms::Column<T> getColumn(const std::string &columnName) const;
198 
207  carma::dbms::Column<long long> getBigIntColumn(const unsigned& index) const;
208 
219  (const std::string& columnName) const;
220 
230  carma::dbms::Column<double> getDoubleColumn(const unsigned& index) const;
231 
241  carma::dbms::Column<double> getDoubleColumn(const std::string& columnName)
242  const;
243 
253  carma::dbms::Column<float> getFloatColumn(const unsigned& index) const;
254 
264  carma::dbms::Column<float> getFloatColumn(const std::string& columnName)
265  const;
266 
276  carma::dbms::Column<int> getIntColumn(const unsigned& index) const;
277 
287  carma::dbms::Column<int> getIntColumn(const std::string& columnName)
288  const;
289 
299  carma::dbms::Column<short> getShortColumn(const unsigned& index) const;
300 
309  carma::dbms::Column<short> getShortColumn(const std::string& columnName)
310  const;
311 
321  carma::dbms::Column<std::string> getStringColumn(const unsigned& index) const;
322 
332  (const std::string& columnName) const;
333 
343  carma::dbms::Column<std::string> getDecimalColumn(const unsigned& index) const;
344 
354  (const std::string& columnName) const;
355 
361  inline std::vector< ColumnType > getColumnTypes() const {
362  return colTypesVector_;
363  }
364 
370  std::vector<std::string> getColumnNames() const { return colNames_; }
371 
375  std::string toString() const;
376 
380  static std::string columnType2String(const ColumnType & ctype);
381 
388  (const std::vector<std::string>& colNames,
389  const std::string& tableName="subtable") const;
390 
391 protected:
392  // add other column types as necesary
393 
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_;
399  // Decimals are implemented as strings.
400  std::vector<carma::dbms::Column<std::string> > decimalColumns_;
401  std::vector<carma::dbms::Column<short> > shortColumns_;
402 
403  std::vector<int> indicesVector_;
404  std::vector< ColumnType > colTypesVector_;
405  std::map<std::string, unsigned> colNames2Indices_;
406  std::vector<std::string> colNames_;
407  std::string name_;
408  unsigned nRows_;
409 
414  void addNewColumn_(const unsigned int& columnSize,
415  const std::string& columnName);
416 
420  void getColumn_(const unsigned& index, const ColumnType & colType) const;
421 
422 
423  void getColumn_(const std::string& columnName) const;
424 
425  private:
432  template<typename T>
433  int addElementsToPrivateColumns(const Column<T> &col);
434 
441  template<typename T>
442  Column<T> getPrivateColumn(const unsigned &index) const;
443 
444 };
445 
446  /* default instantiation for type not specified below */
447  template < typename T >
448  inline int
449  Table::addElementsToPrivateColumns( const Column< T > & col ) {
450  // Deliberately cause a compile-time error if code tries to use this
451  // "generic" version of the template
452 
453  struct we_should_not_be_here;
454 
455  if ( sizeof( we_should_not_be_here ) != 3 )
456  throw -11;
457 
458  return 0;
459  }
460 
461  /* instantiations of specified templates for adding elements to
462  columns */
463  template<>
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();
468  }
469 
470  template<>
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();
475  }
476 
477  template<>
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();
482  }
483 
484  template<>
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();
489  }
490 
491  template<>
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();
496  }
497 
498  template<>
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();
503  }
504 
505  /* end instantiations of templates for adding elements to columns */
506 
507 
508  /* template instantiation for adding a Column to the Table */
509  template<typename T>
510  inline void Table::addColumn(const Column<T> &col) {
511  addNewColumn_(col.size(), col.getName());
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());
516  nRows_ = col.size();
517  }
518 
519 
520  /* default instantiation of template for getting private column of
521  type not specified below */
522  template < typename T >
523  inline Column< T >
524  Table::getPrivateColumn( const unsigned & index ) const {
525  // Deliberately cause a compile-time error if code tries to use this
526  // "generic" version of the template
527 
528  struct we_should_not_be_here;
529 
530  if ( sizeof( we_should_not_be_here ) != 3 )
531  throw -13;
532 
533  return Column< T >( );
534  }
535 
536  /* instantiations for specific templates for getting columns of a
537  specific type */
538  template<>
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]];
543  }
544 
545  template<>
546  inline Column<double>
547  Table::getPrivateColumn<double>(const unsigned &index) const {
548  getColumn_(index, Table::DOUBLE_COLUMN_TYPE);
549  return doubleColumns_[indicesVector_[index]];
550  }
551 
552  template<>
553  inline Column<float>
554  Table::getPrivateColumn<float>(const unsigned &index) const {
555  getColumn_(index, Table::FLOAT_COLUMN_TYPE);
556  return floatColumns_[indicesVector_[index]];
557  }
558 
559  template<>
560  inline Column<int>
561  Table::getPrivateColumn<int>(const unsigned &index) const {
562  getColumn_(index, Table::INT_COLUMN_TYPE);
563  return intColumns_[indicesVector_[index]];
564  }
565 
566  template<>
567  inline Column<short>
568  Table::getPrivateColumn<short>(const unsigned &index) const {
569  getColumn_(index, Table::SHORT_COLUMN_TYPE);
570  return shortColumns_[indicesVector_[index]];
571  }
572 
573  template<>
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]];
578  }
579 
580  /* end instantiations for getting columns of a specific type */
581 
582  /* instantiation for template that gets a column from a name */
583  template<typename T>
585  Table::getColumn(const std::string &columnName) const {
586  getColumn_(columnName);
587  return getColumn<T>((colNames2Indices_.find(columnName))->second);
588  }
589 
590  /* instantiation for template that gets a column from an index */
591  template<typename T>
593  Table::getColumn(const unsigned &index) const {
594  return getPrivateColumn<T>(index);
595  }
596 
597 }}
598 
613  std::ostream & operator<<( ::std::ostream & os,
614  const carma::dbms::Table & table );
615 
616 #endif // CARMA_DBMS_TABLE_H
617 
Class to mimic a db table The Table class is meant to mimic a database table.
Definition: Table.h:34
void addBigIntColumn(const Column< long long > &col)
add a big (64 bit) int column to the table
template to mimic a db column.
Definition: Column.h:31
~Table()
Table destructor.
std::ostream & operator<<(::std::ostream &os, const carma::dbms::Table &table)
std::string getName() const
get the table name
Definition: Table.h:70
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
Definition: Table.h:370
carma::dbms::Column< T > getColumn(const unsigned &index) const
template function for getting columns for generic type
Definition: Table.h:593
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
Definition: Table.h:76
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
Definition: Table.h:88
std::vector< ColumnType > getColumnTypes() const
get the column types in this table in the order in which the columns were added
Definition: Table.h:361
Column template class.
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
Definition: Table.h:510
ColumnType
describes the type of data a column holds.
Definition: Table.h:42
unsigned columnCount() const
get the number of columns
Definition: Table.h:82
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
Definition: Column.h:53
carma::dbms::Column< std::string > getDecimalColumn(const unsigned &index) const
get a column containing decimal values