
    Vh                         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Z	 d dlZdZd dlmZmZ d	 Zd
 Zd Zd Zd Zedk(  r e        yy# e$ r  e
j                         ZdZY ?w xY w)    )absolute_importdivisionprint_functiona  
module: mssql_db
short_description: Add or remove MSSQL databases from a remote host
description:
  - Add or remove MSSQL databases from a remote host.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  name:
    description:
      - Name of the database to add or remove.
    required: true
    aliases: [db]
    type: str
  login_user:
    description:
      - The username used to authenticate with.
    type: str
    default: ''
  login_password:
    description:
      - The password used to authenticate with.
    type: str
    default: ''
  login_host:
    description:
      - Host running the database.
    type: str
    required: true
  login_port:
    description:
      - Port of the MSSQL server. Requires login_host be defined as other than localhost if login_port is used.
    default: '1433'
    type: str
  state:
    description:
      - The database state.
    default: present
    choices: ["present", "absent", "import"]
    type: str
  target:
    description:
      - Location, on the remote host, of the dump file to read from or write to. Uncompressed SQL files (C(.sql)) files are
        supported.
    type: str
  autocommit:
    description:
      - Automatically commit the change only if the import succeed. Sometimes it is necessary to use autocommit=true, since
        some content can not be changed within a transaction.
    type: bool
    default: false
notes:
  - Requires the pymssql Python package on the remote host. For Ubuntu, this is as easy as pip install pymssql (See M(ansible.builtin.pip)).
requirements:
  - pymssql
author: Vedit Firat Arig (@vedit)
a  
- name: Create a new database with name 'jackdata'
  community.general.mssql_db:
    name: jackdata
    state: present

# Copy database dump file to remote host and restore it to database 'my_db'
- name: Copy database dump file to remote host
  ansible.builtin.copy:
    src: dump.sql
    dest: /tmp

- name: Restore the dump file to database 'my_db'
  community.general.mssql_db:
    name: my_db
    state: import
    target: /tmp/dump.sql
z
#
NTF)AnsibleModulemissing_required_libc                 p    |j                  d|       | j                          t        |j                        S )Nz5SELECT name FROM master.sys.databases WHERE name = %s)executecommitboolrowcountconncursordbs      n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/mssql_db.py	db_existsr   r   s)    
NNJBOKKM      c                 D    |j                  d|z         t        | ||      S )NzCREATE DATABASE [%s])r	   r   r   s      r   	db_creater   x   s#    
NN)B./T62&&r   c                     	 |j                  d|z         |j                  d|z         t        | ||       S # t        $ r Y -w xY w)Nz;ALTER DATABASE [%s] SET single_user WITH ROLLBACK IMMEDIATEzDROP DATABASE [%s])r	   	Exceptionr   r   s      r   	db_deleter   }   sT    TWYYZ NN'",-vr***  s   8 	AAc                 B   t         j                  j                  |      rtt        |d      5 }d|z  }|D ]3  }| n/|j	                  d      r|j                  |       d|z  }/||z  }5 |j                  |       | j                          d d d        yy# 1 sw Y   yxY w)Nrz	USE [%s]
GO)r   zimport successful )   cannot find target filer   )ospathisfileopen
startswithr	   r
   )r   r   moduler   targetbackupsqlQuerylines           r   	db_importr)      s    	ww~~f&# 	&#b(H %<__T*NN8,+b0H$H% NN8$KKM	 *F	 *s   ABBc                     t        t        t        ddg      t        d      t        dd      t        d      t        d      t        d       t        d	d
      t        dg d                  } t        s | j                  t	        d      t
               | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }|}	|dk7  r|d|}	|dk7  r|dk(  r| j                  d       	 t        j                  |||	d      }
|
j                         }
j                  d       d
}t        |
|      rm|d$k(  r	 t        |
||      }n|d&k(  r|
j                  |       t!        |
|| ||      \  }}}|d'k7  r| j                  d(|z         n| j#                  d||)       nz|dk(  r	 t%        |
||      }nf|d&k(  ra	 t%        |
||      }|
j                  |       t!        |
|| ||      \  }}}|d'k7  r| j                  d(|z         n| j#                  d||)       | j#                  ||+       y # t        $ rR}d t        |      v r(|j                  \  }}| j                  d!|d"|       n| j                  d#       Y d }~qd }~ww xY w# t        $ r(}| j                  d%t        |      z          Y d }~d }~ww xY w# t        $ r(}| j                  d*t        |      z          Y d }~d }~ww xY w# t        $ r)}| j                  d*t        |      z          Y d }~Zd }~ww xY w),NTr   )requiredaliasesr   )default)r-   no_log)r+   1433r   F)typer-   present)r1   absentimport)r-   choices)name
login_userlogin_password
login_host
login_portr%   
autocommitstate)argument_specpymssql)msg	exceptionr5   r;   r:   r%   r6   r7   r8   r9   :zCwhen supplying login_user arguments login_password must be provided)r>   master)userpasswordhostdatabasezUnknown databasezERROR:  zunable to connect, check login_user and login_password are correct, or alternatively check your @sysconfdir@/freetds.conf / ${HOME}/.freetds.confr2   zerror deleting database: r3   r   z%s)changedr   r>   zerror creating database: )rG   r   )r   dictmssql_found	fail_jsonr   PYMSSQL_IMP_ERRparamsr=   connectr   r   strargsr:   r   r   r)   	exit_jsonr   )r$   r   r;   r:   r%   r6   r7   r8   r9   login_querystringr   r   eerrnoerrstrrG   rcstdoutstderrs                      r   mainrX      s   tdV4B'48T*F+%7!+JL

F 1)<X	v	BMM'"E|,J]]8$F|,J]]#34N|,J|,J"V'1:>RNb0bc	VJN_jrs 	OODGvr"HK#D&"5 hOOJ'!*4V!LBQw  TF] 3  "& AIK#D&"5 hK#D&"5 OOJ'!*4V!LBQw  TF] 3  "& A
W,[  VQ'FFME65&!AB "U VV  K  %@3q6%I JJK  K  %@3q6%I JJK
  K  %@3q6%I JJKsa   >)J K  :L M 	KAKK 	L)LL	MM  M	M:M55M:__main__)
__future__r   r   r   r0   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   	tracebackrK   r=   rI   ImportError
format_excansible.module_utils.basicr   r   r   r   r   r)   rX   __name__ r   r   <module>re      s    A @=~&
 
 
 K J!'
+G&S-l zF M  *i**,OKs   A	 	A$#A$