From 385ad05205d5bedffaf49aaead7af35237cb602c Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Thu, 11 Nov 2010 16:33:30 +0000 Subject: [PATCH] create an sql_utils header for all sql-based plugins to be able to leverage, and move the postgis table_from_sql function into it --- include/mapnik/sql_utils.hpp | 59 +++++++++++++++++++++++++++++++ plugins/input/postgis/postgis.cpp | 28 ++------------- plugins/input/postgis/postgis.hpp | 1 - 3 files changed, 61 insertions(+), 27 deletions(-) create mode 100644 include/mapnik/sql_utils.hpp diff --git a/include/mapnik/sql_utils.hpp b/include/mapnik/sql_utils.hpp new file mode 100644 index 000000000..8d25c09ba --- /dev/null +++ b/include/mapnik/sql_utils.hpp @@ -0,0 +1,59 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2010 Artem Pavlenko + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + *****************************************************************************/ + +//$Id: sql_utils.hpp 39 2005-04-10 20:39:53Z pavlenko $ + +#ifndef SQL_UTILS_HPP +#define SQL_UTILS_HPP + +// boost +#include + +namespace mapnik +{ + +MAPNIK_DECL std::string table_from_sql(const std::string& sql) +{ + std::string table_name = boost::algorithm::to_lower_copy(sql); + boost::algorithm::replace_all(table_name,"\n"," "); + + std::string::size_type idx = table_name.rfind(" from "); + if (idx!=std::string::npos) + { + + idx = table_name.find_first_not_of(" ",idx+5); + if (idx != std::string::npos) + { + table_name=table_name.substr(idx); + } + idx = table_name.find_first_of(" )"); + if (idx != std::string::npos) + { + table_name = table_name.substr(0,idx); + } + } + return table_name; +} + +} + +#endif //SQL_UTILS_HPP diff --git a/plugins/input/postgis/postgis.cpp b/plugins/input/postgis/postgis.cpp index a0609817b..a4cfeb0ef 100644 --- a/plugins/input/postgis/postgis.cpp +++ b/plugins/input/postgis/postgis.cpp @@ -25,6 +25,7 @@ // mapnik #include #include +#include #ifdef MAPNIK_DEBUG //#include @@ -158,7 +159,7 @@ void postgis_datasource::bind() const if(geometry_table_.empty()) { - geometry_table_ = table_from_sql(table_); + geometry_table_ = mapnik::table_from_sql(table_); } std::string::size_type idx = geometry_table_.find_last_of('.'); if (idx!=std::string::npos) @@ -402,31 +403,6 @@ std::string postgis_datasource::unquote(const std::string& sql) return table_name; } -// TODO - make smarter and potentially move to reusable utilities -// available to other SQL-based plugins -std::string postgis_datasource::table_from_sql(const std::string& sql) -{ - std::string table_name = boost::algorithm::to_lower_copy(sql); - boost::algorithm::replace_all(table_name,"\n"," "); - - std::string::size_type idx = table_name.rfind(" from "); - if (idx!=std::string::npos) - { - - idx=table_name.find_first_not_of(" ",idx+5); - if (idx != std::string::npos) - { - table_name=table_name.substr(idx); - } - idx=table_name.find_first_of(" )"); - if (idx != std::string::npos) - { - table_name = table_name.substr(0,idx); - } - } - return table_name; -} - boost::shared_ptr postgis_datasource::get_resultset(boost::shared_ptr const &conn, const std::string &sql) const { if (cursor_fetch_size_ > 0) { diff --git a/plugins/input/postgis/postgis.hpp b/plugins/input/postgis/postgis.hpp index 943060d41..f5f624464 100644 --- a/plugins/input/postgis/postgis.hpp +++ b/plugins/input/postgis/postgis.hpp @@ -94,7 +94,6 @@ class postgis_datasource : public datasource std::string populate_tokens(const std::string& sql, double const& scale_denom, box2d const& env) const; std::string populate_tokens(const std::string& sql) const; static std::string unquote(const std::string& sql); - static std::string table_from_sql(const std::string& sql); boost::shared_ptr get_resultset(boost::shared_ptr const &conn, const std::string &sql) const; postgis_datasource(const postgis_datasource&); postgis_datasource& operator=(const postgis_datasource&);