# -*- coding: utf8 -*-
'''
DB MODULE - generic ODBC
*******************************
***  DB MODULE - generic ODBC
*******************************
$RCSfile: dbmod_sqlite.py,v $
$Revision: 875 $
$Author: markus $
$Date: 2008-04-24 02:23:32 +0200 (Do, 24. Apr 2008) $

The biocase.wrapper database module to access generic ODBC datasources.
You will need the mxODBC module to connect to a DSN via ODBC.
'''

import os
from string                             import join
from biocase.wrapper.errorclasses       import *
from biocase.tools.various_functions    import unique
from biocase.wrapper.typeconverter      import DBtransformerClass
from biocase.wrapper.sql.operators      import getWhereObjTables
from biocase.wrapper.sql.base           import sqlBase
from biocase.wrapper.sql.select         import sqlSelect
from biocase.wrapper.dbmod.base_dbmod   import DBBasemodule

import logging
log = logging.getLogger("pywrapper.dbmod")


# uses inherited ANSI SQL
        

# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
class DBmodule(DBBasemodule):
    __version__ = '0.2 using pysqlite'
    __dbmod__   = u'pysqlite module for SQLite'
    

    # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    def __init__(self, psfObj):
                # call createDBconnection method
        DBBasemodule.__init__(self, psfObj)
        #
        # read DB 8bit encoding
        #
        #log.debug("8bit DB Encoding: %s"%(psfObj.encoding))
        #self.conn.encoding = psfObj.encoding

    # ------------------   FUNCTION OVERLOAD OF BASE DBMOD MODULE   ------------------
    # --------------------------------------------------------------------------------
    def createDBconnection(self):
        try:
            import sqlite
        except:
            raise DbModuleMissingError('pysqlite')         
        self.__class__.__version__ = self.__class__.__version__[:-1]+sqlite.version
        #
        # connect
        #
        # There is no password for sqlite, you only need to specify the path to the database
        loginParas = self.psfObj.database
        conn = sqlite.dbapi2.connect(loginParas, more_types=True)
        conn.register_converter("int", lambda x: int(x))
        conn.register_converter("float", lambda x: float(x))
        conn.register_converter("bool", lambda x: x == "1")
        conn.register_converter("unicode", lambda x: unicode(x, "utf-8"))
        conn.register_converter("blob", buffer)

        return conn


    
