Commit e8f8a072 authored by Alice Salsé's avatar Alice Salsé
Browse files

🔨 decode values export csv

parent b37cdd15
......@@ -11,7 +11,8 @@ import csv
qconst = """select * from utils.vm_related_table
where pt_schema='{0}'
and pt_name='{1}';"""
and pt_name='{1}'
and fk_column=pk_column;"""
def executeQuery(provider, query):
# provider = layer.dataProvider()
......@@ -455,10 +456,17 @@ class NetworkTools:
if dial.exec_() == QDialog.Accepted:
print(dial.itemsSelected())
l_qtablename=layer.dataProvider().uri().quotedTablename ()
column_names, records = executeQuery(layer.dataProvider()
, "select p.id, p.start_node, p.end_node {2} \
from utils.fnc_order_pipe('{0}'::regclass, ARRAY[{1}]) p \
left outer join {0} x on p.id = x.id;".format(l_qtablename, ','.join([str(i) for i in l_ids]), [','+','.join([str(i) for i in dial.itemsSelected()]), ''][len(dial.itemsSelected())==0]))
if len(dial.itemsSelected()) == 0 :
column_names, records = executeQuery(layer.dataProvider()
, "select p.id, p.start_node, p.end_node \
from utils.fnc_order_pipe('{0}'::regclass, ARRAY[{1}]) p;".format(l_qtablename, ','.join([str(i) for i in l_ids])))
else :
col_names, rcrds = executeQuery(layer.dataProvider(), "select utils.fnc_decode_query('{0}'::regclass, ARRAY['{1}']) as query;".format(l_qtablename, "','".join([str(i) for i in dial.itemsSelected()])));
column_names, records = executeQuery(layer.dataProvider()
, "with decode_values as ( {3} where id = ANY(ARRAY[{1}]) )\
select p.id, p.start_node, p.end_node {2} \
from utils.fnc_order_pipe('{0}'::regclass, ARRAY[{1}]) p \
left outer join decode_values x on p.id = x.id;".format(l_qtablename, ','.join([str(i) for i in l_ids]), [','+','.join([str(i) for i in dial.itemsSelected()]), ''][len(dial.itemsSelected())==0], rcrds[0][0]))
with open(dir[0], 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(column_names)
......
......@@ -27,7 +27,6 @@ CREATE MATERIALIZED VIEW utils.vm_related_table AS
and rco.unique_constraint_name = rel_kcu.constraint_name
and kcu.ordinal_position = rel_kcu.ordinal_position
where tco.constraint_type = 'FOREIGN KEY'
and kcu.column_name=rel_kcu.column_name
order by kcu.table_schema,
kcu.table_name,
kcu.ordinal_position;
......@@ -8,7 +8,7 @@ DECLARE
_relations CURSOR FOR
select * from utils.vm_related_table
where ft_schema=current_setting('tmpvar.schemaname', true)
and no=1;
and fk_column=pk_column;
l record;
_list_columns text;
_view_name text;
......
\encoding UTF8
set search_path to utils, public;
CREATE OR REPLACE FUNCTION fnc_decode_query(
IN i_regclass regclass,
IN i_fields text[],
OUT o_query text
)
RETURNS text AS $BODY$
DECLARE
_rec record;
_field_def text[];
_query text;
BEGIN
--==== Order selected pipes ====
FOR _rec in EXECUTE '
with related_table as (
select * from utils.vm_related_table
where foreign_table = '|| quote_nullable(replace(i_regclass::text,'vw_','')) ||'::text
and fk_column=pk_column
)
, value_lists as(
select s.* from utils.vm_related_table s, related_table r
where (s.foreign_table = r.primary_table or s.foreign_table = r.foreign_table)
and s.fk_column = ANY (ARRAY['''|| array_to_string(i_fields, ''',''') ||'''])
and s.pt_name like ''vl_%''
)
SELECT field, v.*, n.column_name
FROM unnest(ARRAY['''|| array_to_string(i_fields, ''',''') ||''']) field
left outer join value_lists v on v.fk_column = field
left outer join information_schema.columns c
on c.table_schema=v.pt_schema and c.table_name=v.pt_name and c.column_name=v.pk_column
left outer join information_schema.columns n
on n.table_schema=v.pt_schema and n.table_name=v.pt_name and n.ordinal_position=c.ordinal_position+1
;'
LOOP
-- RAISE INFO '%', _rec;
IF _rec.column_name is null THEN
_field_def := array_append(_field_def, 'a.'||_rec.field);
else
_field_def := array_append(_field_def, '(select d.'||_rec.column_name||' from '||_rec.primary_table||' d where d.'||_rec.pk_column||'=a.'||_rec.fk_column||') as '||_rec.fk_column);
END IF;
END LOOP;
o_query := 'select id, '|| array_to_string(_field_def, ',') ||' FROM '||i_regclass||' a';
RETURN;
END;
$BODY$
LANGUAGE plpgsql STABLE SECURITY DEFINER
COST 100;
......@@ -9,7 +9,8 @@ RETURNS void AS $BODY$
DECLARE
_relations CURSOR FOR
select * from utils.vm_related_table
where primary_table = i_nw_regclass::text;
where primary_table = i_nw_regclass::text
and fk_column=pk_column;
_new_obj jsonb;
BEGIN
-- ==== Select foreign table from primary table ===
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment