sqlite: autoquote table names that start with numbers to better support natural earth conversions from shapefiles

This commit is contained in:
Dane Springmeyer 2011-11-10 09:42:04 -08:00
parent 89e13d304d
commit 4e4769745f
3 changed files with 38 additions and 1 deletions

View file

@ -159,6 +159,15 @@ void sqlite_datasource::bind() const
{ {
using_subquery_ = true; using_subquery_ = true;
} }
else
{
// attempt to auto-quote table if needed
if (sqlite_utils::needs_quoting(table_))
{
table_ = std::string("[") + table_ + "]";
geometry_table_ = table_;
}
}
// now actually create the connection and start executing setup sql // now actually create the connection and start executing setup sql
dataset_ = boost::make_shared<sqlite_connection>(dataset_name_); dataset_ = boost::make_shared<sqlite_connection>(dataset_name_);

View file

@ -56,7 +56,7 @@ private:
int type_; int type_;
mutable std::string dataset_name_; mutable std::string dataset_name_;
mutable boost::shared_ptr<sqlite_connection> dataset_; mutable boost::shared_ptr<sqlite_connection> dataset_;
std::string table_; mutable std::string table_;
std::string fields_; std::string fields_;
std::string metadata_; std::string metadata_;
mutable std::string geometry_table_; mutable std::string geometry_table_;

View file

@ -55,6 +55,34 @@ class sqlite_utils
{ {
public: public:
static bool needs_quoting(std::string const& name)
{
if (name.size() > 0)
{
const char first = name[0];
if (is_quote_char(first))
{
return false;
}
if ((first >= '0' && first <= '9') ||
(name.find("-") != std::string::npos)
)
{
return true;
}
}
return false;
}
static bool is_quote_char(const char z)
{
if (z == '"' || z == '\'' || z == '[' || z == '`')
{
return true;
}
return false;
}
static void dequote(std::string & z) static void dequote(std::string & z)
{ {
boost::algorithm::trim_if(z,boost::algorithm::is_any_of("[]'\"`")); boost::algorithm::trim_if(z,boost::algorithm::is_any_of("[]'\"`"));