
`9Sc           @   sg   d  Z  d d l Z d   Z d   Z d   Z d   Z d   Z d   Z d	   Z	 d
   Z
 d   Z d S(   sD   Auxiliary functions to work with Microsoft Access databases via ADO.iNc         C   s,   t  j j j d  } | j t |    | S(   s   Open an existing Access database file.

    Return the opened ADO connection object.

    Raise 'pywintypes.com_error' on failure.s   ADODB.Connection(   t   win32comt   clientt   gencachet   EnsureDispatcht   Opent   getConnectionString(   t   filePatht   dbConnection(    (    s   code\common\utils\access.pyt   openDatabase   s    	c         C   s(   t  j j j d  } | j t |    S(   s   Create a new empty Access database file.

    Return the opened ADO connection object.

    Raise 'pywintypes.com_error' on failure.s   ADOX.Catalog(   R    R   R   R   t   CreateR   (   R   t
   adoCatalog(    (    s   code\common\utils\access.pyt   createDatabase$   s    c         C   sa   |  j  d  } z@ g  } x3 | j sM | j | j j d  j  | j   q WWd | j   X| S(   s@   Return the list of available table names for an opened database.i   t
   TABLE_NAMEN(   t
   OpenSchemat   EOFt   appendt   Fieldst   Itemt   Valuet   MoveNextt   Close(   R   t   recSett
   tableNames(    (    s   code\common\utils\access.pyt   getTableNames/   s    c         C   s   |  j  d  } za g  } xT | j sn | j } | j d  j | k ra | j | j d  j  n  | j   q WWd | j   X| S(   s@   Return the list of field names of a table in an opened database.i   R   t   COLUMN_NAMEN(   R   R   R   R   R   R   R   R   (   R   t	   tableNameR   t
   fieldNamest   fields(    (    s   code\common\utils\access.pyt   getFieldNames@   s    	c            s}   t    t     k o# d k n s. t  d j    f d   t t     D  } |  j d t |  | f  d S(   s   Create a new table in an opened database, given the sequences of its
    field names and SQL data types.

    Raise 'pywintypes.com_error' on failure.i    s   , c         3   s+   |  ]! } t    |  d   | Vq d S(   t    N(   t	   quoteName(   t   .0t   i(   R   t   fieldDataTypes(    s   code\common\utils\access.pys	   <genexpr>\   s   s   CREATE TABLE %s (%s)N(   t   lent   AssertionErrort   joint   ranget   ExecuteR   (   R   R   R   R!   t   fieldsDefinition(    (   R!   R   s   code\common\utils\access.pyt   createTableS   s
    .	c         C   s   |  d k r d S|  d k r  d S|  d k r0 d S|  d k r@ d	 S|  d
 k rP d S|  d k r` d S|  d k rp d S|  d k r d S|  d k r d Sd S(   s   Return the ADO 'DataTypeEnum' constant for a SQL data type with the
    given name or 'None' if the data type is not supported.

    Warning: data type names in different database systems may differ.t   DATEt   TIMEi   t   INTi   t   SMALLINTi   t   SINGLEi   t   DOUBLEi   t   BITi   t   VARCHARi   t   LONGTEXTi   t
   LONGBINARYi   (   s   DATEs   TIMEN(   t   None(   t   sqlDataType(    (    s   code\common\utils\access.pyt   getDataTypeCodeb   s&    c         C   s   |  d k r d S|  d k r  d S|  d k r0 d S|  d k r@ d S|  d	 k rP d
 S|  d k r` d S|  d k rp d S|  d k r d S|  d k r d S|  d k r d Sd S(   st   Return a descriptive string for a SQL data type with the given name or
    'None' if the data type is not supported.R)   t   dateR*   t   timeR+   s   four-byte signed integerR,   s   two-byte signed integerR-   s&   single precision floating point numberR.   s&   double precision floating point numberR/   s   boolean valueR0   s(   null-terminated Unicode character stringR1   s-   long null-terminated Unicode character stringR2   s   long binary valueN(   R3   (   R4   (    (    s   code\common\utils\access.pyt   getDataTypeName   s*    c         C   s   d |  d S(   sC   Return a safe SQL representation of a database field or table name.t   [t   ](    (   t	   fieldName(    (    s   code\common\utils\access.pyR      s    c         C   s"   d |  j  d d  d } d | S(   sw   Return the connection string to be used with an 'ADODB.Connection'
    object to open an existing Access database file.t   "s   ""s0   Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;(   t   replace(   R   t   quotedFilePath(    (    s   code\common\utils\access.pyR      s    
(   t   __doc__t   win32com.clientR    R   R   R   R   R(   R5   R8   R   R   (    (    (    s   code\common\utils\access.pyt   <module>   s   								