B
    3Rc%                 @   s<   d Z ddlmZmZmZ G dd deZG dd deZdS )zF defines class _DbConnect_, for abstracting connections to databases

    )DbUtilsDbInfoDbModulec               @   s   e Zd ZdS )DbErrorN)__name__
__module____qualname__ r	   r	   7lib/python3.7/site-packages/rdkit/Dbase/DbConnection.pyr      s   r   c               @   s   e Zd ZdZd&ddZd'dd	Zd(ddZd)ddZd*ddZd+ddZ	d,ddZ
dd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% ZdS )-	DbConnectz  This class is intended to abstract away many of the details of
    interacting with databases.

    It includes some GUI functionality

   sysdba	masterkeyc             C   s(   || _ || _|| _|| _d| _d| _dS )a   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)dbName	tableNameuserpasswordcncursor)selfr   r   r   r   r	   r	   r
   __init__   s    zDbConnect.__init__r   c             C   s   t j| j| j| j|| jdS )a   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_

    )includeViewsr   )r   GetTableNamesr   r   r   r   )r   r   r	   r	   r
   r   4   s    zDbConnect.GetTableNames*c          	   K   s*   |p| j }tj| j|| j| j||| jdS )z gets a list of columns available in the current table

      **Returns**

          a list of column names

      **Notes**

       - this uses _DbInfo.GetColumnNames_

    )joinwhatr   )r   r   GetColumnNamesr   r   r   r   )r   tabler   r   wherekwargsr	   r	   r
   r   H   s    
zDbConnect.GetColumnNamesc          	   K   s*   |p| j }tj| j|| j| j||| jdS )a   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   GetColumnNamesAndTypesr   r   r   r   )r   r   r   r   r   r   r	   r	   r
   r    X   s    
z DbConnect.GetColumnNamesAndTypesc             K   s&   |p| j }tj| j||| j| j|dS )a   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   
GetColumnsr   r   r   )r   fieldsr   r   r   r	   r	   r
   r!   l   s    
zDbConnect.GetColumnsN   c       	      K   sJ   |p| j }|dd|d< tj| j|f||| j| j||| j||d	|S )a-   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_

    	forceListr   )	fieldStringwhereStringr   r   
removeDupsr   r   	transformrandomAccess)r   getr   GetDatar   r   r   r   )	r   r   r"   r   r(   r   r)   r*   r   r	   r	   r
   r,      s
    
zDbConnect.GetDatac             K   s6   |p| j }tj| j|d|| j| j| j|dd	d d S )ag   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_

    zcount(*)r   )r&   r'   r   r   r   r   r%   )r   r   r,   r   r   r   r   )r   r   r   r   r   r	   r	   r
   GetDataCount   s    
zDbConnect.GetDataCountc             C   s8   | j dk	r| j S t| j| j| j| _| j  | _ | j S )z\ returns a cursor for direct manipulation of the DB
      only one cursor is available

    N)r   r   Zconnectr   r   r   r   )r   r	   r	   r
   	GetCursor   s
    
zDbConnect.GetCursorc             C   s$   d| _ | jdk	r| j  d| _dS )z closes the cursor

    N)r   r   close)r   r	   r	   r
   
KillCursor   s    

zDbConnect.KillCursorc             C   s   |   }y|d|  W n< tk
rV   y|d|  W n tk
rP   Y nX Y nX |   d||f }y|| W n. tk
r   ddl}td| |  Y n
X |   dS )aL   adds a table to the database

    **Arguments**

      - tableName: the name of the table to add

      - colString: a string containing column definitions

    **Notes**

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

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

    zdrop table %s cascadezdrop table %szcreate table %s (%s)r   Nzcommand failed:)r.   execute	ExceptionCommit	tracebackprint	print_exc)r   r   Z	colStringcZaddStrr4   r	   r	   r
   AddTable   s"    
zDbConnect.AddTablec             C   s   |   }t|tkrt|}ddtjgt|  d }d||f }y||| W nD tk
r   ddl	}t
d t
| t
d |  td	Y nX dS )
z inserts data into a table

    **Arguments**

      - tableName: the name of the table to manipulate

      - vals: a sequence with the values to be inserted

    (,)zinsert into %s values %sr   Nzinsert failed:zthe error was:zInsert Failed)r.   typetupler   r   placeHolderlenr1   r2   r4   r5   r6   r   )r   r   Zvalsr7   ZinsTxtcmdr4   r	   r	   r
   
InsertData   s    
zDbConnect.InsertDatac             C   s,   |   }d||tj|f }|||f dS )a%   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

    zupdate %s set %s=%s where %sN)r.   r   r>   r1   )r   r   Z
columnNamevaluer   r7   r@   r	   r	   r
   InsertColumnData  s    zDbConnect.InsertColumnDatac             C   sB   |   }y|d|||f  W n tk
r<   td Y nX dS )z 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

    zalter table %s add %s %szAddColumn failedN)r.   r1   r2   r5   )r   r   ZcolNameZcolTyper7   r	   r	   r
   	AddColumn  s
    zDbConnect.AddColumnc             C   s   | j   dS )z& commits the current transaction

    N)r   Zcommit)r   r	   r	   r
   r3   1  s    zDbConnect.Commit)r   r   r   r   )r   )r   r   r   r   )r   r   r   r   )r   r   )Nr   r   r#   r   Nr$   )Nr   r   )r   r   r   __doc__r   r   r   r    r!   r,   r-   r.   r0   r8   rA   rC   rD   r3   r	   r	   r	   r
   r      s    




 

	$r   N)	rE   Zrdkit.Dbaser   r   r   RuntimeErrorr   objectr   r	   r	   r	   r
   <module>   s   