
    VhS                         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mZ  G d d	e      Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: postgresql_sequence
short_description: Create, drop, or alter a PostgreSQL sequence
description:
- Allows to create, drop or change the definition of a sequence generator.
options:
  sequence:
    description:
    - The name of the sequence.
    required: true
    type: str
    aliases:
    - name
  state:
    description:
    - The sequence state.
    - If I(state=absent) other options will be ignored except of I(name) and
      I(schema).
    default: present
    choices: [ absent, present ]
    type: str
  data_type:
    description:
    - Specifies the data type of the sequence. Valid types are bigint, integer,
      and smallint. bigint is the default. The data type determines the default
      minimum and maximum values of the sequence. For more info see the
      documentation
      U(https://www.postgresql.org/docs/current/sql-createsequence.html).
    - Supported from PostgreSQL 10.
    choices: [ bigint, integer, smallint ]
    type: str
  increment:
    description:
    - Increment specifies which value is added to the current sequence value
      to create a new value.
    - A positive value will make an ascending sequence, a negative one a
      descending sequence. The default value is 1.
    type: int
  minvalue:
    description:
    - Minvalue determines the minimum value a sequence can generate. The
      default for an ascending sequence is 1. The default for a descending
      sequence is the minimum value of the data type.
    type: int
    aliases:
      - min
  maxvalue:
    description:
    - Maxvalue determines the maximum value for the sequence. The default for
      an ascending sequence is the maximum
      value of the data type. The default for a descending sequence is -1.
    type: int
    aliases:
      - max
  start:
    description:
    - Start allows the sequence to begin anywhere. The default starting value
      is I(minvalue) for ascending sequences and I(maxvalue) for descending
      ones.
    type: int
  cache:
    description:
    - Cache specifies how many sequence numbers are to be preallocated and
      stored in memory for faster access. The minimum value is 1 (only one
      value can be generated at a time, i.e., no cache), and this is also
      the default.
    type: int
  cycle:
    description:
    - The cycle option allows the sequence to wrap around when the I(maxvalue)
      or I(minvalue) has been reached by an ascending or descending sequence
      respectively. If the limit is reached, the next number generated will be
      the minvalue or maxvalue, respectively.
    - If C(false) (NO CYCLE) is specified, any calls to nextval after the sequence
      has reached its maximum value will return an error. False (NO CYCLE) is
      the default.
    type: bool
    default: false
  cascade:
    description:
    - Automatically drop objects that depend on the sequence, and in turn all
      objects that depend on those objects.
    - Ignored if I(state=present).
    - Only used with I(state=absent).
    type: bool
    default: false
  rename_to:
    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 sequence, use the M(community.postgresql.postgresql_query) module.
    - The new name for the I(sequence).
    - Works only for existing sequences.
    type: str
  owner:
    description:
    - Set the owner for the I(sequence).
    type: str
  schema:
    description:
    - The schema of the I(sequence). This is be used to create and relocate
      a I(sequence) in the given schema.
    default: public
    type: str
  newschema:
    description:
    - The new schema for the I(sequence). Will be used for moving a
      I(sequence) to another I(schema).
    - Works only for existing sequences.
    type: str
  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
  login_db:
    description:
    - Name of database to connect to and run queries against.
    - The V(db) and V(database) aliases are deprecated and will be removed in version 5.0.0.
    type: str
    default: ''
    aliases:
    - database
    - db
  trust_input:
    description:
    - If C(false), check whether values of parameters I(sequence), I(schema), I(rename_to),
      I(owner), I(newschema), 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'

notes:
- If you do not pass db parameter, sequence will be created in the database
  named postgres.

attributes:
  check_mode:
    support: full

seealso:
- module: community.postgresql.postgresql_table
- module: community.postgresql.postgresql_owner
- module: community.postgresql.postgresql_privs
- module: community.postgresql.postgresql_tablespace
- name: CREATE SEQUENCE reference
  description: Complete reference of the CREATE SEQUENCE command documentation.
  link: https://www.postgresql.org/docs/current/sql-createsequence.html
- name: ALTER SEQUENCE reference
  description: Complete reference of the ALTER SEQUENCE command documentation.
  link: https://www.postgresql.org/docs/current/sql-altersequence.html
- name: DROP SEQUENCE reference
  description: Complete reference of the DROP SEQUENCE command documentation.
  link: https://www.postgresql.org/docs/current/sql-dropsequence.html
author:
- Tobias Birkefeld (@tcraxs)
- Thomas O'Donnell (@andytom)
extends_documentation_fragment:
- community.postgresql.postgres

a  
- name: Create an ascending bigint sequence called foobar in the default
        database
  community.postgresql.postgresql_sequence:
    name: foobar

- name: Create an ascending integer sequence called foobar, starting at 101
  community.postgresql.postgresql_sequence:
    name: foobar
    data_type: integer
    start: 101

- name: Create an descending sequence called foobar, starting at 101 and
        preallocated 10 sequence numbers in cache
  community.postgresql.postgresql_sequence:
    name: foobar
    increment: -1
    cache: 10
    start: 101

- name: Create an ascending sequence called foobar, which cycle between 1 to 10
  community.postgresql.postgresql_sequence:
    name: foobar
    cycle: true
    min: 1
    max: 10

- name: Create an ascending bigint sequence called foobar in the default
        database with owner foobar
  community.postgresql.postgresql_sequence:
    name: foobar
    owner: foobar

- name: Change the schema of an existing sequence to foobar
  community.postgresql.postgresql_sequence:
    name: foobar
    newschema: foobar

- name: Change the owner of an existing sequence to foobar
  community.postgresql.postgresql_sequence:
    name: foobar
    owner: foobar

- name: Drop a sequence called foobar
  community.postgresql.postgresql_sequence:
    name: foobar
    state: absent

- name: Drop a sequence called foobar with cascade
  community.postgresql.postgresql_sequence:
    name: foobar
    cascade: true
    state: absent
a  
state:
  description: Sequence state at the end of execution.
  returned: success
  type: str
  sample: 'present'
sequence:
  description: Sequence name.
  returned: success
  type: str
  sample: 'foobar'
queries:
    description: List of queries that was tried to be executed.
    returned: success
    type: str
    sample: [ "CREATE SEQUENCE \"foo\"" ]
schema:
    description: Name of the schema of the sequence.
    returned: success
    type: str
    sample: 'foo'
data_type:
    description: Shows the current data type of the sequence.
    returned: success
    type: str
    sample: 'bigint'
increment:
    description: The value of increment of the sequence. A positive value will
                 make an ascending sequence, a negative one a descending
                 sequence.
    returned: success
    type: int
    sample: -1
minvalue:
    description: The value of minvalue of the sequence.
    returned: success
    type: int
    sample: 1
maxvalue:
    description: The value of maxvalue of the sequence.
    returned: success
    type: int
    sample: 9223372036854775807
start:
    description: The value of start of the sequence.
    returned: success
    type: int
    sample: 12
cycle:
    description: Shows if the sequence cycle or not.
    returned: success
    type: bool
    sample: false
owner:
    description: Shows the current owner of the sequence
                 after the successful run of the task.
    returned: success
    type: str
    sample: 'postgres'
newname:
    description: Shows the new sequence name after rename.
    returned: success
    type: str
    sample: 'barfoo'
newschema:
    description: Shows the new schema of the sequence after schema change.
    returned: success
    type: str
    sample: 'foobar'
)AnsibleModule)check_input)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d Z	d Z
d	 Zy
)Sequenceai  Implements behavior of CREATE, ALTER or DROP SEQUENCE 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
        name (str) -- name of the sequence
        owner (str) -- name of the owner of the sequence
        schema (str) -- name of the schema (default: public)
        data_type (str) -- data type of the sequence
        start_value (int) -- value of the sequence start
        minvalue (int) -- minimum value of the sequence
        maxvalue (int) -- maximum value of the sequence
        increment (int) -- increment value of the sequence
        cycle (bool) -- sequence can cycle or not
        new_name (str) -- name of the renamed sequence
        new_schema (str) -- name of the new schema
        exists (bool) -- sequence exists or not
    c                 R   || _         || _        g | _        | j                   j                  d   | _        d| _        | j                   j                  d   | _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        d| _        | j!                          y )Nsequence schemaF)modulecursorexecuted_queriesparamsnameownerr   	data_typestart_valueminvaluemaxvalue	incrementcyclenew_name
new_schemaexistsget_info)selfr   r   s      |/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/postgresql/plugins/modules/postgresql_sequence.py__init__zSequence.__init__W  s     "KK&&z2	
kk((2
    c                 f   d}t        | || j                  | j                  dd      }|sd| _        y|r}d| _        |d   d   | _        |d   d   | _        |d   d	   | _        |d   d
   | _        |d   d   | _        |d   d   | _        |d   d   | _        |d   d   | _	        |d   d   | _
        yy)z'Getter to refresh and get sequence infoa1  SELECT s.sequence_schema AS schemaname, s.sequence_name AS sequencename, pg_get_userbyid(c.relowner) AS sequenceowner, s.data_type::regtype AS data_type, s.start_value AS start_value, s.minimum_value AS min_value, s.maximum_value AS max_value, s.increment AS increment_by, s.cycle_option AS cycle FROM information_schema.sequences s JOIN pg_class c ON c.relname = s.sequence_name LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE NOT pg_is_other_temp_schema(n.oid) AND c.relkind = 'S'::"char" AND sequence_name = %(name)s AND sequence_schema = %(schema)s)r   r   F)query_paramsadd_to_executedTr   
schemanamesequencenamesequenceownerr   r   	min_value	max_valueincrement_byr   N)r
   r   r   r"   r   r   r   r   r   r   r   )r$   queryress      r%   r#   zSequence.get_infoj  s    4$ tU-1YY$++$N',. DKDKa&.DKA~.DIQ0DJ VK0DN"1vm4DF;/DMF;/DM VN3DNQDJ r'   c                    dg}|j                  | j                                | j                  j                  j	                  d      r+|j                  d| j                  j                  d   z         | j                  j                  j	                  d      r+|j                  d| j                  j                  d   z         | j                  j                  j	                  d      r+|j                  d| j                  j                  d   z         | j                  j                  j	                  d      r+|j                  d	| j                  j                  d   z         | j                  j                  j	                  d
      r+|j                  d| j                  j                  d
   z         | j                  j                  j	                  d      r+|j                  d| j                  j                  d   z         | j                  j                  j	                  d      r|j                  d       t        | dj                  |      d      S )z,Implements CREATE SEQUENCE command behavior.zCREATE SEQUENCEr   zAS %sr   zINCREMENT BY %sr   zMINVALUE %sr   zMAXVALUE %sstartzSTART WITH %scachezCACHE %sr   CYCLE Treturn_boolappend_Sequence__add_schemar   r   getr
   joinr$   r1   s     r%   createzSequence.create  s   "#T&&();;!!+.LL4;;#5#5k#BBC;;!!+.LL*T[[-?-?-LLM;;!!*-LL););J)GGH;;!!*-LL););J)GGH;;!!'*LL4;;+=+=g+FFG;;!!'*LLdkk&8&8&AAB;;!!'*LL!chhuo4@@r'   c                     dg}|j                  | j                                | j                  j                  j	                  d      r|j                  d       t        | dj                  |      d      S )z*Implements DROP SEQUENCE command behavior.zDROP SEQUENCEcascadeCASCADEr7   Tr8   r:   r?   s     r%   dropzSequence.drop  sZ     !T&&();;!!),LL#chhuo4@@r'   c                     dg}|j                  | j                                |j                  d| j                  j                  d   z         t	        | dj                  |      d      S )z5Implements ALTER SEQUENCE RENAME TO command behavior.ALTER SEQUENCEzRENAME TO "%s"	rename_tor7   Tr8   r;   r<   r   r   r
   r>   r?   s     r%   renamezSequence.rename  sW    !"T&&()%(:(:;(GGHchhuo4@@r'   c                     dg}|j                  | j                                |j                  d| j                  j                  d   z         t	        | dj                  |      d      S )z4Implements ALTER SEQUENCE OWNER TO command behavior.rF   zOWNER TO "%s"r   r7   Tr8   rH   r?   s     r%   	set_ownerzSequence.set_owner  sV    !"T&&()_t{{'9'9''BBCchhuo4@@r'   c                     dg}|j                  | j                                |j                  d| j                  j                  d   z         t	        | dj                  |      d      S )z6Implements ALTER SEQUENCE SET SCHEMA command behavior.rF   zSET SCHEMA "%s"	newschemar7   Tr8   rH   r?   s     r%   
set_schemazSequence.set_schema  sW    !"T&&()&););K)HHIchhuo4@@r'   c                 <    d| j                   d| j                  dS )N"z".")r   r   )r$   s    r%   __add_schemazSequence.__add_schema  s    "kk49955r'   N)__name__
__module____qualname____doc__r&   r#   r@   rD   rI   rK   rN   r<    r'   r%   r   r   =  s5    2&&)PA8AAAA6r'   r   c                  $   t               } | j                  t        dddg      t        ddddg      t        dg d	      t        d
      t        d
dg      t        d
dg      t        d
      t        d
      t        dd      t        dd      t        dd      t        ddd      t        d      t        d      t        ddddgddddddddg      t        d      t        dd             t        | dddgddgddgdd gdd!gdd"gdd#gdd$gdd%gdd&gd$dgd$dgd$dgd$d gd$d!gd$d"gd$d#gd$d%gd$d&gg'      }|j                  d(   s_t        ||j                  d)   |j                  d*   |j                  d   |j                  d%   |j                  d&   |j                  d+          |j                   }t        |       t        ||j                        }t        |||,      \  }} |j                  d3i t        }t        ||      }d}|j                  s|j                  d-   dk(  r|j                  j                  d      r"|j                  d.|j                  d)   z  /       |j                  j                  d&      r"|j                  d0|j                  d)   z  /       |j!                         }nO|j                  s|j                  d-   dk(  rd}n.|j                  r"|j                  d-   dk(  r|j#                         }|j                  r]|j                  j                  d      rB|j$                  |j                  d   k7  r&|j'                         }|r|j                  d   |_        |j                  d-   dk(  r|j+                          |j                  d-   dk(  r|j                  r|j                  j                  d%      r,|j,                  |j                  d%   k7  r|j/                         }|j                  j                  d&      rB|j0                  |j                  d&   k7  r&|j3                         }|r|j                  d&   |_        |j                  r|j7                          n|j9                          |j;                          |j;                          t        |d|j$                  |j<                  |j0                  |j>                  |j@                  |jB                  |jD                  |jF                  |jH                  |j,                  1      }	|j                  d-   dk(  r7|j(                  r|j(                  |	d2<   |j4                  r'|j4                  |	d&<   n|j                  d-   dk(  rd|	d-<    |jJ                  d3i |	 y )4NstrTr   )typerequiredaliasespresentabsent)rY   defaultchoices)bigintintegersmallint)rY   r_   int)rY   min)rY   r[   maxboolF)rY   r^   publicz5.0.0zcommunity.postgresql)rY   removed_in_versionremoved_from_collectionr   dbdatabase)r   versioncollection_name)rY   r^   r[   deprecated_aliases)r   stater   r   r   r   r4   r5   r   r   rB   rG   r   rM   login_dbsession_roletrust_inputrG   r   r   r   r   r4   r5   r   rB   r   rM   )argument_specsupports_check_modemutually_exclusiverr   r   r   rq   )
autocommitro   z/Sequence '%s' does not exist, nothing to rename)msgz;Sequence '%s' does not exist, change of schema not possible)changedro   r   queriesr   r   r   r   r   r4   r   r   newnamerV   )&r   updatedictr   r   r   
check_moder	   r   r   r   r   r   r"   r=   	fail_jsonr@   rD   r   rI   r    r#   r   rK   r   rN   r!   rollbackcommitcloser   r   r   r   r   r   r   	exit_json)
rs   r   rv   conn_paramsdb_connectiondummyr   datarx   kws
             r%   mainr     s@   13M54&By8Y:OPE+LME"55'255'2.1&%0Eg/EGE"5"tZ6H"#9 #"#9
^ 
 u%fd3;  > # +&+&*%*%'"'"'")$'"+&$$
#
#    $'
F4 =='MM*%MM(#MM+&MM'"MM+&MM.)	
 &&&J !&&--8K(TM5!]!!3N3F FF#D G ;;6==1Y>==[)!RU[UbUbcmUn!no==[)!^agananoyaz!z{++- [[V]]73x? 
w/8;))+ {{v}}((599k22kkmG &k : }}W* }}W*t{{==W%zzV]]733..* ==[){{fmmK88//+&,mmK&@DO  
LLN 
%%{{....jjjj
B }}W*== MMByM??"ooB{O	w	8	+7Frr'   __main__N)
__future__r   r   r   rY   __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   objectr   r   rR   rV   r'   r%   <module>r      sk    A @dL5nE
P 5 T6v T6vfR zF r'   