diff --git a/types.html b/types.html new file mode 100644 index 00000000..966caeff --- /dev/null +++ b/types.html @@ -0,0 +1,224 @@ + +
++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). + +
+ 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");
+
++The built-in mappings from database type to C++ type for static binding are listed below: +
+
| Oracle Data Type | C++ Static Binding Type |
|---|---|
| NUMBER | double, int, short, unsigned long |
| CHAR, VARCHAR, VARCHAR2 | char, char[], char*, std::string |
| DATE | std::tm, std::time_t |
| BLOB | SOCI::BLOB |
| ROWID | SOCI::RowID |
| REF CURSOR | SOCI::Statement |
+ +
+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. +
+For example, the code below creates an xml document from a selected row of data from an arbitrary table: +
+ 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>";
+
+
+The following table shows the type T parameter that should be passed to
+Row::get+
| Oracle Data Type | SOCI Data Type | T Row::get<T> specializations |
|---|---|---|
| NUMBER | eDouble | double |
| NUMBER | eInteger | int |
| NUMBER | eUnsignedLong | unsigned long |
| CHAR, VARCHAR, VARCHAR2 | eString | std::string |
| DATE | eDate | std::tm, std::time_t |
+
| Oracle Data Type | SOCI Base Type |
|---|---|
| NUMBER | double |
| NUMBER | int |
| NUMBER | unsigned long |
| VARCHAR, VARCHAR2 | std::string |
| CHAR | char |
| DATE | std::tm |
+For example, SOCI's built in support for std::time_t is implemented entirely +as follows, utilizing std::tm as the Base Type: + +
+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);}
+ };
+}
+
+
+There are three required class members for a valid TypeConversion specialization:
++The following example shows how application code could extend SOCI to transparently support boost::gregorian::date: + +
+#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;
+ }
+};
+}
+
+
+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:
+
+
+ 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);
+
+
+
+