
H`Tc           @   s  d  Z  d d l Z d d l Z d d l m Z d d l m Z m Z d d l m Z m	 Z
 d d l m Z d d l m Z m Z d d	 l m Z d d
 l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d e f d     YZ d e e j f d     YZ  d e e j! f d     YZ d e e j" f d     YZ# i e e j! 6e  e j 6e# e j" 6Z$ i e j% d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j  d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d  6e j# d 6e j d! 6e j d" 6e j& d# 6e j' d$ 6Z( d% e j) f d&     YZ* d' e j+ f d(     YZ, d) e j- f d*     YZ. d+ e j/ f d,     YZ0 d- e j1 f d.     YZ2 d/ e j3 f d0     YZ4 d1   Z5 d S(2   s(  
.. dialect:: sqlite
    :name: SQLite


Date and Time Types
-------------------

SQLite does not have built-in DATE, TIME, or DATETIME types, and pysqlite does
not provide out of the box functionality for translating values between Python
`datetime` objects and a SQLite-supported format. SQLAlchemy's own
:class:`~sqlalchemy.types.DateTime` and related types provide date formatting
and parsing functionality when SQlite is used. The implementation classes are
:class:`~.sqlite.DATETIME`, :class:`~.sqlite.DATE` and :class:`~.sqlite.TIME`.
These types represent dates and times as ISO formatted strings, which also
nicely support ordering. There's no reliance on typical "libc" internals for
these functions so historical dates are fully supported.

.. _sqlite_autoincrement:

SQLite Auto Incrementing Behavior
----------------------------------

Background on SQLite's autoincrement is at: http://sqlite.org/autoinc.html

Two things to note:

* The AUTOINCREMENT keyword is **not** required for SQLite tables to
  generate primary key values automatically. AUTOINCREMENT only means that the
  algorithm used to generate ROWID values should be slightly different.
* SQLite does **not** generate primary key (i.e. ROWID) values, even for
  one column, if the table has a composite (i.e. multi-column) primary key.
  This is regardless of the AUTOINCREMENT keyword being present or not.

To specifically render the AUTOINCREMENT keyword on the primary key column
when rendering DDL, add the flag ``sqlite_autoincrement=True`` to the Table
construct::

    Table('sometable', metadata,
            Column('id', Integer, primary_key=True),
            sqlite_autoincrement=True)


.. _sqlite_concurrency:

Database Locking Behavior / Concurrency
---------------------------------------

SQLite is not designed for a high level of write concurrency. The database
itself, being a file, is locked completely during write operations within
transactions, meaning exactly one "connection" (in reality a file handle)
has exclusive access to the database during this period - all other
"connections" will be blocked during this time.

The Python DBAPI specification also calls for a connection model that is
always in a transaction; there is no ``connection.begin()`` method,
only ``connection.commit()`` and ``connection.rollback()``, upon which a
new transaction is to be begun immediately.  This may seem to imply
that the SQLite driver would in theory allow only a single filehandle on a
particular database file at any time; however, there are several
factors both within SQlite itself as well as within the pysqlite driver
which loosen this restriction significantly.

However, no matter what locking modes are used, SQLite will still always
lock the database file once a transaction is started and DML (e.g. INSERT,
UPDATE, DELETE) has at least been emitted, and this will block
other transactions at least at the point that they also attempt to emit DML.
By default, the length of time on this block is very short before it times out
with an error.

This behavior becomes more critical when used in conjunction with the
SQLAlchemy ORM.  SQLAlchemy's :class:`.Session` object by default runs
within a transaction, and with its autoflush model, may emit DML preceding
any SELECT statement.   This may lead to a SQLite database that locks
more quickly than is expected.   The locking mode of SQLite and the pysqlite
driver can be manipulated to some degree, however it should be noted that
achieving a high degree of write-concurrency with SQLite is a losing battle.

For more information on SQLite's lack of write concurrency by design, please
see
`Situations Where Another RDBMS May Work Better - High Concurrency
<http://www.sqlite.org/whentouse.html>`_ near the bottom of the page.

The following subsections introduce areas that are impacted by SQLite's
file-based architecture and additionally will usually require workarounds to
work when using the pysqlite driver.

Transaction Isolation Level
===========================

SQLite supports "transaction isolation" in a non-standard way, along two
axes.  One is that of the `PRAGMA read_uncommitted <http://www.sqlite.org/pragma.html#pragma_read_uncommitted>`_
instruction.   This setting can essentially switch SQLite between its
default mode of ``SERIALIZABLE`` isolation, and a "dirty read" isolation
mode normally referred to as ``READ UNCOMMITTED``.

SQLAlchemy ties into this PRAGMA statement using the
:paramref:`.create_engine.isolation_level` parameter of :func:`.create_engine`.
Valid values for this parameter when used with SQLite are ``"SERIALIZABLE"``
and ``"READ UNCOMMITTED"`` corresponding to a value of 0 and 1, respectively.
SQLite defaults to ``SERIALIZABLE``, however its behavior is impacted by
the pysqlite driver's default behavior.

The other axis along which SQLite's transactional locking is impacted is
via the nature of the ``BEGIN`` statement used.   The three varieties
are "deferred", "immediate", and "exclusive", as described at
`BEGIN TRANSACTION <http://sqlite.org/lang_transaction.html>`_.   A straight
``BEGIN`` statement uses the "deferred" mode, where the the database file is
not locked until the first read or write operation, and read access remains
open to other transactions until the first write operation.  But again,
it is critical to note that the pysqlite driver interferes with this behavior
by *not even emitting BEGIN* until the first write operation.

.. warning::

    SQLite's transactional scope is impacted by unresolved
    issues in the pysqlite driver, which defers BEGIN statements to a greater
    degree than is often feasible. See the section :ref:`pysqlite_serializable`
    for techniques to work around this behavior.

SAVEPOINT Support
=================

SQLite supports SAVEPOINTs, which only function once a transaction is
begun.   SQLAlchemy's SAVEPOINT support is available using the
:meth:`.Connection.begin_nested` method at the Core level, and
:meth:`.Session.begin_nested` at the ORM level.   However, SAVEPOINTs
won't work at all with pysqlite unless workarounds are taken.

.. warning::

    SQLite's SAVEPOINT feature is impacted by unresolved
    issues in the pysqlite driver, which defers BEGIN statements to a greater
    degree than is often feasible. See the section :ref:`pysqlite_serializable`
    for techniques to work around this behavior.

Transactional DDL
=================

The SQLite database supports transactional :term:`DDL` as well.
In this case, the pysqlite driver is not only failing to start transactions,
it also is ending any existing transction when DDL is detected, so again,
workarounds are required.

.. warning::

    SQLite's transactional DDL is impacted by unresolved issues
    in the pysqlite driver, which fails to emit BEGIN and additionally
    forces a COMMIT to cancel any transaction when DDL is encountered.
    See the section :ref:`pysqlite_serializable`
    for techniques to work around this behavior.

.. _sqlite_foreign_keys:

Foreign Key Support
-------------------

SQLite supports FOREIGN KEY syntax when emitting CREATE statements for tables,
however by default these constraints have no effect on the operation of the
table.

Constraint checking on SQLite has three prerequisites:

* At least version 3.6.19 of SQLite must be in use
* The SQLite library must be compiled *without* the SQLITE_OMIT_FOREIGN_KEY
  or SQLITE_OMIT_TRIGGER symbols enabled.
* The ``PRAGMA foreign_keys = ON`` statement must be emitted on all
  connections before use.

SQLAlchemy allows for the ``PRAGMA`` statement to be emitted automatically for
new connections through the usage of events::

    from sqlalchemy.engine import Engine
    from sqlalchemy import event

    @event.listens_for(Engine, "connect")
    def set_sqlite_pragma(dbapi_connection, connection_record):
        cursor = dbapi_connection.cursor()
        cursor.execute("PRAGMA foreign_keys=ON")
        cursor.close()

.. seealso::

    `SQLite Foreign Key Support <http://www.sqlite.org/foreignkeys.html>`_
    - on the SQLite web site.

    :ref:`event_toplevel` - SQLAlchemy event API.

.. _sqlite_type_reflection:

Type Reflection
---------------

SQLite types are unlike those of most other database backends, in that
the string name of the type usually does not correspond to a "type" in a
one-to-one fashion.  Instead, SQLite links per-column typing behavior
to one of five so-called "type affinities" based on a string matching
pattern for the type.

SQLAlchemy's reflection process, when inspecting types, uses a simple
lookup table to link the keywords returned to provided SQLAlchemy types.
This lookup table is present within the SQLite dialect as it is for all
other dialects.  However, the SQLite dialect has a different "fallback"
routine for when a particular type name is not located in the lookup map;
it instead implements the SQLite "type affinity" scheme located at
http://www.sqlite.org/datatype3.html section 2.1.

The provided typemap will make direct associations from an exact string
name match for the following types:

:class:`~.types.BIGINT`, :class:`~.types.BLOB`,
:class:`~.types.BOOLEAN`, :class:`~.types.BOOLEAN`,
:class:`~.types.CHAR`, :class:`~.types.DATE`,
:class:`~.types.DATETIME`, :class:`~.types.FLOAT`,
:class:`~.types.DECIMAL`, :class:`~.types.FLOAT`,
:class:`~.types.INTEGER`, :class:`~.types.INTEGER`,
:class:`~.types.NUMERIC`, :class:`~.types.REAL`,
:class:`~.types.SMALLINT`, :class:`~.types.TEXT`,
:class:`~.types.TIME`, :class:`~.types.TIMESTAMP`,
:class:`~.types.VARCHAR`, :class:`~.types.NVARCHAR`,
:class:`~.types.NCHAR`

When a type name does not match one of the above types, the "type affinity"
lookup is used instead:

* :class:`~.types.INTEGER` is returned if the type name includes the
  string ``INT``
* :class:`~.types.TEXT` is returned if the type name includes the
  string ``CHAR``, ``CLOB`` or ``TEXT``
* :class:`~.types.NullType` is returned if the type name includes the
  string ``BLOB``
* :class:`~.types.REAL` is returned if the type name includes the string
  ``REAL``, ``FLOA`` or ``DOUB``.
* Otherwise, the :class:`~.types.NUMERIC` type is used.

.. versionadded:: 0.9.3 Support for SQLite type affinity rules when reflecting
   columns.

iNi   (   t
   processors(   t   sqlt   exc(   t   typest   schema(   t   util(   t   defaultt
   reflection(   t   compiler(   t   BLOBt   BOOLEANt   CHARt   DATEt   DECIMALt   FLOATt   INTEGERt   REALt   NUMERICt   SMALLINTt   TEXTt	   TIMESTAMPt   VARCHARt   _DateTimeMixinc           B   s5   e  Z d Z d Z d d d   Z d   Z d   Z RS(   c         K   sS   t  t |   j |   | d  k	 r7 t j |  |  _ n  | d  k	 rO | |  _ n  d  S(   N(   t   superR   t   __init__t   Nonet   ret   compilet   _regt   _storage_format(   t   selft   storage_formatt   regexpt   kw(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR     s
    c         K   s]   t  | t  rD |  j r( |  j | d <n  |  j rD |  j | d <qD n  t t |   j | |  S(   NR   R    (   t
   issubclassR   R   R   R   t   adapt(   R   t   clsR!   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR#     s    		c            s"   |  j  |      f d   } | S(   Nc            s   d   |   S(   Ns   '%s'(    (   t   value(   t   bp(    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   process  s    (   t   bind_processor(   R   t   dialectR'   (    (   R&   se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   literal_processor  s    N(   t   __name__t
   __module__R   R   R   R   R#   R*   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR     s
   	t   DATETIMEc           B   s/   e  Z d  Z d Z d   Z d   Z d   Z RS(   s  Represent a Python datetime object in SQLite using a string.

    The default string storage format is::

        "%(year)04d-%(month)02d-%(day)02d %(hour)02d:%(min)02d:%(second)02d.%(microsecond)06d"

    e.g.::

        2011-03-15 12:05:57.10558

    The storage format can be customized to some degree using the
    ``storage_format`` and ``regexp`` parameters, such as::

        import re
        from sqlalchemy.dialects.sqlite import DATETIME

        dt = DATETIME(
            storage_format="%(year)04d/%(month)02d/%(day)02d %(hour)02d:%(min)02d:%(second)02d",
            regexp=r"(\d+)/(\d+)/(\d+) (\d+)-(\d+)-(\d+)"
        )

    :param storage_format: format string which will be applied to the dict
     with keys year, month, day, hour, minute, second, and microsecond.

    :param regexp: regular expression which will be applied to incoming result
     rows. If the regexp contains named groups, the resulting match dict is
     applied to the Python datetime() constructor as keyword arguments.
     Otherwise, if positional groups are used, the datetime() constructor
     is called with positional arguments via
     ``*map(int, match_obj.groups(0))``.
    sW   %(year)04d-%(month)02d-%(day)02d %(hour)02d:%(minute)02d:%(second)02d.%(microsecond)06dc         O   sq   | j  d t  } t t |   j | |   | rm d | k sI t d   d | k sa t d   d |  _ n  d  S(   Nt   truncate_microsecondsR   sD   You can specify only one of truncate_microseconds or storage_format.R    s<   You can specify only one of truncate_microseconds or regexp.sE   %(year)04d-%(month)02d-%(day)02d %(hour)02d:%(minute)02d:%(second)02d(   t   popt   FalseR   R-   R   t   AssertionErrorR   (   R   t   argst   kwargsR.   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR   J  s    c            s4   t  j   t  j   |  j      f d   } | S(   Nc            s   |  d  k r d  St |    rm  i |  j d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6St |     r  i |  j d 6|  j d 6|  j d 6d d 6d d 6d d 6d d 6St	 d	   d  S(
   Nt   yeart   montht   dayt   hourt   minutet   secondt   microsecondi    sL   SQLite DateTime type only accepts Python datetime and date objects as input.(
   R   t
   isinstanceR4   R5   R6   R7   R8   R9   R:   t	   TypeError(   R%   (   t   datetime_datet   formatt   datetime_datetime(    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR'   \  s*    








	(   t   datetimet   dateR   (   R   R)   R'   (    (   R=   R>   R?   se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR(   W  s
    			c         C   s*   |  j  r t j |  j  t j  St j Sd  S(   N(   R   R    t!   str_to_datetime_processor_factoryR@   t   str_to_datetime(   R   R)   t   coltype(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   result_processorx  s    	(   R+   R,   t   __doc__R   R   R(   RE   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR-   "  s
   !		!R   c           B   s&   e  Z d  Z d Z d   Z d   Z RS(   s  Represent a Python date object in SQLite using a string.

    The default string storage format is::

        "%(year)04d-%(month)02d-%(day)02d"

    e.g.::

        2011-03-15

    The storage format can be customized to some degree using the
    ``storage_format`` and ``regexp`` parameters, such as::

        import re
        from sqlalchemy.dialects.sqlite import DATE

        d = DATE(
                storage_format="%(month)02d/%(day)02d/%(year)04d",
                regexp=re.compile("(?P<month>\d+)/(?P<day>\d+)/(?P<year>\d+)")
            )

    :param storage_format: format string which will be applied to the
     dict with keys year, month, and day.

    :param regexp: regular expression which will be applied to
     incoming result rows. If the regexp contains named groups, the
     resulting match dict is applied to the Python date() constructor
     as keyword arguments. Otherwise, if positional groups are used, the
     date() constructor is called with positional arguments via
     ``*map(int, match_obj.groups(0))``.
    s    %(year)04d-%(month)02d-%(day)02dc            s(   t  j   |  j     f d   } | S(   Nc            sU   |  d  k r d  St |    rE   i |  j d 6|  j d 6|  j d 6St d   d  S(   NR4   R5   R6   s;   SQLite Date type only accepts Python date objects as input.(   R   R;   R4   R5   R6   R<   (   R%   (   R>   R=   (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR'     s    

(   R@   RA   R   (   R   R)   R'   (    (   R=   R>   se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR(     s    		c         C   s*   |  j  r t j |  j  t j  St j Sd  S(   N(   R   R    RB   R@   RA   t   str_to_date(   R   R)   RD   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyRE     s    	(   R+   R,   RF   R   R(   RE   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR     s   	t   TIMEc           B   s/   e  Z d  Z d Z d   Z d   Z d   Z RS(   s?  Represent a Python time object in SQLite using a string.

    The default string storage format is::

        "%(hour)02d:%(minute)02d:%(second)02d.%(microsecond)06d"

    e.g.::

        12:05:57.10558

    The storage format can be customized to some degree using the
    ``storage_format`` and ``regexp`` parameters, such as::

        import re
        from sqlalchemy.dialects.sqlite import TIME

        t = TIME(
            storage_format="%(hour)02d-%(minute)02d-%(second)02d-%(microsecond)06d",
            regexp=re.compile("(\d+)-(\d+)-(\d+)-(?:-(\d+))?")
        )

    :param storage_format: format string which will be applied to the dict
     with keys hour, minute, second, and microsecond.

    :param regexp: regular expression which will be applied to incoming result
     rows. If the regexp contains named groups, the resulting match dict is
     applied to the Python time() constructor as keyword arguments. Otherwise,
     if positional groups are used, the time() constructor is called with
     positional arguments via ``*map(int, match_obj.groups(0))``.
    s6   %(hour)02d:%(minute)02d:%(second)02d.%(microsecond)06dc         O   sq   | j  d t  } t t |   j | |   | rm d | k sI t d   d | k sa t d   d |  _ n  d  S(   NR.   R   sD   You can specify only one of truncate_microseconds or storage_format.R    s<   You can specify only one of truncate_microseconds or regexp.s$   %(hour)02d:%(minute)02d:%(second)02d(   R/   R0   R   RH   R   R1   R   (   R   R2   R3   R.   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR     s    c            s(   t  j  |  j      f d   } | S(   Nc            s_   |  d  k r d  St |    rO   i |  j d 6|  j d 6|  j d 6|  j d 6St d   d  S(   NR7   R8   R9   R:   s;   SQLite Time type only accepts Python time objects as input.(   R   R;   R7   R8   R9   R:   R<   (   R%   (   R>   t   datetime_time(    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR'     s    


(   R@   t   timeR   (   R   R)   R'   (    (   R>   RI   se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR(     s    		c         C   s*   |  j  r t j |  j  t j  St j Sd  S(   N(   R   R    RB   R@   RJ   t   str_to_time(   R   R)   RD   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyRE     s    	(   R+   R,   RF   R   R   R(   RE   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyRH     s
   	
	t   BIGINTR	   t   BOOLR
   R   t   DOUBLER   R   t   INTR   R   R   R   R   R   R   t   NVARCHARt   NCHARt   SQLiteCompilerc           B   s   e  Z e j e j j i
 d  d 6d d 6d d 6d d 6d d	 6d
 d 6d d 6d d 6d d 6d d 6 Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d   Z d   Z RS(   s   %mR5   s   %dR6   s   %YR4   s   %SR9   s   %HR7   s   %jt   doys   %MR8   s   %st   epochs   %wt   dows   %Wt   weekc         K   s   d S(   Nt   CURRENT_TIMESTAMP(    (   R   t   fnR!   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   visit_now_func3  s    c         K   s   d S(   Ns(   DATETIME(CURRENT_TIMESTAMP, "localtime")(    (   R   t   funcR!   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   visit_localtimestamp_func6  s    c         K   s   d S(   Nt   1(    (   R   t   exprR!   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt
   visit_true9  s    c         K   s   d S(   Nt   0(    (   R   R]   R!   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   visit_false<  s    c         K   s   d |  j  |  S(   Ns   length%s(   t   function_argspec(   R   RX   R!   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   visit_char_length_func?  s    c         K   s<   |  j  j r% t t |   j | |  S|  j | j |  Sd  S(   N(   R)   t   supports_castR   RR   t
   visit_castR'   t   clause(   R   t   castR3   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyRd   B  s    c         K   sY   y+ d |  j  | j |  j | j |  f SWn' t k
 rT t j d | j   n Xd  S(   Ns#   CAST(STRFTIME('%s', %s) AS INTEGER)s#   %s is not a valid extract argument.(   t   extract_mapt   fieldR'   R]   t   KeyErrorR   t   CompileError(   R   t   extractR!   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   visit_extractH  s    c         C   s   d } | j  d  k	 r; | d |  j t j | j    7} n  | j d  k	 r | j  d  k r| | d |  j t j d   7} n  | d |  j t j | j   7} n  | d |  j t j d   7} | S(   Nt    s   
 LIMIT is    OFFSET i    (   t   _limitR   R'   R   t   literalt   _offset(   R   t   selectt   text(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   limit_clauseR  s    &#& c         C   s   d S(   NRm   (    (   R   Rq   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   for_update_clause^  s    (   R+   R,   R   t   update_copyR   t   SQLCompilerRg   RY   R[   R^   R`   Rb   Rd   Rl   Rs   Rt   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyRR   #  s,   								
	t   SQLiteDDLCompilerc           B   s5   e  Z d    Z d   Z d   Z d   Z d   Z RS(   c         K   s   |  j  j j | j  } |  j j |  d | } |  j |  } | d  k	 r^ | d | 7} n  | j st | d 7} n  | j	 r | j
 j d d r t | j
 j	 j  d k r t | j j t j  r | j r | d 7} n  | S(   Nt    s	    DEFAULT s	    NOT NULLt   sqlitet   autoincrementi   s    PRIMARY KEY AUTOINCREMENT(   R)   t   type_compilerR'   t   typet   preparert   format_columnt   get_column_default_stringR   t   nullablet   primary_keyt   tablet   dialect_optionst   lent   columnsR"   t   _type_affinityt   sqltypest   Integert   foreign_keys(   R   t   columnR3   RD   t   colspecR   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   get_column_specificatione  s    		
c         C   s   t  | j  d k rk t |  d } | j rk | j j d d rk t | j j t	 j
  rk | j rk d  Sn  t t |   j |  S(   Ni   i    Ry   Rz   (   R   R   t   listR   R   R   R"   R|   R   R   R   R   R   R   Rw   t   visit_primary_key_constraint(   R   t
   constraintt   c(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR   x  s    	
c         C   sn   t  | j j    d j j } t  | j j    d j j } | j | j k rT d  St t	 |   j
 |  Sd  S(   Ni    (   R   t	   _elementst   valuest   parentR   R   R   R   R   Rw   t   visit_foreign_key_constraint(   R   R   t   local_tablet   remote_table(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR     s    c         C   s   | j  | d t S(   s=   Format the remote table clause of a CREATE CONSTRAINT clause.t
   use_schema(   t   format_tableR0   (   R   R   R   R}   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   define_constraint_remote_table  s    c         C   s   t  t |   j | d t S(   Nt   include_table_schema(   R   Rw   t   visit_create_indexR0   (   R   t   create(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR     s    (   R+   R,   R   R   R   R   R   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyRw   c  s
   				t   SQLiteTypeCompilerc           B   s   e  Z d    Z RS(   c         C   s   |  j  |  S(   N(   t
   visit_BLOB(   R   t   type_(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   visit_large_binary  s    (   R+   R,   R   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR     s   t   SQLiteIdentifierPreparerc        u   B   s  e  Z e d  d d d d d d d d d	 d
 d d d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= d> d? d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ dR dS dT dU dV dW dX dY dZ d[ d\ d] d^ d_ d` da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds gt  Z e du dt  Z RS(v   t   addt   aftert   allt   altert   analyzet   andt   ast   asct   attachRz   t   beforet   begint   betweent   byt   cascadet   caseRf   t   checkt   collateR   t   committ   conflictR   R   t   crosst   current_datet   current_timet   current_timestampt   databaseR   t
   deferrablet   deferredt   deletet   desct   detacht   distinctt   dropt   eacht   elset   endt   escapet   exceptt	   exclusivet   explaint   falset   failt   fort   foreignt   fromt   fullt   globt   groupt   havingt   ift   ignoret	   immediatet   int   indext   indexedt	   initiallyt   innert   insertt   insteadt	   intersectt   intot   ist   isnullt   joint   keyt   leftt   liket   limitt   matcht   naturalt   nott   notnullt   nullt   oft   offsett   ont   ort   ordert   outert   plant   pragmat   primaryt   queryt   raiset
   referencest   reindext   renamet   replacet   restrictt   rightt   rollbackt   rowRq   t   setR   t   tempt	   temporaryt   thent   tot   transactiont   triggert   truet   uniont   uniquet   updatet   usingt   vacuumR   t   viewt   virtualt   whent   wherec         C   s   | d k r | j } n  |  j | | j  } |  j r{ | r{ t | j d d  r{ |  j | j j | j j  d | } n  | S(   s'   Prepare a quoted index and schema name.R   t   .N(   R   t   namet   quotet   omit_schemat   getattrR   t   quote_schemaR   (   R   R   R   R  t   result(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   format_index  s    
)N(   R+   R,   R   t   reserved_wordst   TrueR   R  (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR     s(   t   SQLiteExecutionContextc           B   s#   e  Z e j d     Z d   Z RS(   c         C   s   |  j  j d t  S(   Nt   sqlite_raw_colnames(   t   execution_optionst   getR0   (   R   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   _preserve_raw_colnames  s    c         C   s;   |  j  r- d | k r- | j d  d | f S| d  f Sd  S(   NR  i(   R  t   splitR   (   R   t   colname(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   _translate_colname  s    (   R+   R,   R   t   memoized_propertyR  R  (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR    s   t   SQLiteDialectc           B   s  e  Z d  Z e Z e Z e Z e Z e Z	 e Z
 e Z e Z d Z e Z e Z e Z e Z e Z e Z e Z d Z e Z
 e Z e j i e d 6f g Z e Z d e d  Z  i d d 6d d 6Z! d   Z" d	   Z# d
   Z$ e% j& d d   Z' d d  Z( e% j& d d   Z) e% j& d d   Z* e% j& d d   Z+ d   Z, d   Z- e% j& d d   Z. e% j& d d   Z/ d   Z0 e% j& d d   Z1 e% j& d d   Z2 RS(   Ry   t   qmarkRz   c         K   s   t  j j |  |  | |  _ | |  _ |  j d  k	 r |  j j d k |  _ |  j j d	 k |  _	 |  j j d
 k |  _
 |  j j d k  |  _ n  d  S(   Ni   i   i   i   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   R   t   DefaultDialectR   t   isolation_levelt   native_datetimet   dbapiR   t   sqlite_version_infot   supports_default_valuesRc   t   supports_multivalues_insertt   _broken_fk_pragma_quotes(   R   R#  R$  R3   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR     s    		i   s   READ UNCOMMITTEDi    t   SERIALIZABLEc         C   s   y |  j  | j d d  } Wn< t k
 r[ t j d | |  j d j |  j   f   n X| j   } | j d |  | j	   d  S(   Nt   _Rx   sL   Invalid value '%s' for isolation_level. Valid isolation levels for %s are %ss   , s   PRAGMA read_uncommitted = %d(
   t   _isolation_lookupR   Ri   R   t   ArgumentErrorR  R   t   cursort   executet   close(   R   t
   connectiont   levelR#  R.  (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   set_isolation_level  s    &c         C   s   | j    } | j d  | j   } | r8 | d } n d } | j   | d k rX d S| d k rh d St s~ t d |   d  S(   Ns   PRAGMA read_uncommittedi    R*  i   s   READ UNCOMMITTEDs   Unknown isolation level %s(   R.  R/  t   fetchoneR0  R0   R1   (   R   R1  R.  t   resR%   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   get_isolation_level  s    
c            s*     j  d  k	 r"   f d   } | Sd  Sd  S(   Nc            s     j  |    j  d  S(   N(   R3  R#  (   t   conn(   R   (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   connect7  s    (   R#  R   (   R   R8  (    (   R   se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt
   on_connect5  s    c   	      K   s   | d  k	 rG |  j j |  } d | } d | f } | j |  } nE y d } | j |  } Wn) t j k
 r d } | j |  } n Xg  | D] } | d ^ q S(   Ns   %s.sqlite_masters4   SELECT name FROM %s WHERE type='table' ORDER BY names}   SELECT name FROM  (SELECT * FROM sqlite_master UNION ALL   SELECT * FROM sqlite_temp_master) WHERE type='table' ORDER BY names?   SELECT name FROM sqlite_master WHERE type='table' ORDER BY namei    (   R   t   identifier_preparert   quote_identifierR/  R   t
   DBAPIError(	   R   R1  R   R!   t   qschemat   mastert   st   rsR   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   get_table_names=  s    

c   
      C   s   |  j  j } | d  k	 r+ d | |  } n d } | |  } d | | f } t | j |   } | j   }	 x  | j r | j   d  k	 r qq W|	 d  k	 S(   Ns
   PRAGMA %s.s   PRAGMA s   %stable_info(%s)(   R:  R;  R   t   _pragma_cursorR/  R4  t   closed(
   R   R1  t
   table_nameR   R  R   t   qtablet	   statementR.  R   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt	   has_tableS  s    c   	      K   s   | d  k	 rG |  j j |  } d | } d | f } | j |  } nE y d } | j |  } Wn) t j k
 r d } | j |  } n Xg  | D] } | d ^ q S(   Ns   %s.sqlite_masters3   SELECT name FROM %s WHERE type='view' ORDER BY names|   SELECT name FROM  (SELECT * FROM sqlite_master UNION ALL   SELECT * FROM sqlite_temp_master) WHERE type='view' ORDER BY names>   SELECT name FROM sqlite_master WHERE type='view' ORDER BY namei    (   R   R:  R;  R/  R   R<  (	   R   R1  R   R!   R=  R>  R?  R@  R   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   get_view_namese  s    

c   
      K   s   | d  k	 rJ |  j j |  } d | } d | | f } | j |  } nM y d | } | j |  } Wn- t j k
 r d | } | j |  } n X| j   }	 |	 r |	 d j Sd  S(   Ns   %s.sqlite_masters3   SELECT sql FROM %s WHERE name = '%s'AND type='view's}   SELECT sql FROM  (SELECT * FROM sqlite_master UNION ALL   SELECT * FROM sqlite_temp_master) WHERE name = '%s' AND type='view's?   SELECT sql FROM sqlite_master WHERE name = '%s' AND type='view'i    (   R   R:  R;  R/  R   R<  t   fetchallR   (
   R   R1  t	   view_nameR   R!   R=  R>  R?  R@  R  (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   get_view_definition{  s"    
c         K   s   |  j  j } | d  k	 r+ d | |  } n d } | |  } d | | f } t | j |   }	 |	 j   }
 g  } xo |
 D]g } | d | d j   | d | d | d f \ } } } } } | j |  j | | | | |   q{ W| S(	   Ns
   PRAGMA %s.s   PRAGMA s   %stable_info(%s)i   i   i   i   i   (	   R:  R;  R   RB  R/  RI  t   uppert   appendt   _get_column_info(   R   R1  RD  R   R!   R  R   RE  RF  R   t   rowsR   R   R  R   R   R   R   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   get_columns  s    ?c         C   sa   |  j  |  } | d  k	 r- t j |  } n  i | d 6| d 6| d 6| d 6| d  k d 6| d 6S(   NR  R|   R   R   Rz   R   (   t   _resolve_type_affinityR   R   t	   text_type(   R   R  R   R   R   R   RD   (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyRN    s    c      
   C   s  t  j d |  } | r9 | j d  } | j d  } n d } d } | |  j k rd |  j | } n d | k r| t j } n d | k s d | k s d | k r t j } nX d	 | k s | r t j } n9 d
 | k s d | k s d | k r t j } n	 t j	 } | d k	 rt  j d |  } y) | g  | D] } t |  ^ q/  } Wqt k
 r~t j d | | f  |   } qXn	 |   } | S(   sZ  Return a data type from a reflected column, using affinity tules.

        SQLite's goal for universal compatibility introduces some complexity
        during reflection, as a column's defined type might not actually be a
        type that SQLite understands - or indeed, my not be defined *at all*.
        Internally, SQLite handles this with a 'data type affinity' for each
        column definition, mapping to one of 'TEXT', 'NUMERIC', 'INTEGER',
        'REAL', or 'NONE' (raw bits). The algorithm that determines this is
        listed in http://www.sqlite.org/datatype3.html section 2.1.

        This method allows SQLAlchemy to support that algorithm, while still
        providing access to smarter reflection utilities by regcognizing
        column definitions that SQLite only supports through affinity (like
        DATE and DOUBLE).

        s   ([\w ]+)(\(.*?\))?i   i   Rm   RO   R   t   CLOBR   R	   R   t   FLOAt   DOUBs   (\d+)sN   Could not instantiate type %s with reflected arguments %s; using no arguments.N(   R   R   R   t   ischema_namesR   R   R   t   NullTypeR   R   R   t   findallt   intR<   R   t   warn(   R   R   R   RD   R2   t   a(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyRQ    s8    $$	)	c         K   s_   |  j  | | | |  } g  } x, | D]$ } | d r% | j | d  q% q% Wi | d 6d  d 6S(   NR   R  t   constrained_columns(   RP  RM  R   (   R   R1  RD  R   R!   t   colst   pkeyst   col(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   get_pk_constraint  s    
c         K   s   |  j  j } | d  k	 r+ d | |  } n d } | |  } d | | f } t | j |   }	 g  }
 i  } xp t r |	 j   } | d  k r Pn  | d | d | d | d f \ } } } } |  j | |
 | | | |  qq W|
 S(   Ns
   PRAGMA %s.s   PRAGMA s   %sforeign_key_list(%s)i    i   i   i   (   R:  R;  R   RB  R/  R  R4  t	   _parse_fk(   R   R1  RD  R   R!   R  R   RE  RF  R   t   fkeyst   fksR   t   numerical_idt   rtblt   lcolt   rcol(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   get_foreign_keys  s     	. c         C   s   | d  k r | } n  |  j r6 t j d d |  } n  y | | } WnQ t k
 r i d  d 6g  d 6d  d 6| d 6g  d 6} | j |  | | | <n X| | d k r | d j |  n  | | d k r | d j |  n  | S(   Ns   ^[\"\[`\']|[\"\]`\']$Rm   R  R\  t   referred_schemat   referred_tablet   referred_columns(   R   R)  R   t   subRi   RM  (   R   Rc  Rb  Rd  Re  Rf  Rg  t   fk(    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyRa    s(    		
c      
   K   so  |  j  j } | d  k	 r+ d | |  } n d } | j d t  } | |  } d | | f }	 t | j |	   }
 g  } xp t r |
 j   } | d  k r Pn  | r | d j	 d  r q} n  | j
 t d | d d g  d	 | d
   q} Wx{ | D]s } d | | | d  f }	 | j |	  }
 | d } x7 t rf|
 j   } | d  k rRPn  | j
 | d
  q0Wq W| S(   Ns
   PRAGMA %s.s   PRAGMA t   include_auto_indexess   %sindex_list(%s)i   t   sqlite_autoindexR  t   column_namesR  i   s   %sindex_info(%s)(   R:  R;  R   R/   R0   RB  R/  R  R4  t
   startswithRM  t   dict(   R   R1  RD  R   R!   R  R   Rn  RE  RF  R   t   indexesR   t   idxR]  (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   get_indexes(  s6    	.
	c         K   s   d } | j  | d | } | j   d } d } g  t j | |  D]E \ }	 }
 i |	 d 6g  |
 j d  D] } | j d  ^ qj d 6^ qD S(	   Ns   
            SELECT sql
            FROM
                sqlite_master
            WHERE
                type='table' AND
                name=:table_name
        RD  i    s$   CONSTRAINT (\w+) UNIQUE \(([^\)]+)\)R  t   ,s    "Rp  (   R/  R4  R   RX  R  t   strip(   R   R1  RD  R   R!   t
   UNIQUE_SQLR   t
   table_datat   UNIQUE_PATTERNR  R]  R_  (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   get_unique_constraintsK  s    
N(3   R+   R,   R  R0   t   supports_alterR  t   supports_unicode_statementst   supports_unicode_bindsR'  t   supports_empty_insertRc   R(  t   supports_right_nested_joinst   default_paramstyleR  t   execution_ctx_clsRR   t   statement_compilerRw   t   ddl_compilerR   R{   R   R}   RV  t   colspecsR   R#  t	   sa_schemat   Tablet   construct_argumentsR)  R   R,  R3  R6  R9  R   t   cacheRA  RG  RH  RK  RP  RN  RQ  R`  Rh  Ra  Ru  R{  (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR     sd   	
					5	"c         C   s(   |  j  r$ d   |  _ d   |  _ n  |  S(   s]   work around SQLite issue whereby cursor.description
    is blank when PRAGMA returns no rows.c           S   s   d  S(   N(   R   (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   <lambda>f  s    c           S   s   g  S(   N(    (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyR  g  s    (   RC  R4  RI  (   R.  (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyRB  a  s    	(6   RF   R@   R   Rm   R    R   R   R   R   R   R  R   t   engineR   R   R   R	   R
   R   R   R   R   R   R   R   R   R   R   R   t   objectR   t   DateTimeR-   t   Datet   TimeRH   R  RL   RP   RQ   RV  Rv   RR   t   DDLCompilerRw   t   GenericTypeCompilerR   t   IdentifierPreparerR   t   DefaultExecutionContextR  R"  R   RB  (    (    (    se   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.pyt   <module>   s^   X^=G





















@:$ 