
    VhU/                         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 d dlmZmZmZmZmZ g Zd 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_functionaR  
---
module: postgresql_lang
short_description: Adds, removes or changes procedural languages with a PostgreSQL database
deprecated:
  removed_in: "4.0.0"
  why: As of PostgreSQL 9.1, most procedural languages have been made into extensions.
  alternative: Use M(community.postgresql.postgresql_ext) instead.
description:
- Adds, removes or changes procedural languages with a PostgreSQL database.
- This module allows you to add a language, remote a language or change the trust
  relationship with a PostgreSQL database.
- The module can be used on the machine where executed or on a remote host.
- When removing a language from a database, it is possible that dependencies prevent
  the database from being removed. In that case, you can specify I(cascade=true) to
  automatically drop objects that depend on the language (such as functions in the
  language).
- In case the language can't be deleted because it is required by the
  database system, you can specify I(fail_on_drop=false) to ignore the error.
- Be careful when marking a language as trusted since this could be a potential
  security breach. Untrusted languages allow only users with the PostgreSQL superuser
  privilege to use this language to create new functions.
options:
  lang:
    description:
    - Name of the procedural language to add, remove or change.
    required: true
    type: str
    aliases:
    - name
  trust:
    description:
    - Make this language trusted for the selected db.
    type: bool
    default: 'false'
  db:
    description:
    - Name of database to connect to and where the language will be added, removed or changed.
    type: str
    aliases:
    - login_db
    required: true
  force_trust:
    description:
    - Marks the language as trusted, even if it's marked as untrusted in pg_pltemplate.
    - Use with care!
    type: bool
    default: 'false'
  fail_on_drop:
    description:
    - If C(true), fail when removing a language. Otherwise just log and continue.
    - In some cases, it is not possible to remove a language (used by the db-system).
    - When dependencies block the removal, consider using I(cascade).
    type: bool
    default: 'true'
  cascade:
    description:
    - When dropping a language, also delete object that depend on this language.
    - Only used when I(state=absent).
    type: bool
    default: 'false'
  session_role:
    description:
    - Switch to session_role after connecting.
    - The specified I(session_role) must be a role that the current I(login_user) is a member of.
    - Permissions checking for SQL commands is carried out as though the
      I(session_role) were the one that had logged in originally.
    type: str
  state:
    description:
    - The state of the language for the selected database.
    type: str
    default: present
    choices: [ absent, present ]
  owner:
    description:
      - Set an owner for the language.
      - Ignored when I(state=absent).
    type: str
    version_added: '0.2.0'
  trust_input:
    description:
    - If C(false), check whether values of parameters I(lang), I(session_role),
      I(owner) 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'
seealso:
- name: PostgreSQL languages
  description: General information about PostgreSQL languages.
  link: https://www.postgresql.org/docs/current/xplang.html
- name: CREATE LANGUAGE reference
  description: Complete reference of the CREATE LANGUAGE command documentation.
  link: https://www.postgresql.org/docs/current/sql-createlanguage.html
- name: ALTER LANGUAGE reference
  description: Complete reference of the ALTER LANGUAGE command documentation.
  link: https://www.postgresql.org/docs/current/sql-alterlanguage.html
- name: DROP LANGUAGE reference
  description: Complete reference of the DROP LANGUAGE command documentation.
  link: https://www.postgresql.org/docs/current/sql-droplanguage.html

attributes:
  check_mode:
    support: full

author:
- Jens Depuydt (@jensdepuydt)
- Thomas O'Donnell (@andytom)

extends_documentation_fragment:
- community.postgresql.postgres
a  
- name: Add language pltclu to database testdb if it doesn't exist
  community.postgresql.postgresql_lang: db=testdb lang=pltclu state=present

# Add language pltclu to database testdb if it doesn't exist and mark it as trusted.
# Marks the language as trusted if it exists but isn't trusted yet.
# force_trust makes sure that the language will be marked as trusted
- name: Add language pltclu to database testdb if it doesn't exist and mark it as trusted
  community.postgresql.postgresql_lang:
    db: testdb
    lang: pltclu
    state: present
    trust: true
    force_trust: true

- name: Remove language pltclu from database testdb
  community.postgresql.postgresql_lang:
    db: testdb
    lang: pltclu
    state: absent

- name: Remove language pltclu from database testdb and remove all dependencies
  community.postgresql.postgresql_lang:
    db: testdb
    lang: pltclu
    state: absent
    cascade: true

- name: Remove language c from database testdb but ignore errors if something prevents the removal
  community.postgresql.postgresql_lang:
    db: testdb
    lang: pltclu
    state: absent
    fail_on_drop: false

- name: In testdb change owner of mylang to alice
  community.postgresql.postgresql_lang:
    db: testdb
    lang: mylang
    owner: alice
zy
queries:
  description: List of executed queries.
  returned: success
  type: list
  sample: ['CREATE LANGUAGE "acme"']
)AnsibleModule)check_input)connect_to_dbensure_required_libsget_conn_paramspg_cursor_argspostgres_common_argument_specc                 L    d}| j                  |d|i       | j                  dkD  S )z Checks if language exists for dbz8SELECT lanname FROM pg_language WHERE lanname = %(lang)slangr   )executerowcountcursorr   querys      x/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/postgresql/plugins/modules/postgresql_lang.pylang_existsr      s(    FE
NN564.)??Q    c                 T    d}| j                  |d|i       | j                         d   S )z$Checks if language is trusted for dbz=SELECT lanpltrusted FROM pg_language WHERE lanname = %(lang)sr   lanpltrustedr   fetchoner   s      r   lang_istrustedr      s+    KE
NN564.)??^,,r   c                     d}| j                  |||d       t        j                  | j                  |||d             y)z%Changes if language is trusted for dbzHUPDATE pg_language SET lanpltrusted = %(trust)s WHERE lanname = %(lang)s)trustr   T)r   executed_queriesappendmogrifyr   r   r   r   s       r   lang_altertrustr"      s;    VE
NN5E489FNN5E42PQRr   c                 j    |rd|z  }nd|z  }t         j                  |       | j                  |       y)zAdds language for dbzCREATE TRUSTED LANGUAGE "%s"zCREATE LANGUAGE "%s"Tr   r   r   r!   s       r   lang_addr%      s6    .5&-E"
NN5r   c                    | j                  d       	 |rd|z  }nd|z  }t        j                  |       | j                  |       | j                  d       y# t        $ r% | j                  d       | j                  d       Y yw xY w)zDrops language for dbz!SAVEPOINT ansible_pgsql_lang_dropzDROP LANGUAGE "%s" CASCADEzDROP LANGUAGE "%s"z-ROLLBACK TO SAVEPOINT ansible_pgsql_lang_dropz)RELEASE SAVEPOINT ansible_pgsql_lang_dropFT)r   r   r   	Exception)r   r   cascader   s       r   	lang_dropr)      s    
NN67
2T9E*T1E&u
 NN>?  FGBCs   3A +BBc                 T    d}| j                  |d|i       | j                         d   S )zvGet language owner.

    Args:
        cursor (cursor): psycopg cursor object.
        lang (str): language name.
    zdSELECT r.rolname FROM pg_language l JOIN pg_roles r ON l.lanowner = r.oid WHERE l.lanname = %(lang)sr   rolnamer   r   s      r   get_lang_ownerr,      s/    *E NN564.)??Y''r   c                 b    d|d|d}t         j                  |       | j                  |       y)zSet language owner.

    Args:
        cursor (cursor): psycopg cursor object.
        lang (str): language name.
        owner (str): name of new owner.
    zALTER LANGUAGE "z" OWNER TO ""Tr$   )r   r   ownerr   s       r   set_lang_ownerr0     s+     8<UCEE"
NN5r   c                     t               } | j                  t        dddg      t        dddg      t        ddddg      t        d	d
      t        d	d
      t        d	d
      t        d	d      t        d      t        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   }|st        |||
|	       t        |       t        ||j                        }t        ||d      \  }} |j                  d"i t        }d}|||d}|dk(  rit        ||      r1t        ||      }|r|r|s|r|j                  rd}nt        |||      }nv|j                  rd}ngt        |||      }|rXt        |||      }nJt        ||      r>|j                  rd}d|d<   n*t!        |||      }|r|sd}|j#                  |       ||d<   |	r-|dk(  r(t        ||      r|	t%        ||      k7  rt'        |||	      }|r-|j                  r|j)                          n|j+                          ||d <   t,        |d!<   |j/                           |j0                  d"i | y )#NstrTlogin_db)typerequiredaliasesnamepresentabsent)r4   defaultchoicesboolfalse)r4   r:   true)r4   )
dbr   stater   force_trustr(   fail_on_dropsession_roler/   trust_input)argument_specsupports_check_moder?   r   r@   r   rA   r(   rB   r/   rC   rD   F)
autocommit)r?   r   r   lang_droppedz[unable to drop language, use cascade to delete dependencies or fail_on_drop=false to ignore)msgchangedqueries )r   updatedictr   paramsr   r	   r
   r   r   r   r   r   
check_moder"   r%   r)   	fail_jsonr,   r0   rollbackcommitr   close	exit_json)rE   moduler?   r   r@   r   rA   r(   rB   r/   rC   rD   conn_paramsdb_connectiondummyr   rJ   kwlang_trustedrI   s                       r   mainr\     s   13MUTJ<@utfX>y8Y:OP0fg6&'2vv6u%ff5   # F
 
t	B== DMM'"EMM'"E--.KmmI&G==0LMM'"E==0L--.KFD,6  !&&--8K(OM5!]!!3N3FGD5	1B	vt$)&$7LULU$$"G-fdEBG  "647-fdEBG vt$  %)>"#FD':TC$$$-%,>")#vt$vt44(u=""$  "ByM$ByMFrr   __main__N)
__future__r   r   r   r4   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Fansible_collections.community.postgresql.plugins.module_utils.databaser   Fansible_collections.community.postgresql.plugins.module_utils.postgresr   r	   r
   r   r   r   r   r   r"   r%   r)   r,   r0   r\   __name__rL   r   r   <module>rg      s    A @pd(T
 5   -$(Wt zF r   