1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
<!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>Class Poco::Data::TypeHandler</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="namespace"><a href="Poco.Data.html" class="namespace">Poco::Data</a></h1>
<h1 class="template">template < class T ></h1>
<h1 class="symbol">class TypeHandler</h1>
</div>
<div class="body">
<p>
<b>Library:</b> Data<br />
<b>Package:</b> DataCore<br />
<b>Header:</b> Poco/Data/TypeHandler.h</p>
<h2>Description</h2>
<div class="description">
<p>Converts Rows to a Type and the other way around. Provide template specializations to support your own complex types. </p>
<p>Take as example the following (simplified) class: </p>
<pre>class Person
{
private:
std::string _lastName;
std::string _firstName;
int _age;
[....] // public set/get methods, a default constructor, optional < operator (for set, multiset) or function operator (for map, multimap)
};
</pre>
<p>The <a href="Poco.Data.TypeHandler.html" title="class Poco::Data::TypeHandler">TypeHandler</a> must provide a costum bind, size, prepare and extract method: </p>
<p></p>
<pre>template <>
class TypeHandler<struct Person>
{
public:
static std::size_t size()
{
return 3; // lastName + firstname + age occupy three columns
}
static void bind(std::size_t pos, const Person& obj, AbstractBinder* pBinder)
{
// the table is defined as Person (LastName VARCHAR(30), FirstName VARCHAR, Age INTEGER(3))
// Note that we advance pos by the number of columns the datatype uses! For string/int this is one.
poco_assert_dbg (pBinder != 0);
TypeHandler<std::string>::bind(pos++, obj.getLastName(), pBinder);
TypeHandler<std::string>::bind(pos++, obj.getFirstName(), pBinder);
TypeHandler<int>::bind(pos++, obj.getAge(), pBinder);
}
static void prepare(std::size_t pos, const Person& obj, AbstractPreparation* pPrepare)
{
// the table is defined as Person (LastName VARCHAR(30), FirstName VARCHAR, Age INTEGER(3))
poco_assert_dbg (pPrepare != 0);
TypeHandler<std::string>::prepare(pos++, obj.getLastName(), pPrepare);
TypeHandler<std::string>::prepare(pos++, obj.getFirstName(), pPrepare);
TypeHandler<int>::prepare(pos++, obj.getAge(), pPrepare);
}
static void extract(std::size_t pos, Person& obj, const Person& defVal, AbstractExtractor* pExt)
{
// defVal is the default person we should use if we encunter NULL entries, so we take the individual fields
// as defaults. You can do more complex checking, ie return defVal if only one single entry of the fields is null etc...
poco_assert_dbg (pExt != 0);
std::string lastName;
std::string firstName;
int age = 0;
// the table is defined as Person (LastName VARCHAR(30), FirstName VARCHAR, Age INTEGER(3))
TypeHandler<std::string>::extract(pos++, lastName, defVal.getLastName(), pExt);
TypeHandler<std::string>::extract(pos++, firstName, defVal.getFirstName(), pExt);
TypeHandler<int>::extract(pos++, age, defVal.getAge(), pExt);
obj.setLastName(lastName);
obj.setFirstName(firstName);
obj.setAge(age);
}
};
</pre>
<p>Note that the <a href="Poco.Data.TypeHandler.html" title="class Poco::Data::TypeHandler">TypeHandler</a> template specialization must always be declared in the namespace <a href="Poco.Data.html" title="namespace Poco::Data">Poco::Data</a>. Apart from that no further work is needed. One can now use Person with into and use clauses. </p>
</div>
<h2>Member Summary</h2>
<p><b>Member Functions: </b><a href="Poco.Data.TypeHandler.html#3135" title="Poco::Data::TypeHandler::bind()">bind</a>, <a href="Poco.Data.TypeHandler.html#3140" title="Poco::Data::TypeHandler::extract()">extract</a>, <a href="Poco.Data.TypeHandler.html#3145" title="Poco::Data::TypeHandler::prepare()">prepare</a>, <a href="Poco.Data.TypeHandler.html#3139" title="Poco::Data::TypeHandler::size()">size</a></p>
<h2>Constructors</h2>
<h2>Destructor</h2>
<h3><a name="3150">~TypeHandler</a> <img src="images/protected.gif" alt="protected" title="protected" style="vertical-align:baseline;" border="0" /> </h3>
<p class="decl">~<a href="Poco.Data.TypeHandler.html" title="class Poco::Data::TypeHandler">TypeHandler</a>();</p>
<div class="description">
<p></p>
</div>
<h2>Member Functions</h2>
<h3><a name="3135">bind</a> <img src="images/static.gif" alt="static" title="static" style="vertical-align:baseline;" border="0" /> <img src="images/inline.gif" alt="inline" title="inline" style="vertical-align:baseline;" border="0" /> </h3>
<p class="decl">static void bind(<br /> std::size_t pos,<br /> const T & obj,<br /> <a href="Poco.Data.AbstractBinder.html" title="class Poco::Data::AbstractBinder">AbstractBinder</a> * pBinder<br />);</p>
<div class="description">
<p></p>
</div>
<h3><a name="3140">extract</a> <img src="images/static.gif" alt="static" title="static" style="vertical-align:baseline;" border="0" /> <img src="images/inline.gif" alt="inline" title="inline" style="vertical-align:baseline;" border="0" /> </h3>
<p class="decl">static void extract(<br /> std::size_t pos,<br /> T & obj,<br /> const T & defVal,<br /> <a href="Poco.Data.AbstractExtractor.html" title="class Poco::Data::AbstractExtractor">AbstractExtractor</a> * pExt<br />);</p>
<div class="description">
<p></p>
</div>
<h3><a name="3145">prepare</a> <img src="images/static.gif" alt="static" title="static" style="vertical-align:baseline;" border="0" /> <img src="images/inline.gif" alt="inline" title="inline" style="vertical-align:baseline;" border="0" /> </h3>
<p class="decl">static void prepare(<br /> std::size_t pos,<br /> const T & obj,<br /> <a href="Poco.Data.AbstractPreparation.html" title="class Poco::Data::AbstractPreparation">AbstractPreparation</a> * pPrepare<br />);</p>
<div class="description">
<p></p>
</div>
<h3><a name="3139">size</a> <img src="images/static.gif" alt="static" title="static" style="vertical-align:baseline;" border="0" /> <img src="images/inline.gif" alt="inline" title="inline" style="vertical-align:baseline;" border="0" /> </h3>
<p class="decl">static std::size_t size();</p>
<div class="description">
<p></p>
</div>
<p class="footer">POCO C++ Libraries 1.3.6-all<br />
Copyright © 2009, <a href="http://pocoproject.org/" target="_blank">Applied Informatics Software Engineering GmbH and Contributors</a></p>
</div>
</body>
</html>
|