# -*- 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) $

A native biocase.wrapper database module to access MS SQL Server databases.
Basedon pymssql 
http://pymssql.sourceforge.net/
'''


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__ = '0.1 using pymssql'
    __dbmod__    = u'pymssql Server module'
    
    # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    def __init__(self, psfObj):
                # call createDBconnection method
        DBBasemodule.__init__(self, psfObj)
        #
        # dont use parameters but integrate all parameters into SQL strings:
        self.trans.integrateParametersInSQLString()
        #self.conn.encoding = psfObj.encoding

    # ------------------   FUNCTION OVERLOAD OF BASE DBMOD MODULE   ------------------
    # --------------------------------------------------------------------------------
    def createDBconnection(self):
        try:
            import pymssql
        except:
            raise DbModuleMissingError('pymssql')
        self.__class__.__version__ = self.__class__.__version__[:-1]+pymssql.__version__
        #
        # connect
        #
        try:
            return pymssql.connect(host=self.psfObj.dbIP,user=self.psfObj.user,password=self.psfObj.password,database=self.psfObj.database)
        except:
            raise ConnectionError()

