Skip to content

Add two functions to sqlpetr package #227

@SophieMYang

Description

@SophieMYang

Add these two functions to the sqlpetr package.
They are developed in file 150 and used in 155 as part of the dplyr exercises.

sp_tbl_pk_fk_sql <- function(table_name) {
dbGetQuery(con
,"SELECT c.table_name
,kcu.column_name
,c.constraint_name
,c.constraint_type
,coalesce(c2.table_name, '') ref_table
,coalesce(kcu2.column_name, '') ref_table_col
FROM information_schema.tables t
LEFT JOIN information_schema.table_constraints c
ON t.table_catalog = c.table_catalog
AND t.table_schema = c.table_schema
AND t.table_name = c.table_name
LEFT JOIN information_schema.key_column_usage kcu
ON c.constraint_schema = kcu.constraint_schema
AND c.constraint_name = kcu.constraint_name
LEFT JOIN information_schema.referential_constraints rc
ON c.constraint_schema = rc.constraint_schema
AND c.constraint_name = rc.constraint_name
LEFT JOIN information_schema.table_constraints c2
ON rc.unique_constraint_schema = c2.constraint_schema
AND rc.unique_constraint_name = c2.constraint_name
LEFT JOIN information_schema.key_column_usage kcu2
ON c2.constraint_schema = kcu2.constraint_schema
AND c2.constraint_name = kcu2.constraint_name
AND kcu.ordinal_position = kcu2.ordinal_position
WHERE c.constraint_type IN ('PRIMARY KEY', 'FOREIGN KEY')
AND c.table_catalog = 'dvdrental'
AND c.table_schema = 'public'
AND (c.table_name = $1 or coalesce(c2.table_name, '') = $1)
ORDER BY c.table_name,c.constraint_type desc"
,param = list(table_name)
)
}

sp_tbl_descr <- function (table_name) {
dbGetQuery(
con,
"select btrim(c.table_name) table_name, c.ordinal_position seq
, c.column_name COL_NAME
, case when c.udt_name = 'varchar'
then c.udt_name ||
case when c.character_maximum_length is not null
then '('||cast(c.character_maximum_length as varchar)||')'
else ''
end
when c.udt_name like ('int%')
then c.udt_name ||'-'||cast(c.numeric_precision as varchar)
else c.udt_name
end COL_TYPE
, c.is_nullable is_null
-- , c.column_default
-- , t.table_catalog
,t.table_schema
from dvdrental.information_schema.columns c
join information_schema.tables t on c.table_name = t.table_name
where 1 = 1
and c.table_catalog = 'dvdrental'
and c.table_name = $1"
,table_name
)
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions