%PDF- %PDF-
| Direktori : /proc/thread-self/root/proc/self/root/lib/python2.7/site-packages/salt/states/ |
| Current File : //proc/thread-self/root/proc/self/root/lib/python2.7/site-packages/salt/states/sqlite3.pyc |
�
���^c @@ s� d Z d d l m Z m Z m Z d d l m Z y d d l Z e Z Wn e
k
ra e Z n Xd � Z d d � Z d e d � Z d � Z e d � Z d d
� Z d � Z d � Z d S(
u�
Management of SQLite3 databases
===============================
.. versionadded:: 2016.3.0
:depends: - SQLite3 Python Module
:configuration: See :py:mod:`salt.modules.sqlite3` for setup instructions
The sqlite3 module is used to create and manage sqlite3 databases
and execute queries
Here is an example of creating a table using sql statements:
.. code-block:: yaml
users:
sqlite3.table_present:
- db: /var/www/data/app.sqlite
- schema: CREATE TABLE `users` (`username` TEXT COLLATE NOCASE UNIQUE NOT NULL, `password` BLOB NOT NULL, `salt` BLOB NOT NULL, `last_login` INT)
Here is an example of creating a table using yaml/jinja instead of sql:
.. code-block:: yaml
users:
sqlite3.table_present:
- db: /var/www/app.sqlite
- schema:
- email TEXT COLLATE NOCASE UNIQUE NOT NULL
- firstname TEXT NOT NULL
- lastname TEXT NOT NULL
- company TEXT NOT NULL
- password BLOB NOT NULL
- salt BLOB NOT NULL
Here is an example of making sure a table is absent:
.. code-block:: yaml
badservers:
sqlite3.table_absent:
- db: /var/www/data/users.sqlite
Sometimes you would to have specific data in tables to be used by other services
Here is an example of making sure rows with specific data exist:
.. code-block:: yaml
user_john_doe_xyz:
sqlite3.row_present:
- db: /var/www/app.sqlite
- table: users
- where_sql: email='john.doe@companyxyz.com'
- data:
email: john.doe@companyxyz.com
lastname: doe
firstname: john
company: companyxyz.com
password: abcdef012934125
salt: abcdef012934125
- require:
- sqlite3: users
Here is an example of removing a row from a table:
.. code-block:: yaml
user_john_doe_abc:
sqlite3.row_absent:
- db: /var/www/app.sqlite
- table: users
- where_sql: email="john.doe@companyabc.com"
- require:
- sqlite3: users
Note that there is no explicit state to perform random queries, however, this
can be approximated with sqlite3's module functions and module.run:
.. code-block:: yaml
zone-delete:
module.run:
- name: sqlite3.modify
- db: {{ db }}
- sql: "DELETE FROM records WHERE id > {{ count[0] }} AND domain_id = {{ domain_id }}"
- watch:
- sqlite3: zone-insert-12
i ( t absolute_importt unicode_literalst print_function( t sixNc C@ s t S( u6
Only load if the sqlite3 module is available
( t HAS_SQLITE3( ( ( s7 /usr/lib/python2.7/site-packages/salt/states/sqlite3.pyt __virtual__n s c
C@ s i | d 6i d 6d d 6d d 6} d } z�y�t j | d t j �} t | _ d } | d k r t | d | d | � } n t | d | d | | � } t | � d k r� t | d <d
| d <n� t | � d k r�t d rt
| d <d | | d <| d
| d d <q�| d k r9| j d | d | � } n | j d | d | | � } | j � | j
d k r�t
| d <d | d <| d
| d d <q�t | d <d | d <n t
| d <d | d <Wn0 t k
r�} t | d <t j | � | d <n XWd | r| j � n X| S( ux
Makes sure the specified row is absent in db. If multiple rows
match where_sql, then the state will fail.
name
Only used as the unique ID
db
The database file name
table
The table name to check
where_sql
The sql to select the row to check
where_args
The list parameters to substitute in where_sql
u nameu changesu resultu u commentt detect_typesu SELECT * FROM `u ` WHERE i u- More than one row matched the specified queryu testu Row will be removed in i u oldu
DELETE FROM `u Row removedu Unable to remove rowu
Row is absentN( t Nonet sqlite3t connectt PARSE_DECLTYPESt
_dict_factoryt row_factoryt _queryt lent Falset __opts__t Truet executet committ rowcountt ExceptionR t text_typet close(
t namet dbt tablet where_sqlt
where_argst changest connt rowst cursort e( ( s7 /usr/lib/python2.7/site-packages/salt/states/sqlite3.pyt
row_absentu sZ
c C@ s2 i | d 6i d 6d# d 6d d 6} d# } z�y�t j | d t j �} t | _ d# } | d# k r t | d | d | � } n t | d | d | | � } t | � d k r� t | d <d
| d <nt | � d k r�x�t j
| � D]�\ }
} |
| d k r� | d |
| k r� | rXt d rCt | d <d
| | d <qqg } g }
x? t j
| � D]. \ }
} | j
d |
d � |
j
| � q_W| d# k r�|
| 7}
n d | d } | d j | � 7} | d 7} | | 7} | j | |
� } | j � | j d k rAt | d <d | d <| d | d d <| | d d <qqt | d <d | d <qtt | d <d d | d <Pq� q� W| d d# k r�t | d <d | d <q�n@t d r�t | d <| | d d <d | | d <n
g } g } g } xL t j
| � D]; \ }
} | j
d � | j
| � | j
d |
d � q�Wd | d } | d j | � 7} | d 7} | d j | � 7} | d 7} | j | | � } | j � | j d k r�t | d <| | d d <d! | d <n t | d <d"