Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Commit

Permalink
quote field names in the view, unicode encode more queries
Browse files Browse the repository at this point in the history
  • Loading branch information
vegitron committed Jan 27, 2017
1 parent f469b2c commit f20d210
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions sqlshare_rest/backend/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def run_query(self, sql, user, params=None, return_cursor=False,
query.save()

cursor = connection.cursor()
cursor.execute(sql, params)
cursor.execute(sql.encode('utf-8'), params)

if return_cursor:
return cursor
Expand Down Expand Up @@ -413,6 +413,7 @@ def _get_view_sql_for_dataset_by_parser(self, table_name, parser, user):
all_unique = self._make_safe_column_name_list(all_unique)

for c in all_unique[0:-1]:
c = '"%s"' % c.replace('"', '_')
cast.append("CAST(%s AS TEXT) AS %s" % (c, c))
plain.append("%s" % c)
base.append(c)
Expand Down Expand Up @@ -449,7 +450,12 @@ def _create_table(self, table_name, column_names, column_types, user):
if not ex_str.find("There is already an object named"):
logger.error("Error creating table: %s" % str(ex))
drop_sql = self._drop_table_sql(user.schema, table_name)
self.run_query(drop_sql, user, return_cursor=True).close()
try:
self.run_query(drop_sql, user, return_cursor=True).close()
except:
# It's ok if this table didn't exist. We want the error
# on create
pass
self.run_query(sql, user, return_cursor=True).close()

# Create a second table that has ... everything we were wrong about
Expand All @@ -469,11 +475,17 @@ def _create_table(self, table_name, column_names, column_types, user):
logger.error("Error creating table: %s" % str(ex))
drop_sql = self._drop_table_sql(user.schema,
"untyped_%s" % table_name)
self.run_query(drop_sql, user, return_cursor=True).close()
try:
self.run_query(drop_sql, user, return_cursor=True).close()
except:
# It's ok if this table didn't exist. We want the error
# on create
pass
self.run_query(sql, user, return_cursor=True).close()

def _create_table_sql(self, user, table_name, column_names, column_types):
def _column_sql(name, col_type):
name = '"%s"' % name.replace('"', '_')
if "int" == col_type["type"]:
return "%s bigint" % name
if "float" == col_type["type"]:
Expand All @@ -498,6 +510,7 @@ def _create_untyped_table_sql(self, user, table_name, names, types):
columns = []
for i in range(0, len(names)):
name = names[i]
name = '"%s"' % name.replace('"', '_')
columns.append("%s text" % name)

return 'CREATE TABLE %s."untyped_%s" (%s)' % (
Expand Down

0 comments on commit f20d210

Please sign in to comment.