FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
June 01, 2006
Supporting Custom C++ Types

How traits allow users to easily implement arbitrary types with the SOCI database library

(Page 1 of 6)
Stephen Hutton
Traits are a powerful technique that lets you create arbitrary user-defined types, among other things.
Stephen is a Senior Software Engineer for Factor 5. He can be reached at www.featurecomplete.com.


Traits are powerful techniques with many applications to generic programming in C++. In this article, I use examples from the SOCI database library (soci.sf.net) to show how a simple TypeConversion traits class can be used to create an extensible framework that supports arbitrary user-defined types. The complete source code that implements this framework is available at http://www.ddj.com/code/.

SOCI is a database library with a minimalist syntax that mimics that of embedded SQL. (For more information, see "A Simple Oracle Call Interface" by Maciej Sobczak; www.ddj.com/184405930.) Listing One is a complete program that connects to a database (Oracle in this case), creates a table, inserts a row, and queries the table.

#include <soci.h>
#include <iostream>
#include <string>

using namespace SOCI; using std::string;

int main() { try { Session sql("oracle", "service=gen1 user=scott " "password=tiger"); sql << "create table Person(id number, name varchar2(50))"; int id(100); string name("Bjarne"); sql << "insert into Person values (:ID, :NAME)", use(id), use(name); int id2; string name2; sql << "select id, name from Person", into(id2), into(name2); assert(id2 == 100); assert(name2 == "Bjarne"); std::cout << name2 << " has id "<< id2 << std::endl; sql << "drop table person"; } catch(std::exception& e) { std::cout<<e.what()<<std::endl; } }

Listing One

To use SOCI to insert (or update) data, you first declare and initialize variables of a type appropriate to the column into which you will be inserting. Then use the free function use() to bind the variables to placeholders in your SQL statement.

Similarly, to use SOCI to select data, you define variables of an appropriate type and pass them by reference to the free function into() to have them populated with the values returned by your select statement.

1 Supporting Custom C++ Types | 2 SOCI Implementation | 3 The Problem: Supporting Additional Types | 4 Traits & TypeConversion<T> | 5 Pass-Through Version of TypeConversion | 6 Aggregate Types and Object-Relational Mapping Next Page
TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Looking for a new job? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:



    MICROSITES
    FEATURED TOPIC

    ADDITIONAL TOPICS

    INFO-LINK



     




    Techweb
    Informationweek Business Technology Network
    InformationweekInformationweek 500Informationweek 500 ConferenceInformationweek AnalyticsInformationweek Events
    Informationweek MagazineGlobal CIOIWK Government ITbMightyByte and SwitchDark Reading
    Digital LibraryIntelligent EnterpriseInternet EvolutionNetwork ComputingPlug Into The CloudDr. DobbsContentinople
    space
    TechWeb Events Network
    InteropVoiceConWeb 2.0 ExpoWeb 2.0 SummitEnterprise 2.0Mobile Business ExpoNoJitter
    Black HatGTECEnergy CampCloud ConnectGov 2.0 ExpoGov 2.0 Summit
    space
    Light Reading Communications Network
    Light ReadingLight Reading AsiaUnstrungCable Digital NewsInternet EvolutionPyramid Research
    Heavy ReadingLight Reading LiveLight Reading InsiderEthrnet ExpoTelco TVTower Technology Summit
    space
    Financial Technology Network
    Advanced TradingBank Systems and TechnologyInsurance and TechnologyWall Street and TechnologyAccelerating WallstreetBST SummitBuyside Trading SummitIT Summit
    space
    Microsoft Technology Network
    MSDNTechNetTotal IT ProTotal Dev ProNET Total Dev Pro CommunitySQL Total Dev Pro Community
    space