# -*- coding: utf8 -*-
'''
DB MODULE - generic ODBC
*******************************
***  DB MODULE - generic ODBC
*******************************
$RCSfile: dbmod_odbc.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.81 using mxODBC ?'
	__dbmod__	= u'generic ODBC DB module'
	

	# _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
	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 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


	
