+ impl output directly to sqlite
This commit is contained in:
parent
34b8d62909
commit
b236012474
2 changed files with 87 additions and 82 deletions
|
@ -268,18 +268,6 @@ namespace mapnik {
|
|||
namespace sqlite = mapnik::sqlite;
|
||||
sqlite::database db(output_filename);
|
||||
|
||||
//db.execute("create table test(id integer, name text)");
|
||||
|
||||
//sqlite::prepared_statement stmt(db,"insert into test values(?,?)");
|
||||
//for (unsigned i=0;i<1000;++i)
|
||||
//{
|
||||
// sqlite::record_type rec;
|
||||
// rec.push_back(sqlite::value_type(int(i)));
|
||||
// rec.push_back(sqlite::value_type("testing ...."));
|
||||
// stmt.insert_record(rec);
|
||||
//}
|
||||
|
||||
|
||||
boost::shared_ptr<ResultSet> rs = conn->executeQuery("select * from " + table_name + " limit 0;");
|
||||
int count = rs->getNumFields();
|
||||
|
||||
|
@ -333,7 +321,7 @@ namespace mapnik {
|
|||
}
|
||||
|
||||
std::cout << select_sql_str << "\n";
|
||||
|
||||
|
||||
std::ostringstream cursor_sql;
|
||||
std::string cursor_name("my_cursor");
|
||||
|
||||
|
@ -348,61 +336,68 @@ namespace mapnik {
|
|||
create_sql << "create table " << table_name << "(PK_UID INTEGER PRIMARY KEY AUTOINCREMENT,";
|
||||
|
||||
int geometry_oid = -1;
|
||||
|
||||
|
||||
std::string output_table_insert_sql = "insert into " + table_name + " values (?";
|
||||
|
||||
for ( unsigned pos = 0; pos < num_fields ; ++pos)
|
||||
{
|
||||
if (pos > 0) create_sql << ",";
|
||||
if (geom_col == cursor->getFieldName(pos))
|
||||
{
|
||||
if (pos > 0)
|
||||
{
|
||||
create_sql << ",";
|
||||
}
|
||||
output_table_insert_sql +=",?";
|
||||
|
||||
if (geom_col == cursor->getFieldName(pos))
|
||||
{
|
||||
geometry_oid = cursor->getTypeOID(pos);
|
||||
create_sql << "'" << cursor->getFieldName(pos) << "' BLOB";
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
create_sql << "'" << cursor->getFieldName(pos) << "' TEXT";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
create_sql << ");";
|
||||
|
||||
|
||||
output_table_insert_sql +=")";
|
||||
|
||||
std::cout << "client_encoding=" << conn->client_encoding() << "\n";
|
||||
std::cout << "geometry_column=" << geom_col << "(" << geom_type
|
||||
<< ") srid=" << srid << " oid=" << geometry_oid << "\n";
|
||||
|
||||
|
||||
db.execute("begin;");
|
||||
|
||||
|
||||
// begin
|
||||
//out << "begin;\n";
|
||||
|
||||
//out << create_sql.str() << "\n";
|
||||
|
||||
// output table sql
|
||||
db.execute(create_sql.str());
|
||||
|
||||
|
||||
// spatial index sql
|
||||
std::string spatial_index_sql = "create virtual table idx_" + table_name + "_" + geom_col + " using rtree(pkid, xmin, xmax, ymin, ymax)";
|
||||
std::string spatial_index_sql = "create virtual table idx_" + table_name
|
||||
+ "_" + geom_col + " using rtree(pkid, xmin, xmax, ymin, ymax)";
|
||||
|
||||
db.execute(spatial_index_sql);
|
||||
|
||||
blob_to_hex hex;
|
||||
int pkid = 0;
|
||||
|
||||
sqlite::prepared_statement spatial_index(db,"");
|
||||
std::string spatial_index_insert_sql = "insert into idx_" + table_name + "_" + geom_col + " values (?,?,?,?,?)" ;
|
||||
sqlite::prepared_statement spatial_index(db,spatial_index_insert_sql);
|
||||
std::cout << output_table_insert_sql << "\n";
|
||||
sqlite::prepared_statement output_table(db,output_table_insert_sql);
|
||||
|
||||
//
|
||||
while (cursor->next())
|
||||
{
|
||||
++pkid;
|
||||
|
||||
std::ostringstream insert_sql;
|
||||
insert_sql << "insert into " << table_name << " values(" << pkid;
|
||||
|
||||
//std::ostringstream insert_sql;
|
||||
//insert_sql << "insert into " << table_name << " values(" << pkid;
|
||||
sqlite::record_type output_rec;
|
||||
|
||||
output_rec.push_back(sqlite::value_type(pkid));
|
||||
bool empty_geom = true;
|
||||
|
||||
for (unsigned pos=0 ; pos < num_fields; ++pos)
|
||||
{
|
||||
insert_sql << ",";
|
||||
//insert_sql << ",";
|
||||
if (! cursor->isNull(pos))
|
||||
{
|
||||
int size=cursor->getFieldLength(pos);
|
||||
|
@ -417,12 +412,13 @@ namespace mapnik {
|
|||
{
|
||||
std::string text(buf);
|
||||
boost::algorithm::replace_all(text,"'","''");
|
||||
insert_sql << "'"<< text << "'";
|
||||
//insert_sql << "'"<< text << "'";
|
||||
output_rec.push_back(sqlite::value_type(text));
|
||||
break;
|
||||
}
|
||||
case 23:
|
||||
insert_sql << int4net(buf);
|
||||
break;
|
||||
output_rec.push_back(sqlite::value_type(int4net(buf)));
|
||||
break;
|
||||
default:
|
||||
{
|
||||
if (oid == geometry_oid)
|
||||
|
@ -435,19 +431,24 @@ namespace mapnik {
|
|||
Envelope<double> bbox = geom.envelope();
|
||||
if (valid_envelope(bbox))
|
||||
{
|
||||
out << "insert into idx_" << table_name << "_" << geom_col << " values (" ;
|
||||
out << pkid << "," << bbox.minx() << "," << bbox.maxx();
|
||||
out << "," << bbox.miny() << "," << bbox.maxy() << ");\n";
|
||||
empty_geom = false;
|
||||
sqlite::record_type rec;
|
||||
|
||||
rec.push_back(sqlite::value_type(pkid));
|
||||
rec.push_back(sqlite::value_type(bbox.minx()));
|
||||
rec.push_back(sqlite::value_type(bbox.maxx()));
|
||||
rec.push_back(sqlite::value_type(bbox.miny()));
|
||||
rec.push_back(sqlite::value_type(bbox.maxy()));
|
||||
|
||||
spatial_index.insert_record(rec);
|
||||
empty_geom = false;
|
||||
}
|
||||
}
|
||||
|
||||
insert_sql << "X'" << hex(buf,size) << "'";
|
||||
|
||||
output_rec.push_back(sqlite::value_type("X'" + hex(buf,size) + "'"));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
insert_sql << "NULL";
|
||||
{
|
||||
output_rec.push_back(sqlite::null_type());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -455,29 +456,36 @@ namespace mapnik {
|
|||
}
|
||||
else
|
||||
{
|
||||
insert_sql << "NULL";
|
||||
output_rec.push_back(sqlite::null_type());
|
||||
//insert_sql << "NULL";
|
||||
}
|
||||
}
|
||||
insert_sql << ");";
|
||||
|
||||
if (!empty_geom) out << insert_sql.str() << "\n";
|
||||
//insert_sql << ");";
|
||||
|
||||
//if (!empty_geom) out << insert_sql.str() << "\n";
|
||||
if (!empty_geom) output_table.insert_record(output_rec);
|
||||
|
||||
if (pkid % 1000 == 0)
|
||||
{
|
||||
std::cout << "\r processing " << pkid << " features";
|
||||
std::cout.flush();
|
||||
}
|
||||
if (pkid % 100000 == 0)
|
||||
|
||||
if (pkid % 100000 == 0)
|
||||
{
|
||||
out << "commit;\n";
|
||||
out << "begin;\n";
|
||||
//out << "commit;\n";
|
||||
//out << "begin;\n";
|
||||
db.execute("commit;begin;");
|
||||
|
||||
}
|
||||
}
|
||||
// commit
|
||||
out << "commit;\n";
|
||||
//out << "commit;\n";
|
||||
db.execute("commit;");
|
||||
|
||||
std::cout << "\r processed " << pkid << " features";
|
||||
std::cout << "\n Done!" << std::endl;
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,9 +29,9 @@
|
|||
#include <sqlite3.h>
|
||||
|
||||
//stl
|
||||
#ifdef MAPNIK_DEBUG
|
||||
//#ifdef MAPNIK_DEBUG
|
||||
#include <iostream>
|
||||
#endif
|
||||
//#endif
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
@ -61,8 +61,8 @@ namespace mapnik { namespace sqlite {
|
|||
bool execute(std::string const& sql);
|
||||
};
|
||||
|
||||
|
||||
typedef boost::variant<int,double,std::string> value_type;
|
||||
struct null_type {};
|
||||
typedef boost::variant<int,double,std::string,null_type> value_type;
|
||||
typedef std::vector<value_type> record_type;
|
||||
|
||||
class prepared_statement : boost::noncopyable
|
||||
|
@ -71,7 +71,17 @@ namespace mapnik { namespace sqlite {
|
|||
{
|
||||
binder(sqlite3_stmt * stmt, unsigned index)
|
||||
: stmt_(stmt), index_(index) {}
|
||||
|
||||
|
||||
bool operator() (null_type )
|
||||
{
|
||||
if (sqlite3_bind_null(stmt_, index_) != SQLITE_OK)
|
||||
{
|
||||
std::cerr << "cannot bind NULL\n";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator() (int val)
|
||||
{
|
||||
if (sqlite3_bind_int(stmt_, index_ , val ) != SQLITE_OK)
|
||||
|
@ -94,7 +104,7 @@ namespace mapnik { namespace sqlite {
|
|||
|
||||
bool operator() (std::string const& val)
|
||||
{
|
||||
if (sqlite3_bind_text(stmt_, index_, val.c_str(), val.length(), 0) != SQLITE_OK)
|
||||
if (sqlite3_bind_text(stmt_, index_, val.c_str(), val.length(), SQLITE_STATIC) != SQLITE_OK)
|
||||
{
|
||||
std::cerr << "cannot bind " << val << "\n";
|
||||
return false;
|
||||
|
@ -117,25 +127,11 @@ namespace mapnik { namespace sqlite {
|
|||
std::cerr << "ERR:"<< res << "\n";
|
||||
throw;
|
||||
}
|
||||
|
||||
// begin transaction
|
||||
//res = sqlite3_exec(db_,"BEGIN;",0,0,&err_msg);
|
||||
//if (res != SQLITE_OK)
|
||||
//{
|
||||
// std::cerr << "ERR:" << err_msg << "\n";
|
||||
// sqlite3_free(err_msg);
|
||||
//}
|
||||
}
|
||||
|
||||
~prepared_statement()
|
||||
{
|
||||
// char * err_msg;
|
||||
//commit transaction
|
||||
//#ifdef MAPNIK_DEBUG
|
||||
// std::cerr << "COMMIT\n";
|
||||
//#endif
|
||||
// sqlite3_exec(db_,"COMMIT;",0,0,&err_msg);
|
||||
int res = sqlite3_finalize(stmt_);
|
||||
int res = sqlite3_finalize(stmt_);
|
||||
if (res != SQLITE_OK)
|
||||
{
|
||||
std::cerr << "ERR:" << res << "\n";
|
||||
|
@ -144,6 +140,9 @@ namespace mapnik { namespace sqlite {
|
|||
|
||||
bool insert_record(record_type const& rec) const
|
||||
{
|
||||
#ifdef MAPNIK_DEBUG
|
||||
assert( sqlite3_bind_parameter_count(stmt_) == rec.size());
|
||||
#endif
|
||||
record_type::const_iterator itr = rec.begin();
|
||||
record_type::const_iterator end = rec.end();
|
||||
int count = 1;
|
||||
|
@ -166,8 +165,6 @@ namespace mapnik { namespace sqlite {
|
|||
sqlite3 * db_;
|
||||
sqlite3_stmt * stmt_;
|
||||
};
|
||||
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue