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

🔨 create related table view

parent a6c4d671
......@@ -6,7 +6,7 @@ schemaname=$1
schematype=$2
export PGPASSWORD=${password}
echo Installation des fonctionnalités pour le schema ${schemaname} de la base ${dbname} sur ${host} ${port}
for fichier in sql/all/function*.sql sql/$schematype/function*.sql sql/$schematype/constraints*.sql sql/all/trigger*.sql sql/$schematype/trigger*.sql sql/$schematype/create_view*.sql
for fichier in sql/all/view*.sql sql/all/function*.sql sql/$schematype/function*.sql sql/$schematype/constraints*.sql sql/all/trigger*.sql sql/$schematype/trigger*.sql sql/$schematype/create_view*.sql
do
psql -h ${host} -d ${dbname} -U ${user} -p ${port} -b -q -v schemaname=${schemaname} -f $fichier
done
......
......@@ -6,7 +6,7 @@ schemaname=$1
networktype=$2
export PGPASSWORD=${password}
echo Installation des fonctionnalités réseau pour le schema ${schemaname} de la base ${dbname} sur ${host} ${port}
for fichier in sql/network/function*.sql sql/all/function*.sql sql/network/$networktype/function*.sql sql/network/$networktype/constraints*.sql sql/all/trigger*.sql sql/network/trigger*.sql sql/network/$networktype/trigger*.sql
for fichier in sql/all/view*.sql sql/network/function*.sql sql/all/function*.sql sql/network/$networktype/function*.sql sql/network/$networktype/constraints*.sql sql/all/trigger*.sql sql/network/trigger*.sql sql/network/$networktype/trigger*.sql
do
psql -h ${host} -d ${dbname} -U ${user} -p ${port} -b -q -v schemaname=${schemaname} -f $fichier
done
......
......@@ -8,38 +8,9 @@ from qgis.core import QgsDataSourceUri, QgsWkbTypes, QgsMapLayerType,QgsFeature
# execute : pyrcc5 -o resources.py resources.qrc
from . import resources
qconst = """select kcu.table_schema as ft_schema,
kcu.table_name as ft_name,
kcu.table_schema || '.' || kcu.table_name as foreign_table,
kcu.column_name as fk_column,
rel_kcu.table_schema as pt_schema,
rel_kcu.table_name as pt_name,
rel_kcu.table_schema || '.' || rel_kcu.table_name as primary_table,
rel_kcu.column_name as pk_column,
kcu.ordinal_position as no,
kcu.constraint_name
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on tco.constraint_schema = kcu.constraint_schema
and tco.constraint_name = kcu.constraint_name
join information_schema.referential_constraints rco
on tco.constraint_schema = rco.constraint_schema
and tco.constraint_name = rco.constraint_name
join information_schema.key_column_usage rel_kcu
on rco.unique_constraint_schema = rel_kcu.constraint_schema
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.table_schema='{0}'
and rel_kcu.table_name='{1}'
and kcu.column_name=rel_kcu.column_name
order by kcu.table_schema,
kcu.table_name,
kcu.ordinal_position;"""
qconst2="""select constraint_schema, constraint_name from information_schema.table_constraints
where constraint_type = 'FOREIGN KEY'
and constraint_schema='{0}'
and constraint_name like '%\_level\_fkey'
and is_deferrable='YES';"""
qconst = """select * from utils.related_table
where pt_schema='{0}'
and pt_name='{1}';"""
def executeQuery(provider, query):
# provider = layer.dataProvider()
......
\encoding UTF8
set search_path to utils, public;
-- VUE recensant les tables reliées
CREATE OR REPLACE VIEW utils.related_table AS
select kcu.table_schema as ft_schema,
kcu.table_name as ft_name,
kcu.table_schema || '.' || kcu.table_name as foreign_table,
kcu.column_name as fk_column,
rel_kcu.table_schema as pt_schema,
rel_kcu.table_name as pt_name,
rel_kcu.table_schema || '.' || rel_kcu.table_name as primary_table,
rel_kcu.column_name as pk_column,
kcu.ordinal_position as no,
kcu.constraint_name
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on tco.constraint_schema = kcu.constraint_schema
and tco.constraint_name = kcu.constraint_name
join information_schema.referential_constraints rco
on tco.constraint_schema = rco.constraint_schema
and tco.constraint_name = rco.constraint_name
join information_schema.key_column_usage rel_kcu
on rco.unique_constraint_schema = rel_kcu.constraint_schema
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;
......@@ -6,34 +6,9 @@ SET tmpvar.schemaname TO :'schemaname';
DO $$
DECLARE
_relations CURSOR FOR
select kcu.table_schema as ft_schema,
kcu.table_name as ft_name,
kcu.table_schema || '.' || kcu.table_name as foreign_table,
kcu.column_name as fk_column,
rel_kcu.table_schema as pt_schema,
rel_kcu.table_name as pt_name,
rel_kcu.table_schema || '.' || rel_kcu.table_name as primary_table,
rel_kcu.column_name as pk_column,
kcu.ordinal_position as no,
kcu.constraint_name
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on tco.constraint_schema = kcu.constraint_schema
and tco.constraint_name = kcu.constraint_name
join information_schema.referential_constraints rco
on tco.constraint_schema = rco.constraint_schema
and tco.constraint_name = rco.constraint_name
join information_schema.key_column_usage rel_kcu
on rco.unique_constraint_schema = rel_kcu.constraint_schema
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.table_schema=current_setting('tmpvar.schemaname', true)
and kcu.column_name=rel_kcu.column_name
and kcu.ordinal_position=1
order by kcu.table_schema,
kcu.table_name,
kcu.ordinal_position;
select * from utils.related_table
where ft_schema=current_setting('tmpvar.schemaname', true)
and no=1;
l record;
_list_columns text;
_view_name text;
......
......@@ -8,33 +8,8 @@ CREATE OR REPLACE FUNCTION fnc_duplicate_attributes(
RETURNS void AS $BODY$
DECLARE
_relations CURSOR FOR
select kcu.table_schema as ft_schema,
kcu.table_name as ft_name,
kcu.table_schema || '.' || kcu.table_name as foreign_table,
kcu.column_name as fk_column,
rel_kcu.table_schema as pt_schema,
rel_kcu.table_name as pt_name,
rel_kcu.table_schema || '.' || rel_kcu.table_name as primary_table,
rel_kcu.column_name as pk_column,
kcu.ordinal_position as no,
kcu.constraint_name
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on tco.constraint_schema = kcu.constraint_schema
and tco.constraint_name = kcu.constraint_name
join information_schema.referential_constraints rco
on tco.constraint_schema = rco.constraint_schema
and tco.constraint_name = rco.constraint_name
join information_schema.key_column_usage rel_kcu
on rco.unique_constraint_schema = rel_kcu.constraint_schema
and rco.unique_constraint_name = rel_kcu.constraint_name
and kcu.ordinal_position = rel_kcu.ordinal_position
where tco.constraint_type = 'FOREIGN KEY'
and (rel_kcu.table_schema || '.' || rel_kcu.table_name) = i_nw_regclass::text
and kcu.column_name = rel_kcu.column_name
order by kcu.table_schema,
kcu.table_name,
kcu.ordinal_position;
select * from utils.related_table
where foreign_table = i_nw_regclass::text;
_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