dtl


DynamicDBView <ParamObj>

Category: containers Component type: type

Description

The DynamicDBView container is a refinement of DBView used for queries where the number and types of the fields in the query is unknown until runtime. DynamicDBView specialises the DBView template to use a variant_row object to hold rows from the query. The variant_row object provides a mechanism to hold an arbitrary number of fields with values of arbitrary types. Individual fields within the row are accessed by using methods from the variant_row object. Note that if the view uses autokeys that the autokey will become part of the variant_row objects returned in the view.

Refinement of

DBView.

Associated types

None, except for those defined by DBView.

Example 1:

// Using a DynamicDBView to read rows from the database.

// Read the contents of a table and print the resulting rows
void SimpleDynamicRead() {

	// Our query will be "SELECT * FROM DB_EXAMPLE"
	DynamicDBView<> view("DB_EXAMPLE", "*");

	// NOTE: We need to construct r from the view itself since we
	// don't know what fields the table will contain.
	// We therefore make a call to the DataObj() function to have the
	// table return us a template row with the correct number of fields
	// and field types.
	// We use this construction since we can't be guaranteed that the table
	// is non-empty & we want to still display column names in this case.
	variant_row s(view.GetDataObj());

	// Print out the column names
	vector<string> colNames = s.GetNames();
	for (vector<string>::iterator name_it = colNames.begin(); name_it != colNames.end(); name_it++)
	{
		cout << (*name_it) << " ";
	}
	cout << endl;

	// Print out all rows and columns from our query
	DynamicDBView<>::select_iterator print_it = view.begin();
	for (print_it = view.begin(); print_it != view.end(); print_it++)
	{
		variant_row r = *print_it;
		for (size_t i = 0; i < r.size(); i++)
		{
			cout << r[i] << " ";
		}
		cout << endl;
	}
}

Example 2:

Example: Using a DynamicDBView to insert records into the database. Illustrates writing NULL values to the database.

// Using a DynamicDBView to insert records into the database.

// this example also shows how to set NULL fields in a variant_row

// Insert two rows into a table with unknown fields
void SimpleDynamicWrite() {
	TIMESTAMP_STRUCT paramDate = {2012, 12, 23, 0, 0, 0, 0}; 
	// Mayan DOOMSDAY! End of the Mayan 5126 year long calendar cycle starting from May 1, 3094 B.C.
	// Date is 13.13.13.0.0.0.0  4 Ahaw, 3 K'ank'in
	
	DynamicDBView<> view("DB_EXAMPLE", "*");

	DynamicDBView<>::insert_iterator write_it = view;

	// NOTE: We need to construct r from the view itself since we
	// don't know what fields the table will contain.
	// We therefore make a call to the DataObj() function to have the
	// table return us a template row with the correct number of fields
	// and field types.
	variant_row r(view.GetDataObj());

	// Prepare the number of the beast!
	// Set all fields to the value 6,
	// except for the last column which is a date and cannot
	// currently accept numeric values
	for (size_t i = 0; i < r.size()-1; i++)
	{
		 r[i] = 6;
	}
	r[i] = paramDate;  // set the Doomsdate

	// insert the number
	*write_it = r;
	write_it++;

	// Prepare the number of angels who stand before
	// the throne of God!
	// Set all fields to the value 7,
	// except for the last column which is a date and cannot
	// currently accept numeric values
	for (i = 0; i < r.size()-1; i++)
	{
		 r[i] = 7;
	}
	r[i] = paramDate;

	// insert the number
	*write_it = r;
	write_it++;

	// Insert Purgatory (the void) into the database.
	// Set all fields to NULL
	for (i = 0; i < r.size()-1; i++)
	{
		 r[i] = NullField();
	}
	r[i] = NullField();

	// insert the number
	*write_it = r;
	write_it++;

	// For more on this example - see the *REAL* DTL homepage!
}

Public base classes

DBView<variant_row, ParamObj>

Template parameters

Parameter Description Default
ParamObj The type of object that will be used to specify the postfix parameters to the DynamicDBView. DefaultParamObj<variant_row> 

 

Notation

X A type that is a model of DynamicDBView
a Object of type X
t Object of type X::value_type
p, q Object of type X::iterator

Expression semantics

Name Expression Precondition Semantics Postcondition
Main constructor
DynamicDBView(const string &tableList, 
const string &fieldList,
const string postfix = "", 
const BPA bpa_functor = DefaultBPA<ParamObj>(),
const SelVal sel_val = DefaultSelVal<variant_row>(),
const InsVal ins_val = DefaultInsVal<variant_row>(),
const IOHandler<variant_row, ParamObj> io_hand = DEFAULT_IO_HANDLER<variant_row, ParamObj>(),
DBConnection &connection = DBConnection::GetDefaultConnection(),
const KeyMode km = USE_AUTO_KEY, const string &keyList = "")
  Creates a dynamic container bound to a table, using the database connection specified in the connection object. The actual queries run againt this table are built by the select, insert, update and delete iterators (see these iterators for details) but essentially follow the following format:

"SELECT " + fieldList + " FROM " + tableList + postfix

In the SQL clause, users may also specify dynamic parameters through the use of SQL '(?)' syntax. In this case BPA is a class that is used to hold these runtime parameters and bpa_functor is a functor used to associate fields in the parameters class with parameter fields in the query. The ability to override the default SQL query is inherited from DBView with the BuildSpecialQry functor, but you will need to downcast the DBView reference to get the DynamicDBView for use in the operator()() implementation for his functor.

The km parameter specifies whether the view supports autokeys, which are used for updates and deletes. If km == USE_ALL_FIELDS, then updates and deletes will affect all objects whose DB fields are all equal to that of the referred to object in the view. If km == USE_AUTO_KEY, then updates and deletes will use the underlying DBMS's autokey mechanism so that only the referred to object is updated or deleted; however, if the underlying DBMS does not support autokeys, the key mode is coerced to USE_ALL_FIELDS.

The keyList parameter is a comma separated list of fields to be used for the key by select_update_iterator when km == USE_ALL_FIELDS.

The size of the container is 0. Rows are operated on by obtaining the appropriate iterator from the container.
Main constructor using an Args object DynamicDBView(Args args) !args.tableList.empty() Construct as above, based on the arguments in the Args object. See the description of DBView::Args for more details.  

Members

As per DBView with the following changes.

Member Where defined Description
DynamicDBView(const string &tableList, 
const string &fieldList,
const string &postfix = "", 
const BPA bpa_functor = DefaultBPA<ParamObj>(),
const SelVal sel_val = DefaultSelVal<variant_row>(),
const InsVal ins_val = DefaultInsVal<variant_row>(),
const IOHandler<variant_row, ParamObj> io_hand = DEFAULT_IO_HANDLER<variant_row, ParamObj>(),
DBConnection &connection = DBConnection::GetDefaultConnection(), const KeyMode km = USE_AUTO_KEY,
const string &keyList = "")
DynamicDBView Creates an empty DynamicDBView.
DynamicDBView(Args args) DynamicDBView Same as above based on an arguments object.
DynamicDBView<ParamObj> & operator=(const DynamicDBView<ParamObj> & &other)   Assignment operator.
void swap(DynamicDBView<ParamObj> &other)   Swap *this with other.

See also

DBView, Associative Container, Multiple Associative Container, Unique Sorted Associative Container, Multiple Sorted Associative Container,

BPA, SelVal, InsVal, DBConnection


[DTL Home]

Copyright © 2002, Michael Gradman and Corwin Joy.

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appears in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Corwin Joy and Michael Gradman make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.

This site written using the ORB. [The ORB]

1