All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
Classes | Public Member Functions | Protected Member Functions | Protected Attributes
database_interface::PostgresqlDatabase Class Reference

#include <postgresql_database.h>

List of all members.

Classes

class  PGresultAutoPtr

Public Member Functions

bool countList (const DBClass *example, int &count, std::string where_clause) const
 Counts the number of instances of a certain type in the database.
template<typename T >
bool countList (int &count, const FilterClause clause=FilterClause()) const
 templated implementation of count list that works on filter clauses.
bool deleteFromDatabase (DBClass *instance)
 Deletes an instance of a DBClass from the database.
template<class T >
bool getList (std::vector< boost::shared_ptr< T > > &vec) const
template<class T >
bool getList (std::vector< boost::shared_ptr< T > > &vec, const FilterClause clause) const
template<class T >
bool getList (std::vector< boost::shared_ptr< T > > &vec, std::string where_clause) const
template<class T >
bool getList (std::vector< boost::shared_ptr< T > > &vec, const T &example) const
template<class T >
bool getList (std::vector< boost::shared_ptr< T > > &vec, const T &example, const FilterClause clause) const
bool insertIntoDatabase (DBClass *instance)
 Inserts a new instance of a DBClass into the database.
bool isConnected () const
 Returns true if the interface is connected to the database and ready to go.
bool loadFromDatabase (DBFieldBase *field) const
 Reads the value of one particular fields of a DBClass from the database.
 PostgresqlDatabase (std::string host, std::string port, std::string user, std::string password, std::string dbname)
 Attempts to connect to the specified database.
 PostgresqlDatabase (const PostgresqlDatabaseConfig &config)
 Attempts to connect to the specified database.
bool saveToDatabase (const DBFieldBase *field)
 Writes the value of one particular field of a DBClass to the database.
 ~PostgresqlDatabase ()
 Closes the connection to the database.

Protected Member Functions

bool begin ()
 Isses the "begin" command to the database.
bool commit ()
 Issues the "commit" command to the database.
bool deleteFromTable (std::string table_name, const DBFieldBase *key_field)
 Helper function that deletes a row from a table based on the value of the specified field.
template<class T >
bool getList (std::vector< boost::shared_ptr< T > > &vec, const T &example, std::string where_clause) const
 Retreives the list of objects of a certain type from the database.
bool getListRawResult (const DBClass *example, std::vector< const DBFieldBase * > &fields, std::vector< int > &column_ids, std::string where_clause, boost::shared_ptr< PGresultAutoPtr > &result, int &num_tuples) const
 Helper function for getList, separates SQL from (templated) instantiation.
bool getSequence (std::string name, std::string &value)
 Returns the 'currval' for the database sequence identified by name.
bool getVariable (std::string name, std::string &value) const
 Gets the text value of a given variable.
bool insertIntoTable (std::string table_name, const std::vector< const DBFieldBase * > &fields)
 Helper function for inserting an instance into the database.
void pgMDBconstruct (std::string host, std::string port, std::string user, std::string password, std::string dbname)
bool populateListEntry (DBClass *entry, boost::shared_ptr< PGresultAutoPtr > result, int row_num, const std::vector< const DBFieldBase * > &fields, const std::vector< int > &column_ids) const
 Helper function for getList, separates SQL from (templated) instantiation.
bool rollback ()
 Issues the "rollback" command to the database.

Protected Attributes

PGconnconnection_
 The PostgreSQL database connection we are using.
bool in_transaction_

Detailed Description

Definition at line 90 of file postgresql_database.h.


Constructor & Destructor Documentation

database_interface::PostgresqlDatabase::PostgresqlDatabase ( std::string  host,
std::string  port,
std::string  user,
std::string  password,
std::string  dbname 
)

Attempts to connect to the specified database.

Definition at line 82 of file postgresql_database.cpp.

Attempts to connect to the specified database.

Definition at line 75 of file postgresql_database.cpp.

Closes the connection to the database.

Definition at line 89 of file postgresql_database.cpp.


Member Function Documentation

Isses the "begin" command to the database.

Returns true if the begin query itself succeeds, false if it does not

Definition at line 114 of file postgresql_database.cpp.

Issues the "commit" command to the database.

Returns true if the commit query itself succeeds, false if it does not

Definition at line 129 of file postgresql_database.cpp.

bool database_interface::PostgresqlDatabase::countList ( const DBClass example,
int &  count,
std::string  where_clause 
) const

Counts the number of instances of a certain type in the database.

The example is used *only* to indicate instances of what class we are counting. To perform additional pruning, the where_clause is used. In the future, we might use the example to do actual decisions on counting based on the contents of the fields.

The counting is performed only on the primary key of the given class.

Definition at line 328 of file postgresql_database.cpp.

template<typename T >
bool database_interface::PostgresqlDatabase::countList ( int &  count,
const FilterClause  clause = FilterClause() 
) const [inline]

templated implementation of count list that works on filter clauses.

Definition at line 194 of file postgresql_database.h.

Deletes an instance of a DBClass from the database.

It removes the entry from the table that holds our primary key, but also from other tables that might hold our fields. Those tables have to be removed first.

Definition at line 794 of file postgresql_database.cpp.

bool database_interface::PostgresqlDatabase::deleteFromTable ( std::string  table_name,
const DBFieldBase key_field 
) [protected]

Helper function that deletes a row from a table based on the value of the specified field.

Deletes a row from a table based on the value of the specified field

Definition at line 772 of file postgresql_database.cpp.

template<class T >
bool database_interface::PostgresqlDatabase::getList ( std::vector< boost::shared_ptr< T > > &  vec,
const T &  example,
std::string  where_clause 
) const [protected]

Retreives the list of objects of a certain type from the database.

The datatype T is expected to be derived from DBClass.

The example is used only to decide which fields should be retrieved from the database. Note that the primary key field is ALWAYS retrieved; you can expect the returned list to have the primary key set. Any other fields are retrieved ONLY if they are marked with syncFromDatabase in the example.

Note that the example is not used to decide which instanced to retrieve (but only which fields* of the instances). To retrieve only certain fields, you must use the where_clause. This is not ideal, as much functionality is hidden from the user who is not exposed to SQL syntax. For those functions where the external user needs the where_clause (even if he does not know it) we are currently providing public wrappers, but that might change in the future.

The significant difference between this function and the version that reads a certain field is that this function creates new instances of the DBClass and gives them the right values of the primary key. The function that reads a certain field expects the instance of DBClass to already exist, and its primary key field to be set correctly already.

Definition at line 235 of file postgresql_database.h.

template<class T >
bool database_interface::PostgresqlDatabase::getList ( std::vector< boost::shared_ptr< T > > &  vec) const [inline]

Definition at line 159 of file postgresql_database.h.

template<class T >
bool database_interface::PostgresqlDatabase::getList ( std::vector< boost::shared_ptr< T > > &  vec,
const FilterClause  clause 
) const [inline]

Definition at line 165 of file postgresql_database.h.

template<class T >
bool database_interface::PostgresqlDatabase::getList ( std::vector< boost::shared_ptr< T > > &  vec,
std::string  where_clause 
) const [inline]

Definition at line 171 of file postgresql_database.h.

template<class T >
bool database_interface::PostgresqlDatabase::getList ( std::vector< boost::shared_ptr< T > > &  vec,
const T &  example 
) const [inline]

Definition at line 179 of file postgresql_database.h.

template<class T >
bool database_interface::PostgresqlDatabase::getList ( std::vector< boost::shared_ptr< T > > &  vec,
const T &  example,
const FilterClause  clause 
) const [inline]

Definition at line 184 of file postgresql_database.h.

bool database_interface::PostgresqlDatabase::getListRawResult ( const DBClass example,
std::vector< const DBFieldBase * > &  fields,
std::vector< int > &  column_ids,
std::string  where_clause,
boost::shared_ptr< PGresultAutoPtr > &  result,
int &  num_tuples 
) const [protected]

Helper function for getList, separates SQL from (templated) instantiation.

Creates and runs the SQL query for retrieveing the list. Has been separated from the rest of the getList function so that we can have only the part that instantiates the entries separated from the parts that speak SQL, so that we don't have to have SQL in the header of the PostgresqlDatabase class.

See the general getList(...) documentation for more details. This function will run the query, return its raw result, and populate the fields and columns_ids vectors with the fields that were retrieved and the columns in the result that they correspond to.

Definition at line 190 of file postgresql_database.cpp.

bool database_interface::PostgresqlDatabase::getSequence ( std::string  name,
std::string &  value 
) [protected]

Returns the 'currval' for the database sequence identified by name.

The value is returned as a string; caller has to convert it. Returns false if the query fails or the sequence is not found.

Definition at line 162 of file postgresql_database.cpp.

bool database_interface::PostgresqlDatabase::getVariable ( std::string  name,
std::string &  value 
) const [protected]

Gets the text value of a given variable.

Definition at line 141 of file postgresql_database.cpp.

Inserts a new instance of a DBClass into the database.

Inserts only the fields marked with writeToDatabase.

The primary key must either be inserted specifically (and thus marked with writeToDatabase) or have a default value associated with a sequence. In the latter case, the value is retrieved after insertion, and set to the primary key field of the instance.

Definition at line 647 of file postgresql_database.cpp.

bool database_interface::PostgresqlDatabase::insertIntoTable ( std::string  table_name,
const std::vector< const DBFieldBase * > &  fields 
) [protected]

Helper function for inserting an instance into the database.

Inserts into the database the fields of an instance that go into a single table.

If that table is the table of the primary key, everything is inserted normally.

If not, the *first* entry in the vector of fields is expected to be the primary key, with the value already set correctly. The table that we are inserting in is expected to have a foreign key that references the primary key field of our class.

Definition at line 541 of file postgresql_database.cpp.

Returns true if the interface is connected to the database and ready to go.

Definition at line 94 of file postgresql_database.cpp.

Reads the value of one particular fields of a DBClass from the database.

The instance of DBClass that this implicitly refers to is the *owner* of the field that is passed in. If the field that is passed in is not in the same table as the primary key, tables are joined based on the primary key which is assumed to be the foreign key in the changed field's table.

Definition at line 452 of file postgresql_database.cpp.

void database_interface::PostgresqlDatabase::pgMDBconstruct ( std::string  host,
std::string  port,
std::string  user,
std::string  password,
std::string  dbname 
) [protected]

Definition at line 63 of file postgresql_database.cpp.

bool database_interface::PostgresqlDatabase::populateListEntry ( DBClass entry,
boost::shared_ptr< PGresultAutoPtr result,
int  row_num,
const std::vector< const DBFieldBase * > &  fields,
const std::vector< int > &  column_ids 
) const [protected]

Helper function for getList, separates SQL from (templated) instantiation.

Parses a single, already instantiated entry (an instance of DBClass) from a raw database result, given the list of fields that were retrieved and their respective column ids in the result. Helper function for getList(...)

Definition at line 298 of file postgresql_database.cpp.

Issues the "rollback" command to the database.

Returns true if the rollback query itself succeeds, false if it does not

Definition at line 101 of file postgresql_database.cpp.

Writes the value of one particular field of a DBClass to the database.

The instance of DBClass that this implicitly refers to is the *owner* of the field that is passed in. If the field that is passed in is not in the same table as the primary key, tables are joined based on the primary key.

TODO: fix this so that we don't always have to join on the primary key.

Definition at line 362 of file postgresql_database.cpp.


Member Data Documentation

The PostgreSQL database connection we are using.

Definition at line 97 of file postgresql_database.h.

Definition at line 100 of file postgresql_database.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends


database_interface
Author(s): Matei Ciocarlie and Lorenz Mosenlechner
autogenerated on Wed Jan 16 2013 14:34:58