Thread: [Cppcms-users] Is dbixx::row available in an associative arary?
Brought to you by:
artyom-beilis
From: augustin <aug...@ov...> - 2010-12-13 08:43:35
|
Hello, http://art-blog.no-ip.info/wikipp/en/page/ref_dbixx_row It's not too clear from the documentation above if the data in dbixx::row is available from an associative array. dbixx::result res; sql<<"SELECT name,age,birthday " "FROM person ", res; dbixx::row myRow; while (res.next(myRow)) { // Can we do something like the following? std::string name = myRow["name"]; // ... } My problem is that I want to pass the row data into my classes' constructors. Currently, I must declare temporary variables on the stack, extract the row data into those variables, and pass them to my object constructors: dbixx::row myRow; while (res.next(myRow)) { std::string tmp_name; int tmp_age, tmp_birthday; myRow >> tmp_name >> tmp_age >> tmp_birthday; // Call my class constructor: myClass person = myClass(tmp_name, tmp_age, tmp_birthday); } I'd be neater if we could do something like this: dbixx::row myRow; while (res.next(myRow)) { // Call my class constructor: myClass person = myClass(myRow["name"], myRow["age"], myRow["birthday"]); } I know there is the method fetch(): bool fetch(int idx,int &value); but again, it requires temporary variables to be created, adding to code bloat and overheads... :-/ Or how do you handle such things? Thanks, Augustin. -- Friends: http://www.reuniting.info/ http://activistsolutions.org/ My projects: http://astralcity.org/ http://3enjeux.overshoot.tv/ http://linux.overshoot.tv/ http://overshoot.tv/ http://charityware.info/ http://masquilier.org/ http://openteacher.info/ http://minguo.info/ http://www.wechange.org/ http://searching911.info/ . |
From: Artyom <art...@ya...> - 2010-12-13 09:30:58
|
> > Hello, > > > http://art-blog.no-ip.info/wikipp/en/page/ref_dbixx_row > It's not too clear from the documentation above if the data in dbixx::row is > available from an associative array. > > dbixx::result res; > sql<<"SELECT name,age,birthday " > "FROM person ", res; > > dbixx::row myRow; > while (res.next(myRow)) { > // Can we do something like the following? > std::string name = myRow["name"]; > // ... > } No you can't. The reason is simple - what type should operator[] should return? Today operator[] overloaded to check if the variable is null. > > My problem is that I want to pass the row data into my classes' constructors. > > Currently, I must declare temporary variables on the stack, extract the row > data into those variables, and pass them to my object constructors: > > > dbixx::row myRow; > while (res.next(myRow)) { > std::string tmp_name; > int tmp_age, tmp_birthday; > myRow >> tmp_name >> tmp_age >> tmp_birthday; > // Call my class constructor: > myClass person = myClass(tmp_name, tmp_age, tmp_birthday); > } > > I'd be neater if we could do something like this: > > > > dbixx::row myRow; > while (res.next(myRow)) { > // Call my class constructor: > myClass person = myClass(myRow["name"], myRow["age"], myRow["birthday"]); > } > > I know there is the method fetch(): > bool fetch(int idx,int &value); > but again, it requires temporary variables to be created, adding to code bloat > > and overheads... :-/ > > > Or how do you handle such things? > > Thanks, > > Augustin. > I've added an option to fetch a value by column index (not name) as r.get<std::string>(1), take a look on the version in trunk. However... I'd suggest to take a look on cppdb - this is a new project drop in replacement of dbixx, unlike dbixx it does not depend on libdbi library but rather implements all by its own. Its API is different from dbixx but conversion is mostly mechanical. There you can do: cppdb::result r = sql << "SELECT name,age,birthday FROM users"; while(r.next()) { myClass person = myClass(r.get<std::string>("name"), r.get<int>("age"), r.get<std::tm>("birthday")); } You may check the beta version at: https://cppcms.svn.sourceforge.net/svnroot/cppcms/cppdb/trunk The documentation is avalible via Doxygen. So after checkout run doxygen and take a look on the file html/index.html for docs. i.e.: svn co https://cppcms.svn.sourceforge.net/svnroot/cppcms/cppdb/trunk cppdb cd cppdb doxygen cd html firefox index.html It will be officially released withing a week (only some last toches on documentation should be done) Regards, Artyom |
From: augustin <aug...@ov...> - 2010-12-15 07:25:18
|
On Monday 13 December 2010 05:30:51 pm Artyom wrote: > I'd suggest to take a look on cppdb - this is a new project > drop in replacement of dbixx, unlike dbixx it does not depend on > libdbi library but rather implements all by its own. Its API > is different from dbixx but conversion is mostly mechanical. > > There you can do: > > cppdb::result r = sql << "SELECT name,age,birthday FROM users"; > while(r.next()) { > myClass person = myClass(r.get<std::string>("name"), > r.get<int>("age"), r.get<std::tm>("birthday")); > } Thank you Artyom for the code sample. I'll upgrade to cppdb very soon and try it out. Augustin. -- Friends: http://www.reuniting.info/ http://activistsolutions.org/ My projects: http://astralcity.org/ http://3enjeux.overshoot.tv/ http://linux.overshoot.tv/ http://overshoot.tv/ http://charityware.info/ http://masquilier.org/ http://openteacher.info/ http://minguo.info/ http://www.wechange.org/ http://searching911.info/ . |
From: Mario P. <mp...@us...> - 2010-12-13 12:44:51
|
2010-12-13 10:30, Artyom wrote: [...] > > I'd suggest to take a look on cppdb - this is a new project > drop in replacement of dbixx, unlike dbixx it does not depend on > libdbi library but rather implements all by its own. Its API > is different from dbixx but conversion is mostly mechanical. > [...] Hello, I have found that libdbi is quite slow (at least using MySQL) and I have evidence that compared with a native implementation (libmysqlclient) where libdbi is up to 4 times slower in returning the results of a series of querys. I wonder if this is the reason you have decided to implement cppdb, and whether you are going to implement these using the native APIs (ex: libmysqlclient in the case of MySQL) and what other databases are supported or plan to support? Thanks and regards. Mario |
From: Artyom <art...@ya...> - 2010-12-13 13:13:04
|
> > Hello, > > I have found that libdbi is quite slow (at least using MySQL) and I have > evidence that compared with a native implementation (libmysqlclient) where > libdbi is up to 4 times slower in returning the results of a series of querys. Make sure that everything is compiled in "release" mode - i.e. take a look if during compilation "-O2" or "-O3" flag is given for both libdbi and and dbixx (if you are using the last one). AFAIK libdbi uses mysql client and AFAIK does it quite well. I did once comparisons: http://art-blog.no-ip.info/cppcms/blog/post/12 and it was fine. However there still may be some corner cases that are not handled well. > > I wonder if this is the reason you have decided to implement cppdb, > Yes and not. Several reasons: 1. I wanted to work with prepared statements by default (wich have performance impact) 2. I want to have an ability to cache them (and also have connection pool) 3. I need some better API (for example libdbi date-time support is quite bad)/ > and whether > you are going to implement these using the native APIs (ex: libmysqlclient in > the case of MySQL) and what other databases are supported or plan to support? First of all I had already implemented :-) Second I support natively: - mysql via libmysqlclient) - sqlite3 via native sqlite3 library) - postgresql via official libpq - odbc for any other databases (tested with the three above and MS SQL Server)/ > Thanks and regards. > > Mario Best, Artyom P.S: If you are going to test cppdb performance via libmysql client do not forget the cppdb uses prepared statements by default and they do not use queries cache as non-prepared statements (some mysql "feature"), so sometimes it is better to use non-prepared statements with mysql backend. |