B
    3Rc~                 @   sl   d dl Z d dlmZ d dlmZ ejZejZejZejZddd	Z	dd
dZ
dd ZdddZdddZdS )    N)RDConfig)DbModulesysdba	masterkey.::template1c       	      C   s   t jr|s>yt || |}W n  tk
r<   td|  g S X | }|t j tjrldd |	 D }ndd |	 D }|
| n2t jrddl}ddl}||j|t j}ng }|S )z returns a list of databases that are available

      **Arguments**

        - user: the username for DB access

        - password: the password to be used for DB access

      **Returns**

        - a list of db names (strings)

    zProblems opening database: %sc             S   s   g | ]}d t |d  qS )z::r   )str).0x r   1lib/python3.7/site-packages/rdkit/Dbase/DbInfo.py
<listcomp>/   s    zGetDbNames.<locals>.<listcomp>c             S   s   g | ]}d t |d  qS )z::r   )r   )r	   r
   r   r   r   r   1   s    r   N)r   ZgetDbSqlconnect	Exceptionprintcursorexecuter   usePgSQLfetchallremoveZfileWildcardos.pathglobpathjoin)	userpasswordZdirNamedBasecncnamesosr   r   r   r   
GetDbNames   s&    r!   c             C   s   |s8yt | ||}W n  tk
r6   td|   g S X | }|sLt j}nt j}|| dd | D }t	j
rd|kr|d |S )a   returns a list of tables available in a database

      **Arguments**

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

        - user: the username for DB access

        - password: the password to be used for DB access

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

      **Returns**

        - a list of table names (strings)

    zProblems opening database: %sc             S   s   g | ]}t |d   qS )r   )r   upper)r	   r
   r   r   r   r   \   s    z!GetTableNames.<locals>.<listcomp>ZPG_LOGDIR_LS)r   r   r   r   r   ZgetTablesSqlZgetTablesAndViewsSqlr   r   r   r   r   )r   r   r   ZincludeViewsr   r   Zcommr   r   r   r   GetTableNames<   s    

r#   c       
      C   sD  | d ks| j d krg S g }tjsx|| j D ]r}|d }|d }|tkrJd}nB|tkrXd}n4|tkrfd}n&|tkrtd}ntj	d|t
|f  |||f q(W n|  }|s|S xt|D ]\}}| j | d }t|}	t|t
rd}nF|	tkrd}n8|	tkrd}n(|	ttfkrd}ntj	d||	f  |||f qW |S )Nr      stringZintegerfloatZbinaryzodd type in col %s: %s
)descriptionr   Z
useSqlLitesqlTextTypessqlIntTypessqlFloatTypessqlBinTypessysstderrwriter   appendfetchone	enumeratetype
isinstanceintr&   
memoryviewbytes)
r   ZresultsitemZcNameZcTypeZtypeStrrivtypr   r   r   GetColumnInfoFromCursorb   sD    

r<    *c       	      C   sH   |st | ||}| }d||f }|r6|d| 7 }|| t|S )a!   gets a list of columns available in a DB table along with their types

      **Arguments**

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

        - table: the name of the table to query

        - user: the username for DB access

        - password: the password to be used for DB access

        - join: an optional join clause (omit the verb 'join')

        - what: an optional clause indicating what to select

      **Returns**

        - a list of 2-tuples containing:

            1) column name

            2) column type

    zselect %s from %sz join %s)r   r   r   r   r<   )	r   tabler   r   r   whatr   r   cmdr   r   r   GetColumnNamesAndTypes   s    
rB   c             C   sz   |st | ||}| }d||f }|rP| ddkrDd| }|d| 7 }|| |  |j}	dd |	D }
|
S )a   gets a list of columns available in a DB table

      **Arguments**

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

        - table: the name of the table to query

        - user: the username for DB access

        - password: the password to be used for DB access

        - join: an optional join clause  (omit the verb 'join')

        - what: an optional clause indicating what to select

      **Returns**

        -  a list of column names

    zselect %s from %sr   r   zjoin %s c             S   s   g | ]}t |d  qS )r   )r   )r	   r
   r   r   r   r      s    z"GetColumnNames.<locals>.<listcomp>)r   r   r   stripfindr   r0   r'   )r   r?   r   r   r   r@   r   r   rA   ZdescZresr   r   r   GetColumnNames   s    
rF   )r   r   r   r   N)r   r   r   N)r   r   r=   r>   N)r   r   r=   r>   N)r,   Zrdkitr   Zrdkit.Dbaser   r(   r)   r*   r+   r!   r#   r<   rB   rF   r   r   r   r   <module>   s   
%
&( 
$