ó
ø¢TQc           @   sè  d  Z  d d l Z d d l Z d d l m Z d d l Td d l m Z d d l m	 Z	 d d l
 m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z m Z d d l m Z d d l m Z y d d l Z d d l Z Wn3 e k
 r/Z d d l  m! Z! e! d e ƒ ‚ n Xe j" Z" e j# Z# e j$ j% e j$ j& ƒ e j$ j' e e j$ j( ƒ e j$ j' e e j$ j( ƒ e j) d ƒ Z* d „  Z+ d e, f d „  ƒ  YZ- d e. f d „  ƒ  YZ/ d e0 f d „  ƒ  YZ1 d S(   sa   
PostgreSQL database backend for Django.

Requires psycopg 2: http://initd.org/projects/psycopg2
iÿÿÿÿN(   t   utils(   t   *(   t   connection_created(   t   DatabaseOperations(   t   DatabaseClient(   t   DatabaseCreation(   t   get_version(   t   DatabaseIntrospection(   t	   force_str(   t   SafeTextt	   SafeBytes(   t   six(   t   utc(   t   ImproperlyConfigureds!   Error loading psycopg2 module: %ss   django.db.backendsc         C   s   |  d k r t  d ƒ ‚ n  t S(   Ni    s$   database connection isn't set to UTC(   t   AssertionErrorR   (   t   offset(    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   utc_tzinfo_factory&   s    t   CursorWrapperc           B   s>   e  Z d  Z d „  Z d d „ Z d „  Z d „  Z d „  Z RS(   s   
    A thin wrapper around psycopg2's normal cursor class so that we can catch
    particular exception instances and reraise them with the right types.
    c         C   s   | |  _  d  S(   N(   t   cursor(   t   selfR   (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   __init__1   s    c         C   s­   y |  j  j | | ƒ SWn t j k
 ra } t j t j t j t | j ƒ Œ  t	 j
 ƒ  d ƒ nH t j k
 r¨ } t j t j t j t | j ƒ Œ  t	 j
 ƒ  d ƒ n Xd  S(   Ni   (   R   t   executet   Databaset   IntegrityErrorR   t   reraiseR    t   tuplet   argst   syst   exc_infot   DatabaseError(   R   t   queryR   t   e(    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyR   4   s    5c         C   s­   y |  j  j | | ƒ SWn t j k
 ra } t j t j t j t | j ƒ Œ  t	 j
 ƒ  d ƒ nH t j k
 r¨ } t j t j t j t | j ƒ Œ  t	 j
 ƒ  d ƒ n Xd  S(   Ni   (   R   t   executemanyR   R   R   R   R    R   R   R   R   R   (   R   R   R   R   (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyR    <   s    5c         C   s.   | |  j  k r |  j  | St |  j | ƒ Sd  S(   N(   t   __dict__t   getattrR   (   R   t   attr(    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   __getattr__D   s    c         C   s   t  |  j ƒ S(   N(   t   iterR   (   R   (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   __iter__J   s    N(	   t   __name__t
   __module__t   __doc__R   t   NoneR   R    R$   R&   (    (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyR   +   s   			t   DatabaseFeaturesc           B   sJ   e  Z e Z e Z e Z e Z e Z e Z	 e Z
 e Z e Z e Z e Z RS(    (   R'   R(   t   Falset   needs_datetime_string_castt   Truet   can_return_id_from_insertt&   requires_rollback_on_dirty_transactiont   has_real_datatypet   can_defer_constraint_checkst   has_select_for_updatet   has_select_for_update_nowaitt   has_bulk_insertt   supports_tablespacest   supports_transactionst   can_distinct_on_fields(    (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyR+   M   s   t   DatabaseWrapperc           B   sÖ   e  Z d  Z i d d 6d d 6d d 6d d 6d	 d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6Z d „  Z d" d „ Z d „  Z d „  Z e	 e ƒ Z
 d „  Z d „  Z d „  Z d  „  Z d! „  Z RS(#   t
   postgresqls   = %st   exacts   = UPPER(%s)t   iexacts   LIKE %st   containss   LIKE UPPER(%s)t	   icontainss   ~ %st   regexs   ~* %st   iregexs   > %st   gts   >= %st   gtes   < %st   lts   <= %st   ltet
   startswitht   endswitht   istartswitht	   iendswithc         O   sÓ   t  t |  ƒ j | | Ž  t |  ƒ |  _ |  j d j d t ƒ } | |  j _ | rb t	 j
 j } n t	 j
 j } |  j | ƒ t |  ƒ |  _ t |  ƒ |  _ t |  ƒ |  _ t |  ƒ |  _ t |  ƒ |  _ d  |  _ d  S(   Nt   OPTIONSt
   autocommit(   t   superR9   R   R+   t   featurest   settings_dictt   getR,   t   uses_autocommitt   psycopg2t
   extensionst   ISOLATION_LEVEL_AUTOCOMMITt   ISOLATION_LEVEL_READ_COMMITTEDt   _set_isolation_levelR   t   opsR   t   clientR   t   creationR   t   introspectiont   BaseDatabaseValidationt
   validationR*   t   _pg_version(   R   R   t   kwargsRJ   t   level(    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyR   m   s    c         C   s*   |  j  ƒ  j d ƒ |  j  ƒ  j d ƒ d S(   s•   
        To check constraints, we set constraints to immediate. Then, when, we're done we must ensure they
        are returned to deferred.
        s   SET CONSTRAINTS ALL IMMEDIATEs   SET CONSTRAINTS ALL DEFERREDN(   R   R   (   R   t   table_names(    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   check_constraints   s    c         C   sw   |  j  ƒ  |  j d  k r d  Sy |  j j ƒ  d  |  _ Wn9 t j k
 rr d  |  _ t j d d t j	 ƒ  ƒ‚  n Xd  S(   Ns,   psycopg2 error while closing the connection.R   (
   t   validate_thread_sharingt
   connectionR*   t   closeR   t   Errort   loggert   warningR   R   (   R   (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyRb   ‡   s    
	c         C   s+   |  j  d  k r$ t |  j ƒ |  _  n  |  j  S(   N(   R[   R*   R   Ra   (   R   (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   _get_pg_versionš   s    c         C   s  |  j  } |  j d  k rí| d sA d d l m } | d ƒ ‚ n  i | d d 6} | j | d ƒ d | k ry | d =n  | d r” | d | d	 <n  | d
 rµ t | d
 ƒ | d <n  | d rÐ | d | d <n  | d rë | d | d <n  t j |   |  _ |  j j	 d ƒ t
 j rd n | j d ƒ } | r´y |  j j } Wn t k
 rZd  } n X| d ƒ } | | k r´|  j j t j j ƒ |  j j ƒ  j |  j j ƒ  | g ƒ q´n  |  j j |  j ƒ |  j ƒ  t j d |  j d |  ƒ n  |  j j ƒ  } t
 j rt n d  | _ t | ƒ S(   Nt   NAMEiÿÿÿÿ(   R   sJ   settings.DATABASES is improperly configured. Please supply the NAME value.t   databaseRI   RJ   t   USERt   usert   PASSWORDt   passwordt   HOSTt   hostt   PORTt   portt   UTF8t   UTCt	   TIME_ZONEt   TimeZonet   senderRa   (   RM   Ra   R*   t   django.core.exceptionsR   t   updateR   R   t   connectt   set_client_encodingt   settingst   USE_TZRN   t   get_parameter_statust   AttributeErrort   set_isolation_levelRP   RQ   RR   R   R   RU   t   set_time_zone_sqlt   isolation_levelRf   R   t   sendt	   __class__R   t   tzinfo_factoryR   (   R   RM   R   t   conn_paramst   tzR|   t   conn_tzR   (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   _cursor    sN    	






	
c         C   s6   |  j  j r2 | r2 |  j r2 |  j t j j ƒ n  d S(   s–   
        Switch the isolation level when needing transaction support, so that
        the same transaction is visible across all the queries.
        N(   RL   RO   R€   RT   RP   RQ   RS   (   R   t   managed(    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   _enter_transaction_managementÏ   s    c         C   s6   |  j  j r2 | r2 |  j r2 |  j t j j ƒ n  d S(   s€   
        If the normal operating mode is "autocommit", switch back to that when
        leaving transaction management.
        N(   RL   RO   R€   RT   RP   RQ   RR   (   R   Rˆ   (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   _leave_transaction_management×   s    c         C   sa   | t  d ƒ k s t ‚ z& |  j d k	 r= |  j j | ƒ n  Wd | |  _ t | ƒ |  j _ Xd S(   s×   
        Do all the related feature configurations for changing isolation
        levels. This doesn't touch the uses_autocommit feature, since that
        controls the movement *between* isolation levels.
        i   N(	   t   rangeR   Ra   R*   R~   R€   t   boolRL   t   uses_savepoints(   R   R]   (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyRT   ß   s    	c         C   sr   |  j  d  k	 rn y |  j  j ƒ  SWqn t j k
 rj } t j t j t j t | j	 ƒ Œ  t
 j ƒ  d ƒ qn Xn  d  S(   Ni   (   Ra   R*   t   commitR   R   R   R   R    R   R   R   R   (   R   R   (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   _commití   s
    N(   R'   R(   t   vendort	   operatorsR   R*   R_   Rb   Rf   t   propertyt
   pg_versionR‡   R‰   RŠ   RT   R   (    (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyR9   Z   s4   
				/			(2   R)   t   loggingR   t	   django.dbR    t   django.db.backendst   django.db.backends.signalsR   t1   django.db.backends.postgresql_psycopg2.operationsR   t-   django.db.backends.postgresql_psycopg2.clientR   t/   django.db.backends.postgresql_psycopg2.creationR   t.   django.db.backends.postgresql_psycopg2.versionR   t4   django.db.backends.postgresql_psycopg2.introspectionR   t   django.utils.encodingR   t   django.utils.safestringR	   R
   t   django.utilsR   t   django.utils.timezoneR   RP   R   t   psycopg2.extensionst   ImportErrorR   Rv   R   R   R   RQ   t   register_typet   UNICODEt   register_adaptert   QuotedStringt	   getLoggerRd   R   t   objectR   t   BaseDatabaseFeaturesR+   t   BaseDatabaseWrapperR9   (    (    (    sD   ../Django//lib/python/django/db/backends/postgresql_psycopg2/base.pyt   <module>   s<   
			"