Sophie

Sophie

distrib > Fedora > 13 > i386 > by-pkgid > d4089b27bfd3289c6baf8b0975a53f9e > files > 3

poco-doc-1.3.6p1-1.fc13.i686.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>POCO Data User Guide</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="author" content="Applied Informatics Software Engineering GmbH and Contributors"/>
<meta name="publisher" content="Applied Informatics Software Engineering GmbH and Contributors"/>
<meta name="copyright" content="Copyright (c) 2009, Applied Informatics Software Engineering GmbH and Contributors"/>
<meta name="language" content="en"/>
<meta name="date" content="2009-11-24"/>
<meta name="generator" content="PocoDoc"/>
<link rel="stylesheet" href="css/styles.css" type="text/css"/>
</head>
<body bgcolor="#ffffff" leftmargin="0" topmargin="0">
<div class="header">
<h1 class="category">POCO Data Library</h1>
<h1 class="title">POCO Data User Guide</h1>
</div>
<div class="body">
<h2>Contents</h2>
<div class="toc"><ul>
<li class="level1"><a href="#0">First Steps</a></li>
<li class="level1"><a href="#1">Creating Sessions</a></li>
<li class="level1"><a href="#2">Inserting and Retrieving Data</a></li>
<li class="level2"><a href="#3">Handling NULL entries</a></li>
<li class="level1"><a href="#4">Working with Statements</a></li>
<li class="level2"><a href="#5">Prepared Statements</a></li>
<li class="level2"><a href="#6">Things NOT To Do</a></li>
<li class="level1"><a href="#7">Collection Support</a></li>
<li class="level1"><a href="#8">The limit clause</a></li>
<li class="level1"><a href="#9">Complex Data Type Mapping</a></li>
<li class="level1"><a href="#10">RecordSet</a></li>
<li class="level1"><a href="#11">Tuples</a></li>
<li class="level1"><a href="#12">Session Pooling</a></li>
</ul></div>
<div class="description">
<p></p><h2><a name="0">First Steps</a></h2><p>
POCO Data is POCO's database abstraction layer which allows users to easily send/retrieve data to/from various different SQL databases. The following complete example shows how to use it: </p>
<p></p>
<p></p>
<pre>#include &quot;Poco/Data/Common.h&quot;
#include &quot;Poco/Data/SQLite/Connector.h&quot;
#include &lt;iostream&gt;

using namespace Poco::Data;


void init()
{
    SQLite::Connector::registerConnector();
}


void shutdown()
{
    SQLite::Connector::unregisterConnector();
}


int main(int argc, char* argv[])
{
    init();
    Session ses(&quot;SQLite&quot;, &quot;sample.db&quot;);
    int count = 0;
    ses &lt;&lt; &quot;SELECT COUNT(*) FROM PERSON&quot;, into(count), now;
    std::cout &lt;&lt; &quot;People in DB &quot; &lt;&lt; count;
    shutdown();
}
</pre>
<p> </p>
<p>The above example is pretty much self explanatory. The <tt><a href="Poco.html" title="namespace Poco">Poco</a>/Data/Common.h</tt> file pulls in some common includes, the SQLite::Connector is used to register the SQLite connector so that we can later create an SQLite session via the SessionFactory. The two-argument constructor </p>
<p></p>
<pre>Sesssion ses(&quot;SQLite&quot;, &quot;sample.db&quot;);
</pre>
<p> </p>
<p>is actually equivalent to: </p>
<pre>Session ses(SessionFactory::instance()::create(&quot;SQLite&quot;, &quot;sample.db&quot;));
</pre>
<p> </p>
<p>The &lt;&lt; operator is used to send SQL statements to the Session, the <i>into(count)</i> simply informs the session where to store the result of the query. Take note of the <b>now</b> at the end of the SQL statement. It is required, otherwise the statement would not be executed.  The <i> <tt> using namespace <a href="Poco.Data.html" title="namespace Poco::Data">Poco::Data</a> </tt> </i> is for convenience only but highly recommended for good readable code  (while <i> <tt> ses &lt;&lt; &quot;SELECT COUNT(*) FROM PERSON&quot;, <a href="Poco.Data.html#2708" title="Poco::Data::into()">Poco::Data::into</a>(count), <a href="Poco.Data.html#3057" title="Poco::Data::now()">Poco::Data::now</a>; </tt> </i> is valid, it simply looks... strange). </p>
<p>The remainder of this tutorial is split up into the following parts: </p>
<ul>
<li>Creating Sessions </li>
<li>Inserting and Retrieving Data: the magic of <i> into </i> and <i>use</i> </li>
<li>Working with Statements </li>
<li>Working with Collections: vector, set, multiset, map and multimap </li>
<li>Working with Limits </li>
<li>Working with complex data types: how to map C++ objects to a database table </li>
</ul>
<p></p><h2><a name="1">Creating Sessions</a></h2><p>
Sessions are always created via the SessionFactory create method, or implicitly via the two-argument Session constructor. </p>
<p>Session create(const std::string&amp; connectorKey, const std::string&amp; connectionString);  The first parameter contains the type of the Session one wants to create. For the moment &quot;SQLite&quot; is supported  directly, and via the ODBC driver support for Oracle, SQLite, DB2, SQLServer and PostgreSQL is available.  The second parameter contains the (connector-specific) connection string. In the case of SQLite, the location of the database file is sufficient. </p>
<p></p><h2><a name="2">Inserting and Retrieving Data</a></h2><p>
Inserting data works by <i> using </i> the content of other variables. Assume we have a table that stores only forenames: </p>
<p></p>
<pre>ForeName (Name VARCHAR(30))
</pre>
<p> If we want to insert one single forename we could simply write: </p>
<p></p>
<pre>std::string aName(&quot;Peter&quot;);
ses &lt;&lt; &quot;INSERT INTO FORENAME VALUES(&quot; &lt;&lt; aName &lt;&lt; &quot;)&quot;, now;
</pre>
<p> </p>
<p>Well, we could do that, but we won't. A much better solution is to use <b>placeholders</b> and connect each placeholder via a <b>use</b>  expression with a variable that will provide the value during execution.  Placeholders are recognized by having a <b>:</b> in front of their name. Rewriting the above code now simply gives </p>
<p></p>
<pre>std::string aName(&quot;Peter&quot;);
ses &lt;&lt; &quot;INSERT INTO FORENAME VALUES(:name)&quot;, use(aName), now;
</pre>
<p> </p>
<p>In this example the <b>use</b> expression matches the <i> :name </i> with the <i>Peter</i> value.  Note that apart from the nicer syntax, the real benefit of placeholders - which is performance - doesn't show here.  Check the <i>Working with Statements</i> section to find out more. </p>
<p>Retrieving data from the Database works similar. The <b>into</b> expression matches the returned database values to  C++ objects, it also allows to provide a default value in case null data is returned from the database: </p>
<p></p>
<pre>std::string aName;
ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(aName), now; // the default is the empty string
ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(aName, &quot;default&quot;), now;
</pre>
<p> </p>
<p>It is also possible to combine into and use expressions: </p>
<p></p>
<pre>std::string aName;
std::string match(&quot;Peter&quot;)
ses &lt;&lt; &quot;SELECT NAME FROM FORENAME WHERE NAME=:name&quot;, into(aName), use(match), now;
poco_assert (aName == match);
</pre>
<p> </p>
<p>Typically, tables will not be so trivial, ie. they will have more than one column which allows for more than one into/use. Lets assume we have a Person table that contains an age, a first and a last name: </p>
<p></p>
<pre>std::string firstName(&quot;Peter&quot;;
std::string lastName(&quot;Junior&quot;);
int age = 0;
ses &lt;&lt; INSERT INTO PERSON VALUES (:fn, :ln, :age)&quot;, use(firstName), use(lastName), use(age), now;
ses &lt;&lt; &quot;SELECT (firstname, lastname, age) FROM Person&quot;, into(firstName), into(lastName), into(age), now;
</pre>
<p> </p>
<p>Most important here is the <b>order</b> of the into and use expressions. The first placeholder is matched by the first <i>use</i>,  the 2nd by the 2nd <i>use</i> etc. The same is true for the <i>into</i> statement. We select <i>firstname</i> as the first column of the result set,  thus <i>into(firstName)</i> must be the first into clause. </p>
<p></p><h3><a name="3">Handling NULL entries</a></h3><p>
A common case with databases are optional data fields that can contain NULL. To accomodate for NULL, the <i>into</i> expression allows  you to define default values. For example, assume that age is such an optional field and we want to provide as default value <b>-1</b>  which is done by writing <b>into(age, -1)</b>: </p>
<p></p>
<pre>std::string firstName(&quot;Peter&quot;;
std::string lastName(&quot;Junior&quot;);
int age = 0;
ses &lt;&lt; INSERT INTO PERSON VALUES (:fn, :ln, :age)&quot;, use(firstName), use(lastName), use(age), now;
ses &lt;&lt; &quot;SELECT (firstname, lastname, age) FROM Person&quot;, into(firstName), into(lastName), into(age, -1), now;
</pre>
<p> </p>
<p>While you can achieve the same effect by initializing age previously to -1 (<i>int age = -1</i>),  this won't work with collection types. Here you must provide the second parameter to init. Otherwise, values will be initialized to compiler specific values. </p>
<p></p><h2><a name="4">Working with Statements</a></h2><p>
We often mentioned the term <i>Statement</i> in the previous section, yet we only worked with database session objects so far,  or at least, that's what you have been made believe ;-). In reality, you have already worked with Statements. Lets take a look at the method signature of the &lt;&lt; operator at Session: </p>
<p></p>
<pre>template &lt;typename T&gt;
Statement Session::operator &lt;&lt; (const T&amp; t)
</pre>
<p> </p>
<p>Simply ignore the template stuff in front, you won't need it. The only thing that counts here is that the operator <tt> &lt;&lt; </tt> creates a  <i>Statement</i> internally and returns it. What happened in the previous examples is that the returned Statement was never assigned to a variable but simply passed on to the <i>now</i>  part which executed the statement. Afterwards the statement was destroyed. Let's take one of the previous examples and change it so that we assign the statement: </p>
<p></p>
<pre>std::string aName(&quot;Peter&quot;);
Statement stmt = ( ses &lt;&lt; &quot;INSERT INTO FORENAME VALUES(:name)&quot;, use(aName) );
</pre>
<p> Note that we must put brackets around the right part of the assignment, otherwise the compiler will complain. If you don't like the above syntax, the following alternative is equivalent: </p>
<p></p>
<pre>Statement stmt(ses);
stmt &lt;&lt; &quot;INSERT INTO FORENAME VALUES(:name)&quot;, use(aName);
</pre>
<p> </p>
<p>What did we achieve by assigning the statement to a variable? Well, currently nothing, apart that we can control when to <i>execute</i>: </p>
<p></p>
<pre>std::string aName(&quot;Peter&quot;);
Statement stmt = ( ses &lt;&lt; &quot;INSERT INTO FORENAME VALUES(:name)&quot;, use(aName) );
stmt.execute();
poco_assert (stmt.done());
</pre>
<p> </p>
<p>By calling <i>execute</i> we asserted that our query was executed and that the value was inserted. The check to <tt>stmt.done()</tt> simply guarantees that the statement was fully completed.  </p>
<p></p><h3><a name="5">Prepared Statements</a></h3><p>
A prepared statement is created by omitting the <i>now</i> clause. </p>
<p></p>
<pre>Statement stmt = ( ses &lt;&lt; &quot;INSERT INTO FORENAME VALUES(:name)&quot;, use(aName) );
</pre>
<p> </p>
<p>The advantage of a prepared statement is performance. Assume the following loop: </p>
<p></p>
<pre>std::string aName();
Statement stmt = ( ses &lt;&lt; &quot;INSERT INTO FORENAME VALUES(:name)&quot;, use(aName) );
for (int i = 0; i &lt; 100; ++i)
{
    aName.append(&quot;x&quot;);
    stmt.execute();
}
</pre>
<p> </p>
<p>Instead of creating and parsing the Statement 100 times, we only do this once and then use the placeholder in combination with the <i>use</i> clause to insert 100 different values into the database. Still, this isn't the best way to insert a collection of values into a database. </p>
<p></p><h3><a name="6">Things NOT To Do</a></h3><p>
<b>use</b> expects as input a <b>reference</b> parameter, which is bound later during execution.  Thus, one can only use variables, but never constants. The following code will very likely fail (but this is platform/compiler dependent and also depends if your  building in release or debug mode, it will work from Monday to Thursday but will always fail on Friday, so shortly spoken: the kind of bugs software developers <i>really</i> love): </p>
<p></p>
<pre>Statement stmt = (ses &lt;&lt; INSERT INTO PERSON VALUES (:fn, :ln, :age)&quot;, use(&quot;Peter&quot;), use(&quot;Junior&quot;), use(4)); //ERR!
stmt.execute();
</pre>
<p> The constant values <i>Junior</i>, <i>Peter</i> and <i>4</i> must be assigned to variables prior, otherwise their values will be invalid when execute is called. </p>
<p></p><h2><a name="7">Collection Support</a></h2><p>
If one needs to handle many values at once, one ought to use a collection class. Per default, the following collection types are supported: </p>
<ul>
<li>vector: no requirements </li>
<li>set: the &lt; operator must be supported by the datatype. Note that duplicate key/value pairs are ignored. </li>
<li>multiset: the &lt; operator must be supported by the datatype </li>
<li>map: the () operator must be supported by the datatype and return the key of the object. Note that duplicate key/value pairs are ignored. </li>
<li>multimap: the () operator must be supported by the datatype and return the key of the object </li>
</ul>
<p>A bulk insert example via vector would be: </p>
<p></p>
<pre>std::string aName(&quot;&quot;);
std::vector&lt;std::string&gt; data;
for (int i = 0; i &lt; 100; ++i)
{
    aName.append(&quot;x&quot;);
    data.push_back(aName);
}
ses &lt;&lt; &quot;INSERT INTO FORENAME VALUES(:name)&quot;, use(data), now;
</pre>
<p> </p>
<p>The same example would work with set or multiset but not with map and multimap (std::string has no () operator). Note that <b>use</b> requires <i>non-empty</i> collections! </p>
<p>Now reconsider the following example: </p>
<p></p>
<pre>std::string aName;
ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(aName), now;
</pre>
<p> </p>
<p>Previously, it worked because the table contained only one single entry but now the database table contains at least 100 strings,  yet we only offer storage space for one single result. Thus, the above code will fail and throw an exception. One possible way to handle this is: </p>
<p></p>
<pre>std::vector&lt;std::string&gt; names;
ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(names), now;
</pre>
<p> </p>
<p>And again, instead of vector, one could use set or multiset. </p>
<p></p><h2><a name="8">The limit clause</a></h2><p>
Working with collections might be convenient to bulk process data but there is also the risk that large operations will  block your application for a very long time. In addition, you might want to have better fine-grained control over your  query, e.g. you only want to extract a subset of data until a condition is met. To elevate that problem, one can use the <b>limit</b> keyword. </p>
<p>Let's assume we are retrieving thousands of rows from a database to render the data to a GUI.  To allow the user to stop fetching data any time (and to avoid having the user franatically click inside the GUI because  it doesn't show anything for seconds), we have to partition this process: </p>
<p></p>
<pre>std::vector&lt;std::string&gt; names;
ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(names), limit(50), now;
</pre>
<p> </p>
<p>The above example will retrieve up to 50 rows from the database (note that returning nothing is valid!) and <i>append</i>  it to the names collection, i.e. the collection is not cleared! If one wants to make sure that <b>exactly</b> 50 rows are returned one must set the 2nd limit parameter  (which per default is set to  <i>false</i>) to <i>true</i>: </p>
<p></p>
<pre>std::vector&lt;std::string&gt; names;
ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(names), limit(50, true), now;
</pre>
<p> </p>
<p>Iterating over a complete result collection is done via the Statement object until statement.done() returns true. For the next example, we assume that our system knows about 101 forenames: </p>
<p></p>
<pre>std::vector&lt;std::string&gt; names;
Statement stmt = (ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(names), limit(50)); 
stmt.execute(); //names.size() == 50
poco_assert (!stmt.done());
stmt.execute(); //names.size() == 100
poco_assert (!stmt.done());
stmt.execute(); //names.size() == 101
poco_assert (stmt.done()); 
</pre>
<p> </p>
<p>We previously stated that if no data is returned this is valid too. Thus, executing the following statement on an empty database table will work: </p>
<p></p>
<pre>std::string aName;
ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(aName), now;
</pre>
<p> </p>
<p>To guarantee that at least one valid result row is returned use the <b>lowerLimit</b> clause: </p>
<p></p>
<pre>std::string aName;
ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(aName), lowerLimit(1), now;
</pre>
<p> If the table is now empty, an exception will be thrown. If the query succeeds, aName is guaranteed to be initialized. Note that <b>limit</b> is only the short name for <b>upperLimit</b>. To iterate over a result set step-by-step, e.g. one wants to avoid  using a collection class, one would write: </p>
<p></p>
<pre>std::string aName;
Statement stmt = (ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(aName), lowerLimit(1), upperLimit(1));
while (!stmt.done())
    stmt.execute();
</pre>
<p> </p>
<p>And for the lazy ones, there is the <b>range</b> command: </p>
<p></p>
<pre>std::string aName;
Statement stmt = (ses &lt;&lt; &quot;SELECT NAME FROM FORENAME&quot;, into(aName), range(1,1));
while (!stmt.done())
    stmt.execute();
</pre>
<p> The third parameter to range is an optional boolean value which specifies if the upper limit is a hard limit, ie.  if the amount of rows returned by the query must match exactly. Per default exact matching is off. </p>
<p></p><h2><a name="9">Complex Data Type Mapping</a></h2><p>
All the previous examples were contented to work with only the most basic data types: integer, string, ... a situation, unlikely to occur in real-world scenarios. Assume you have a class Person: </p>
<p></p>
<pre>class Person
{
public:
    // default constructor+destr.
    // getter and setter methods for all members
    [...] 

    bool operator &lt;(const Person&amp; p) const
        /// we need this for set and multiset support
    {
        return _socialSecNr &lt; p._socialSecNr;
    }

    Poco::UInt64 operator()() const
        /// we need this operator to return the key for the map and multimap
    {
        return _socialSecNr;
    }

private:
    std::string _firstName;
    std::string _lastName;
    Poco::UInt64 _socialSecNr;
}
</pre>
<p> </p>
<p>Ideally, one would like to use a Person as simple as one used a string. All that is needed is a template specialization of the <i>TypeHandler</i>  template. Note that template specializations must be declared in the <b>same namespace</b> as the original template, i.e. <i><a href="Poco.Data.html" title="namespace Poco::Data">Poco::Data</a></i>.  The template specialization must implement the following methods: </p>
<p></p>
<pre>namespace Poco {
namespace Data {

template &lt;&gt;
class TypeHandler&lt;class Person&gt;
{
public:
    static std::size_t size()
    {
        return 3; // we handle three columns of the Table!
    }

   static void bind(std::size_t pos, const Person&amp; obj, AbstractBinder* pBinder)
    {
        poco_assert_dbg (pBinder != 0);
        // the table is defined as Person (FirstName VARCHAR(30), lastName VARCHAR, SocialSecNr INTEGER(3))
        // Note that we advance pos by the number of columns the datatype uses! For string/int this is one.
        TypeHandler&lt;std::string&gt;::bind(pos++, obj.getFirstName(), pBinder);
        TypeHandler&lt;std::string&gt;::bind(pos++, obj.getLastName(), pBinder);
        TypeHandler&lt;Poco::UInt64&gt;::bind(pos++, obj.getSocialSecNr(), pBinder);
    }

    static void prepare(std::size_t pos, const Person&amp; obj, AbstractPreparation* pPrepare)
    {
        poco_assert_dbg (pBinder != 0);
        // the table is defined as Person (FirstName VARCHAR(30), lastName VARCHAR, SocialSecNr INTEGER(3))
        // Note that we advance pos by the number of columns the datatype uses! For string/int this is one.
        TypeHandler&lt;std::string&gt;::prepare(pos++, obj.getFirstName(), pPrepare);
        TypeHandler&lt;std::string&gt;::prepare(pos++, obj.getLastName(), pPrepare);
        TypeHandler&lt;Poco::UInt64&gt;::prepare(pos++, obj.getSocialSecNr(), pPrepare);
    }

    static void extract(std::size_t pos, Person&amp; obj, const Person&amp; defVal, AbstractExtractor* pExt)
        /// obj will contain the result, defVal contains values we should use when one column is NULL
    {
        poco_assert_dbg (pExt != 0);
        std::string firstName;
        std::string lastName;
        Poco::UInt64 socialSecNr = 0;
        TypeHandler&lt;std::string&gt;::extract(pos++, firstName, defVal.getFirstName(), pExt);
        TypeHandler&lt;std::string&gt;::extract(pos++, lastName, defVal.getLastName(), pExt);
        TypeHandler&lt;Poco::UInt64&gt;::extract(pos++, socialSecNr, defVal.getSocialSecNr(), pExt);
        obj.setFirstName(firstName);
        obj.setLastName(lastName);
        obj.setSocialSecNr(socialSecNr);
    }
};

} } // namespace Poco::Data
</pre>
<p> </p>
<p>And that's all you have to do. Working with Person is now as simple as working with a string: </p>
<p></p>
<pre>std::map&lt;Poco::UInt64, Person&gt; people;
ses &lt;&lt; &quot;SELECT * FROM Person&quot;, into(people), now;
</pre>
<p> </p>
<p></p><h2><a name="10">RecordSet</a></h2><p>
The <a href="Poco.Data.RecordSet.html" title="class Poco::Data::RecordSet">Poco::Data::RecordSet</a> class provides a generic way to work with database tables. Using a <tt>RecordSet</tt>, one can: </p>
<ul>
<li>iterate over all columns and rows in a table </li>
<li>obtain meta information about columns (such as name, type, length, etc.) </li>
</ul>
<p>To work with a RecordSet, first create a Statement, execute it, and create the RecordSet from the Statement, as follows: </p>
<p></p>
<pre>Statement select(session);
select &lt;&lt; &quot;SELECT * FROM Person&quot;;
select.execute();
RecordSet rs(select);
</pre>
<p> </p>
<p>The number of rows in the RecordSet can be limited by specifying a limit for the Statement. </p>
<p>Following example demonstrates how to iterate over all rows and columns in a RecordSet: </p>
<p></p>
<pre>bool more = rs.moveFirst();
while (more)
{
    for (std::size_t col = 0; col &lt; cols; ++col)
    {
        std::cout &lt;&lt; rs[col].convert&lt;std::string&gt;() &lt;&lt; &quot; &quot;;
    }
    std::cout &lt;&lt; std::endl;
    more = rs.moveNext();
}
</pre>
<p> </p>
<p>As mentioned above, the number of rows retrieved into a RecordSet at a time can be limited using the <tt>limit</tt> or <tt>range</tt> clause. Iterating over all rows in a table a bunch of rows at a time can thus be done as follows: </p>
<p></p>
<pre>Statement select(session);
select &lt;&lt; &quot;SELECT * FROM Person&quot;, range(0, 10);
RecordSet rs(select);
while (!select.done())
{
    select.execute();
    bool more = rs.moveFirst();
    while (more)
    {
        for (std::size_t col = 0; col &lt; cols; ++col)
        {
            std::cout &lt;&lt; rs[col].convert&lt;std::string&gt;() &lt;&lt; &quot; &quot;;
        }
        std::cout &lt;&lt; std::endl;
        more = rs.moveNext();
    }
}
</pre>
<p> </p>
<p></p>
<p></p><h2><a name="11">Tuples</a></h2><p>
<a href="Poco.Tuple.html" title="struct Poco::Tuple">Poco::Tuple</a> and vectors of <a href="Poco.Tuple.html" title="struct Poco::Tuple">Poco::Tuple</a> provide a convenient way to work with rows when column types are known, because TypeHandlers for them are readily available. </p>
<p>Consider the following example: </p>
<p></p>
<pre>typedef Poco::Tuple&lt;std::string, std::string, int&gt; Person;
typedef std::vector&lt;Person&gt; People;

People people;
people.push_back(Person(&quot;Bart Simpson&quot;, &quot;Springfield&quot;, 12));
people.push_back(Person(&quot;Lisa Simpson&quot;, &quot;Springfield&quot;, 10));

Statement insert(session);
insert &lt;&lt; &quot;INSERT INTO Person VALUES(:name, :address, :age)&quot;,
    use(people), now;
</pre>
<p> </p>
<p>Of course, tuples can also be used in queries: </p>
<p></p>
<pre>Statement select(session);
select &lt;&lt; &quot;SELECT Name, Address, Age FROM Person&quot;,
    into(people),
    now;

for (People::const_iterator it = people.begin(); it != people.end(); ++it)
{
    std::cout &lt;&lt; &quot;Name: &quot; &lt;&lt; it-&gt;get&lt;0&gt;() &lt;&lt; 
        &quot;, Address: &quot; &lt;&lt; it-&gt;get&lt;1&gt;() &lt;&lt; 
        &quot;, Age: &quot; &lt;&lt; it-&gt;get&lt;2&gt;() &lt;&lt;std::endl;
}
</pre>
<p> </p>
<p></p><h2><a name="12">Session Pooling</a></h2><p>
Creating a connection to a database is often a time consuming operation. Therefore it makes sense to save a session object for  later reuse once it is no longer needed. </p>
<p>A <a href="Poco.Data.SessionPool.html" title="class Poco::Data::SessionPool">Poco::Data::SessionPool</a> manages a collection of sessions. When a session is requested, the SessionPool first looks in its set of already initialized sessions for an available object. If one is found, it is returned to the client and marked as &quot;in-use&quot;. If no session is available, the SessionPool attempts to create a new one for the client. To avoid excessive creation of sessions, a limit can be set on the maximum number of objects. </p>
<p>The following code fragment shows how to use the SessionPool: </p>
<p></p>
<pre>SessionPool pool(&quot;ODBC&quot;, &quot;...&quot;);
// ...
Session sess(pool.get());
</pre>
<p> </p>
<p>Pooled sessions are automatically returned to the pool when the Session variable holding them is destroyed. </p>
<p></p>
</div>
<p class="footer">POCO C++ Libraries 1.3.6-all<br />
Copyright &copy; 2009, <a href="http://pocoproject.org/" target="_blank">Applied Informatics Software Engineering GmbH and Contributors</a></p>
</body>
</html>