
    VhO                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZmZ d dlmZmZmZmZmZmZ  G d d	e      Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: postgresql_table
short_description: Create, drop, or modify a PostgreSQL table
description:
- Allows to create, drop, truncate a table, or change some table attributes.
options:
  table:
    description:
    - Table name.
    required: true
    aliases:
    - name
    type: str
  state:
    description:
    - The table state. I(state=absent) is mutually exclusive with I(tablespace), I(owner), I(unlogged),
      I(like), I(including), I(columns), I(truncate), I(storage_params) and, I(rename).
    type: str
    default: present
    choices: [ absent, present ]
  tablespace:
    description:
    - Set a tablespace for the table.
    type: str
  owner:
    description:
    - Set a table owner.
    type: str
  unlogged:
    description:
    - Create an unlogged table.
    type: bool
    default: false
  like:
    description:
    - Create a table like another table (with similar DDL).
      Mutually exclusive with I(columns), I(rename), and I(truncate).
    type: str
  including:
    description:
    - Keywords that are used with like parameter, may be DEFAULTS, CONSTRAINTS, INDEXES, STORAGE, COMMENTS or ALL.
      Needs I(like) specified. Mutually exclusive with I(columns), I(rename), and I(truncate).
    type: str
  columns:
    description:
    - Columns that are needed.
    type: list
    elements: str
  rename:
    description:
    - DEPRECATED (see the L(discussion,https://github.com/ansible-collections/community.postgresql/issues/820)). This option will be removed in version 5.0.0.
      To rename a table, use the M(community.postgresql.postgresql_query) module.
    - New table name. Mutually exclusive with I(tablespace), I(owner),
      I(unlogged), I(like), I(including), I(columns), I(truncate), and I(storage_params).
    type: str
  truncate:
    description:
    - Truncate a table. Mutually exclusive with I(tablespace), I(owner), I(unlogged),
      I(like), I(including), I(columns), I(rename), and I(storage_params).
    type: bool
    default: false
  storage_params:
    description:
    - Storage parameters like fillfactor, autovacuum_vacuum_treshold, etc.
      Mutually exclusive with I(rename) and I(truncate).
    type: list
    elements: str
  login_db:
    description:
    - Name of database to connect and where the table will be created.
    - The V(db) alias is deprecated and will be removed in version 5.0.0.
    type: str
    default: ''
    aliases:
    - db
  session_role:
    description:
    - Switch to session_role after connecting.
      The specified session_role must be a role that the current login_user is a member of.
    - Permissions checking for SQL commands is carried out as though
      the session_role were the one that had logged in originally.
    type: str
  cascade:
    description:
    - Automatically drop objects that depend on the table (such as views).
      Used with I(state=absent) only.
    type: bool
    default: false
  trust_input:
    description:
    - If C(false), check whether values of parameters are potentially dangerous.
    - It makes sense to use C(false) only when SQL injections are possible.
    type: bool
    default: true
    version_added: '0.2.0'

notes:
- If you do not pass db parameter, tables will be created in the database
  named postgres.
- PostgreSQL allows to create columnless table, so columns param is optional.
- Unlogged tables are available from PostgreSQL server version 9.1.
- If the table already exists and columns are specified they will be ignored.
  Columns can not be altered on an existing table.

attributes:
  check_mode:
    support: full

seealso:
- module: community.postgresql.postgresql_sequence
- module: community.postgresql.postgresql_idx
- module: community.postgresql.postgresql_info
- module: community.postgresql.postgresql_tablespace
- module: community.postgresql.postgresql_owner
- module: community.postgresql.postgresql_privs
- module: community.postgresql.postgresql_copy
- name: CREATE TABLE reference
  description: Complete reference of the CREATE TABLE command documentation.
  link: https://www.postgresql.org/docs/current/sql-createtable.html
- name: ALTER TABLE reference
  description: Complete reference of the ALTER TABLE  command documentation.
  link: https://www.postgresql.org/docs/current/sql-altertable.html
- name: DROP TABLE reference
  description: Complete reference of the DROP TABLE command documentation.
  link: https://www.postgresql.org/docs/current/sql-droptable.html
- name: PostgreSQL data types
  description: Complete reference of the PostgreSQL data types documentation.
  link: https://www.postgresql.org/docs/current/datatype.html
author:
- Andrei Klychkov (@Andersson007)
extends_documentation_fragment:
- community.postgresql.postgres
a  
- name: Create tbl2 in the acme database with the DDL like tbl1 with testuser as an owner
  community.postgresql.postgresql_table:
    login_db: acme
    name: tbl2
    like: tbl1
    owner: testuser

- name: Create tbl2 in the acme database and tablespace ssd with the DDL like tbl1 including comments and indexes
  community.postgresql.postgresql_table:
    login_db: acme
    table: tbl2
    like: tbl1
    including: comments, indexes
    tablespace: ssd

- name: Create test_table with several columns in ssd tablespace with fillfactor=10 and autovacuum_analyze_threshold=1
  community.postgresql.postgresql_table:
    name: test_table
    columns:
    - id bigserial primary key
    - num bigint
    - stories text
    tablespace: ssd
    storage_params:
    - fillfactor=10
    - autovacuum_analyze_threshold=1

- name: Create an unlogged table in schema acme
  community.postgresql.postgresql_table:
    name: acme.useless_data
    columns: waste_id int
    unlogged: true

- name: Set owner to someuser
  community.postgresql.postgresql_table:
    name: foo
    owner: someuser

- name: Change tablespace of foo table to new_tablespace and set owner to new_user
  community.postgresql.postgresql_table:
    name: foo
    tablespace: new_tablespace
    owner: new_user

- name: Truncate table foo
  community.postgresql.postgresql_table:
    name: foo
    truncate: true

- name: Drop table foo from schema acme
  community.postgresql.postgresql_table:
    name: acme.foo
    state: absent

- name: Drop table bar cascade
  community.postgresql.postgresql_table:
    name: bar
    state: absent
    cascade: true

- name: Create table with composite primary key
  community.postgresql.postgresql_table:
    name: composite_pk_table
    columns:
    - id bigserial
    - num bigint
    - primary key (id, num)
a  
table:
  description: Name of a table.
  returned: success
  type: str
  sample: 'foo'
state:
  description: Table state.
  returned: success
  type: str
  sample: 'present'
owner:
  description: Table owner.
  returned: success
  type: str
  sample: 'postgres'
tablespace:
  description: Tablespace.
  returned: success
  type: str
  sample: 'ssd_tablespace'
queries:
  description: List of executed queries.
  returned: success
  type: str
  sample: [ 'CREATE TABLE "test_table" (id bigint)' ]
storage_params:
  description: Storage parameters.
  returned: success
  type: list
  sample: [ "fillfactor=100", "autovacuum_analyze_threshold=1" ]
)AnsibleModule)check_inputpg_quote_identifier)connect_to_dbensure_required_libsexec_sqlget_conn_paramspg_cursor_argspostgres_common_argument_specc                   \    e Zd Zd Zd Zd Z	 	 ddZ	 	 ddZd Zd Z	d Z
dd	Zd
 Zd Zy)Tablec                     || _         || _        || _        ddg d| _        d| _        | j                          g | _        y )N ownertblspacestorage_paramsF)namemodulecursorinfoexists_Table__exists_in_dbexecuted_queries)selfr   r   r   s       y/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/postgresql/plugins/modules/postgresql_table.py__init__zTable.__init__  sF    	 
	
  "    c                 $    | j                          y)z$Getter to refresh and get table infoN)r   )r   s    r   get_infozTable.get_info  s    r!   c                 r   d| j                   v r=| j                   j                  d      d   }| j                   j                  d      d   }nd}| j                   }d}t        | |||dd      }|rAd	| _        t	        |d
   d   |d
   d   r|d
   d   nd|d
   d   r|d
   d   ng       | _        y	d| _        y)z#Check table exists and refresh info.publica  SELECT t.tableowner, t.tablespace, c.reloptions FROM pg_tables AS t INNER JOIN pg_class AS c ON  c.relname = t.tablename INNER JOIN pg_namespace AS n ON t.schemaname = n.nspname AND c.relnamespace = n.oid WHERE t.tablename = %(tblname)s AND n.nspname = %(schema)s)tblnameschemaF)query_paramsadd_to_executedTr   
tableowner
tablespacer   
reloptionsr   )r   splitr   r   dictr   )r   r*   r)   queryress        r   __exists_in_dbzTable.__exists_in_db  s    $))YY__S)"-Fiiooc*2.GFiiG. tUWPV1W',.DK!f\*14Q1EQ-27:1vl7Ks1vl3QSDI DKr!   c                    t        | j                  d      }d}| j                  r|dk(  r| j                  d   n'|r%| j                  d   |k7  r| j	                  |       d}|r%| j                  d   |k7  r| j                  |       d}|r_|j                  d      D cg c]  }|j                  d       }	}d}
|	D ]  }|| j                  d	   vsd}
 |
r| j                  |       d}|ryyd
}|r	|d|z  z  }n|d|z  z  }|r	|d|z  z  }n|dz  }|r|d|z  z  }|r|d|z  z  }t        | |d      rd}|r| j                  |      }|S c c}w )a  
        Create table.
        If table exists, check passed args (params, tblspace, owner) and,
        if they're different from current, change them.
        Arguments:
        params - storage params (passed by "WITH (...)" in SQL),
            comma separated.
        tblspace - tablespace.
        owner - table owner.
        unlogged - create unlogged table.
        columns - column string (comma separated).
        tableF
pg_defaultr   Tr   , r   CREATE UNLOGGED TABLE %s	 TABLE %sz (%s)z ()
 WITH (%s) TABLESPACE "%s"return_bool)
r   r   r   r   set_tblspace	set_ownerr0   stripset_stor_paramsr   )r   columnsparamsr   unloggedr   r   changedp
param_list	new_paramr2   s               r   createzTable.create<  s    #499g6;;<'DIIj,A,Idii
3x?!!(+7+u4u%4:LL4EFqaggclF
F!	# )A		*: ;;$(	) ((0"G)D00E[4''EWw&&EUNE\F**E'(22ED%T2GnnU+GM Gs   Ec                 @   d}t        | j                  d      }d}	|r	|	d|z  z  }	n|	d|z  z  }	|	dt        |d      z  z  }	|r |j                  d      }|D ]
  }
|	d|
z  z  }	 |	d	z  }	|r|	d
|z  z  }	|r|	d|z  z  }	t        | |	d      rd}|r| j	                  |      }|S )a  
        Create table like another table (with similar DDL).
        Arguments:
        src_table - source table.
        including - corresponds to optional INCLUDING expression
            in CREATE TABLE ... LIKE statement.
        params - storage params (passed by "WITH (...)" in SQL),
            comma separated.
        tblspace - tablespace.
        owner - table owner.
        unlogged - create unlogged table.
        Fr6   r:   r;   r<   z	 (LIKE %sr8   z INCLUDING %s)r=   r>   Tr?   )r   r   r0   r   rB   )r   	src_table	includingr   rG   rF   r   rH   r   r2   is              r   create_likezTable.create_like  s     "499g6)D00E[4''E29gFFF!,I -1,,- 	\F**E'(22ED%T2GnnU+Gr!   c                 P    dt        | j                  d      z  }t        | |d      S )NzTRUNCATE TABLE %sr6   Tr?   r   r   r   )r   r2   s     r   truncatezTable.truncate  s'    #&9$))W&MMe66r!   c                 j    dt        | j                  d      dt        |d      }t        | |d      S )NALTER TABLE r6   z RENAME TO Tr?   rT   )r   newnamer2   s      r   renamezTable.rename  s2    1DTYYPW1X1DWg1VXe66r!   c                 X    dt        | j                  d      d|d}t        | |d      S )NrW   r6   z OWNER TO ""Tr?   rT   )r   usernamer2   s      r   rB   zTable.set_owner  s(    2EdiiQX2Y[cde66r!   c                 x    | j                   sydt        | j                  d      z  }|r|dz  }t        | |d      S )NFzDROP TABLE %sr6   z CASCADETr?   )r   r   r   r   )r   cascader2   s      r   dropz
Table.drop  s>    {{"5dii"IIZEe66r!   c                 X    dt        | j                  d      d|d}t        | |d      S )NrW   r6   z SET TABLESPACE "r[   Tr?   rT   )r   r   r2   s      r   rA   zTable.set_tblspace  s(    8KDIIW^8_aije66r!   c                 X    dt        | j                  d      d|d}t        | |d      S )NrW   r6   z SET (rN   Tr?   rT   )r   rF   r2   s      r   rD   zTable.set_stor_params  s'    -@G-TV\]e66r!   N)r   r   r   Fr   )r   r   Fr   r   )F)__name__
__module____qualname__r    r#   r   rL   rR   rU   rY   rB   r_   rA   rD    r!   r   r   r     sL    #< 68%'DL =?57-^77
7777r!   r   c                  	   t               } | j                  t        dddg      t        ddddg      t        ddd	gd	d
ddg      t        d      t        d      t        dd      t        d      t        d      t        dd
d      t        dd      t        dd      t        dd      t        d      t        dd      t        dd             t        | d      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }	|j                  d   }
|j                  d    }|j                  d!   }|j                  d"   }|j                  d#   }|j                  d$   }|st        |||||||	|
||
       |dk(  r|r|j                  d%       |dk(  r'|s|	s|s|s
|s|
s|s|s|r|j                  d&|z  '       |r%|	s|s|s
|s|
s|s|s|r|j                  d(|z  '       |	r#|s|s
|s|
s|s|s|r|j                  d)|z  '       |r|r|j                  d*|z  '       |r|s|j                  d+|z  '       t        |       t        ||j                        }t        ||d,      \  }} |j                  d3i t        }|
rd-j                  |
      }
|rd-j                  |      }t        |||      }d}i }||d<   d|d<   |j                  r7t        |d|j                   d   |j                   d.   |j                   d   /      }|dk(  r|j#                  |0      }n|r|j%                         }nm|	r2|j'                  |	      }|j(                  }t        |	||      }||_        n9|dk(  r|s|j+                  ||
|||      }n|dk(  r|r|j-                  |||||
      }|r|j.                  r|j1                          n|j3                          |j5                          |j3                          |j                  r8t        |d|j                   d   |j                   d.   |j                   d   /      }nd|d<   |j(                  |d1<   ||d2<   |j7                           |j8                  d3i | y )4NstrTr   )typerequiredaliasespresentabsent)rh   defaultchoicesr   dbz5.0.0zcommunity.postgresql)r   versioncollection_name)rh   rm   rj   deprecated_aliases)rh   boolF)rh   rm   )rh   removed_in_versionremoved_from_collectionlist)rh   elements)r6   statelogin_dbr.   r   rG   likerP   rY   rU   rE   r   session_roler^   trust_input)argument_specsupports_check_moder6   rx   r.   r   rG   rz   rP   rY   r   rU   rE   r^   r{   r|   z*cascade=true is ignored when state=presentz%s: state=absent is mutually exclusive with: truncate, rename, columns, tablespace, including, like, storage_params, unlogged, owner)msgzv%s: truncate is mutually exclusive with: rename, columns, like, unlogged, including, storage_params, owner, tablespacezl%s: rename is mutually exclusive with: columns, like, unlogged, including, storage_params, owner, tablespacez2%s: like and columns params are mutually exclusivez.%s: including param needs like param specified)
autocommitr8   r   )r6   rx   r   r.   r   )r^   queriesrH   re   )r   updater1   r   rF   r   warn	fail_jsonr
   r   r	   r   r   joinr   r   r   r_   rU   rY   r   rL   rR   
check_moderollbackcommitr#   close	exit_json)r}   r   r6   rx   r.   r   rG   rz   rP   rX   r   rU   rE   r^   r{   r|   conn_paramsdb_connectiondummyr   	table_objrH   kwqs                           r   mainr     s   13Mvh?y8Y:OP5"tf"#9R 
 U#651uE"7,BD651&51%8u%&%0fd3-  0 # F
 MM'"EMM'"E|,JMM'"E}}Z(H== Dk*ImmH%G]]#34N}}Z(HmmI&GmmI&G==0L--.KFE:udI^Wl	D 	g@A h'W
dVdhpty  ~G PRWX 	Y W48~QVZdhq ACHI 	J Gtx>Uj\e ACHI 	J QTYYZMPUUV  !&&--8K(OM5!]!!3N3F.1((7# eVV,I G	BBwKBwK..) ~~j1$>>*:;
 ...1	$$&	""7+&&'662	%&	"	)	D""7N#-x@ 
)	''i(0.B ""$  " 	nnW-$>>*5(~~.>?B #BwK..ByMByMFrr!   __main__N)
__future__r   r   r   rh   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Fansible_collections.community.postgresql.plugins.module_utils.databaser   r   Fansible_collections.community.postgresql.plugins.module_utils.postgresr	   r
   r   r   r   r   objectr   r   rb   re   r!   r   <module>r      sk    A @ENDL
B 5 A7F A7RWt zF r!   