dtl


DTL_TABLE and DTL_STRUCT

Category: utilities Component type: type

Description

The DTL_TABLE and DTL_STRUCT macros provide a quick way to generate C++ structures and bind them to a database table. The DTL_STRUCT macro takes a table name, field types and field names as arguments. Given these, it creates a C++ structure with these field names as members and creates a BCA that associates the members in the struct to fields with the same name in a database table. The DTL_TABLE macro calls the DTL_STRUCT macro, once the basic struct has been created it then creates an actual DBView to access the records. Both macros require you to specify the number of fields in the table just afer the macro name: DTL_TABLE4 would indicate that there are 4 fields in the table, for example. These macros are best used at the global level or as part of a class defintion. These macros cannot be used locally in a function because they would create a locally defined struct and 14.3.1 [temp.arg.type] in the C++ standard does not allow programs to pass locally defined structures as template paramters.

Definition

Defined in the table.h header file.

Here is conceptually what these macros do:



#define DTL_BIND_FIELD(FIELD) 

	cols[_TEXT( #FIELD )] >> row. ## FIELD



#define DTL_STRUCTN(STRUCT_NAME,TYPE1,FIELD1,TYPE2,FIELD2,...,TYPEN, FIELDN) 		

														

      //Create struct with all the field names as given above	

      struct STRUCT_NAME { 										

        TYPE1 FIELD1; 											

        TYPE2 FIELD2; 											

        ...													

        TYPEN FIELDN;											

      }; 													

														

      //Declare a BCA to associate members in the structure with fields in the database of the same name	

      BEGIN_DTL_NAMESPACE 										

      template<> class DefaultBCA<struct_name> 							

      { 												

      public:												

            void operator()(BoundIOs &cols, STRUCT_NAME &row) 				

            { 												

                  DTL_BIND_FIELD(FIELD1); 							

                  DTL_BIND_FIELD(FIELD2); 							

                  ...											

                  DTL_BIND_FIELD(FIELDN); 							

                  } 												

            };													

      END_DTL_NAMESPACE 										

														

      dtl::tostream &operator<<(dtl::tostream &o, const STRUCT_NAME &s) 		

      { 													

        //Basic ostream operator for the class	

      }; 													

														

      bool operator<(const STRUCT_NAME &lhs, const STRUCT_NAME &rhs) { 			

        //Basic operator < for the class, compare fields in the order they are declared					

      } 													





	

#define DTL_TABLEN(TABLE_NAME,TYPE1,FIELD1,TYPE2,FIELD2,...,TYPEN,FIELDN)

  //Define a structure named after the TABLE_NAME provided by the user 

  //The structure will be named TABLE_NAME_row to indicate that this structure holds a row of data from the table 

  DTL_STRUCTN(TABLE_NAME ## _row, TYPE1,FIELD1,TYPE2,FIELD2,...TYPEN,FIELDN); 



  //Define a type for the DBView against the requested table

  //The general form will be TABLE_NAME_view 

  typedef dtl::DBView<TABLE_NAME ## _row> TABLE_NAME ## _view;



  //Create an actual instance of the DBView.  Name this after the TABLE_NAME.  

  TABLE_NAME ## _view TABLE_NAME(_TEXT( #TABLE_NAME ))

													

Refinement of

None.

Associated types

None.

Example:

// Generate a simple structure to read data from a table called 'db_example' 

// with five fields called 'int_value', 'string_value', 'double_value', 'example_long' and 'example_date'.

// Note: the macro must be invoked at namespace scope because templates are 

// not allowed  to take locally declared classes as template parameters.  

// See [temp.arg.type] 14.3.1  in the C++ standard for details.



DTL_TABLE5(db_example,

   int, int_value,

   std::string, string_value,

   double, double_value,

   long, example_long,

   jtime_c, example_date

);



//Note that the field names in the table are the same as the member names in the structure

vector<db_example_row> ReadData()

{

   cout << "Read rows from the database: " << endl;

   vector<db_example_row> results;



   for (db_example_view::select_iterator read_it = db_example.begin();

          read_it  != db_example.end(); ++read_it)

   {

       cout << read_it->int_value << " " 

                 << read_it->string_value << " " 

                 << read_it->double_value << " "

                 << read_it->example_long << " " 

                 << read_it->example_date   

                 << endl;



       results.push_back(*read_it);

   }



   return results;

}

Public Base Classes

None.

Notation

None.

Expression semantics

None.

Notes

None.

See also

None.


[DTL Home]

Copyright © 2002, Michael Gradman and Corwin Joy.

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appears in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Corwin Joy and Michael Gradman make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.

SourceForge Logo

This site written using the ORB. [The ORB]