# Path: hecatoncheir/DbDriverBase.py
# class DbDriverBase:
#     __metaclass__ = ABCMeta
# 
#     @abstractmethod
#     def __init__(self, connstr, dbuser, dbpass):
#         raise NotImplementedError
# 
#     @abstractmethod
#     def connect(self):
#         raise NotImplementedError
# 
#     @abstractmethod
#     def query_to_resultset(self, label, query, max_rows=10000, timeout=None):
#         raise NotImplementedError
# 
#     def q2rs(self, query, max_rows=10000, timeout=None):
#         return self.query_to_resultset(query, max_rows, timeout)
# 
#     @abstractmethod
#     def disconnect(self):
#         raise NotImplementedError
# 
# Path: hecatoncheir/logger.py
# def to_unicode(s):
# def str2unicode(s):
# def setup():
# def ts():
# def trace(msg):
# def debug(msg):
# def info(msg):
# def info_cont(msg):
# def info_end(msg):
# def warning(msg):
# def error(msg, detail=None, query=None):
# def dump_datetime_default(o):
# def dump(msg, obj):
# 
# Path: hecatoncheir/QueryResult.py
# class QueryResult():
#     query = None
#     column_names = None
#     resultset = None
# 
#     def __init__(self, query):
#         self.query = query
#         self.column_names = []
#         self.resultset = []
# 
#     def __repr__(self):
#         return u'QueryResult:' + repr({'query': self.query,
#                                        'column': self.column_names,
#                                        'result': self.resultset})
# 
# Path: hecatoncheir/exception.py
# class DriverError(Exception):
#     source = None
# 
#     def __init__(self, value, source=None):
#         self.value = value
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class InternalError(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query=None, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class QueryError(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class QueryTimeout(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# Path: hecatoncheir/logger.py
# def to_unicode(s):
#     if isinstance(s, unicode):
#         return s
#     if not isinstance(s, str):
#         s = str(s)
# 
#     encodings = ('utf_8', 'euc_jp', 'euc_jis_2004', 'euc_jisx0213',
#                  'shift_jis', 'shift_jis_2004', 'shift_jisx0213',
#                  'iso2022jp', 'iso2022_jp_1', 'iso2022_jp_2', 'iso2022_jp_3',
#                  'iso2022_jp_ext', 'latin_1', 'ascii')
#     for enc in encodings:
#         try:
#             s2 = s.decode(enc)
#             if isinstance(s2, unicode):
#                 return s2
#         except:
#             pass
#     raise TypeError('Could not determine encoding of the string.')


from copy import deepcopy
from hecatoncheir import DbDriverBase, logger as log
from hecatoncheir.QueryResult import QueryResult
from hecatoncheir.exception import (DriverError, InternalError, QueryError,
                                    QueryTimeout)
from hecatoncheir.logger import to_unicode
import re
import threading
class MSSQLDriver(DbDriverBase.DbDriverBase):
    host = None
    port = None
    dbname = None
    dbuser = None
    dbpass = None
    conn = None
    driver = None

    def __init__(self, host, dbname, dbuser, dbpass):
        self.host = host
        self.dbname = dbname
        self.dbuser = dbuser
        self.dbpass = dbpass

        name = "pymssql"
        try:
            self.driver = __import__(name, fromlist=[''])
        except Exception as e:
            raise DriverError(
                u"Could not load the driver module: %s" % name, source=e)

    def connect(self):
        try:
            self.conn = self.driver.connect(self.host, self.dbuser,
                                            self.dbpass, self.dbname)
        except Exception as e:
            # assuming OperationalError
            msg = to_unicode(e[0][1]).replace('\n', ' ')
            msg = re.sub(r'DB-Lib.*', '', msg)

# Path: hecatoncheir/DbDriverBase.py
# class DbDriverBase:
#     __metaclass__ = ABCMeta
# 
#     @abstractmethod
#     def __init__(self, connstr, dbuser, dbpass):
#         raise NotImplementedError
# 
#     @abstractmethod
#     def connect(self):
#         raise NotImplementedError
# 
#     @abstractmethod
#     def query_to_resultset(self, label, query, max_rows=10000, timeout=None):
#         raise NotImplementedError
# 
#     def q2rs(self, query, max_rows=10000, timeout=None):
#         return self.query_to_resultset(query, max_rows, timeout)
# 
#     @abstractmethod
#     def disconnect(self):
#         raise NotImplementedError
# 
# Path: hecatoncheir/logger.py
# def to_unicode(s):
# def str2unicode(s):
# def setup():
# def ts():
# def trace(msg):
# def debug(msg):
# def info(msg):
# def info_cont(msg):
# def info_end(msg):
# def warning(msg):
# def error(msg, detail=None, query=None):
# def dump_datetime_default(o):
# def dump(msg, obj):
# 
# Path: hecatoncheir/QueryResult.py
# class QueryResult():
#     query = None
#     column_names = None
#     resultset = None
# 
#     def __init__(self, query):
#         self.query = query
#         self.column_names = []
#         self.resultset = []
# 
#     def __repr__(self):
#         return u'QueryResult:' + repr({'query': self.query,
#                                        'column': self.column_names,
#                                        'result': self.resultset})
# 
# Path: hecatoncheir/exception.py
# class DriverError(Exception):
#     source = None
# 
#     def __init__(self, value, source=None):
#         self.value = value
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class InternalError(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query=None, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class QueryError(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# class QueryTimeout(Exception):
#     source = None
#     query = None
# 
#     def __init__(self, value, query, source=None):
#         self.value = value
#         self.query = query
#         self.source = source
# 
#     def __str__(self):
#         return self.value
# 
# Path: hecatoncheir/logger.py
# def to_unicode(s):
#     if isinstance(s, unicode):
#         return s
#     if not isinstance(s, str):
#         s = str(s)
# 
#     encodings = ('utf_8', 'euc_jp', 'euc_jis_2004', 'euc_jisx0213',
#                  'shift_jis', 'shift_jis_2004', 'shift_jisx0213',
#                  'iso2022jp', 'iso2022_jp_1', 'iso2022_jp_2', 'iso2022_jp_3',
#                  'iso2022_jp_ext', 'latin_1', 'ascii')
#     for enc in encodings:
#         try:
#             s2 = s.decode(enc)
#             if isinstance(s2, unicode):
#                 return s2
#         except:
#             pass
#     raise TypeError('Could not determine encoding of the string.')


# Path: hecatoncheir/mssql/MSSQLDriver.py
from copy import deepcopy
from hecatoncheir import DbDriverBase, logger as log
from hecatoncheir.QueryResult import QueryResult
from hecatoncheir.exception import (DriverError, InternalError, QueryError,
                                    QueryTimeout)
from hecatoncheir.logger import to_unicode
import re
import threading

class MSSQLDriver(DbDriverBase.DbDriverBase):
    host = None
    port = None
    dbname = None
    dbuser = None
    dbpass = None
    conn = None
    driver = None

    def __init__(self, host, dbname, dbuser, dbpass):
        self.host = host
        self.dbname = dbname
        self.dbuser = dbuser
        self.dbpass = dbpass

        name = "pymssql"
        try:
            self.driver = __import__(name, fromlist=[''])
        except Exception as e:
            raise DriverError(
                u"Could not load the driver module: %s" % name, source=e)

    def connect(self):
        try:
            self.conn = self.driver.connect(self.host, self.dbuser,
                                            self.dbpass, self.dbname)
        except Exception as e:
            # assuming OperationalError
            msg = to_unicode(e[0][1]).replace('\n', ' ')
            msg = re.sub(r'DB-Lib.*', '', msg)

            raise DriverError(