
    Vh                         d dl mZmZmZ eZdZdZdZd dl	m
Z
mZ d dlmZ dZ	 d dlZdZd
 Zedk(  r e        yy# e$ rZd	ZY dZ[dZ[ww xY w)    )absolute_importdivisionprint_functiona  
module: odbc
author: "John Westcott IV (@john-westcott-iv)"
version_added: "1.0.0"
short_description: Execute SQL using ODBC
description:
  - Read/Write info using ODBC drivers.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  dsn:
    description:
      - The connection string passed into ODBC.
    required: true
    type: str
  query:
    description:
      - The SQL query to perform.
    required: true
    type: str
  params:
    description:
      - Parameters to pass to the SQL query.
    type: list
    elements: str
  commit:
    description:
      - Perform a commit after the execution of the SQL query.
      - Some databases allow a commit after a select whereas others raise an exception.
      - Default is V(true) to support legacy module behavior.
    type: bool
    default: true
    version_added: 1.3.0
requirements:
  - "pyodbc"

notes:
  - Like the command module, this module always returns V(changed=true) whether or not the query would change the database.
  - 'To alter this behavior you can use C(changed_when): [true or false].'
  - For details about return values (RV(description) and RV(row_count)) see U(https://github.com/mkleehammer/pyodbc/wiki/Cursor).
a+  
- name: Set some values in the test db
  community.general.odbc:
    dsn: "DRIVER={ODBC Driver 13 for SQL Server};Server=db.ansible.com;Database=my_db;UID=admin;PWD=password;"
    query: "Select * from table_a where column1 = ?"
    params:
      - "value1"
    commit: false
  changed_when: false
a  
results:
  description: List of lists of strings containing selected rows, likely empty for DDL statements.
  returned: success
  type: list
  elements: list
description:
  description: "List of dicts about the columns selected from the cursors, likely empty for DDL statements. See notes."
  returned: success
  type: list
  elements: dict
row_count:
  description: "The number of rows selected or modified according to the cursor defaults to V(-1). See notes."
  returned: success
  type: str
)AnsibleModulemissing_required_lib)	to_nativeNTFc                  f   t        t        t        ddd      t        dd      t        dd      t        dd      	      
      } | j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }t        s| j                  t        d             d }	 t        j                  |      }t        dg dg       }	 |j                         }|r|j                  ||       n|j                  |       |r|j                          	 |j                         D ]?  }	g }
|	D ]"  }|
j!                  dj                  |             $ |d   j!                  |
       A |j"                  D ]Z  }i }|d   |d<   |d   j$                  |d<   |d   |d<   |d   |d<   |d   |d<   |d    |d!<   |d"   |d#<   |d$   j!                  |       \ |j&                  |d%<   |j+                          |j+                           | j,                  d(i | y # t        $ r5}| j                  dj                  t        |                   Y d }~d }~ww xY w# t        j(                  $ r
}Y d }~d }~wt        $ r4}| j                  d&j                  t        |                   Y d }~d }~ww xY w# t        $ r4}| j                  d'j                  t        |                   Y d }~d }~ww xY w# |j+                          w xY w))NstrT)typerequiredno_log)r   r   list)r   elementsbool)r   default)dsnqueryparamscommit)argument_specr   r   r   r   pyodbc)msgzFailed to connect to DSN: {0})changeddescription	row_countresultsz{0}r   r   name   r      display_size   internal_size   	precision   scale   nullabler   r   z!Exception while reading rows: {0}zFailed to execute query: {0} )r   dictr   get
HAS_PYODBC	fail_jsonr   r   connect	Exceptionformatr   cursorexecuter   fetchallappendr   __name__rowcountProgrammingErrorclose	exit_json)moduler   r   r   r   
connectioneresultr2   rownew_rowcolumnrow_descriptionr   pes                  j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/odbc.pymainrE   a   s   %$t<ED1Ve4VT2	
F --

E
"CMMg&E]]x(F]]x(F1(;< JS^^C(
 	F'""$NN5&)NN5!MMO	[( 2! 9FNN5<<#789y!((1	2 $*#5#5 	: &5a&8F#&5a&8&A&AF#.=a.@N+/>q/AO,+:1+=K('6q'9G$*9!*<J'}%,,[9	: #)//F; 	 	Fvg  S<CCIaLQRRSP && 	 	[!D!K!KIVWL!YZZ	[  R;BB9Q<PQQR 	sz   I <AK C
J K 	J *I;;J KK K'*KK KK 	L'*LL LL L0__main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   r-   r   ImportErrorr=   rE   r6   r*       rD   <module>rP      sw    A @-^	
" K A
J
IX zF a  Js   ; A AA