ó
a#nSc           @   sŒ   d  Z  d d l m Z d d l Z d d l Z d d l Z d e f d „  ƒ  YZ d d l m	 Z	 m
 Z
 d d l Z d e f d „  ƒ  YZ d S(	   sF    defines class _DbConnect_, for abstracting connections to databases

iÿÿÿÿ(   t   RDConfigNt   DbErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyR      s   (   t   DbUtilst   DbInfot	   DbConnectc           B   sà   e  Z d  Z d d d d d „ Z d „  Z d d „ Z d d d d d	 „ Z d d d d d
 „ Z d d d „ Z d d d d d d d d „ Z
 d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s�     This class is intended to abstract away many of the details of
    interacting with databases.

    It includes some GUI functionality
    
  t    t   sysdbat	   masterkeyc         C   s:   | |  _  | |  _ | |  _ | |  _ d |  _ d |  _ d S(   s   Constructor

      **Arguments**  (all optional)

        - dbName: the name of the DB file to be used

        - tableName: the name of the table to be used

        - user: the username for DB access

        - password: the password to be used for DB access
      

    N(   t   dbNamet	   tableNamet   usert   passwordt   Nonet   cnt   cursor(   t   selfR
   R   R   R   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyt   __init__    s    					c         C   sÁ   |  j  j ƒ  |  _ |  j j ƒ  |  _ |  j j ƒ  |  _ x- t |  j j	 ƒ  ƒ D] } |  j j
 d ƒ qL W|  j ƒ  } x | D] } |  j j | ƒ qy W| j j | ƒ | j j | ƒ | j ƒ  d S(   s    Modifies a connect dialog to reflect new table names

     **Arguments**

       - dlg: the dialog to be updated
       

    i    N(   t	   userEntryt   GetValueR   t   passwdEntryR   t   dbBrowseButtonR
   t   xranget   dbTableChoicet   Numbert   Deletet   GetTableNamest   Appendt   sizert   Fitt   SetSizeHintst   Refresh(   R   t   dlgt   it   namest   name(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyt   UpdateTableNames7   s    	i    c         C   s+   t  j |  j |  j |  j d | d |  j ƒS(   s   gets a list of tables available in a database

      **Arguments**

      - includeViews: if this is non-null, the views in the db will
        also be returned

      **Returns**

          a list of table names

      **Notes**

       - this uses _DbInfo.GetTableNames_
       

    t   includeViewsR   (   R   R   R
   R   R   R   (   R   R&   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyR   N   s    t   *c         K   sF   | s |  j  } n  t j |  j | |  j |  j d | d | d |  j ƒS(   s¶    gets a list of columns available in the current table

      **Returns**

          a list of column names

      **Notes**

       - this uses _DbInfo.GetColumnNames_
       

    t   joint   whatR   (   R   R   t   GetColumnNamesR
   R   R   R   (   R   t   tableR(   R)   t   wheret   kwargs(    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyR*   c   s
     c         K   sF   | s |  j  } n  t j |  j | |  j |  j d | d | d |  j ƒS(   s   gets a list of columns available in the current table along with their types

      **Returns**

          a list of 2-tuples containing:

            1) column name

            2) column type

      **Notes**

       - this uses _DbInfo.GetColumnNamesAndTypes_
       

    R(   R)   R   (   R   R   t   GetColumnNamesAndTypesR
   R   R   R   (   R   R+   R(   R)   R,   R-   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyR.   t   s
     c         K   s:   | s |  j  } n  t j |  j | | |  j |  j d | ƒS(   s   gets a set of data from a table

      **Arguments**

       - fields: a string with the names of the fields to be extracted,
         this should be a comma delimited list

      **Returns**

          a list of the data

      **Notes**

        - this uses _DbUtils.GetColumns_

    R(   (   R   R   t
   GetColumnsR
   R   R   (   R   t   fieldsR+   R(   R-   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyR/   ‰   s
     iÿÿÿÿi   c   	      K   sƒ   | d k r |  j } n  | j d d ƒ | d <t j |  j | d | d | d |  j d |  j d | d | d	 |  j d
 | d | | �	S(   s<   a more flexible method to get a set of data from a table

      **Arguments**

       - table: (optional) the table to use
       
       - fields: a string with the names of the fields to be extracted,
         this should be a comma delimited list

       - where: the SQL where clause to be used with the DB query

       - removeDups: indicates which column should be used to recognize
         duplicates in the data.  -1 for no duplicate removal.

      **Returns**

          a list of the data

      **Notes**

        - this uses _DbUtils.GetData_
       

    t	   forceListi    t   fieldStringt   whereStringR   R   t
   removeDupsR(   R   t	   transformt   randomAccessN(	   R   R   t   getR   t   GetDataR
   R   R   R   (	   R   R+   R0   R,   R4   R(   R5   R6   R-   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyR8   Ÿ   s    c         K   sf   | d
 k r |  j } n  t j |  j | d d d | d |  j d |  j d |  j d | d d	 ƒd	 d	 S(   sn   returns a count of the number of results a query will return

      **Arguments**

       - table: (optional) the table to use
       
       - where: the SQL where clause to be used with the DB query

       - join: the SQL join clause to be used with the DB query


      **Returns**

          an int

      **Notes**

        - this uses _DbUtils.GetData_

    R2   s   count(*)R3   R   R   R   R(   R1   i    N(   R   R   R   R8   R
   R   R   R   (   R   R+   R,   R(   R-   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyt   GetDataCountÁ   s
    c         C   sP   |  j  d k	 r |  j  St j |  j |  j |  j ƒ |  _ |  j j  ƒ  |  _  |  j  S(   s\    returns a cursor for direct manipulation of the DB
      only one cursor is available

    N(   R   R   t   DbModulet   connectR
   R   R   R   (   R   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyt	   GetCursorÝ   s
    !c         C   s   d |  _ d |  _ d S(   s    closes the cursor

    N(   R   R   R   (   R   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyt
   KillCursoré   s    	c         C   s¬   |  j  ƒ  } y | j d | ƒ Wn& y | j d | ƒ WqJ qJ Xn X|  j ƒ  d | | f } y | j | ƒ Wn& d d l } d G| GH| j ƒ  n X|  j ƒ  d S(   sR   adds a table to the database

    **Arguments**

      - tableName: the name of the table to add

      - colString: a string containing column defintions

    **Notes**

      - if a table named _tableName_ already exists, it will be dropped

      - the sqlQuery for addition is: "create table %(tableName) (%(colString))"
      

    s   drop table %s cascades   drop table %ss   create table %s (%s)iÿÿÿÿNs   command failed:(   R<   t   executet   Committ	   tracebackt	   print_exc(   R   R   t	   colStringt   ct   addStrR@   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyt   AddTableð   s"    
	c         C   s·   |  j  ƒ  } t | ƒ t j k r0 t | ƒ } n  d d j t j g t | ƒ ƒ d } d | | f } y | j	 | | ƒ Wn5 d d l
 } d GH| GHd GH| j ƒ  t d	 ‚ n Xd S(
   s¤    inserts data into a table

    **Arguments**

      - tableName: the name of the table to manipulate

      - vals: a sequence with the values to be inserted

    t   (t   ,t   )s   insert into %s values %siÿÿÿÿNs   insert failed:s   the error was:s   Insert Failed(   R<   t   typet   typest	   TupleTypet   tupleR(   R:   t   placeHoldert   lenR>   R@   RA   R   (   R   R   t   valsRC   t   insTxtt   cmdR@   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyt
   InsertData  s    
'
c         C   s<   |  j  ƒ  } d | | t j | f } | j | | f ƒ d S(   s%   inserts data into a particular column of the table

    **Arguments**

      - tableName: the name of the table to manipulate

      - columnName: name of the column to update

      - value: the value to insert

      - where: a query yielding the row where the data should be inserted

    s   update %s set %s=%s where %sN(   R<   R:   RM   R>   (   R   R   t
   columnNamet   valueR,   RC   RQ   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyt   InsertColumnData/  s    	c         C   s=   |  j  ƒ  } y | j d | | | f ƒ Wn d GHn Xd S(   sÉ    adds a column to a table

    **Arguments**

      - tableName: the name of the table to manipulate

      - colName: name of the column to insert

      - colType: the type of the column to add

    s   alter table %s add %s %ss   AddColumn failedN(   R<   R>   (   R   R   t   colNamet   colTypeRC   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyt	   AddColumnB  s
    c         C   s   |  j  j ƒ  d S(   s+    commits the current transaction
    

    N(   R   t   commit(   R   (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyR?   T  s    N(   R   R   t   __doc__R   R%   R   R*   R.   R/   R   R8   R9   R<   R=   RE   RR   RU   RX   R?   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyR      s"   	!			$			(   RZ   t   rdkitR    t   sysRJ   t
   exceptionst   RuntimeErrorR   t   rdkit.DbaseR   R   R:   t   objectR   (    (    (    sZ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Dbase/DbConnection.pyt   <module>   s   