00001 #ifndef DVMYSQL_TABLE_H 00002 #define DVMYSQL_TABLE_H 00003 // $Id: table.h,v 1.50 2008/03/18 19:55:40 dvermeir Exp $ 00004 #include <string> 00005 #include <vector> 00006 #include <map> 00007 #include <set> 00008 #include <iostream> 00009 #include <stdexcept> 00010 #include <dvutil/shared_ptr.h> 00011 #include <dvutil/cslisttag.h> 00012 #include <dvmysql/column.h> 00013 00014 namespace Dv { 00015 namespace Sql { 00016 00017 class Db; 00018 class CommandImpl; 00019 /** A Table object represents information about a database table 00020 * or a query result table. 00021 * 00022 * This information includes the name, the list of columns, 00023 * the primary key (also a list of columns) and a reference to 00024 * a connection to the database where the table is implemented 00025 * (for query table, the database connection is the connection 00026 * of the query command). 00027 * 00028 * Since its constructors are private, tables can only be created 00029 * by instance of its single friend class Dv::Sql::CommandImpl. 00030 * Tables should not be used directly but only through reference 00031 * counted pointers Table::Ref. 00032 * Moreover, table information cannot be changed. 00033 * @see Dv::Sql::Table::Column 00034 * @see Dv::Sql::Table::Ref 00035 */ 00036 class Table { 00037 public: 00038 friend class CommandImpl; 00039 00040 /** Type of reference-counted pointer to a Table */ 00041 typedef Dv::shared_ptr<Table> Ref; 00042 00043 /** Type of set of (reference-counted pointers) to tables */ 00044 typedef std::set<Ref> Set; 00045 00046 /** A Column contains information about a column in a table. 00047 * Since its constructor is private, Columns can only be 00048 * created by Table objects. Moreover, all data members are 00049 * constant. 00050 * @see Dv::Sql::Table 00051 */ 00052 class Column: public Dv::Sql::Column { 00053 friend class Table; 00054 public: 00055 /** Simplified representation of "type" of column. */ 00056 enum Type { ALPHA, BLOB, INTEGER, LONGINT, REAL, DATETIME, TIMESTAMP }; 00057 /** Type of list of columns. */ 00058 typedef std::vector<const Column*> List; 00059 typedef List::const_iterator const_iterator; 00060 00061 virtual ~Column() {} 00062 00063 /** A full description of a Column. 00064 * @return a full description of a column. 00065 */ 00066 std::string str() const; 00067 00068 /** Generate info on a list of columns. 00069 * @param columns the list of columns to generate info for 00070 * @return a string describing the list of columns 00071 * @see Dv::Sql::Table::Column::str 00072 */ 00073 static std::string str(const Column::List& columns); 00074 00075 /** The name of the column. */ 00076 const std::string name; 00077 /** The type of the column data. */ 00078 const Type type; 00079 /** Is this an auto_increment column? */ 00080 const bool auto_inc; 00081 /** Does this column belong to the table's primary key. */ 00082 const bool in_primary_key; 00083 /** Can the column value be null? */ 00084 const bool can_be_null; 00085 /** The table of the column. */ 00086 const Table::Ref table; 00087 private: 00088 /** Constructor. 00089 * @param table of the column 00090 * @param name of the column 00091 * @param type of the column 00092 * @param auto_inc if true, the column is auto_inc 00093 * @param in_primary_key if true, the column is part of 00094 * the table's primary key 00095 * @param null_possible if true, the column may have value null 00096 * @param index the position (0-based) of the column in 00097 * the table. 00098 */ 00099 Column(const Table::Ref& table, const std::string& name, Type type, bool auto_inc, 00100 bool in_primary_key, bool null_possible, size_t index); 00101 }; 00102 00103 /** Destructor. */ 00104 ~Table(); 00105 00106 /** The name of the table. 00107 * @return the name of the table. 00108 */ 00109 std::string name() const { return name_; } 00110 /** The database connection of the table. 00111 * For database tables, this returns a connection to the 00112 * database containing the table. For query tables, this 00113 * returns the database connection associated with the 00114 * query command. 00115 * @return the databsae connection of the table 00116 * @see Dv::Sql::Command 00117 */ 00118 Db& db() const { return const_cast<Table*>(this)->db_; } 00119 /** The list of columns of this table. 00120 * @return the list of columns of this table. 00121 */ 00122 const Column::List& columns() const { return columns_; } 00123 /** The list of columns constituting the primary key of this table. 00124 * @return the list of columns constituting the primary key of this table. 00125 */ 00126 const Column::List& primary_key() const { return pri_key_; } 00127 00128 /** Return a reference to the column with a given name 00129 * @param name of the column to find 00130 * @return the column with the given name in this table 00131 * @exception std::logic_error if the table does not have a 00132 * column with the given name. 00133 */ 00134 const Column& find(const std::string& name) const throw (std::logic_error); 00135 00136 /** Return a reference to the column with a given index 00137 * @param index of the column to find 00138 * @return the column with the given index in this table 00139 * @exception std::logic_error if @p index+1 exceeds the 00140 * number of columns in this table. 00141 * @see Dv::Sql::Table::arity 00142 */ 00143 const Column& column(size_t index) const throw (std::logic_error); 00144 /** Return a reference to the column with a given name 00145 * @param name of the column to find 00146 * @return the column with the given name in this table 00147 * @exception std::logic_error if the table does not have a 00148 * column with the given name. 00149 * @see Dv::Sql::Table::find 00150 */ 00151 const Column& column(const std::string& name) const throw (std::logic_error) { 00152 return find(name); 00153 } 00154 00155 /** The number of columns in this table. 00156 * @return the number of columns in this table. 00157 */ 00158 size_t arity() const { return columns().size(); } 00159 00160 00161 /** Print information on a table to a stream. 00162 * @param os stream to print to 00163 * @param table to print info on 00164 * @return @p os 00165 */ 00166 friend std::ostream& operator<<(std::ostream& os, const Table& table); 00167 00168 /** Return "select" clause corresponding all the columns of this table. 00169 * @param distinct if true, use "select distinct" instead of "select" 00170 * @return A string of the form 00171 * @code table for which to generate the clause 00172 * select table.column_1 , .. , table.column_n 00173 * @endcode 00174 * or 00175 * @code table for which to generate the clause 00176 * select distinct table.column_1 , .. , table.column_n 00177 * @endcode 00178 * where @p column_1, .. , @p column_n are the names 00179 * of all the columns of this table. 00180 * @see Dv::Sql::SelectExp::select 00181 */ 00182 std::string select(bool distinct = true) const; 00183 00184 private: 00185 /** Add a column to this table. 00186 * The Column object will be dynamically allocated. 00187 * @param table a reference-counted pointer to this table 00188 * @param name of the new column 00189 * @param type of the new column 00190 * @param auto_inc true iff the new column is "auto increment" 00191 * @param in_primary_key true iff the new column is part of 00192 * the table's primary key 00193 * @param can_be_null true iff the new column can have null 00194 * value 00195 * @return a pointer to the newly created column 00196 * @warning this has no effect on the database, only on 00197 * the description of a table. 00198 * @see Dv::Sql::Table::Column 00199 */ 00200 const Column* push_back(const Table::Ref& table, 00201 const std::string& name, Column::Type type, bool auto_inc, bool in_primary_key, 00202 bool can_be_null); 00203 00204 /** The name of the table. */ 00205 std::string name_; 00206 /** A connection to the database of this table. */ 00207 Db& db_; 00208 /** The columns of this table. */ 00209 Column::List columns_; 00210 /** The columns constituting the primary key of this table. */ 00211 Column::List pri_key_; 00212 /** The copy ctor is forbidden */ 00213 Table(const Table&); 00214 00215 /** Constructor. 00216 * @param name of the table 00217 * @param db database connection of table 00218 */ 00219 explicit Table(const std::string& name, Db& db): name_(name), db_(db) {} 00220 }; 00221 00222 /** Print a short identification of this column. 00223 * Currently, this prints a string of the form 00224 * @code 00225 * table_name . column_name 00226 * @endcode 00227 * @param os stream to print on 00228 * @param c column to print 00229 * @warning this function should perhaps depend on the DBMS. 00230 */ 00231 std::ostream& operator<<(std::ostream& os, const Table::Column& c); 00232 00233 } 00234 } 00235 #endif
dvmysql-1.0.3 | [17 November, 2010] |