From bdc726a525d919436aa92c0a7b22ced315458803 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Wed, 9 Nov 2011 19:18:12 -0800 Subject: [PATCH] sqlite: return success or failure from rtree indexing function and report available tables if the pragma table_info() fails --- plugins/input/sqlite/sqlite_datasource.cpp | 24 +++++++---- plugins/input/sqlite/sqlite_utils.hpp | 47 +++++++++++++++++++++- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/plugins/input/sqlite/sqlite_datasource.cpp b/plugins/input/sqlite/sqlite_datasource.cpp index 990d583ca..2bf12fcb3 100644 --- a/plugins/input/sqlite/sqlite_datasource.cpp +++ b/plugins/input/sqlite/sqlite_datasource.cpp @@ -197,12 +197,19 @@ void sqlite_datasource::bind() const if (! found_table) { std::ostringstream s; - s << "Sqlite Plugin: could not query table '" << geometry_table_ << "' "; - if (using_subquery_) s << " from subquery '" << table_ << "' "; - s << "using 'PRAGMA table_info(" << geometry_table_ << ")' "; + s << "Sqlite Plugin: could not query table '" << geometry_table_ << "'"; + if (using_subquery_) + { + s << " from subquery '" << table_ << "'"; + } - std::string sq_err = std::string(sqlite3_errmsg(*(*dataset_))); - if (sq_err != "unknown error") s << ": " << sq_err; + // report get available tables + std::vector tables; + sqlite_utils::get_tables(dataset_,tables); + if (tables.size() > 0) + { + s << " (available tables for " << dataset_name_ << " are: '" << boost::algorithm::join(tables, ", ") << "')"; + } throw datasource_exception(s.str()); } @@ -228,7 +235,6 @@ void sqlite_datasource::bind() const { dataset_->execute("attach database '" + index_db + "' as " + index_table_); } - has_spatial_index_ = sqlite_utils::has_rtree(index_table_,dataset_); } @@ -246,8 +252,10 @@ void sqlite_datasource::bind() const << " FROM (" << geometry_table_ << ")"; boost::shared_ptr rs = dataset_->execute_query(query.str()); - sqlite_utils::create_spatial_index(index_db,index_table_,rs,extent_); - extent_initialized_ = true; + if (sqlite_utils::create_spatial_index(index_db,index_table_,rs,extent_)) + { + extent_initialized_ = true; + } } else { diff --git a/plugins/input/sqlite/sqlite_utils.hpp b/plugins/input/sqlite/sqlite_utils.hpp index fb1c31947..b7b6d4e6a 100644 --- a/plugins/input/sqlite/sqlite_utils.hpp +++ b/plugins/input/sqlite/sqlite_utils.hpp @@ -75,6 +75,47 @@ public: //{ return file + ".index"; //} + } + + static void get_tables(boost::shared_ptr ds, + std::vector & tables) + { + std::ostringstream sql; + // todo handle finding tables from attached db's + sql << " SELECT name FROM sqlite_master" + << " WHERE type IN ('table','view')" + << " AND name NOT LIKE 'sqlite_%'" + << " AND name NOT LIKE 'idx_%'" + << " AND name NOT LIKE '%geometry_columns%'" + << " AND name NOT LIKE '%ref_sys%'" + << " UNION ALL" + << " SELECT name FROM sqlite_temp_master" + << " WHERE type IN ('table','view')" + << " ORDER BY 1"; + sqlite3_stmt* stmt = 0; + const int rc = sqlite3_prepare_v2 (*(*ds), sql.str().c_str(), -1, &stmt, 0); + std::clog << "hey\n"; + if (rc == SQLITE_OK) + { + std::clog << "a\n"; + boost::shared_ptr rs = boost::make_shared(stmt); + while (rs->is_valid() && rs->step_next()) + { + std::clog << "b\n"; + const int type_oid = rs->column_type(0); + if (type_oid == SQLITE_TEXT) + { + std::clog << "c\n"; + const char * data = rs->column_text(0); + if (data) + { + + std::clog << "d\n"; + tables.push_back(std::string(data)); + } + } + } + } } static void query_extent(boost::shared_ptr rs, @@ -111,7 +152,7 @@ public: } } - static void create_spatial_index(std::string const& index_db, + static bool create_spatial_index(std::string const& index_db, std::string const& index_table, boost::shared_ptr rs, mapnik::box2d& extent) @@ -123,7 +164,7 @@ public: */ if (!rs->is_valid()) - return; + return false; #if SQLITE_VERSION_NUMBER >= 3005000 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; @@ -233,6 +274,7 @@ public: if (one_success) { ds->execute("COMMIT"); + return true; } else if (!existed) { @@ -243,6 +285,7 @@ public: } catch (...) {}; } + return false; } static bool detect_extent(boost::shared_ptr ds,