postgis: support mixed case table names - closes #1159

This commit is contained in:
Dane Springmeyer 2012-04-02 19:21:14 -07:00
parent 253a1e4b4b
commit d9cb76f7a1
2 changed files with 36 additions and 4 deletions

View file

@ -31,14 +31,14 @@ namespace mapnik { namespace sql_utils {
inline std::string unquote_double(const std::string& sql)
{
std::string table_name = boost::algorithm::to_lower_copy(sql);
std::string table_name = sql;
boost::algorithm::trim_if(table_name,boost::algorithm::is_any_of("\""));
return table_name;
}
inline std::string unquote(const std::string& sql)
{
std::string table_name = boost::algorithm::to_lower_copy(sql);
std::string table_name = sql;
boost::algorithm::trim_if(table_name,boost::algorithm::is_any_of("\"\'"));
return table_name;
}
@ -58,10 +58,11 @@ namespace mapnik { namespace sql_utils {
inline std::string table_from_sql(const std::string& sql)
{
std::string table_name = boost::algorithm::to_lower_copy(sql);
std::string table_name = sql;
boost::algorithm::replace_all(table_name,"\n"," ");
boost::algorithm::ireplace_all(table_name," from "," FROM ");
std::string::size_type idx = table_name.rfind(" from ");
std::string::size_type idx = table_name.rfind(" FROM ");
if (idx!=std::string::npos)
{
idx = table_name.find_first_not_of(" ",idx+5);

View file

@ -111,6 +111,14 @@ INSERT INTO test5(non_id, manual_id, geom) values (0, -1, GeomFromEWKT('SRID=432
INSERT INTO test5(non_id, manual_id, geom) values (0, 1, GeomFromEWKT('SRID=4326;POINT(0 0)'));
"""
insert_table_6 = '''
CREATE TABLE "tableWithMixedCase"(gid serial PRIMARY KEY, geom geometry);
INSERT INTO "tableWithMixedCase"(geom) values (ST_MakePoint(0,0));
INSERT INTO "tableWithMixedCase"(geom) values (ST_MakePoint(0,1));
INSERT INTO "tableWithMixedCase"(geom) values (ST_MakePoint(1,0));
INSERT INTO "tableWithMixedCase"(geom) values (ST_MakePoint(1,1));
'''
def postgis_setup():
call('dropdb %s' % MAPNIK_TEST_DBNAME,silent=True)
call('createdb -T %s %s' % (POSTGIS_TEMPLATE_DBNAME,MAPNIK_TEST_DBNAME),silent=False)
@ -121,6 +129,7 @@ def postgis_setup():
call('''psql -q %s -c "%s"''' % (MAPNIK_TEST_DBNAME,insert_table_3),silent=False)
call('''psql -q %s -c "%s"''' % (MAPNIK_TEST_DBNAME,insert_table_4),silent=False)
call('''psql -q %s -c "%s"''' % (MAPNIK_TEST_DBNAME,insert_table_5),silent=False)
call("""psql -q %s -c '%s'""" % (MAPNIK_TEST_DBNAME,insert_table_6),silent=False)
def postgis_takedown():
pass
@ -328,6 +337,28 @@ if 'postgis' in mapnik.DatasourceCache.instance().plugin_names() \
eq_(fs.next().id(),1)
eq_(fs.next().id(),2)
def test_querying_table_with_mixed_case():
ds = mapnik.PostGIS(dbname=MAPNIK_TEST_DBNAME,table='"tableWithMixedCase"',
geometry_field='geom',
require_key=True)
fs = ds.featureset()
eq_(fs.next().id(),1)
eq_(fs.next().id(),2)
eq_(fs.next().id(),3)
eq_(fs.next().id(),4)
eq_(fs.next(),None)
def test_querying_subquery_with_mixed_case():
ds = mapnik.PostGIS(dbname=MAPNIK_TEST_DBNAME,table='(SeLeCt * FrOm "tableWithMixedCase") as MixedCaseQuery',
geometry_field='geom',
require_key=True)
fs = ds.featureset()
eq_(fs.next().id(),1)
eq_(fs.next().id(),2)
eq_(fs.next().id(),3)
eq_(fs.next().id(),4)
eq_(fs.next(),None)
atexit.register(postgis_takedown)
if __name__ == "__main__":