mirror of
https://github.com/allankoechke/soci.git
synced 2026-11-30 10:58:24 +00:00
basic doc on various ways SOCI maps C++ types to Oracle types
This commit is contained in:
224
types.html
Normal file
224
types.html
Normal file
@@ -0,0 +1,224 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>SOCI Data Types</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Data Types Overview</h2>
|
||||
|
||||
SOCI supports seemless access to database input and output values using C++
|
||||
data types, by mapping them internally to database types.
|
||||
<p>
|
||||
C++ native and standard library types are supported wherever possible, and SOCI
|
||||
specific types are provided in cases where there is no natural mapping from
|
||||
the database type (e.g. BLOB, RowID).
|
||||
|
||||
<h2>Static Type Binding</h2>
|
||||
|
||||
The simplest and most common way to use SOCI is to utilize static type binding -
|
||||
in this case, the database types of the columns being operated upon are known
|
||||
at compile time.
|
||||
|
||||
This can be done very concisely, e.g. for the case of inserting/selecting
|
||||
a std::string into/from a VARCHAR2 column:
|
||||
|
||||
<pre>
|
||||
Session sql("db1", "scott", "tiger");
|
||||
sql << "create table ex1(greeting varchar2(20))";
|
||||
|
||||
std::string greet_in("hello");
|
||||
sql << "insert into ex1 values(:greeting)", use(greet_in);
|
||||
|
||||
std::string greet_out;
|
||||
sql << "select greeting from ex1", into(greet_out);
|
||||
|
||||
assert(greet_out == "hello");
|
||||
</pre>
|
||||
<p>
|
||||
The built-in mappings from database type to C++ type for static binding are listed below:
|
||||
<p>
|
||||
<table border=1>
|
||||
<tr><th>Oracle Data Type</th><th>C++ Static Binding Type</th></tr>
|
||||
<tr><td>NUMBER</td><td>double, int, short, unsigned long</td></tr>
|
||||
<tr><td>CHAR, VARCHAR, VARCHAR2</td><td>char, char[], char*, std::string</td></tr>
|
||||
<tr><td>DATE</td><td>std::tm, std::time_t</td></tr>
|
||||
<tr><td>BLOB</td><td>SOCI::BLOB</td></tr>
|
||||
<tr><td>ROWID</td><td>SOCI::RowID</td></tr>
|
||||
<tr><td>REF CURSOR</td><td>SOCI::Statement</td></tr>
|
||||
</table>
|
||||
<p>
|
||||
|
||||
<h2>Dynamic Resultset Binding</h2>
|
||||
|
||||
For certain applications it is desirable to be able to select data from arbitrarily
|
||||
structured tables (e.g. via "select * ") and format the resulting data based upon its type. SOCI supports this through the SOCI::Row and SOCI::ColumnProperties classes.
|
||||
<p>
|
||||
Data is selected into a Row object, which holds ColumnProperties objects describing
|
||||
the type of data contained in each column. Once the data type for each column is known,
|
||||
the data can be formatted appropriately.
|
||||
<p>
|
||||
For example, the code below creates an xml document from a selected row of data from an arbitrary table:
|
||||
<pre>
|
||||
Session sql("db1", "scott", "tiger");
|
||||
|
||||
sql << "create table ex2(num NUMBER, str VARCHAR2(20), dt DATE)";
|
||||
|
||||
int num_in = 123;
|
||||
std::string str_in("my name");
|
||||
std::time_t dt_in = time(0);
|
||||
|
||||
sql << "insert into ex2 values(:num1, :str1, :dt1)",
|
||||
use(num_in), use(str_in), use(dt_in);
|
||||
|
||||
Row r;
|
||||
sql << "select * from ex2", into(r);
|
||||
|
||||
std::ostringstream doc;
|
||||
doc << "<row>" << std::endl;
|
||||
for(int i=0; i<r.size(); ++i)
|
||||
{
|
||||
const ColumnProperties& props = r.getProperties(i);
|
||||
doc << '<' << props.getName() << '>';
|
||||
switch(props.getDataType())
|
||||
{
|
||||
case eString:
|
||||
doc << r.get<std::string>(i);
|
||||
break;
|
||||
case eDouble:
|
||||
doc << r.get<double>(i);
|
||||
break;
|
||||
case eInteger:
|
||||
doc << r.get<int>(i);
|
||||
break;
|
||||
case eUnsignedLong:
|
||||
doc << r.get<unsigned long>(i);
|
||||
break;
|
||||
case eDate:
|
||||
std::tm when = r.get<std::tm>(i);
|
||||
doc << asctime(&when);
|
||||
break;
|
||||
}
|
||||
doc << "</" << props.getName() << '>' << std::endl;
|
||||
}
|
||||
doc << "</row>";
|
||||
</pre>
|
||||
|
||||
The following table shows the type T parameter that should be passed to
|
||||
Row::get<T>() for each possible SOCI Data Type that can be returned from
|
||||
ColumnProperties::getDataType(). Row::get<T>() throws an exception of type
|
||||
std::bad_cast if an incorrect type T is used.
|
||||
<p>
|
||||
<table border=1>
|
||||
<tr><th>Oracle Data Type</th><th>SOCI Data Type</th><th>T Row::get<T> specializations</th></tr>
|
||||
<tr><td>NUMBER</td><td>eDouble</td><td>double</tr>
|
||||
<tr><td>NUMBER</td><td>eInteger</td><td>int</tr>
|
||||
<tr><td>NUMBER</td><td>eUnsignedLong</td><td>unsigned long</tr>
|
||||
<tr><td>CHAR, VARCHAR, VARCHAR2</td><td>eString</td><td>std::string</td></tr>
|
||||
<tr><td>DATE</td><td>eDate</td><td>std::tm, std::time_t</td></tr>
|
||||
</table>
|
||||
|
||||
<h2>Extending SOCI to support 3rd party C++ types</h2>
|
||||
|
||||
SOCI can be easily extended through user code so that 3rd party types can
|
||||
be used transparently to insert into, update, and select from the database.
|
||||
|
||||
To do so, provide an appropriate implementation of the TypeConversion class
|
||||
that converts to and from one of the SOCI Base Types listed below:
|
||||
<p>
|
||||
<table border=1>
|
||||
<tr><th>Oracle Data Type</th><th>SOCI Base Type</th></tr>
|
||||
<tr><td>NUMBER</td><td>double</tr>
|
||||
<tr><td>NUMBER</td><td>int</tr>
|
||||
<tr><td>NUMBER</td><td>unsigned long</tr>
|
||||
<tr><td>VARCHAR, VARCHAR2</td><td>std::string</td></tr>
|
||||
<tr><td>CHAR</td><td>char</td></tr>
|
||||
<tr><td>DATE</td><td>std::tm</td></tr>
|
||||
</table>
|
||||
<p>
|
||||
For example, SOCI's built in support for std::time_t is implemented entirely
|
||||
as follows, utilizing std::tm as the Base Type:
|
||||
|
||||
<pre>
|
||||
namespace SOCI
|
||||
{
|
||||
template<>
|
||||
class TypeConversion<std::time_t>
|
||||
{
|
||||
public:
|
||||
typedef std::tm base_type;
|
||||
static std::time_t from(std::tm& t){return mktime(&t);}
|
||||
static std::tm to(std::time_t& t){return *localtime(&t);}
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
There are three required class members for a valid TypeConversion specialization:
|
||||
<ul>
|
||||
<li>the base_type trait, defining the base type</li>
|
||||
<li>the from() static member fuction, converting from the base type</li>
|
||||
<li>the to() static member function, converting to the base type</li>
|
||||
</ul>
|
||||
(Note that no database-specific code is required.)
|
||||
<p>
|
||||
The following example shows how application code could extend SOCI to transparently support boost::gregorian::date:
|
||||
|
||||
<pre>
|
||||
#include "boost/date_time/gregorian/gregorian.hpp"
|
||||
namespace SOCI
|
||||
{
|
||||
template<>
|
||||
class TypeConversion<boost::gregorian::date>
|
||||
{
|
||||
public:
|
||||
typedef std::tm base_type;
|
||||
static boost::gregorian::date from(std::tm& t)
|
||||
{
|
||||
boost::gregorian::months_of_year month =
|
||||
static_cast<boost::gregorian::months_of_year>(t.tm_mon + 1);
|
||||
|
||||
boost::gregorian::date d(t.tm_year + 1900, month, t.tm_mday);
|
||||
return d;
|
||||
}
|
||||
static std::tm to(boost::gregorian::date& d)
|
||||
{
|
||||
std::tm t;
|
||||
t.tm_year = d.year() - 1900;
|
||||
t.tm_mon = d.month() - 1;
|
||||
t.tm_mday = d.day();
|
||||
t.tm_hour = 0;
|
||||
t.tm_min = 0;
|
||||
t.tm_sec = 0;
|
||||
std::mktime(&t); //normalize and compute remaining fields
|
||||
|
||||
return t;
|
||||
}
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
With the above TypeConversion specialization in place, it becames possible to
|
||||
use boost::gregorian::date directly with SOCI, binding input or output, either
|
||||
statically or via a dynamic resultset:
|
||||
|
||||
<pre>
|
||||
using boost::gregorian::date;
|
||||
|
||||
Session sql("db1", "scott", "tiger");
|
||||
|
||||
sql << "create table test_boost(when DATE)";
|
||||
date d_in(1999, boost::gregorian::Feb, 10);
|
||||
|
||||
sql << "insert into test_boost values(:when)", use(d_in);
|
||||
|
||||
date d_out(boost::gregorian::not_a_date_time);
|
||||
|
||||
sql << "select when from test_boost", into(d_out);
|
||||
assert(d_in == d_out);
|
||||
|
||||
Row r;
|
||||
sql << "select * from test_boost", into(r);
|
||||
assert(r.get<date>(0) == d_in);
|
||||
</pre>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user