B
    3Rc8                 @   s"  d Z ddl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 Z
d1ddZd2ddZd3ddZd4ddZdd Zd5ddZd6ddZd7d d!Zd8d#d$Zd9d&d'Zed(kre	 Zed) ed* ed+ ed, ed dd-lmZ ddlZejejd.d/Zeed0e dS ):z a set of functions for interacting with databases

 When possible, it's probably preferable to use a _DbConnection.DbConnect_ object

    N)DbInfo)DbModule)DbResultSetRandomAccessDbResultSet)StringIOc                s    fdd|D S )zl Given a list fromL, returns an iterator of the elements specified using their
    indices in the list what c                s   g | ]} | qS  r   ).0x)fromLr   2lib/python3.7/site-packages/rdkit/Dbase/DbUtils.py
<listcomp>   s    z_take.<locals>.<listcomp>r   )r
   Zwhatr   )r
   r   _take   s    r   sysdba	masterkey c       	      C   sb   |st | ||}| }d||f }|rP| ddkrDd| }|d| 7 }|| | S )ax   gets a set of data from a table

      **Arguments**

       - dBase: database name

       - table: table name

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

       - user and  password:

       - join: a join clause (omit the verb 'join')


      **Returns**

       - a list of the data

    zselect %s from %sjoinr   zjoin %s )r   connectcursorstripfindexecutefetchall)	dBasetablefieldStringuserpasswordr   cnccmdr   r   r   
GetColumns    s    
r!   *   c             C   s  |r|	dk	rt d|r$|
s$t d|dkr0d}|sBt| ||}| }d||f }|r| ddkrtd	| }|d
| 7 }|r| ddkrd| }|d
| 7 }|ry |s|| n||| W n6 tk
r   tj	
d|  ddl}|  dS X |	dk	rt d|
s(t d| }|dkrt }xd|dd D ].}|| |krl|| n|||  qNW n$|
rt}nt}||||||	|d}|S )a;   a more flexible method to get 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

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

       - removeDups indicates the column which should be used to screen
          out duplicates.  Only the first appearance of a duplicate will
          be left in the dataset.

      **Returns**

        - a list of the data


      **Notes**

        - EFF: this isn't particularly efficient

    Nz4forceList and transform arguments are not compatiblez5when forceList is set, randomAccess must also be usedr#   Tzselect %s from %sr   r   zjoin %sr   wherezwhere %sz#the command "%s" generated errors:
)
removeDups	transformextras)
ValueErrorr   r   r   r   r   r   	Exceptionsysstderrwrite	traceback	print_excr   setremoveaddr   r   )r   r   r   ZwhereStringr   r   r&   r   Z	forceListr'   ZrandomAccessr(   r   r   r    r.   dataseenentryklassr   r   r   GetDataB   sX    

r7   ,c	             C   s   t |r"| ddkr"d| }t |rD| ddkrDd| }d||||f }	|sft| ||}| }
|
|	 g }g }xFtt |
jD ]4}|
j| }|d t	j
kr|| ||d  qW g }||| |
 }x0|D ](}t||}||d	d
 |D  qW d|S )ap   Pulls the contents of a database and makes a deliminted text file from them

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

        - table: the name of the table to query

        - fields: the fields to select with the SQL query

        - join: the join clause of the SQL query
          (e.g. 'join foo on foo.bar=base.bar')

        - where: the where clause of the SQL query
          (e.g. 'where foo = 2' or 'where bar > 17.6')

        - user: the username for DB access

        - password: the password to be used for DB access

      **Returns**

        - the CSV data (as text)

    r%   r#   zwhere %sr   zjoin %szselect %s from %s %s %sr$   r   c             S   s   g | ]}t |qS r   )str)r   r	   r   r   r   r      s    z"DatabaseToText.<locals>.<listcomp>
)lenr   r   r   r   r   r   rangeZdescriptionr   ZsqlBinTypesappendr   r   r   )r   r   fieldsr   r%   r   r   delimr   Z
sqlCommandr   ZheadersZ
colsToTakeiitemlinesZresultsresdr   r   r   DatabaseToText   s.    




rE   c             C   s  t dtdtdddi}dg| }xt|D ]t}ddg}x\t|D ]N}| | | }	|	dkr`qDt|	}
|
t kr|
tkrt}
yt|	}	W q tk
r } z0td|d |f  tdt|	  t|W dd}~X Y qX nt|d t	t|	|d< t
|	tr0|dks|	|krtt	|	|d }t|g}qDyt t|	}W n tk
rZ   t }
Y nX ||	krjt}
t
|d tsD||
 ||d  krD|
|d< qDW |||< q*W |S )	z

      finds the types of the columns in _data_

      if nullMarker is not None, elements of the data table which are
        equal to nullMarker will not count towards setting the type of
        their columns.

          r$   r#   Nz2cannot convert text from row %d col %d to a stringz	>%sr   )floatintr9   r<   typeUnicodeErrorprintreprmaxr;   
isinstanceOverflowError)r3   nRowsnCols
nullMarkerZ
prioritiesrC   colZtypeHererowrD   ZlocTypemsglZfDr   r   r   
TypeFinder   s@    




"rX   c             C   s   xt t| D ]}| |  | |< | | dd| |< | | dd| |< | | dd| |< t| | |kr| | dd}|d| }td| | |f  || |< qW | S )z~ *For Internal Use*

      removes illegal characters from column headings
      and truncates those which are too long.

    r   _-.r   Nz#	Heading %s too long, changed to %s)r<   r;   r   replacerL   )colHeadingsmaxColLabelLenr@   ZnewHeadr   r   r   _AdjustColHeadings   s    r_   c             C   s   g }xt t|D ]}|| }|d tkr>|d| |   n:|d tkr^|d| |   n|d| | |d f  | | |krd|d  |d< qW |S )z)  returns a list of SQL type strings
    r   z%s double precisionz
%s integerz%s varchar(%d)r$   z%s not null primary keyr#   )r<   r;   rH   r=   rI   )r]   colTypeskeyColtypeStrsr@   typr   r   r   GetTypeStrings  s    rd   Fc             C   s   y|   || W n tk
r   d}|   xv|D ]n}y |   |t| |d7 }W n@ tk
r   |sdd l}|  td| tdt	| Y q6X |   q6W Y n
X t
|}|S )Nr   r$   zinsert failed:	)r   Zexecutemanyr*   commitr   tupler.   r/   rL   rM   r;   )ZconnsqlStrblockZsilentrC   rU   r.   r   r   r   _insertBlock"  s$    

rj   d   c
             C   s   |	st | ||}	|	 }
y|
d|  W n  tk
rL   td|  Y nX yd||f }|
| W n6 tk
r   td| td ddl}|  dS X |	  d}
g }t j	gt
|d  }d|}d	||f }d}x|D ]}dgt
| }xtt
|D ]}|| dk	r|dks0|| |kr|| d tkrTt|| ||< n4|| d tkrxt|| ||< nt|| ||< nd||< qW |t| t
||kr|t|	||7 }t|	d
r|	js|	  g }qW t
|r |t|	||7 }t|	d
r|	js|	  dS )zX *For Internal Use*

      (drops and) creates a table and then inserts the values

    zdrop table %szcannot drop table %szcreate table %s (%s)zcreate table failed: zhere is the exception:r   Nr8   zinsert into %s values (%s)
autocommit)r   r   r   r   r*   rL   r.   r/   rf   ZplaceHolderr;   r   r<   rH   rI   r9   r=   rg   rj   hasattrrl   )r   r   r   r   colDefsr`   r3   rS   Z	blockSizer   r   rh   r.   ri   ZentryTxtZdStrZnDonerU   entriesrT   r   r   r   _AddDataToDb9  sX    


rp      c	             C   sb  | dd | dd | |}	t|	| t|	}
g }| }x|r| dd}| dd}||}t||
krtdt| t||
kstdg }x^|D ]V}yt|}W n: t	k
r   yt
|}W n t	k
r   |}Y nX Y nX || qW || | }qFW t|}t|||
|d	}t|	||d
}d|}t| |||||||d	 dS )aP  loads the contents of the text file into a database.

      **Arguments**

        - dBase: the name of the DB to use

        - table: the name of the table to create/overwrite

        - inF: the file like object from which the data should
          be pulled (must support readline())

        - delim: the delimiter used to separate fields

        - user: the user name to use in connecting to the DB

        - password: the password to use in connecting to the DB

        - maxColLabelLen: the maximum length a column label should be
          allowed to have (truncation otherwise)

        - keyCol: the column to be used as an index for the db

      **Notes**

        - if _table_ already exists, it is destroyed before we write
          the new data

        - we assume that the first row of the file contains the column names

    rZ   rY   r   r   r:   z>>>zunequal length)rS   )ra   r8   N)r\   readlinesplitr_   r;   rL   rM   AssertionErrorrI   r)   rH   r=   rX   rd   r   rp   )r   r   ZinFr?   r   r   r^   ra   rS   r]   rR   r3   ZinLZsplitLZtmpVectr5   valrQ   r`   rb   rn   r   r   r   TextFileToDatabaseq  s>     




rw   Nonec             C   sF   t  }|t| ||||||d |d t||||||	|
d dS )z.

     FIX: at the moment this is a hack

    )r>   r   r%   r   r   r   )r   r   ra   rS   N)r   r-   rE   seekrw   )ZfromDbZfromTblZtoDbZtoTblr>   r   r%   r   r   ra   rS   sior   r   r   DatabaseToDatabase  s    

r{   __main__zfoo,bar,baz
z1,2,3
z1.1,4,5
z4,foo,6
)RDConfigZDbasezTEST.GDBZfromtext)r   r   r   N)r"   r   r   r   r#   r   r   Nr$   NN)r"   r   r   r   r   r8   N)N)N)F)Nrk   N)r8   r   r   rq   NN)r"   r   r   r   r   Nrx   ) __doc__r+   Zrdkit.Dbaser   r   Zrdkit.Dbase.DbResultSetr   r   ior   r   r!   r7   rE   rX   r_   rd   rj   rp   rw   r{   __name__rz   r-   ry   Zrdkitr}   ospathr   Z	RDCodeDirZdirLocr   r   r   r   <module>   sB   
"  
N 
8
/

 
7 
D 






