ó
[³XMc           @   sˆ   d  d l  m Z d  d l m Z m Z m Z m Z d  d l m Z d  d l m	 Z	 d  d l
 m Z d e j f d „  ƒ  YZ d g Z d S(	   iÿÿÿÿ(   t	   reflector(   t   DBErrort   getKeyColumnt   quotet   safe(   t
   _TableInfo(   t	   RowObject(   t   reflectt   SQLReflectorc           B   sð   e  Z d  Z d Z i d e j 6d e j 6d e j 6d e j 6Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s  
    DEPRECATED.
    
    I reflect on a database and load RowObjects from it.

    In order to do this, I interrogate a relational database to
    extract schema information and interface with RowObject class
    objects that can interact with specific tables. 
    i    t   =t   <t   >t   likec         C   s    t  j j |  | ƒ | |  _ d S(   s*   Initialize me against a database.
        N(   R    t	   Reflectort   __init__t   dbpool(   t   selfR   t
   rowClasses(    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR      s    c         C   s   |  j  ƒ  d  S(   N(   t   _transPopulateSchema(   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt	   _populate%   s    c         C   s\   xL |  j  D]A } t | t ƒ s> t d t j | j ƒ ƒ ‚ n  |  j | ƒ q
 Wd |  _ d S(   sC   Used to construct the row classes in a single interaction.
        s-   Stub class (%s) is not derived from RowObjecti   N(	   R   t
   issubclassR   R   R   t   qualt   rowClasst   _populateSchemaFort	   populated(   R   t   rc(    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR   (   s
    c         C   s’   d } x6 | D]. } t  | | ƒ s t d | | f ƒ ‚ q q Wt | ƒ } |  j | ƒ | _ |  j | ƒ | _ |  j | ƒ | _ |  j	 | ƒ d S(   s‰   Construct all the SQL templates for database operations on
        <tableName> and populate the class <rowClass> with that info.
        t
   rowColumnst   rowKeyColumnst   rowTableNames(   RowClass %s must have class variable: %sN(   s
   rowColumnss   rowKeyColumnss   rowTableName(
   t   hasattrR   R   t   buildUpdateSQLt	   updateSQLt   buildInsertSQLt	   insertSQLt   buildDeleteSQLt	   deleteSQLt   populateSchemaFor(   R   R   t
   attributest   attt	   tableInfo(    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR   2   s    c         C   s
   t  | ƒ S(   sä   Escape a string for use in an SQL statement. The default
        implementation escapes ' with '' and \ with \. Redefine this
        function in a subclass if your database server uses different
        escaping rules.
        (   R   (   R   t   text(    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt   escape_stringA   s    c         C   s   t  | | d |  j ƒS(   s›   Format a value for use in an SQL statement.

        @param value: a value to format as data in SQL.
        @param type: a key in util.dbTypeMap.
        t   string_escaper(   R   R)   (   R   t   valuet   type(    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt   quote_valueI   s    c         C   s…   | r | r t  d ƒ ‚ n  | rT |  j | ƒ } | j | ƒ } |  j | | ƒ } n | r] n g  } |  j j |  j | | | | | ƒ S(   se  Load a set of RowObjects from a database.

        Create a set of python objects of <rowClass> from the contents
        of a table populated with appropriate data members.
        Example::

          |  class EmployeeRow(row.RowObject):
          |      pass
          |
          |  def gotEmployees(employees):
          |      for emp in employees:
          |          emp.manager = "fred smith"
          |          manager.updateRow(emp)
          |
          |  reflector.loadObjectsFrom("employee",
          |                          data = userData,
          |                          whereClause = [("manager" , EQUAL, "fred smith")]
          |                          ).addCallback(gotEmployees)

        NOTE: the objects and all children should be loaded in a single transaction.
        NOTE: can specify a parentRow _OR_ a whereClause.

        s.   Must specify one of parentRow _OR_ whereClause(   R   t   getTableInfot   getRelationshipFort   buildWhereClauseR   t   runInteractiont
   _rowLoader(   R   t	   tableNamet	   parentRowt   datat   whereClauset   forceChildrent   infot   relationship(    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt   loadObjectsFromQ   s    	c      
   C   s§  |  j  | } d } d }	 x> | j D]3 \ }
 } |	 r> d }	 n
 | d } | d |
 } q# W| d | } | rü | d 7} d }	 x{ | D]p } |	 rš d }	 n
 | d 7} | \ } } } |  j | | ƒ } |  j | | ƒ } | d	 | |  j | | f 7} q… Wn  | j | ƒ | j ƒ  } g  } g  } xó | D]ë } i  } xu t d t | ƒ ƒ D]^ } | j | d j	 ƒ  } x> | j
 j D]0 \ } } | j	 ƒ  | k rt| | | | <PqtqtWqJW|  j | j
 | ƒ } | s| j d } | | j
 | | ƒ } |  j | ƒ | j | ƒ n  | j | ƒ q(W| r3|  j | | | ƒ n  xm | j D]b } | rZ| j rZq=n  xB | D]: } |  j | | ƒ } |  j | | j j | | | | ƒ qaWq=W| S(
   sM   immediate loading of rowobjects from the table with the whereClause.
        s   SELECT i   i    t   ,s    %ss	    FROM %s s    WHERE s    AND s   %s %s %s(   t   schemaR   t   findTypeForR-   t   conditionalLabelst   executet   fetchallt   ranget   lent   lowerR   t   findInCachet   rowFactoryMethodt
   addToCachet   appendt   addToParentt   relationshipst   autoLoadR0   R2   t   childRowClassR   (   R   t   transactionR3   R4   R5   R6   R7   R'   t   sqlt   firstt   columnR,   t   wItemt
   columnNamet   condR+   t   tt   quotedValuet   rowst   resultst   newRowst   argst   kwt   it
   ColumnNamet   attrt   resultObjectt   methR9   t   rowt   childWhereClause(    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR2   x   sf    	

	
		c         C   sM   |  j  | } | j ƒ  } x- | j D]" \ } } | j ƒ  | k r# | Sq# Wd  S(   N(   R<   RC   R   (   R   R3   RQ   R'   RO   R,   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR=   ¿   s
    c         C   sÍ   d | j  } d } x\ | j D]Q \ } } t | j | ƒ rA q n  | sT | d } n  | d | d f } d } q Wd } | d } xD | j D]9 \ } } | s« | d } n  | d	 | d f } d } qŒ W| S(
   s   (Internal) Build SQL template to update a RowObject.

        Returns: SQL that is used to contruct a rowObject class.
        s   UPDATE %s SETi   s   , s    %s = %ss   %si    s    WHERE s    AND s	    %s = %s (   R   R   R   R   R   (   R   R'   RM   RN   RO   R,   t	   keyColumn(    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR   Æ   s"    


c         C   s«   d | j  } d } x: | j D]/ \ } } | s< | d } n  | | } d } q W| d } d } x: | j D]/ \ } } | s‰ | d } n  | d } d } qj W| d } | S(   s²   (Internal) Build SQL template to insert a new row.

        Returns: SQL that is used to insert a new row for a rowObject
        instance not created from the database.
        s   INSERT INTO %s (i   s   , i    s    ) VALUES (s   %st   )(   R   R   (   R   R'   RM   RN   RO   R,   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR    à   s     





c         C   sh   d | j  } d } | d } xD | j D]9 \ } } | sF | d } n  | d | d f } d } q' W| S(   s?   Build the SQL template to delete a row from the table.
        s   DELETE FROM %s i   s    WHERE s    AND s	    %s = %s s   %si    (   R   R   (   R   R'   RM   RN   Ra   R,   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR"   ü   s    

c         C   s½   g  } |  j  | j } xN | j D]C \ } } t | j | ƒ s  | j |  j | j | ƒ | ƒ ƒ q  q  Wx9 | j D]. \ } } | j |  j | j | ƒ | ƒ ƒ qq W|  j	 | ƒ j
 t | ƒ S(   s7   Build SQL to update the contents of rowObject.
        (   R<   R   R   R   t	   __class__RG   R-   t   findAttributeR   R.   R   t   tuple(   R   t	   rowObjectRX   R'   RO   R,   Ra   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt   updateRowSQL
  s    c         C   s,   |  j  | ƒ } | j d ƒ |  j j | ƒ S(   s:   Update the contents of rowObject to the database.
        i    (   Rg   t   setDirtyR   t   runOperation(   R   Rf   RM   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt	   updateRow  s    c         C   sl   g  } |  j  | j } x9 | j D]. \ } } | j |  j | j | ƒ | ƒ ƒ q  W|  j | ƒ j t | ƒ S(   s7   Build SQL to insert the contents of rowObject.
        (	   R<   R   R   RG   R-   Rd   R.   R!   Re   (   R   Rf   RX   R'   RO   R,   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt   insertRowSQL"  s
    &c         C   s,   | j  d ƒ |  j | ƒ } |  j j | ƒ S(   s(   Insert a new row for rowObject.
        i    (   Rh   Rk   R   Ri   (   R   Rf   RM   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt	   insertRow,  s    c         C   sl   g  } |  j  | j } x9 | j D]. \ } } | j |  j | j | ƒ | ƒ ƒ q  W|  j | ƒ j t | ƒ S(   s9   Build SQL to delete rowObject from the database.
        (	   R<   R   R   RG   R-   Rd   R.   R#   Re   (   R   Rf   RX   R'   Ra   R,   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt   deleteRowSQL3  s    c         C   s,   |  j  | ƒ } |  j | ƒ |  j j | ƒ S(   s8   Delete the row for rowObject from the database.
        (   Rm   t   removeFromCacheR   Ri   (   R   Rf   RM   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt	   deleteRow?  s    N(   t   __name__t
   __module__t   __doc__R   R    t   EQUALt   LESSTHANt   GREATERTHANt   LIKER>   R   R   R   R   R)   R-   t   NoneR:   R2   R=   R   R    R"   Rg   Rj   Rk   Rl   Rm   Ro   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR      s4   	


			
			&	G							
		N(   t   twisted.enterpriseR    t   twisted.enterprise.utilR   R   R   R   R   t   twisted.enterprise.rowR   t   twisted.pythonR   R   R   t   __all__(    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt   <module>   s   "ÿ ;