-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added all like options while creating a table. #6377
- Loading branch information
1 parent
ac50b4d
commit 27c7ea2
Showing
40 changed files
with
9,612 additions
and
82 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
235 changes: 235 additions & 0 deletions
235
...er/server_groups/servers/databases/schemas/tables/templates/tables/sql/14_plus/create.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
{% import 'macros/schemas/security.macros' as SECLABEL %} | ||
{% import 'macros/schemas/privilege.macros' as PRIVILEGE %} | ||
{% import 'macros/variable.macros' as VARIABLE %} | ||
{% import 'columns/macros/security.macros' as COLUMN_SECLABEL %} | ||
{% import 'columns/macros/privilege.macros' as COLUMN_PRIVILEGE %} | ||
{% import 'tables/sql/macros/constraints.macro' as CONSTRAINTS %} | ||
{% import 'types/macros/get_full_type_sql_format.macros' as GET_TYPE %} | ||
{#===========================================#} | ||
{#====== MAIN TABLE TEMPLATE STARTS HERE ======#} | ||
{#===========================================#} | ||
{# | ||
If user has not provided any details but only name then | ||
add empty bracket with table name | ||
#} | ||
{% set empty_bracket = ""%} | ||
{% if data.coll_inherits|length == 0 and data.columns|length == 0 and not data.typname and not data.like_relation and data.primary_key|length == 0 and data.unique_constraint|length == 0 and data.foreign_key|length == 0 and data.check_constraint|length == 0 and data.exclude_constraint|length == 0 %} | ||
{% set empty_bracket = "\n(\n)"%} | ||
{% endif %} | ||
{% set with_clause = false%} | ||
{% if data.fillfactor or data.parallel_workers or data.toast_tuple_target or (data.autovacuum_custom and data.add_vacuum_settings_in_sql) or data.autovacuum_enabled in ('t', 'f') or (data.toast_autovacuum and data.add_vacuum_settings_in_sql) or data.toast_autovacuum_enabled in ('t', 'f') %} | ||
{% set with_clause = true%} | ||
{% endif %} | ||
CREATE {% if data.relpersistence %}UNLOGGED {% endif %}TABLE{% if add_not_exists_clause %} IF NOT EXISTS{% endif %} {{conn|qtIdent(data.schema, data.name)}}{{empty_bracket}} | ||
{% if data.typname %} | ||
OF {{ data.typname }} | ||
{% endif %} | ||
{% if data.like_relation or data.coll_inherits or data.columns|length > 0 or data.primary_key|length > 0 or data.unique_constraint|length > 0 or data.foreign_key|length > 0 or data.check_constraint|length > 0 or data.exclude_constraint|length > 0 %} | ||
( | ||
{% endif %} | ||
{% if data.like_relation %} | ||
LIKE {{ data.like_relation }}{% if data.like_default_value %} | ||
|
||
INCLUDING DEFAULTS{% endif %}{% if data.like_constraints %} | ||
|
||
INCLUDING CONSTRAINTS{% endif %}{% if data.like_indexes %} | ||
|
||
INCLUDING INDEXES{% endif %}{% if data.like_storage %} | ||
|
||
INCLUDING STORAGE{% endif %}{% if data.like_comments %} | ||
|
||
INCLUDING COMMENTS{% endif %}{% if data.like_compression %} | ||
|
||
INCLUDING COMPRESSION{% endif %}{% if data.like_generated %} | ||
|
||
INCLUDING GENERATED{% endif %}{% if data.like_identity %} | ||
|
||
INCLUDING IDENTITY{% endif %}{% if data.like_statistics %} | ||
|
||
INCLUDING STATISTICS{% endif %}{% if data.columns|length > 0 %}, | ||
{% endif %} | ||
|
||
{% endif %} | ||
{### Add columns ###} | ||
{% if data.columns and data.columns|length > 0 %} | ||
{% for c in data.columns %} | ||
{% if c.name and c.cltype %} | ||
{% if c.inheritedfromtable %}-- Inherited from table {{c.inheritedfromtable}}: {% elif c.inheritedfromtype %}-- Inherited from type {{c.inheritedfromtype}}: {% endif %}{{conn|qtIdent(c.name)}} {% if is_sql %}{{c.displaytypname}}{% else %}{{ GET_TYPE.CREATE_TYPE_SQL(conn, c.cltype, c.attlen, c.attprecision, c.hasSqrBracket) }}{% endif %}{% if c.collspcname %} COLLATE {{c.collspcname}}{% endif %}{% if c.attnotnull %} NOT NULL{% endif %}{% if c.defval is defined and c.defval is not none and c.defval != '' and c.colconstype != 'g' %} DEFAULT {{c.defval}}{% endif %} | ||
{% if c.colconstype == 'i' and c.attidentity and c.attidentity != '' %} | ||
{% if c.attidentity == 'a' %} GENERATED ALWAYS AS IDENTITY{% elif c.attidentity == 'd' %} GENERATED BY DEFAULT AS IDENTITY{% endif %} | ||
{% if c.seqincrement or c.seqcycle or c.seqincrement or c.seqstart or c.seqmin or c.seqmax or c.seqcache %} ( {% endif %} | ||
{% if c.seqcycle is defined and c.seqcycle %} | ||
CYCLE {% endif %}{% if c.seqincrement is defined and c.seqincrement|int(-1) > -1 %} | ||
INCREMENT {{c.seqincrement|int}} {% endif %}{% if c.seqstart is defined and c.seqstart|int(-1) > -1%} | ||
START {{c.seqstart|int}} {% endif %}{% if c.seqmin is defined and c.seqmin|int(-1) > -1%} | ||
MINVALUE {{c.seqmin|int}} {% endif %}{% if c.seqmax is defined and c.seqmax|int(-1) > -1%} | ||
MAXVALUE {{c.seqmax|int}} {% endif %}{% if c.seqcache is defined and c.seqcache|int(-1) > -1%} | ||
CACHE {{c.seqcache|int}} {% endif %} | ||
{% if c.seqincrement or c.seqcycle or c.seqincrement or c.seqstart or c.seqmin or c.seqmax or c.seqcache %}){% endif %} | ||
{% endif %} | ||
{% if c.colconstype == 'g' and c.genexpr and c.genexpr != '' %} GENERATED ALWAYS AS ({{c.genexpr}}) STORED{% endif %} | ||
{% if not loop.last %}, | ||
{% endif %} | ||
{% endif %} | ||
{% endfor %} | ||
{% endif %} | ||
{# Macro to render for constraints #} | ||
{% if data.primary_key|length > 0 %}{% if data.columns|length > 0 %},{% endif %} | ||
{{CONSTRAINTS.PRIMARY_KEY(conn, data.primary_key[0])}}{% endif %}{% if data.unique_constraint|length > 0 %}{% if data.columns|length > 0 or data.primary_key|length > 0 %},{% endif %} | ||
{{CONSTRAINTS.UNIQUE(conn, data.unique_constraint)}}{% endif %}{% if data.foreign_key|length > 0 %}{% if data.columns|length > 0 or data.primary_key|length > 0 or data.unique_constraint|length > 0 %},{% endif %} | ||
{{CONSTRAINTS.FOREIGN_KEY(conn, data.foreign_key)}}{% endif %}{% if data.check_constraint|length > 0 %}{% if data.columns|length > 0 or data.primary_key|length > 0 or data.unique_constraint|length > 0 or data.foreign_key|length > 0 %},{% endif %} | ||
{{CONSTRAINTS.CHECK(conn, data.check_constraint)}}{% endif %}{% if data.exclude_constraint|length > 0 %}{% if data.columns|length > 0 or data.primary_key|length > 0 or data.unique_constraint|length > 0 or data.foreign_key|length > 0 or data.check_constraint|length > 0 %},{% endif %} | ||
{{CONSTRAINTS.EXCLUDE(conn, data.exclude_constraint)}}{% endif %} | ||
{% if data.like_relation or data.coll_inherits or data.columns|length > 0 or data.primary_key|length > 0 or data.unique_constraint|length > 0 or data.foreign_key|length > 0 or data.check_constraint|length > 0 or data.exclude_constraint|length > 0 %} | ||
|
||
){% endif %}{% if data.relkind is defined and data.relkind == 'p' %} PARTITION BY {{ data.partition_scheme }}{% endif %} | ||
{% if not data.coll_inherits and not data.spcname and not with_clause %};{% endif %} | ||
|
||
{### If we are inheriting it from another table(s) ###} | ||
{% if data.coll_inherits %} | ||
INHERITS ({% for val in data.coll_inherits %}{% if loop.index != 1 %}, {% endif %}{{val}}{% endfor %}){% if not data.spcname and not with_clause %};{% endif %} | ||
{% endif %} | ||
|
||
{% if with_clause %} | ||
{% set ns = namespace(add_comma=false) %} | ||
WITH ( | ||
{% if data.fillfactor %}{% set ns.add_comma = true%} | ||
FILLFACTOR = {{ data.fillfactor }}{% endif %}{% if data.parallel_workers %} | ||
{% if ns.add_comma %}, | ||
{% endif %} | ||
parallel_workers = {{ data.parallel_workers }}{% set ns.add_comma = true%}{% endif %}{% if data.toast_tuple_target %} | ||
{% if ns.add_comma %}, | ||
{% endif %} | ||
toast_tuple_target = {{ data.toast_tuple_target }}{% set ns.add_comma = true%}{% endif %}{% if data.autovacuum_enabled in ('t', 'f') %} | ||
{% if ns.add_comma %}, | ||
{% endif %} | ||
autovacuum_enabled = {% if data.autovacuum_enabled == 't' %}TRUE{% else %}FALSE{% endif %}{% set ns.add_comma = true%}{% endif %}{% if data.toast_autovacuum_enabled in ('t', 'f') %} | ||
{% if ns.add_comma %}, | ||
{% endif %} | ||
toast.autovacuum_enabled = {% if data.toast_autovacuum_enabled == 't' %}TRUE{% else %}FALSE{% endif %}{% set ns.add_comma = true%}{% endif %}{% if data.autovacuum_custom %} | ||
{% for opt in data.vacuum_table %}{% if opt.name and opt.value is defined %} | ||
{% if ns.add_comma %}, | ||
{% endif %} | ||
{{opt.name}} = {{opt.value}}{% set ns.add_comma = true%}{% endif %} | ||
{% endfor %}{% endif %}{% if data.toast_autovacuum %} | ||
{% for opt in data.vacuum_toast %}{% if opt.name and opt.value is defined %} | ||
{% if ns.add_comma %}, | ||
{% endif %} | ||
toast.{{opt.name}} = {{opt.value}}{% set ns.add_comma = true%}{% endif %} | ||
{% endfor %}{% endif %} | ||
|
||
{% if data.spcname %}){% else %});{% endif %} | ||
|
||
{% endif %} | ||
{### SQL for Tablespace ###} | ||
{% if data.spcname %} | ||
TABLESPACE {{ conn|qtIdent(data.spcname) }}; | ||
{% endif %} | ||
{### Alter SQL for Owner ###} | ||
{% if data.relowner %} | ||
|
||
ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}} | ||
OWNER to {{conn|qtIdent(data.relowner)}}; | ||
{% endif %} | ||
|
||
{#####################################################} | ||
{## Enable Row Level Security Policy on table ##} | ||
{#####################################################} | ||
{% if data.rlspolicy %} | ||
ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}} | ||
ENABLE ROW LEVEL SECURITY; | ||
{% endif %} | ||
|
||
{#####################################################} | ||
{## Force Enable Row Level Security Policy on table ##} | ||
{#####################################################} | ||
{% if data.forcerlspolicy %} | ||
ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}} | ||
FORCE ROW LEVEL SECURITY; | ||
{% endif %} | ||
|
||
{### Security Labels on Table ###} | ||
{% if data.seclabels and data.seclabels|length > 0 %} | ||
|
||
{% for r in data.seclabels %} | ||
{{ SECLABEL.SET(conn, 'TABLE', data.name, r.provider, r.label, data.schema) }} | ||
{% endfor %} | ||
{% endif %} | ||
{### ACL on Table ###} | ||
{% if data.revoke_all %} | ||
{% for priv in data.revoke_all %} | ||
{{ PRIVILEGE.UNSETALL(conn, "TABLE", priv, data.name, data.schema)}} | ||
{% endfor %} | ||
{% endif %} | ||
{% if data.relacl %} | ||
|
||
{% for priv in data.relacl %} | ||
{{ PRIVILEGE.SET(conn, 'TABLE', priv.grantee, data.name, priv.without_grant, priv.with_grant, data.schema) }} | ||
{% endfor %} | ||
{% endif %} | ||
{### SQL for COMMENT ###} | ||
{% if data.description %} | ||
COMMENT ON TABLE {{conn|qtIdent(data.schema, data.name)}} | ||
IS {{data.description|qtLiteral(conn)}}; | ||
{% endif %} | ||
{#===========================================#} | ||
{#====== MAIN TABLE TEMPLATE ENDS HERE ======#} | ||
{#===========================================#} | ||
{#===========================================#} | ||
{# COLUMN SPECIFIC TEMPLATES STARTS HERE #} | ||
{#===========================================#} | ||
{% if data.columns and data.columns|length > 0 %} | ||
{% for c in data.columns %} | ||
{% if c.description %} | ||
|
||
COMMENT ON COLUMN {{conn|qtIdent(data.schema, data.name, c.name)}} | ||
IS {{c.description|qtLiteral(conn)}}; | ||
{% endif %} | ||
{### Add variables to column ###} | ||
{% if c.attoptions and c.attoptions|length > 0 %} | ||
|
||
ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}} | ||
{{ VARIABLE.SET(conn, 'COLUMN', c.name, c.attoptions) }} | ||
|
||
{% endif %} | ||
{### Alter column statistics value ###} | ||
{% if c.attstattarget is defined and c.attstattarget > -1 %} | ||
ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}} | ||
ALTER COLUMN {{conn|qtTypeIdent(c.name)}} SET STATISTICS {{c.attstattarget}}; | ||
|
||
{% endif %} | ||
{### Alter column storage value ###} | ||
{% if c.attstorage is defined and c.attstorage != c.defaultstorage %} | ||
ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}} | ||
ALTER COLUMN {{conn|qtTypeIdent(c.name)}} SET STORAGE {%if c.attstorage == 'p' %} | ||
PLAIN{% elif c.attstorage == 'm'%}MAIN{% elif c.attstorage == 'e'%} | ||
EXTERNAL{% elif c.attstorage == 'x'%}EXTENDED{% endif %}; | ||
|
||
{% endif %} | ||
{### ACL ###} | ||
{% if c.attacl and c.attacl|length > 0 %} | ||
|
||
{% for priv in c.attacl %} | ||
{{ COLUMN_PRIVILEGE.APPLY(conn, data.schema, data.name, c.name, priv.grantee, priv.without_grant, priv.with_grant) }} | ||
{% endfor %} | ||
{% endif %} | ||
{### Security Lables ###} | ||
{% if c.seclabels and c.seclabels|length > 0 %} | ||
|
||
{% for r in c.seclabels %} | ||
{{ COLUMN_SECLABEL.APPLY(conn, 'COLUMN',data.schema, data.name, c.name, r.provider, r.label) }} | ||
{% endfor %} | ||
{% endif %} | ||
{% endfor %} | ||
{% endif %} | ||
{#===========================================#} | ||
{# COLUMN SPECIFIC TEMPLATES ENDS HERE #} | ||
{#===========================================#} | ||
{#======================================#} | ||
{# CONSTRAINTS SPECIFIC TEMPLATES #} | ||
{#======================================#} | ||
{{CONSTRAINTS.CONSTRAINT_COMMENTS(conn, data.schema, data.name, data.primary_key)}} | ||
{{CONSTRAINTS.CONSTRAINT_COMMENTS(conn, data.schema, data.name, data.unique_constraint)}} | ||
{{CONSTRAINTS.CONSTRAINT_COMMENTS(conn, data.schema, data.name, data.foreign_key)}} | ||
{{CONSTRAINTS.CONSTRAINT_COMMENTS(conn, data.schema, data.name, data.check_constraint)}} | ||
{{CONSTRAINTS.CONSTRAINT_COMMENTS(conn, data.schema, data.name, data.exclude_constraint)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.