2009/6/8 Warren Young <mysqlpp@stripped>:
> No, that's not right. This is saying that the table name includes the quote
> marks. The quotes should only be added by the bits of code that create SQL.
> The table name remains the same regardless of whether it needs quotes or
> not. Also, it ignores SSQLS::table() and friends.
>
> By the way, it's easier to generate patches against svn. Just modify the
> code in the svn tree, then say "svn diff" to get the patch. This generates
> unified diffs instead of ... context? ... diffs, which patch(1) likes
> better.
Warren,
Please find attached a patch to quote database/table and field names
in sql statements. This was applied to SVN trunk.
dtest completes successfully/
Some of the bmark.txt results have changed due to quoted names.
Also I had to include a few extra #include for gcc4.4
Adrian Cornish
patch include inline as well:
Index: lib/connection.cpp
===================================================================
--- lib/connection.cpp (revision 2519)
+++ lib/connection.cpp (working copy)
@@ -128,7 +128,7 @@
{
error_message_.clear();
Query q(this, throw_exceptions());
- q << "SELECT COUNT(*) FROM " << table;
+ q << "SELECT COUNT(*) FROM `" << table << '`';
if (StoreQueryResult res = q.store()) {
return res[0][0];
}
@@ -143,7 +143,7 @@
{
error_message_.clear();
Query q(this, throw_exceptions());
- q << "CREATE DATABASE " << db;
+ q << "CREATE DATABASE `" << db << '`';
return q.exec();
}
@@ -161,7 +161,7 @@
{
error_message_.clear();
Query q(this, throw_exceptions());
- q << "DROP DATABASE " << db;
+ q << "DROP DATABASE `" << db << '`';
return q.exec();
}
Index: lib/dbdriver.cpp
===================================================================
--- lib/dbdriver.cpp (revision 2519)
+++ lib/dbdriver.cpp (working copy)
@@ -29,6 +29,7 @@
#include "exceptions.h"
#include <sstream>
+#include <cstring>
#include <memory>
// An argument was added to mysql_shutdown() in MySQL 4.1.3 and 5.0.1.
Index: lib/ssqls.pl
===================================================================
--- lib/ssqls.pl (revision 2519)
+++ lib/ssqls.pl (working copy)
@@ -327,7 +327,7 @@
$enums .= " NAME##_##I$j";
$enums .= ",\n" unless $j == $i;
- $field_list .= " s << obj.manip << obj.obj->names[".($j-1)."]";
+ $field_list .= " s << obj.manip << '`' <<
obj.obj->names[".($j-1)."] << '`'";
$field_list .= " << obj.delim;\n" unless $j == $i;
$value_list .= " s << obj.manip << obj.obj->I$j";
@@ -346,18 +346,18 @@
$cus_field_list .= " if ((*obj.include)[".($j-1)."]) { \n";
$cus_field_list .= " if (before) s << obj.delim;\n" unless $j == 1;
- $cus_field_list .= " s << obj.manip <<
obj.obj->names[".($j-1)."];\n";
+ $cus_field_list .= " s << obj.manip << '`' <<
obj.obj->names[".($j-1)."] << '`';\n";
$cus_field_list .= " before = true; \n" unless $j == $i;
$cus_field_list .= " } \n";
$cus_equal_list .= " if ((*obj.include)[".($j-1)."]) { \n";
$cus_equal_list .= " if (before) s << obj.delim;\n" unless $j == 1;
- $cus_equal_list .= " s << obj.obj->names[".($j-1)."] << obj.comp";
+ $cus_equal_list .= " s << '`' << obj.obj->names[".($j-1)."]
<<
'`' << obj.comp";
$cus_equal_list .= " << obj.manip << obj.obj->I$j;\n";
$cus_equal_list .= " before = true; \n" unless $j == $i;
$cus_equal_list .= " } \n";
- $equal_list .= " s << obj.obj->names[".($j-1)."] << obj.comp";
+ $equal_list .= " s << '`' << obj.obj->names[".($j-1)."] << '`'
<< obj.comp";
$equal_list .= " << obj.manip << obj.obj->I$j";
$equal_list .= " << obj.delim;\n" unless $j == $i;
Index: lib/query.h
===================================================================
--- lib/query.h (revision 2519)
+++ lib/query.h (working copy)
@@ -556,8 +556,9 @@
template <class SSQLS, typename Function>
Function for_each(const SSQLS& ssqls, Function fn)
{
- std::string query("select * from ");
+ std::string query("select * from `");
query += ssqls.table();
+ query += '`';
mysqlpp::UseQueryResult res = use(query);
if (res) {
mysqlpp::NoExceptions ne(res);
@@ -618,8 +619,9 @@
template <class Sequence, class SSQLS, typename Function>
Function store_if(Sequence& con, const SSQLS& ssqls, Function fn)
{
- std::string query("select * from ");
+ std::string query("select * from `");
query += ssqls.table();
+ query += '`';
mysqlpp::UseQueryResult res = use(query);
if (res) {
mysqlpp::NoExceptions ne(res);
@@ -947,7 +949,7 @@
// lookup logic. For an explanation of the problem, see:
//
http://groups-beta.google.com/group/microsoft.public.vc.stl/browse_thread/thread/9a68d84644e64f15
MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
- "UPDATE " << o.table() << " SET " << n.equal_list() <<
+ "UPDATE `" << o.table() << "` SET " << n.equal_list() <<
" WHERE " << o.equal_list(" AND ", sql_use_compare);
return *this;
}
@@ -966,7 +968,7 @@
reset();
MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
- "INSERT INTO " << v.table() << " (" <<
+ "INSERT INTO `" << v.table() << "` (" <<
v.field_list() << ") VALUES (" <<
v.value_list() << ')';
return *this;
@@ -994,7 +996,7 @@
}
MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
- "INSERT INTO " << first->table() << " (" <<
+ "INSERT INTO `" << first->table() << "` (" <<
first->field_list() << ") VALUES (" <<
first->value_list() << ')';
@@ -1037,7 +1039,7 @@
if (policy.can_add(tellp(), *it)) {
if (empty) {
MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
- "INSERT INTO " << it->table() << " (" <<
+ "INSERT INTO `" << it->table() << "` (" <<
it->field_list() << ") VALUES (";
}
else {
@@ -1062,7 +1064,7 @@
// If we _still_ can't add, the policy is too strict
if (policy.can_add(tellp(), *it)) {
MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
- "INSERT INTO " << it->table() << " (" <<
+ "INSERT INTO `" << it->table() << "` (" <<
it->field_list() << ") VALUES (" <<
it->value_list() << ')';
@@ -1110,7 +1112,7 @@
reset();
MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
- "REPLACE INTO " << v.table() << " (" <<
+ "REPLACE INTO `" << v.table() << "` (" <<
v.field_list() << ") VALUES (" << v.value_list() << ')';
return *this;
}
Index: test/insertpolicy.cpp
===================================================================
--- test/insertpolicy.cpp (revision 2519)
+++ test/insertpolicy.cpp (working copy)
@@ -27,6 +27,7 @@
#include <mysql++.h>
#include <iostream>
+#include <climits>
static const unsigned char nonzero = 4;
Index: bmark.txt
===================================================================
--- bmark.txt (revision 2519)
+++ bmark.txt (working copy)
@@ -135,7 +135,7 @@
================ END ssqls1 OUTPUT ================
---------------- BEGIN ssqls2 OUTPUT ----------------
-Query: INSERT INTO stock (item,num,weight,price,sdate,description)
VALUES ('Hot Dogs',100,1.5,NULL,'1998-09-25',NULL)
+Query: INSERT INTO `stock`
(`item`,`num`,`weight`,`price`,`sdate`,`description`) VALUES ('Hot
Dogs',100,1.5,NULL,'1998-09-25',NULL)
Query: select * from stock
Records found: 5
@@ -149,7 +149,7 @@
================ END ssqls2 OUTPUT ================
---------------- BEGIN ssqls3 OUTPUT ----------------
-Query: UPDATE stock SET item = 'Nuerenberger Bratwurst',num =
97,weight = 1.5,price = 8.7899999999999991,sdate =
'2005-03-10',description = NULL WHERE item = 'Nürnberger Brats'
+Query: UPDATE `stock` SET `item` = 'Nuerenberger Bratwurst',`num` =
97,`weight` = 1.5,`price` = 8.7899999999999991,`sdate` =
'2005-03-10',`description` = NULL WHERE `item` = 'Nürnberger Brats'
Query: select * from stock
Records found: 5
@@ -178,7 +178,7 @@
---------------- BEGIN ssqls5 OUTPUT ----------------
Custom query:
-select * from stock where weight = 1.5 and price = 8.7899999999999991
+select * from stock where `weight` = 1.5 and `price` = 8.7899999999999991
================ END ssqls5 OUTPUT ================
---------------- BEGIN ssqls6 OUTPUT ----------------
Attachment: [text/x-patch] quote_table_fields.patch