postgresql - C++ : Generic interface design for Database -
i have class used create connection database:
class dbhandler { public: dbhandler(); ~dbhandler(); int connect(); int execquery( string query); string getfield( int row, int col); };
now there class used fetch info database,
class dbmanager { public: dbmanager(); ~dbmanager(); //approach 1 string getusername() { //create query here , use object of dbhandler class execute it. } //approach 2 string getusername (struct querydetails& qdetails) { //create query using fields of structure , execute query using dbhandler class. } };
now here problem:
1 ) approach should follow:
- a) if use approach 1, need hard code query.
- b) if use approach 2, need fill structure each time before calling function getusername.
2 ) there better solution except these 2 generic ?
ps : definition of structure
struct querydetails { string tablename; vector<string> collist; ... };
your question broad, , elements give not permit propose objective best answer.
your approach 1 has following advantages:
- it robust , secure approach : queries written knowledge of relevant object
- if database evolve it's easy find out (text search) specific queries made tables, , updated querying code object
- if object evolves, needless say, you'll realise have change on database side
the main inconvenience, you're tightly linked database. if tomorrow change postgres else, have rewrite every query.
your approach 2 has following advantages:
- it flexible
- if database change, have change generic functions.
the inconvenience flexibility bears lot of risks maintenance: can't sure correct query send client, , impact assessment of database layout changes difficult assess.
so finally, it's decide 1 more fit needs.
i'd tend favour 1. subjective, , i'd anyway introduce additional layer make application code more independent of database system implements access database.
however, depending on need, greater flexibility of advantage. instance, if class in fact meant middle layer other calsses fetch own data, approach 2 best option.
Comments
Post a Comment