Skip to content

Commit

Permalink
sqlite: autoquote table names that start with numbers to better suppo…
Browse files Browse the repository at this point in the history
…rt natural earth conversions from shapefiles
  • Loading branch information
Dane Springmeyer committed Nov 10, 2011
1 parent 89e13d3 commit 4e47697
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
9 changes: 9 additions & 0 deletions plugins/input/sqlite/sqlite_datasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ void sqlite_datasource::bind() const
{
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
dataset_ = boost::make_shared<sqlite_connection>(dataset_name_);
Expand Down
2 changes: 1 addition & 1 deletion plugins/input/sqlite/sqlite_datasource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class sqlite_datasource : public mapnik::datasource
int type_;
mutable std::string dataset_name_;
mutable boost::shared_ptr<sqlite_connection> dataset_;
std::string table_;
mutable std::string table_;
std::string fields_;
std::string metadata_;
mutable std::string geometry_table_;
Expand Down
28 changes: 28 additions & 0 deletions plugins/input/sqlite/sqlite_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ class sqlite_utils
{
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)
{
boost::algorithm::trim_if(z,boost::algorithm::is_any_of("[]'\"`"));
Expand Down

0 comments on commit 4e47697

Please sign in to comment.