Using/Learning DTL for my DB connections

This is a discussion on Using/Learning DTL for my DB connections within the C++ Programming forums, part of the General Programming Boards category; Came across Database Template Libraries ... I'm deciding to use them for my DB management on my fleet program. I'm ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Using/Learning DTL for my DB connections

    Came across Database Template Libraries ... I'm deciding to use them for my DB management on my fleet program. I'm still a complete novice but i think i get the point ... I have created a file "DB_operate.h" as a start point and have copy-paste the existing code for just testing my connections to the MySQL DB i have...

    here's the file

    Code:
    #ifndef DB_OPERATE_H
    #define DB_OPERATE_H
    
    #include <DTL.h>
    using namespace dtl;
    
    // STEP 1 //// "Example" class to hold rows from our database table
    class Example 
    {
    public:                         // tablename.columnname:
    	int exampleInt;                 // DB_EXAMPLE.INT_VALUE
    	string exampleStr;              // DB_EXAMPLE.STRING_VALUE
    	double exampleDouble;           // DB_EXAMPLE.DOUBLE_VALUE
    	long exampleLong;               // DB_EXAMPLE.EXAMPLE_LONG
    	TIMESTAMP_STRUCT exampleDate;   // DB_EXAMPLE.EXAMPLE_DATE
    
    	Example(int exInt, const string &exStr, double exDouble, long exLong,
    		const TIMESTAMP_STRUCT &exDate) : exampleInt(exInt), exampleStr(exStr), 
    		exampleDouble(exDouble),exampleLong(exLong), exampleDate(exDate) { }
    };
    
    // STEP 2 ////
    // Create an association between table columns and fields in our object
    template<> class dtl::DefaultBCA<Example> 
    {
    public:
    	void operator()(BoundIOs &cols, Example &rowbuf) {
    		cols["INT_VALUE"] == rowbuf.exampleInt;
    		cols["STRING_VALUE"] == rowbuf.exampleStr;
    		cols["DOUBLE_VALUE"] == rowbuf.exampleDouble;
    		cols["EXAMPLE_LONG"] == rowbuf.exampleLong;
    		cols["EXAMPLE_DATE"] == rowbuf.exampleDate;
    	}
    }
    
    // STEP 3 & 4
    // Read the contents of the DB_EXAMPLE table and return a vector of the resulting rows
    vector<Example> ReadData() 
    {
    	// Read the data
    	vector<Example> results;     DBView<Example> view("DB_EXAMPLE");
    	DBView<Example>::select_iterator read_it = view.begin();
    
    	for( ; read_it != view.end(); read_it++) results.push_back(*read_it);
    	return results;
    }
    
    // Using a DBView to insert rows into a database
    // ... Class definitions for Example and BCAExample as per our ReadData example .....
    // Specialization of DefaultInsValidate for Example
    // This defines a business rule we wish to enforce for all Example objects before they 
    // are allowed to be inserted into the database
    template<> class dtl::DefaultInsValidate<Example> 
    {
    public:
    	bool operator()(BoundIOs &boundIOs, Example &rowbuf) 
    	{  
    		// data is valid if rowbuf.exampleStr is nonempty and rowbuf.exampleDouble is 
    		// between 0 and 100 (like a percentage)
    		return(rowbuf.exampleStr.length()>0 && rowbuf.exampleDouble >= 0.0 
    			&& rowbuf.exampleLong <= 100.0);
    	}
    };
    
    // Insert rows from the vector<Example> parameter into the database
    void WriteData(const vector<Example> &examples) 
    {
    	DBView<Example> view("DB_EXAMPLE");
    
    	// loop through vector and write Example objects to DB
    	// write_it.GetCount() records written in loop
    	DBView<Example>::insert_iterator write_it = view;
    
    	for (vector<Example>::const_iterator ex_it = examples.begin(); 	
    		ex_it != examples.end(); ex_it++, write_it++)
    	{
    			*write_it = *ex_it;
    			cout << "Writing element #" << write_it.GetCount() + 1<< endl;
    	}
    }
    
    #endif
    Q.

    1. I aim at separating this file to two files "DB_operate.h" & "DB_operate.cpp" ... What do you think should go to the .cpp file and remain on the .h file?

    2. Is this
    Code:
    template<> class dtl::DefaultBCA<Example>
    a. A functor
    b. treated as a normal function, which therefore mean if its definition goes to the .cpp, its declaration remains on the .h (example please)

    will stop here for now ...

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Anything that doesn't need to be inlined should go into a cpp file and everything else should go into a header.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by master5001 View Post
    Anything that doesn't need to be inlined should go into a cpp file and everything else should go into a header.
    By inline you mean these

    Code:
    template<> class dtl::DefaultBCA<Example> 
    template<> class dtl::DefaultInsValidate<Example>
    Should the following functions be strictly free, or can i make them member functions to my Objects which will be mapped to my tables (i.e Examples)

    Code:
    vector<Example> ReadData() 
    void WriteData(const vector<Example> &examples)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with bootstrap
    By abachler in forum Linux Programming
    Replies: 0
    Last Post: 01-23-2009, 10:41 PM
  2. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  3. Assembly Tutorials
    By JoshR in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-11-2005, 10:56 AM
  4. C Oracle Db connections
    By uffmoc in forum Tech Board
    Replies: 11
    Last Post: 07-20-2004, 03:00 AM
  5. if is faster than switch?
    By skorman00 in forum C++ Programming
    Replies: 32
    Last Post: 03-06-2004, 01:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21