remove all explicit quoting (#36488)

The schemas must contain quoted names for fields and tables when
necessary.
This commit is contained in:
Benjamin Dauvergne 2019-09-28 02:56:06 +02:00
parent f92ae54d47
commit 29932ac31c
3 changed files with 18 additions and 18 deletions

View File

@ -67,7 +67,7 @@ class EngineDimension(object):
with self.engine.get_cursor() as cursor:
sql = self.members_query
if not sql:
table_expression = '"%s"' % self.engine_cube.fact_table
table_expression = '%s' % self.engine_cube.fact_table
if self.join:
table_expression = self.engine_cube.build_table_expression(
self.join, self.engine_cube.fact_table)
@ -111,12 +111,12 @@ class SchemaJSONDimension(schemas.Dimension):
self.value_label = expr
self.value = expr
self.join = ['json_' + name]
sql = ('SELECT DISTINCT "{json_field}"->>\'%s\' AS v, "{json_field}"->>\'%s\' AS v'
' FROM "{{fact_table}}" WHERE ("{json_field}"->>\'%s\') IS NOT NULL ORDER BY v' %
sql = ('SELECT DISTINCT {json_field}->>\'%s\' AS v, {json_field}->>\'%s\' AS v'
' FROM {{fact_table}} WHERE ({json_field}->>\'%s\') IS NOT NULL ORDER BY v' %
(self.name, self.name, self.name))
self.members_query = sql.format(json_field=json_field)
self.filter_expression = ('("{fact_table}".id IS NULL '
'OR ("{fact_table}.%s"->>\'%s\') IN (%%s))'
self.filter_expression = ('({fact_table}.id IS NULL '
'OR ({fact_table}.%s->>\'%s\') IN (%%s))'
% (json_field, name))
self.filter_needs_join = False
@ -223,7 +223,7 @@ class EngineCube(object):
def count(self):
with self.engine.get_cursor() as cursor:
cursor.execute('SELECT count(%s) FROM "%s"' % (self.key, self.fact_table))
cursor.execute('SELECT count(%s) FROM %s' % (self.key, self.fact_table))
return cursor.fetchone()[0]
def get_join(self, name):
@ -231,9 +231,9 @@ class EngineCube(object):
json_key = name[5:]
return schemas.Join(
name=name,
table='(SELECT DISTINCT "%s"."%s"->>\'%s\' AS value FROM "%s" ORDER BY value)' % (
table='(SELECT DISTINCT %s.%s->>\'%s\' AS value FROM %s ORDER BY value)' % (
self.fact_table, self.json_field, json_key, self.fact_table),
master='"%s"->>\'%s\'' % (self.json_field, json_key),
master='%s->>\'%s\'' % (self.json_field, json_key),
detail='value',
)
return self.cube.get_join(name)
@ -276,7 +276,7 @@ class EngineCube(object):
if measure.expression not in projections:
projections.append(measure.expression + ' AS ' + measure.name)
sql = 'SELECT ' + ', '.join(projections)
table_expression = ' "%s"' % self.cube.fact_table
table_expression = ' %s' % self.cube.fact_table
if joins:
table_expression = self.build_table_expression(
joins, self.fact_table, other_conditions=join_conditions)
@ -331,9 +331,9 @@ class EngineCube(object):
if table_name.strip().startswith('('):
sql = table_name
else:
sql = '"%s"' % table_name
sql = '%s' % table_name
if alias:
sql += ' AS "%s"' % alias
sql += ' AS %s' % alias
add_paren = False
for kind in ['left', 'inner', 'right', 'full']:
joins = join_tree.get(alias or table_name, {}).get(kind)
@ -355,7 +355,7 @@ class EngineCube(object):
for join_name, join in joins.iteritems():
sub_joins.append(
build_table_expression_helper(join_tree, join.table, alias=join.name, top=False))
conditions.append('"%s".%s = "%s"."%s"' % (
conditions.append('%s.%s = %s.%s' % (
alias or table_name,
join.master.split('.')[-1],
join.name, join.detail))
@ -391,6 +391,6 @@ class Engine(object):
def get_cursor(self):
with contextlib.closing(psycopg2.connect(self.warehouse.pg_dsn)) as connection:
with connection.cursor() as cursor:
search_path = ', '.join(['"%s"' % namespace for namespace in self.warehouse.search_path])
search_path = ', '.join(['%s' % namespace for namespace in self.warehouse.search_path])
cursor.execute('SET SEARCH_PATH = %s' % search_path)
yield cursor

View File

@ -7,7 +7,7 @@
{
"name": "facts1",
"label": "Facts 1",
"fact_table": "facts",
"fact_table": "\"Facts\"",
"key": "id",
"joins": [
{
@ -168,7 +168,7 @@
"name": "string",
"label": "String",
"type": "string",
"value": "string"
"value": "\"String\""
}
],
"measures": [

View File

@ -15,7 +15,7 @@ CREATE TABLE subcategory (
ord integer default(0) not null,
label varchar not null);
CREATE TABLE facts (
CREATE TABLE "Facts" (
id serial primary key,
date date,
datetime timestamp with time zone,
@ -26,7 +26,7 @@ CREATE TABLE facts (
leftsubcategory_id integer references schema1.subcategory(id),
rightsubcategory_id integer references schema1.subcategory(id),
outersubcategory_id integer references schema1.subcategory(id),
string varchar
"String" varchar
);
INSERT INTO category (ord, label) VALUES
@ -46,7 +46,7 @@ INSERT INTO subcategory (category_id, ord, label) VALUES
(3, 0, 'subé9');
INSERT INTO facts (date, datetime, integer, boolean, cnt, innersubcategory_id, leftsubcategory_id, rightsubcategory_id, outersubcategory_id, string) VALUES
INSERT INTO "Facts" (date, datetime, integer, boolean, cnt, innersubcategory_id, leftsubcategory_id, rightsubcategory_id, outersubcategory_id, "String") VALUES
('2017-01-01', '2017-01-01 10:00', 1, FALSE, 10, 1, 1, 1, 1, 'a'),
('2017-01-02', '2017-01-02 10:00', 1, TRUE, 10, 3, 3, 3, 3, 'b'),
('2017-01-03', '2017-01-03 10:00', 1, FALSE, 10, NULL, NULL, NULL, NULL, 'a'),