sqlite: proper cleanup if rtree index creation fails

This commit is contained in:
Dane Springmeyer 2011-11-09 17:44:50 -08:00
parent 3b554ca308
commit 3ed640a15f

View file

@ -37,6 +37,7 @@
#include <boost/make_shared.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem/operations.hpp>
// sqlite
extern "C" {
@ -130,16 +131,17 @@ public:
int flags;
#endif
bool existed = boost::filesystem::exists(index_db);
boost::shared_ptr<sqlite_connection> ds = boost::make_shared<sqlite_connection>(index_db,flags);
ds->execute("PRAGMA synchronous=OFF");
ds->execute("BEGIN TRANSACTION");
// first drop the index if it already exists
std::ostringstream spatial_index_drop_sql;
spatial_index_drop_sql << "DROP TABLE IF EXISTS " << index_table;
ds->execute(spatial_index_drop_sql.str());
ds->execute("PRAGMA synchronous=OFF");
ds->execute("BEGIN TRANSACTION");
// create the spatial index
std::ostringstream create_idx;
create_idx << "create virtual table "
@ -156,6 +158,9 @@ public:
prepared_index_statement ps(ds,insert_idx.str());
bool one_success = false;
try
{
bool first = true;
while (rs->is_valid() && rs->step_next())
{
@ -206,11 +211,39 @@ public:
}
ps.step_next();
one_success = true;
}
}
}
}
catch (mapnik::datasource_exception const& ex)
{
ds->execute("ROLLBACK");
if (!existed)
{
try
{
boost::filesystem::remove(index_db);
}
catch (...) {};
}
throw mapnik::datasource_exception(ex.what());
}
if (one_success)
{
ds->execute("COMMIT");
}
else if (!existed)
{
ds->execute("ROLLBACK");
try
{
boost::filesystem::remove(index_db);
}
catch (...) {};
}
}
static bool detect_extent(boost::shared_ptr<sqlite_connection> ds,
bool has_spatial_index,