Author: mysqlpp
Date: Sat Feb 19 01:02:26 2011
New Revision: 2686
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2686&view=rev
Log:
Query::insert() and replace() overloads taking a pair of iterators now
work with containers providing only a forward iterator. Previous code
only worked with things like std::vector, due to its use of iterator
arithmetic. Idea suggested by Adrian Cornish, implementation by me.
Modified:
trunk/lib/query.h
Modified: trunk/lib/query.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/query.h?rev=2686&r1=2685&r2=2686&view=diff
==============================================================================
--- trunk/lib/query.h (original)
+++ trunk/lib/query.h Sat Feb 19 01:02:26 2011
@@ -1000,19 +1000,22 @@
Query& insert(Iter first, Iter last)
{
reset();
- if (first == last) {
- return *this; // empty set!
- }
-
- MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
- "INSERT INTO `" << first->table() << "` (" <<
- first->field_list() << ") VALUES (" <<
- first->value_list() << ')';
-
- Iter it = first + 1;
- while (it != last) {
- MYSQLPP_QUERY_THISPTR << ",(" << it->value_list() <<
')';
- ++it;
+
+ if (first != last) {
+ // Build SQL for first item in the container. It's
special
+ // because we need the table name and field list.
+ MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
+ "INSERT INTO `" << first->table() << "`
(" <<
+ first->field_list() << ") VALUES (" <<
+ first->value_list() << ')';
+
+ // Now insert any remaining container elements. Be
careful
+ // hacking on the iterator use here: we want it to work
+ // with containers providing only a forward iterator.
+ Iter it = first;
+ while (++it != last) {
+ MYSQLPP_QUERY_THISPTR << ",(" <<
it->value_list() << ')';
+ }
}
return *this;
@@ -1144,19 +1147,21 @@
Query& replace(Iter first, Iter last)
{
reset();
- if (first == last) {
- return *this; // empty set!
- }
-
- MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
- "REPLACE INTO " << first->table() << " (" <<
- first->field_list() << ") VALUES (" <<
- first->value_list() << ')';
-
- Iter it = first + 1;
- while (it != last) {
- MYSQLPP_QUERY_THISPTR << ",(" << it->value_list() <<
')';
- ++it;
+ if (first != last) {
+ // Build SQL for first item in the container. It's
special
+ // because we need the table name and field list.
+ MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
+ "REPLACE INTO " << first->table() << "
(" <<
+ first->field_list() << ") VALUES (" <<
+ first->value_list() << ')';
+
+ // Now insert any remaining container elements. Be
careful
+ // hacking on the iterator use here: we want it to work
+ // with containers providing only a forward iterator.
+ Iter it = first;
+ while (++it != last) {
+ MYSQLPP_QUERY_THISPTR << ",(" <<
it->value_list() << ')';
+ }
}
return *this;
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits