# -*- coding: utf8 -*-
'''
DB MODULE - ODBC for TSQL
*******************************
***  DB MODULE - ODBC for TSQL
*******************************
$RCSfile: dbmod_odbc_tsql.py,v $
$Revision: 905 $
$Author: markus $
$Date: 2008-04-30 11:57:10 +0200 (Mi, 30. Apr 2008) $

The biocase.wrapper database module to access MS SQL Server databases.
You will need the mxODBC module to connect to a MS SQL Server DB via ODBC.
'''


import os
from string                                import join
from datetime                           import datetime
from biocase.wrapper.errorclasses         import *
from biocase.wrapper.typeconverter        import DBtransformerClass
from biocase.wrapper.sql.base             import sqlBase
from biocase.wrapper.dbmod.base_dbmod   import DBBasemodule

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




#############################################
#
# CUSTOMIZATION for SQL Server
#
# OVERLOAD BASE CLASSES AND OTHER METHODS
#
#############################################

# uses inherited ANSI SQL apart from the following.
sqlBase.templates['attribute'] = '[#1].[#2]'
sqlBase.templates['table'] = '[#1]'
sqlBase.templates['select-limit'] = 'SELECT DISTINCT TOP #3 #1 FROM #2'



# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
class DBmodule(DBBasemodule):
    __version__ = '1.01 using mxODBC ?'
    __dbmod__    = u'MS SQL Server module'
    
    # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    def __init__(self, psfObj):
        # setting the psfObj reference to 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 mx.ODBC
        except:
            raise DbModuleMissingError('mx_ODBC')
        self.__class__.__version__ = self.__class__.__version__[:-1]+mx.ODBC.__version__
        #
        # connect
        #
        # import OS specific ODBC version:
        # 'posix', 'nt', 'dos', 'mac', 'os2', 'ce', 'java', 'riscos'
        if os.name in ("nt","dos"):
            import mx.ODBC.Windows    as odbc
        elif os.name in ("unix","posix","java"):
            import mx.ODBC.unixODBC as odbc
        else:
            raise ConnectionError(os=os.name)
        loginParas = "DSN=%s;UID=%s;PWD=%s" % (self.psfObj.database, self.psfObj.user ,self.psfObj.password )
        conn = odbc.DriverConnect( loginParas )
        conn.stringformat = odbc.NATIVE_UNICODE_STRINGFORMAT
        return conn 

