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
Q.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
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 thisa. A functorCode:template<> class dtl::DefaultBCA<Example>
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 ...



LinkBack URL
About LinkBacks



