
    Vh7'                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZmZmZ d d	lmZmZmZmZmZmZ g Z G d
 de      Zd Zd Zd Zd Zd Z d Z!d Z"e#dk(  r e"        yy)    )absolute_importdivisionprint_functiona	  
---
module: postgresql_schema
short_description: Add or remove PostgreSQL schema
description:
- Add or remove PostgreSQL schema.
options:
  name:
    description:
    - Name of the schema to add or remove.
    required: true
    type: str
    aliases:
    - schema
  login_db:
    description:
    - Name of the database to connect to and add or remove the schema.
    - The V(db) alias is deprecated and will be removed in version 5.0.0.
    type: str
    default: postgres
    aliases:
    - db
    - database
  owner:
    description:
    - Name of the role to set as owner of the schema.
    type: str
    default: ''
  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
  state:
    description:
    - The schema state.
    type: str
    default: present
    choices: [ absent, present ]
  cascade_drop:
    description:
    - Drop schema with CASCADE to remove child objects.
    type: bool
    default: false
  trust_input:
    description:
    - If C(false), check whether values of parameters I(schema), I(owner), I(session_role) are potentially dangerous.
    - It makes sense to use C(false) only when SQL injections via the parameters are possible.
    type: bool
    default: true
    version_added: '0.2.0'
  comment:
    description:
    - Sets a comment on the schema.
    - To reset the comment, pass an empty string.
    type: str
    version_added: '3.3.0'
seealso:
- name: PostgreSQL schemas
  description: General information about PostgreSQL schemas.
  link: https://www.postgresql.org/docs/current/ddl-schemas.html
- name: CREATE SCHEMA reference
  description: Complete reference of the CREATE SCHEMA command documentation.
  link: https://www.postgresql.org/docs/current/sql-createschema.html
- name: ALTER SCHEMA reference
  description: Complete reference of the ALTER SCHEMA command documentation.
  link: https://www.postgresql.org/docs/current/sql-alterschema.html
- name: DROP SCHEMA reference
  description: Complete reference of the DROP SCHEMA command documentation.
  link: https://www.postgresql.org/docs/current/sql-dropschema.html

attributes:
  check_mode:
    support: full

author:
- Flavien Chantelot (@Dorn-) <contact@flavien.io>
- Thomas O'Donnell (@andytom)

extends_documentation_fragment:
- community.postgresql.postgres
a  
- name: Create a new schema with name acme in test database
  community.postgresql.postgresql_schema:
    login_db: test
    name: acme
    comment: 'My test schema'

- name: Create a new schema acme with a user bob who will own it
  community.postgresql.postgresql_schema:
    name: acme
    owner: bob

- name: Drop schema "acme" with cascade
  community.postgresql.postgresql_schema:
    name: acme
    state: absent
    cascade_drop: true
z
schema:
  description: Name of the schema.
  returned: success
  type: str
  sample: "acme"
queries:
  description: List of executed queries.
  returned: success
  type: list
  sample: ["CREATE SCHEMA \"acme\""]
N)	to_native)AnsibleModule)SQLParseErrorcheck_inputpg_quote_identifier)connect_to_dbensure_required_libsget_conn_paramspg_cursor_argspostgres_common_argument_specset_commentc                       e Zd Zy)NotSupportedErrorN)__name__
__module____qualname__     z/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/postgresql/plugins/modules/postgresql_schema.pyr   r      s    r   r   c                 v    dt        |d      d|d}| j                  |       t        j                  |       y)NzALTER SCHEMA schemaz OWNER TO ""T)r
   executeexecuted_queriesappend)cursorr   ownerquerys       r   	set_ownerr"      s2    15:E
NN5E"r   c                 N    d}| j                  |d|i       | j                         S )NzSELECT obj_description((SELECT oid FROM pg_namespace WHERE nspname = %(schema)s), 'pg_namespace') AS comment, schema_owner AS owner FROM information_schema.schemata WHERE schema_name = %(schema)sr   )r   fetchoner   r   r!   s      r   get_schema_infor&      s+    .E
 NN58V,-??r   c                 L    d}| j                  |d|i       | j                  dk(  S )NzRSELECT schema_name FROM information_schema.schemata WHERE schema_name = %(schema)sr      )r   rowcountr%   s      r   schema_existsr*      s+    .E
NN58V,-??ar   c                     t        | |      r=dt        |d      z  }|r|dz  }| j                  |       t        j	                  |       yy)NzDROP SCHEMA %sr   z CASCADETF)r*   r
   r   r   r   )r   r   cascader!   s       r   schema_deleter-      sK    VV$ #6vx#HHZEu&r   c                    t        | |      stdt        |d      z  g}|r|j                  d|z         dj                  |      }| j	                  |       t
        j                  |       |t        | |d|dt
               yt        | |      }d}|r||d   k7  rt        | ||      }|)|d   |d   nd	}||k7  rt        | |d|dt
              xs |}|S )
NzCREATE SCHEMA %sr   zAUTHORIZATION "%s" FTr    comment )	r*   r
   r   joinr   r   r   r&   r"   )	r   r   r    r0   query_fragmentsr!   schema_infochangedcurrent_comments	            r   schema_creater7      s    (-0CFH0UUV""#7%#?@)u&65BRS%ff5Uk'226G8CI8N8Zk)4`bO/)%fgxP`alelr   c                 t    t        | |      syt        | |      }|r	||d   k7  ry||d   |d   nd}||k7  ryy)NFr    r0   r1   T)r*   r&   )r   r   r    r0   r4   r6   s         r   schema_matchesr9      sX    (%ff5Uk'228CI8N8Zk)4`bO/)r   c                     t               } | j                  t        dddg      t        dd      t        dddd	gdd
ddd	d
ddg      t        dd      t        ddddg      t        d      t        dd      t        dd              t        | d      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|st        |||||       d}	t        |       t        ||j                        }
t        ||
d      \  }} |j                  d"i t        }	 |j                  r9|dk(  rt        ||      }	n|dk(  rt        ||||       }	|j                  |	|       |dk(  r	 t        |||      }	n|dk(  r	 t+        ||||      }	|j3                          |j                  |	|t4        !       y # t         $ r8}|j#                  t%        |      t'        j(                                Y d }~ed }~ww xY w# t         $ r8}|j#                  t%        |      t'        j(                                Y d }~d }~ww xY w# t,        $ r8}|j#                  t%        |      t'        j(                                Y d }~d }~wt.        $ r  t0        $ r<}|j#                  d t%        |      z  t'        j(                                Y d }~9d }~ww xY w)#NstrTname)typerequiredaliasesr1   )r=   defaultpostgresdbdatabasez5.0.0zcommunity.postgresql)r<   versioncollection_name)r=   r@   r?   deprecated_aliasesboolFpresentabsent)r=   r@   choices)r=   )r   r    login_dbcascade_dropstatesession_roletrust_inputr0   )argument_specsupports_check_moder   r    rM   rL   rN   rO   r0   )
autocommit)r5   r   )msg	exceptionzDatabase query failed: %s)r5   r   queriesr   )r   updatedictr   paramsr	   r   r   r   r   r   
check_moder*   r9   	exit_jsonr-   r   	fail_jsonr   	traceback
format_excr7   r   
SystemExit	Exceptioncloser   )rP   moduler   r    rM   rL   rN   rO   r0   r5   conn_paramsdb_connectiondummyr   es                  r   mainrf      s   13Mx@r*5*tZ>P"#9 #"#9
f 
 vu5y8Y:OPu%fd3%.'  , # F
 ]]8$FMM'"EMM'"E==0L==0L--.KmmI&GFFE<AG  !&&--8K(NM5!]!!3N3Fk '7)#,VVUGLLWV<HU'E iU'wG 
WV=MN# ! U  Yq\Y=Q=Q=S TTU ! U  Yq\Y=Q=Q=S TTU MYq\Y5I5I5KLL  k89Q<GS\SgSgSijjkss    A
I> +G6 8I> ?H: 6	H7?.H2-I> 2H77I> :	I;.I61I> 6I;;I> >	L.J::L1LL__main__)$
__future__r   r   r   r=   __metaclass__DOCUMENTATIONEXAMPLESRETURNr\   ansible.module_utils._textr   ansible.module_utils.basicr   Fansible_collections.community.postgresql.plugins.module_utils.databaser   r	   r
   Fansible_collections.community.postgresql.plugins.module_utils.postgresr   r   r   r   r   r   r   r_   r   r"   r&   r*   r-   r7   r9   rf   r   r   r   r   <module>rq      s    A @Sj&
  0 4 
   		 	 	2(MO` zF r   