A class that represents a row in a database table or in a query result. More...
#include <object.h>
Classes | |
class | ColumnRef |
A class that represents a reference to a column value in an Object. More... | |
Public Types | |
typedef Dv::Sql::Set < Dv::Sql::Object > | Set |
Type representing a set of rows. | |
typedef Dv::shared_ptr< Object > | Ref |
Type of (reference counted) pointer to Object. | |
Public Member Functions | |
Object (Dv::Sql::Db &db, size_t arity) | |
Create an empty object with a given arity. | |
Object (Table::Ref table) throw (std::logic_error) | |
Create an empty object associated with a given table. | |
Object (Table::Ref table, SelectExp select_exp, const std::string &rest="") throw (std::logic_error, std::runtime_error) | |
Create an object containing the first row of a "select *" statement from a database table. | |
Object (Dv::Sql::Command &q, unsigned int r=0, const Table::Ref &table=Table::Ref(0)) throw (std::logic_error, std::runtime_error) | |
Create an object containing the r'th row of the answer of a query. | |
Object (Dv::Sql::Db &db, const std::string &prefix, SelectExp select_exp, const std::string &rest="") throw (std::logic_error, std::runtime_error) | |
Create an object containing the first row of the answer of a query. | |
Object (Dv::Sql::Db &db, const std::string &query) throw (std::logic_error, std::runtime_error) | |
Create an object containing the first row of the answer of a query for which the complete text is provided. | |
Object (const Dv::Sql::Object &source, const Dv::Sql::Reference &ref) throw (std::runtime_error, std::logic_error) | |
Create an object from the result of following a reference. | |
virtual | ~Object () |
Destructor. | |
bool | set (Dv::Sql::Command &q, unsigned int r=0) throw (std::logic_error) |
Set the column values of this object to those of the r'th row of the result of a query. | |
size_t | arity () const |
Number of column values in this object. | |
ColumnRef | operator[] (size_t i) throw (std::logic_error) |
Return a reference to the i'th column in this object. | |
ColumnRef | operator[] (const std::string &col_name) throw (std::logic_error) |
Return a reference to a named column in this object. | |
ColumnRef | operator[] (const Table::Column &column) throw (std::logic_error) |
Return a reference to a column in this object. | |
virtual std::ostream & | print (std::ostream &os) const |
Print a readable representation of this object to a stream. | |
Dv::Sql::Db & | db () const |
The database connection of this object. | |
Object & | operator= (const Object &o) throw (std::logic_error) |
Assignment operator. | |
const Dv::Sql::Table::Ref | table () const throw (std::logic_error) |
Return a reference to the associated Dv::Sql::Table of this object. | |
bool | has_db_table () const |
Is this object associated with a database table? | |
Dv::Sql::Table::Ref | db_table () const throw (std::logic_error) |
Return a reference to the associated database Dv::Sql::Table of this object. | |
const Dv::Sql::Table::Column * | column (size_t i) const throw (std::logic_error) |
Retrieve column information for this object. | |
size_t | match_first () throw (std::logic_error) |
Retrieve the first row, if any, that matches the dirty column values in this object. | |
bool | refresh () throw (std::logic_error) |
Retrieve the unique row, if any, that matches the primary key column values of this object. | |
void | insert () throw (std::runtime_error) |
Insert the present object into a database, using an Sql 'insert' statement. | |
void | update () throw (std::runtime_error) |
Save dirty column values of an object to the database. | |
void | replace () throw (std::runtime_error) |
Save current state of the object to the database. | |
void | erase () throw (std::runtime_error) |
Remove the object from the database. | |
void | erase (const Dv::Sql::SelectExp &exp) const |
Remove all objects from the table that satisfy some select expression. | |
Dv::Sql::Command::Ref | match_query (const std::string &rest="") const throw (std::logic_error) |
Create (and execute) a 'select' query out of the dirty columns of this object. | |
Dv::Sql::SelectExp | match_exp () const throw (std::logic_error) |
Return a boolean expression that expresses a match with the dirty columns of this object. | |
Dv::Sql::Command::Ref | select (const Dv::Sql::SelectExp &where_exp, const std::string &rest="") const throw (std::logic_error) |
Create (and execute) a query. | |
template<typename T > | |
Object & | set (size_t i, const T &t) throw (std::logic_error) |
Set a column value for this object. | |
template<typename T > | |
Object & | set (const Table::Column &c, const T &t) throw (std::logic_error) |
Set a column value for this object. | |
template<typename T > | |
Object & | set (const std::string &col_name, const T &t) throw (std::logic_error) |
Set a column value for this object. | |
template<typename T > | |
T | get (size_t i) const throw (std::logic_error) |
Retrieve a column value for this object. | |
template<typename T > | |
T | get (const Table::Column &c) const throw (std::logic_error) |
Retrieve a column value for this object. | |
template<typename T > | |
T | get (const std::string &col_name) const throw (std::logic_error) |
Retrieve a column value for this object. | |
Set::Ref | references (const Reference &ref) const throw (std::logic_error) |
Return a reference to the set of objects obtained by following a given reference in reverse. | |
Object & | copy_data (size_t to_i, const Object &from, size_t from_i, bool dirty) throw (std::logic_error) |
Copy a column value from another object to this one. | |
const std::string & | get_data (const Table::Column &c) const |
Obtain the data value associated with this object. | |
Protected Member Functions | |
Object & | set_data (size_t i, const std::string &value, bool dirty) throw (std::logic_error) |
Set the value of column i in this object to value . | |
const std::string & | data (size_t i) const |
Obtain the data value associated with this object. | |
void | set_null (size_t i, bool new_value) throw () |
Set the null status of a column. | |
bool | null (size_t i) const throw () |
Is a column value null? | |
void | set_dirty (size_t i, bool new_value) throw () |
Set the dirty status of a column. | |
bool | dirty (size_t i) const throw () |
Is a column value dirty? | |
Private Member Functions | |
void | set_table (Dv::Sql::Table::Ref table) throw (std::logic_error) |
Change the associated table of this object. | |
Private Attributes | |
Dv::Sql::Db & | db_ |
Database connection associated with this object. | |
std::vector< bool > | dirty_ |
Dirty flags for each column value in this object. | |
std::vector< bool > | null_ |
Null flags for each column value in this object. | |
std::vector< std::string > | data_ |
Actual column data. | |
const Dv::Sql::Table::Ref | table_ |
Table of this object. | |
Friends | |
bool | operator== (const Object &o1, const Object &o2) |
Test two objects for equality. | |
bool | operator!= (const Object &o1, const Object &o2) |
Test two objects for inequality. |
A class that represents a row in a database table or in a query result.
Objects represent an intermediate abstraction level between the low level interface provided by Dv::Sql::Command and the slightly higher level interface provided by Dv::Sql::Row. Nevertheless, objects may be useful for ad-hoc queries that e.g. apply sql aggregate functions.
table
, either a database table or a query table. The former have a name and correspond to the actual relations in the database. The latter have the empty string as name and correspond to the layout of a query result. An object's table is used to translate column names to indexes in the data array of the Object. If the table is a database table, it can also be used to e.g. insert, erase, etc. the object. Definition at line 45 of file object.h.
Type representing a set of rows.
typedef Dv::shared_ptr<Object> Dv::Sql::Object::Ref |
Dv::Sql::Object::Object | ( | Dv::Sql::Db & | db, | |
size_t | arity | |||
) |
Create an empty object with a given arity.
db | database connection to be used in further SQL statements with this object | |
arity | number of column values that may be stored in this object |
Dv::Sql::Object::Object | ( | Table::Ref | table | ) | throw (std::logic_error) |
Create an empty object associated with a given table.
The object will be copy the database connection from the table.
table | reference |
std::logic_error | if table is a null reference |
Dv::Sql::Object::Object | ( | Table::Ref | table, | |
SelectExp | select_exp, | |||
const std::string & | rest = "" | |||
) | throw (std::logic_error, std::runtime_error) |
Create an object containing the first row of a "select *" statement from a database table.
The select statement has the form
table | to associate with this object | |
select_exp | where clause of the query | |
rest | of the query |
// y is the youngest customer older than 21 (and younger than 100) Dv::Sql::Object y(customer, Customer::age > 21 && Customer::age < 100, "order by age");
std::logic_error | if the query fails or table is a null reference | |
std::runtime_error | if the query does not return any answers |
Dv::Sql::Object::Object | ( | Dv::Sql::Command & | q, | |
unsigned int | r = 0 , |
|||
const Table::Ref & | table = Table::Ref(0) | |||
) | throw (std::logic_error, std::runtime_error) |
Create an object containing the r'th row of the answer of a query.
Dv::Sql::Command(db, "select amount from transaction", Transaction::acct_code = code, "order by amount"); // retrieve the amount of the transaction on the account // with the largest amount Dv::Sql::Object max_amount(q, q.nrows()-1);
q | query from whose answer the r'th row will be retrieved | |
r | number of row in query answer to be retrieved | |
table | optional database table to associate with this object If table is null, the object will be associated with the table of the query result. |
std::logic_error | if execution of the query failed | |
std::logic_error | if the table is non-null and does not have the same arity as the query answer | |
std::runtime_error | if the query answer contains less than r rows |
Dv::Sql::Object::Object | ( | Dv::Sql::Db & | db, | |
const std::string & | prefix, | |||
SelectExp | select_exp, | |||
const std::string & | rest = "" | |||
) | throw (std::logic_error, std::runtime_error) |
Create an object containing the first row of the answer of a query.
The query is composed by prefix followed by the select expression, followed by the optional rest. Example
db | database connection to be used in further queries | |
prefix | of query string | |
select_exp | condition to follow "where" | |
rest | of query string |
std::logic_error | if mysql rejects the query | |
std::runtime_error | if the query returns 0 rows |
Dv::Sql::Object::Object | ( | Dv::Sql::Db & | db, | |
const std::string & | query | |||
) | throw (std::logic_error, std::runtime_error) |
Create an object containing the first row of the answer of a query for which the complete text is provided.
Example
db | database connection to be used in further queries | |
query | text of query |
std::logic_error | if mysql rejects the query | |
std::runtime_error | if the query returns 0 rows |
Dv::Sql::Object::Object | ( | const Dv::Sql::Object & | source, | |
const Dv::Sql::Reference & | ref | |||
) | throw (std::runtime_error, std::logic_error) |
Create an object from the result of following a reference.
Specifically, a query of the following form will be constructed:
const Dv::Sql::Reference acct_cust( Database::account, (cols = &Account::customer_id), Database::customer, (cols = &Customer::id) ); Object acct(..); Object cust(acct, acct_cust); // customer owning the account // the executed query is // select * from customer where customer.id = account.customer_id
std::logic_error | if there is an error executing the query | |
std::logic_error | if the arities of the source object does not match the arity of ref.from_table | |
std::runtime_error | if the query does not return any answers |
virtual Dv::Sql::Object::~Object | ( | ) | [virtual] |
Destructor.
bool Dv::Sql::Object::set | ( | Dv::Sql::Command & | q, | |
unsigned int | r = 0 | |||
) | throw (std::logic_error) |
Set the column values of this object to those of the r'th row of the result of a query.
In addition, the object's table will be set to the table associated with the query, unless the object already has an associated database table. In the latter case, the arity of the query result must match the arity of the database table. The function will set all column values to dirty, and null values as appropriate.
q | query from which the r'th answer row will be used to set the column values of this object | |
r | the number of the row in the query result that will be used to set this object's column values. |
std::logic_error | if the object is associated with a database table and the arity of the query result does not match this table's arity. |
size_t Dv::Sql::Object::arity | ( | ) | const [inline] |
ColumnRef Dv::Sql::Object::operator[] | ( | size_t | i | ) | throw (std::logic_error) |
Return a reference to the i'th column in this object.
i | the number (0-based) of the column to refer to |
std::logic_error | if i exceeds this object's arity |
ColumnRef Dv::Sql::Object::operator[] | ( | const std::string & | col_name | ) | throw (std::logic_error) |
Return a reference to a named column in this object.
col_name | the name of the column to retrieve |
std::logic_error | if the table associated with this object does not contain a column with this name |
ColumnRef Dv::Sql::Object::operator[] | ( | const Table::Column & | column | ) | throw (std::logic_error) |
Return a reference to a column in this object.
column | the column to retrieve. Normally, a column from the table associated with this object. |
std::logic_error | if column.index exceeds the arity of this object. |
virtual std::ostream& Dv::Sql::Object::print | ( | std::ostream & | os | ) | const [virtual] |
Print a readable representation of this object to a stream.
os | stream to print to. |
Dv::Sql::Db& Dv::Sql::Object::db | ( | ) | const [inline] |
Assignment operator.
Note that both objects must have the same associated table.
o | object to be assigned |
std::logic_error | if the table of o does not match the table associated with this object. |
const Dv::Sql::Table::Ref Dv::Sql::Object::table | ( | ) | const throw (std::logic_error) |
Return a reference to the associated Dv::Sql::Table of this object.
There are two kinds of table: database tables and query tables. The former have a name and correspond to the actual relations in the database. The latter have the empty string as name and correspond to the layout of a query result. An object's table is used to translate column names to indexes in the data array of the Object. If the table is a database table, it can also be used to e.g. insert, erase, etc. the object.
std::logic_error | if the object has no associated table. |
bool Dv::Sql::Object::has_db_table | ( | ) | const |
Is this object associated with a database table?
Dv::Sql::Table::Ref Dv::Sql::Object::db_table | ( | ) | const throw (std::logic_error) |
Return a reference to the associated database Dv::Sql::Table of this object.
std::logic_error | if the object has no associated table or if the associated table is a query table. |
const Dv::Sql::Table::Column* Dv::Sql::Object::column | ( | size_t | i | ) | const throw (std::logic_error) |
Retrieve column information for this object.
i | the index (0-based) of the column for which info is to be retrieved |
std::logic_error | if the associated table is null | |
std::logic_error | if i exceeds the object's arity. |
size_t Dv::Sql::Object::match_first | ( | ) | throw (std::logic_error) |
Retrieve the first row, if any, that matches the dirty column values in this object.
Dv::Sql::Object cust; cust["name"] = "fred"; // will set dirty for "name" cust.match_first(); // first customer called "fred"
std::logic_error | iff the query execution failed |
bool Dv::Sql::Object::refresh | ( | ) | throw (std::logic_error) |
Retrieve the unique row, if any, that matches the primary key column values of this object.
The primary key information is obtained from the object's table.
Dv::Sql::Object cust; cust["name"] = "fred"; // will set dirty for "name" cust.match_first(); // first customer called "fred" cust["name"] = "jane"; cust,refresh(); // name will be "fred" again
std::logic_error | iff the query execution failed |
void Dv::Sql::Object::insert | ( | ) | throw (std::runtime_error) |
Insert the present object into a database, using an Sql 'insert' statement.
All non-null column values will be stored except for auto_inc
columns. After the insert operation, the object will be refreshed thus causing auto_inc
and default values to be available, as appropriate.
Dv::Sql::Object cust; cust.set("last_name", last_nm) .set("first_name", first_nm) .set("birth_date", Dv::Util::Date(1970, 8, 20)) .set("sex", "M") .set("email", "dvermeir@vub.ac.be") .insert();
std::runtime_error | iff executing the insert statement failed. |
void Dv::Sql::Object::update | ( | ) | throw (std::runtime_error) |
Save dirty column values of an object to the database.
This function will execute an update statement using all dirty columns. The update statement will be executed on the row in the database that has the same primary key as the current object. See the Mysql documentation for the precise semantics.
Customer::Row cust(..); cust["id"] = cust_id; cust["last_name"] = new_name; cust.update();
std::runtime_error | iff the corresponding sql statement failed. |
void Dv::Sql::Object::replace | ( | ) | throw (std::runtime_error) |
Save current state of the object to the database.
This function will execute a replace statement using all dirty columns as well as the columns in all unique keys. See the Mysql documentation for the precise semantics.
std::runtime_error | iff the corresponding sql statement failed. |
void Dv::Sql::Object::erase | ( | ) | throw (std::runtime_error) |
Remove the object from the database.
This function will execute a delete statement using the primary key columns. See the Mysql documentation for the precise semantics.
std::runtime_error | iff the corresponding sql statement failed. |
void Dv::Sql::Object::erase | ( | const Dv::Sql::SelectExp & | exp | ) | const |
Remove all objects from the table that satisfy some select expression.
The constructed sql statement uses the object's database and associated (database) table (only).
Dv::Sql::Command::Ref Dv::Sql::Object::match_query | ( | const std::string & | rest = "" |
) | const throw (std::logic_error) |
Create (and execute) a 'select' query out of the dirty columns of this object.
rest | optional suffix of the query |
std::logic_error | iff executing the query fails. |
Dv::Sql::SelectExp Dv::Sql::Object::match_exp | ( | ) | const throw (std::logic_error) |
Return a boolean expression that expresses a match with the dirty columns of this object.
Dv::Sql::Table::Ref customer; Dv::Sql::Object cust(customer); cust["first_name"] = "fred"; Dv::Sql::SelectExp e = cust.match_exp(); // Get all customers called "fred" that are older than 21 Dv::Sql::Object::Set freds(customer, e && (customer.age > 21));
Dv::Sql::Command::Ref Dv::Sql::Object::select | ( | const Dv::Sql::SelectExp & | where_exp, | |
const std::string & | rest = "" | |||
) | const throw (std::logic_error) |
Create (and execute) a query.
The query has the form
select * from table_name where where_exp rest
where_exp | to use in the query | |
rest | of the text of the query |
std::logic_error | iff executing the query fails. |
Object& Dv::Sql::Object::set | ( | size_t | i, | |
const T & | t | |||
) | throw (std::logic_error) [inline] |
Set a column value for this object.
i | index (0-based) of the column | |
t | value to store |
std::logic_error | if i exceeds the object's arity | |
std::logic_error | if t cannot be converted to a string using Dv::Util::tostring |
Definition at line 801 of file object.h.
References set_data().
Object& Dv::Sql::Object::set | ( | const Table::Column & | c, | |
const T & | t | |||
) | throw (std::logic_error) [inline] |
Set a column value for this object.
c | the column to set | |
t | value to store |
std::logic_error | if the index of c exceeds the object's arity | |
std::logic_error | if t cannot be converted to a string using Dv::Util::tostring |
Object& Dv::Sql::Object::set | ( | const std::string & | col_name, | |
const T & | t | |||
) | throw (std::logic_error) [inline] |
Set a column value for this object.
col_name | name of the column to set | |
t | value to store |
std::logic_error | if the object has no column with the given name | |
std::logic_error | if t cannot be converted to a string using Dv::Util::tostring |
Definition at line 830 of file object.h.
References table().
T Dv::Sql::Object::get | ( | size_t | i | ) | const throw (std::logic_error) [inline] |
T Dv::Sql::Object::get | ( | const Table::Column & | c | ) | const throw (std::logic_error) [inline] |
T Dv::Sql::Object::get | ( | const std::string & | col_name | ) | const throw (std::logic_error) [inline] |
Retrieve a column value for this object.
col_name | name of the column to retrieve |
std::logic_error | if the object has no column with the given name | |
std::logic_error | if the conversion from a string fails |
Definition at line 876 of file object.h.
References table().
Return a reference to the set of objects obtained by following a given reference in reverse.
const Dv::Sql::Reference acct_cust( Database::account, (cols = &Account::customer_id), Database::customer, (cols = &Customer::id) ); Object cust(..); // all accounts of this customer Object::Set::Ref accounts = cust.references(acct_cust);
Object& Dv::Sql::Object::copy_data | ( | size_t | to_i, | |
const Object & | from, | |||
size_t | from_i, | |||
bool | dirty | |||
) | throw (std::logic_error) |
Copy a column value from another object to this one.
If the source column is null, the target column in this object will be null.
to_i | index of target column in this object | |
from | source object for copy | |
from_i | index of source column in source object | |
dirty | the dirty attribute of the target column will be set to this value |
std::logic_error | if one of the column indexes is out of range |
const std::string& Dv::Sql::Object::get_data | ( | const Table::Column & | c | ) | const [inline] |
Obtain the data value associated with this object.
This function does not do any checking, it also does not take into account the null status of this column.
i | index (0-based) of the column to retrieve the value of |
Definition at line 917 of file object.h.
References data_, Dv::Sql::Column::index(), and null().
Object& Dv::Sql::Object::set_data | ( | size_t | i, | |
const std::string & | value, | |||
bool | dirty | |||
) | throw (std::logic_error) [protected] |
Set the value of column i
in this object to value
.
This function checks the validity of i
.
i | index (0-based) of the column to set the value of | |
value | to store in the column | |
dirty | new value of the dirty attribute of the column |
std::logic_error | if i exceeds the object's arity |
Referenced by Dv::Sql::Object::ColumnRef::operator=(), and set().
const std::string& Dv::Sql::Object::data | ( | size_t | i | ) | const [inline, protected] |
Obtain the data value associated with this object.
This function does not do any checking, it also does not take into account the null status of this column.
i | index (0-based) of the column to retrieve the value of |
Definition at line 945 of file object.h.
References data_.
void Dv::Sql::Object::set_null | ( | size_t | i, | |
bool | new_value | |||
) | throw () [protected] |
bool Dv::Sql::Object::null | ( | size_t | i | ) | const throw () [inline, protected] |
Is a column value null?
i | index (0-based) of the column to retrieve the null status for |
Definition at line 959 of file object.h.
References null_.
Referenced by get_data().
void Dv::Sql::Object::set_dirty | ( | size_t | i, | |
bool | new_value | |||
) | throw () [protected] |
bool Dv::Sql::Object::dirty | ( | size_t | i | ) | const throw () [inline, protected] |
void Dv::Sql::Object::set_table | ( | Dv::Sql::Table::Ref | table | ) | throw (std::logic_error) [private] |
Change the associated table of this object.
Note that if the object currently has a database table, this function will throw an exception.
table | new table |
std::logic_error | if the object is currently associated with a database table. |
Test two objects for equality.
The objects are equal iff they have the same associated table, their null_ arrays are equal and the corresponding non-null data_ values are also equal.
o1 | object to test for equality with o2 | |
o2 | object to test for equality with o1 |
Test two objects for inequality.
The objects are equal iff they have the same associated table, their null_ arrays are equal and the corresponding non-null data_ values are also equal.
o1 | object to test for inequality with o2 | |
o2 | object to test for inequality with o1 |
Dv::Sql::Db& Dv::Sql::Object::db_ [private] |
std::vector<bool> Dv::Sql::Object::dirty_ [private] |
std::vector<bool> Dv::Sql::Object::null_ [private] |
Null flags for each column value in this object.
Definition at line 981 of file object.h.
Referenced by null(), and Dv::Sql::Object::ColumnRef::operator==().
std::vector<std::string> Dv::Sql::Object::data_ [private] |
Actual column data.
Definition at line 983 of file object.h.
Referenced by arity(), Dv::Sql::Object::ColumnRef::convert(), data(), get(), get_data(), and Dv::Sql::Object::ColumnRef::operator==().
const Dv::Sql::Table::Ref Dv::Sql::Object::table_ [private] |
dvmysql-1.0.3 | [17 November, 2010] |