diff --git a/types.html b/types.html new file mode 100644 index 00000000..966caeff --- /dev/null +++ b/types.html @@ -0,0 +1,224 @@ + + +SOCI Data Types + + + +

Data Types Overview

+ +SOCI supports seemless access to database input and output values using C++ +data types, by mapping them internally to database types. +

+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). + +

Static Type Binding

+ +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: + +
+  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 TypeC++ Static Binding Type
NUMBERdouble, int, short, unsigned long
CHAR, VARCHAR, VARCHAR2char, char[], char*, std::string
DATEstd::tm, std::time_t
BLOBSOCI::BLOB
ROWIDSOCI::RowID
REF CURSORSOCI::Statement
+

+ +

Dynamic Resultset Binding

+ +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. +

+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() for each possible SOCI Data Type that can be returned from +ColumnProperties::getDataType(). Row::get() throws an exception of type +std::bad_cast if an incorrect type T is used. +

+ + + + + + + +
Oracle Data TypeSOCI Data TypeT Row::get<T> specializations
NUMBEReDoubledouble
NUMBEReIntegerint
NUMBEReUnsignedLongunsigned long
CHAR, VARCHAR, VARCHAR2eStringstd::string
DATEeDatestd::tm, std::time_t
+ +

Extending SOCI to support 3rd party C++ types

+ +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: +

+ + + + + + + + +
Oracle Data TypeSOCI Base Type
NUMBERdouble
NUMBERint
NUMBERunsigned long
VARCHAR, VARCHAR2std::string
CHARchar
DATEstd::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: + +(Note that no database-specific code is required.) +

+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);
+
+ + +