
    Vh5                         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 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_copy
short_description: Copy data between a file/program and a PostgreSQL table
description:
- Copy data between a file/program and a PostgreSQL table.

options:
  copy_to:
    description:
    - Copy the contents of a table to a file.
    - Can also copy the results of a SELECT query.
    - Mutually exclusive with I(copy_from) and I(dst).
    type: path
    aliases: [ to ]
  copy_from:
    description:
    - Copy data from a file to a table (appending the data to whatever is in the table already).
    - Mutually exclusive with I(copy_to) and I(src).
    type: path
    aliases: [ from ]
  src:
    description:
    - Copy data from I(copy_from) to I(src=tablename).
    - Used with I(copy_to) only.
    type: str
    aliases: [ source ]
  dst:
    description:
    - Copy data to I(dst=tablename) from I(copy_from=/path/to/data.file).
    - Used with I(copy_from) only.
    type: str
    aliases: [ destination ]
  columns:
    description:
    - List of column names for the src/dst table to COPY FROM/TO.
    type: list
    elements: str
    aliases: [ column ]
  program:
    description:
    - Mark I(src)/I(dst) as a program. Data will be copied to/from a program.
    - See block Examples and PROGRAM arg description U(https://www.postgresql.org/docs/current/sql-copy.html).
    type: bool
    default: false
  options:
    description:
    - Options of COPY command.
    - See the full list of available options U(https://www.postgresql.org/docs/current/sql-copy.html).
    type: dict
  login_db:
    description:
    - Name of database to connect to.
    - The V(db) alias is deprecated and will be removed in version 5.0.0.
    type: str
    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
  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:
- Supports PostgreSQL version 9.4+.
- COPY command is only allowed to database superusers.

attributes:
  check_mode:
    support: partial
    details:
      - If I(check_mode=true), we just check the src/dst table availability
        and return the COPY query that actually has not been executed.
      - If i(check_mode=true) and the source has been passed as SQL, the module
        will execute it and roll the transaction back, but pay attention
        it can affect database performance (e.g., if SQL collects a lot of data).

seealso:
- name: COPY command reference
  description: Complete reference of the COPY command documentation.
  link: https://www.postgresql.org/docs/current/sql-copy.html

author:
- Andrew Klychkov (@Andersson007)

extends_documentation_fragment:
- community.postgresql.postgres
a  
- name: Copy text TAB-separated data from file /tmp/data.txt to acme table
  community.postgresql.postgresql_copy:
    copy_from: /tmp/data.txt
    dst: acme

- name: Copy CSV (comma-separated) data from file /tmp/data.csv to columns id, name of table acme
  community.postgresql.postgresql_copy:
    copy_from: /tmp/data.csv
    dst: acme
    columns: id,name
    options:
      format: csv

- name: >
    Copy text vertical-bar-separated data from file /tmp/data.txt to bar table.
    The NULL values are specified as N
  community.postgresql.postgresql_copy:
    copy_from: /tmp/data.csv
    dst: bar
    options:
      delimiter: '|'
      null: 'N'

- name: Copy data from acme table to file /tmp/data.txt in text format, TAB-separated
  community.postgresql.postgresql_copy:
    src: acme
    copy_to: /tmp/data.txt

- name: Copy data from SELECT query to/tmp/data.csv in CSV format
  community.postgresql.postgresql_copy:
    src: 'SELECT * FROM acme'
    copy_to: /tmp/data.csv
    options:
      format: csv

- name: Copy CSV data from my_table to gzip
  community.postgresql.postgresql_copy:
    src: my_table
    copy_to: 'gzip > /tmp/data.csv.gz'
    program: true
    options:
      format: csv

- name: >
    Copy data from columns id, name of table bar to /tmp/data.txt.
    Output format is text, vertical-bar-separated, NULL as N
  community.postgresql.postgresql_copy:
    src: bar
    columns:
    - id
    - name
    copy_to: /tmp/data.csv
    options:
      delimiter: '|'
      null: 'N'
ak  
queries:
  description: List of executed queries.
  returned: success
  type: str
  sample: [ "COPY test_table FROM '/tmp/data_file.txt' (FORMAT csv, DELIMITER ',', NULL 'NULL')" ]
src:
  description: Data source.
  returned: success
  type: str
  sample: "mytable"
dst:
  description: Data destination.
  returned: success
  type: str
  sample: "/tmp/data.csv"
)AnsibleModule)	iteritems)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 Zd Zd Zy)
PgCopyDataa  Implements behavior of COPY FROM, COPY TO PostgreSQL command.

    Arguments:
        module (AnsibleModule) -- object of AnsibleModule class
        cursor (cursor) -- cursor object of psycopg library

    Attributes:
        module (AnsibleModule) -- object of AnsibleModule class
        cursor (cursor) -- cursor object of psycopg library
        changed (bool) --  something was changed after execution or not
        executed_queries (list) -- executed queries
        dst (str) -- data destination table (when copy_from)
        src (str) -- data source table (when copy_to)
        opt_need_quotes (tuple) -- values of these options must be passed
            to SQL in quotes
    c                 f    || _         || _        g | _        d| _        d| _        d| _        d| _        y )NF )	DELIMITERNULLQUOTEESCAPEENCODING)modulecursorexecuted_querieschangeddstsrcopt_need_quotes)selfr   r   s      x/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/postgresql/plugins/modules/postgresql_copy.py__init__zPgCopyData.__init__   s7     " 
    c                    | j                   j                  d   | _        | j                   j                  d   | _        dt	        | j                  d      z  g}| j                   j                  j                  d      r:|j                  ddj                  | j                   j                  d         z         |j                  d       | j                   j                  j                  d	      r|j                  d
       |j                  d| j                  z         | j                   j                  j                  d      r|j                  | j                                | j                   j                  rX| j                  | j                        | _        | j                  r+| j                  j                  dj                  |             yyt        | dj                  |      d      rd| _        yy)z&Implements COPY FROM command behavior.	copy_fromr   COPY %stablecolumns(%s),FROMprogramPROGRAM'%s'options Treturn_boolN)r   paramsr   r   r	   getappendjoin_PgCopyData__transform_options
check_mode_PgCopyData__check_tabler   r   r   r    query_fragmentss     r!   r%   zPgCopyData.copy_from   sv   ;;%%k2;;%%e,$':488W'MMN;;!!),""6CHHT[[5G5G	5R,S#STv&;;!!),""9-v01;;!!),""4#;#;#=> ;;!!--dhh7DL||%%,,SXXo-FG  chh7TJ# Kr#   c                 @   | j                   j                  d   | _        | j                   j                  d   | _        d| j                  j	                         v rd| j                  z  g}ndt        | j                  d      z  g}| j                   j                  j                  d      r:|j                  dd	j                  | j                   j                  d         z         |j                  d
       | j                   j                  j                  d      r|j                  d       |j                  d| j                  z         | j                   j                  j                  d      r|j                  | j                                | j                   j                  rX| j                  | j                        | _        | j                  r+| j                  j                  dj                  |             yyt        | dj                  |      d      rd| _        yy)z$Implements COPY TO command behavior.r   copy_toSELECT z	COPY (%s)r&   r'   r(   r)   r*   TOr,   r-   r.   r/   r0   Tr1   N)r   r3   r   r   upperr	   r4   r5   r6   r7   r8   r9   r   r   r   r:   s     r!   r=   zPgCopyData.copy_to  s   ;;%%e,;;%%i0((*TXX56O  )+>txx+QQRO;;!!),""6CHHT[[5G5G	5R,S#STt$;;!!),""9-v01;;!!),""4#;#;#=> ;;!!--dhh7DL||%%,,SXXo-FG  chh7TJ# Kr#   c                 t   t        | j                  j                  d         D ]A  \  }}|j                         | j                  v s#d|z  | j                  j                  d   |<   C t        | j                  j                  d         D cg c]  \  }}|d| }}}ddj                  |      z  S c c}}w )z.Transform options dict into a suitable string.r/   r.   r0   r)   z, )r   r   r3   r@   r   r6   )r    keyvalopts       r!   __transform_optionszPgCopyData.__transform_options)  s    #DKK$6$6y$AB 	BJS#yy{d2225;c\""9-c2	B 7@@R@RS\@]6^_
c#s#__		#&& `s   B4c                 |    d|j                         v rt        | |d       yt        | dt        |d      z  d       y)a  Check table or SQL in transaction mode for check_mode.

        Return True if it is OK.

        Arguments:
            table (str) - Table name that needs to be checked.
                It can be SQL SELECT statement that was passed
                instead of the table name.
        r>   F)add_to_executedTzSELECT 1 FROM %sr'   )r@   r   r	   )r    r'   s     r!   __check_tablezPgCopyData.__check_table2  sE     % T5%8),?w,OO!&	( r#   N)	__name__
__module____qualname____doc__r"   r%   r=   r7   r9    r#   r!   r   r      s!    "
$<!$F'r#   r   c                  d   t               } | j                  t        ddg      t        ddg      t        ddg      t        ddg      t        ddd	g
      t        d      t        dd      t        ddgddddg      t        d      t        dd      
       t        | dddgddgddgg      }|j                  d   sd }|j                  d   r/t        |j                  d         D cg c]  \  }}|d| }}}t        ||j                  d   |j                  d   |j                  d   |j                  d   ||j                  d   |j                  d           |j                  j                  d      r.|j                  j                  d      s|j                  d!"       nH|j                  j                  d      r-|j                  j                  d      s|j                  d#"       t        |       t        ||j                        }t        ||d$      \  }} |j                  d&i t        }t        ||      }	|j                  j                  d      r|	j                          n+|j                  j                  d      r|	j!                          |j"                  r|j%                          n|j'                          |j)                          |j)                          |j+                  |	j,                  |	j.                  |	j0                  |	j2                  %       y c c}}w )'Npathto)typealiasesfromstrsourcedestinationlistcolumn)rQ   elementsrR   dict)rQ   boolF)rQ   defaultdbz5.0.0zcommunity.postgresql)nameversioncollection_name)rQ   rR   deprecated_aliasesT)
r=   r%   r   r   r(   r/   r,   login_dbsession_roletrust_inputr%   r=   r   r   )argument_specsupports_check_modemutually_exclusiverd   r/   r0   r(   rc   z%dst param is necessary with copy_from)msgz#src param is necessary with copy_to)
autocommit)r   queriesr   r   rM   )r   updaterZ   r   r3   r   r   r4   	fail_jsonr   r   r
   r   r   r   r=   r%   r8   rollbackcommitclose	exit_jsonr   r   r   r   )
re   r   opt_listrB   rC   conn_paramsdb_connectiondummyr   datas
             r!   mainrv   N  s   13M&4&1FVH5ehZ0em_5&58*E&!&%054&"#9F 
 u%fd3!  $ # )$% 
F =='==#?HW`Ia?bc#s3,cHcFMM),MM+.MM%(MM%(MM),MM.1	3 }}%fmm.?.?.FDE			9	%fmm.?.?.FBC  !&&--8K(OM5!]!!3N3F ff%D }}#			;	'  
LLN %%HHHH	  g ds   1L,__main__N)
__future__r   r   r   rQ   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   ansible.module_utils.sixr   Fansible_collections.community.postgresql.plugins.module_utils.databaser   r	   Fansible_collections.community.postgresql.plugins.module_utils.postgresr
   r   r   r   r   r   objectr   rv   rI   rM   r#   r!   <module>r      sl    A @^@8t
$ 5 . @ @PZz zF r#   