
    VhD%                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
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 d d
lmZ g Zd Zd Zd ZddZd Zd Ze dk(  r e        yy)    )absolute_importdivisionprint_functionaf  
---
module: mysql_variables

short_description: Manage MySQL or MariaDB global variables
description:
- Query / Set MySQL or MariaDB variables.
author:
- Balazs Pocze (@banyek)
options:
  variable:
    description:
    - Variable name to operate.
    type: str
    required: true
  value:
    description:
    - If set, then sets variable value to this.
    - With boolean values, use C(0)|C(1) or quoted C("ON")|C("OFF").
    type: str
  mode:
    description:
    - C(global) assigns C(value) to a global system variable which will be changed at runtime
      but won't persist across server restarts.
    - C(persist) assigns C(value) to a global system variable and persists it to
      the mysqld-auto.cnf option file in the data directory
      (the variable will survive service restarts).
    - C(persist_only) persists C(value) to the mysqld-auto.cnf option file in the data directory
      but without setting the global variable runtime value
      (the value will be changed after the next service restart).
    - Supported by MySQL 8.0 or later.
    - For more information see U(https://dev.mysql.com/doc/refman/8.0/en/set-variable.html).
    type: str
    choices: ['global', 'persist', 'persist_only']
    default: global
    version_added: '0.1.0'

attributes:
  check_mode:
    support: none

seealso:
- module: community.mysql.mysql_info
- name: MySQL SET command reference
  description: Complete reference of the MySQL SET command documentation.
  link: https://dev.mysql.com/doc/refman/8.0/en/set-statement.html

notes:
   - Compatible with MariaDB or MySQL.

extends_documentation_fragment:
- community.mysql.mysql
a!  
# If you encounter the "Please explicitly state intended protocol" error,
# use the login_unix_socket argument
- name: Check for sync_binlog setting
  community.mysql.mysql_variables:
    variable: sync_binlog
    login_unix_socket: /run/mysqld/mysqld.sock

- name: Set read_only variable to 1 persistently
  community.mysql.mysql_variables:
    variable: read_only
    value: 1
    mode: persist

- name: Set a boolean using ON/OFF notation
  mysql_variables:
    variable: log_slow_replica_statements
    value: "ON"  # Make sure it's quoted
z
queries:
  description: List of executed queries which modified DB's state.
  returned: if executed
  type: list
  sample: ["SET GLOBAL `read_only` = 1"]
  version_added: '0.1.0'
N)match)AnsibleModule)SQLParseErrormysql_quote_identifier)mysql_connectmysql_drivermysql_driver_fail_msgmysql_common_argument_spec)	to_nativec                     d}	 |j                  ||f       |j                         }r|d   S y# t        $ r)}dt        |      v r| j	                  d       Y d}~5d}~ww xY w)z*Check variable's value in mysqld-auto.cnf.zZSELECT VARIABLE_VALUE FROM performance_schema.persisted_variables WHERE VARIABLE_NAME = %sz<Table 'performance_schema.persisted_variables' doesn't existz&Server version must be 8.0 or greater.msgNr   )executefetchone	Exceptionstr	fail_json)modulecursormysqlvarqueryreses         s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/mysql/plugins/modules/mysql_variables.pycheck_mysqld_autor   j   sp    (EKuxk*oo
 1v  KISQRVS!IJKs   #/ 	A!AA!c                 p    	 t        |       S # t        $ r Y nw xY w	 t        |       S # t        $ r Y | S w xY w)z
    Convert value to number whenever possible, return same value
    otherwise.

    >>> typedvalue('3')
    3
    >>> typedvalue('3.0')
    3.0
    >>> typedvalue('foobar')
    'foobar'

    )int
ValueErrorfloat)values    r   
typedvaluer$   |   sI    5z U| Ls   
 	
( 	55c                 v    | j                  d|f       | j                         }t        |      dk(  r|d   d   S y )Nz'SHOW VARIABLES WHERE Variable_name = %s   r   )r   fetchalllen)r   r   mysqlvar_vals      r   getvariabler*      s>    
NN<xkJ??$L
<AAq!!    c                 N   |dk(  rdt        |d      z  }n)|dk(  rdt        |d      z  }n|dk(  rdt        |d      z  }	 | j                  dz   |f       t        j                  |d|z  z          | j	                          d	}|S # t
        $ r}t        |      }Y d
}~|S d
}~ww xY w)z Set a global mysql variable to a given value

    The DB driver will handle quoting of the given value based on its
    type, thus numeric strings like '3.0' or '8' are illegal, they
    should be passed as numeric literals.

    persistzSET PERSIST %s = varsglobalzSET GLOBAL %s = persist_onlyzSET PERSIST_ONLY %s = z%sTN)r	   r   executed_queriesappendr'   r   r   )r   r   r#   moder   resultr   s          r   setvariabler5      s     y#&<Xv&NN		"%;Hf%MM		(+A(F+SSut|eX.u 45 M  1Ms    AB 	B$BB$c                 "    | dv rd} | S | dv rd} | S )z^Converts passed value from 0,1,on,off to ON/OFF
       as it's represented in the server.
    )onr&   ON)offr   OFF )vals    r   !convert_bool_setting_value_wantedr=      s,     i J 

	Jr+   c                     t               } | j                  t        dd      t        d      t        dg dd             t        | 	      }|j                  d
   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }	d}
|j                  d   }|j                  d   }|j                  d   }||j                  d       t        d|      |j                  d|z         t        |j                  t               n%t        j                  dt        j                         	 t        ||||	||||
||
      \  }}d }d }t%        |      }||j                  d!|z  d"#       ||j'                  |       |d$v rt)        |||      }|d%k(  r|d"}n|}t+        |      }t+        |      }|d&v r|d&vrt-        |      }d }|t+        |      }||k(  r:|d'v r6|d(k(  r||k(  r|j'                  d)d"#       n|dk(  r|j'                  d*d"#       |d%k(  r||k(  r|j'                  d+d"#       	 t/        ||||      }|du r|j'                  d,|z  dt2        -       y |j                  |d"#       y # t        $ rm}t        j                  j!                  |	      r"|j                  d|	dt#        |             n!|j                  d|	d t#        |             Y d }~d }~ww xY w# t0        $ r}t#        |      }Y d }~d }~ww xY w).Nr   T)typerequired)r?   )r/   r-   r0   r/   )r?   choicesdefault)variabler#   r3   )argument_spec
login_userlogin_passwordconnect_timeoutclient_cert
client_keyca_certcheck_hostnameconfig_filemysqlrC   r#   r3   z+Cannot run without variable to operate withr   z^[0-9A-Za-z_.]+$zinvalid variable name "%s"error)category)rG   rK   zRunable to connect to database, check login_user and login_password are correct or z) has the credentials. Exception message: zunable to find z. Exception message: zVariable not available "%s"F)r   changed)r-   r0   r0   )r8   r:   )r/   r-   r-   zXVariable is already set to requested value globallyand stored into mysqld-auto.cnf file.z+Variable is already set to requested value.zEVariable is already stored into mysqld-auto.cnf with requested value.z'Variable change succeeded prev_value=%s)r   rP   queries)r   updatedictr   paramsr   r   r   r   warningsfilterwarningsWarningr
   r   ospathexistsr   r*   	exit_jsonr   r$   r=   r5   r   r1   )rD   r   userpasswordrG   ssl_certssl_keyssl_carK   rL   dbr   r#   r3   r   db_connr   r)   var_in_mysqld_auto_cnfvalue_wantedvalue_actualvalue_in_auto_cnfr4   s                          r   mainrg      s   .0M540u&KU]^   #F ==&D}}-.Hmm$56O}}]+HmmL)G]]9%F]]#34N--.K	B}}Z(HMM'"E== DJK*2;hFG23,2F2FG	k'hXW^`fhj8GXfh L!vx0L<xGQVW}\***!2668!L>!%-$5 e$Ll+L}$])J8F)&'=>|#0E(E91B!B "IRW  Y X!NX]^~,2C"C 5>C 	 	EVX|TB ~FU!%/? 	 	A 	VU3u  k77>>+&=H)TU,#X Z {\efg\h!ijkf  1s1   -J5 3L. 5	L+>A"L&&L+.	M7MM__main__)r/   )!
__future__r   r   r   r?   __metaclass__DOCUMENTATIONEXAMPLESRETURNrX   rU   rer   ansible.module_utils.basicr   Aansible_collections.community.mysql.plugins.module_utils.databaser   r	   >ansible_collections.community.mysql.plugins.module_utils.mysqlr
   r   r   r   ansible.module_utils._textr   r1   r   r$   r*   r5   r=   rg   __name__r;   r+   r   <module>rt      s    A @4l(
 
   4 s Z  Z 0 $44	_4D zF r+   