<commit_before>from sqlalchemy.exc import SQLAlchemyError, DBAPIError
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from api.ODM2.models import Variables as Variable2, change_schema
from ODM1_1_1.models import Variable as Variable1


class SessionFactory():
    def __init__(self, connection_string, echo):
        self.engine = create_engine(connection_string, encoding='utf-8', echo=echo, pool_recycle=3600, pool_timeout=5,
                                    pool_size=20,
                                    max_overflow=0)
        self.psql_test_engine = create_engine(connection_string, encoding='utf-8', echo=echo, pool_recycle=3600,
                                              pool_timeout=5,
                                              max_overflow=0, connect_args={'connect_timeout': 1})
        self.ms_test_engine = create_engine(connection_string, encoding='utf-8', echo=echo, pool_recycle=3600,
                                            pool_timeout=5,
                                            max_overflow=0, connect_args={'timeout': 1})

        # Create session maker

        self.Session = sessionmaker(bind=self.engine)
        self.psql_test_Session = sessionmaker(bind=self.psql_test_engine)
        self.ms_test_Session = sessionmaker(bind=self.ms_test_engine)

    def getSession(self):
        return self.Session()

    def __repr__(self):
        return "<SessionFactory('%s')>" % (self.engine)


class dbconnection():
    def __init__(self, debug=False):
        self.debug = debug
        self._connections = []
        self.version = 0
        self._connection_format = "%s+%s://%s:%s@%s/%s"

    @classmethod
    def createConnection(self, engine, address, db, user, password):
        connection_string = dbconnection.buildConnDict(dbconnection(), engine, address, db, user, password)
        # if self.testConnection(connection_string):
        if self.testEngine(connection_string):
            # print "sucess"
            return SessionFactory(connection_string, echo=False)
        else:
            return None

    @staticmethod
    def _getSchema(engine):
        from sqlalchemy.engine import reflection

        insp=reflection.Inspector.from_engine(engine)

        for name in insp.get_schema_names():
            if 'odm2'== name.lower():
                return name
        else:
            return insp.default_schema_name

    @classmethod
    def _setSchema(self, engine):

        s = self._getSchema(engine)

        print "orig", Variable2.__table__.schema
        print "New", s
        change_schema(s)

        print "set new", Variable2.__table__.schema

    @classmethod
    def testEngine(self, connection_string):
        s = SessionFactory(connection_string, echo=False)

        try:
            if 'mssql' in connection_string:
                self._setSchema(s.ms_test_engine)
                from api.ODM2.models import Variables as Variable2
                s.ms_test_Session().query(Variable2.VariableCode).limit(1).first()
            elif 'postgresql' in connection_string:
                self._setSchema(s.psql_test_engine)
                from api.ODM2.models import Variables as Variable2
                s.psql_test_Session().query(Variable2.VariableCode).limit(1).first()
            elif 'mysql' in connection_string:
                self._setSchema(s.psql_test_engine)
                from api.ODM2.models import Variables as Variable2
                s.psql_test_Session().query(Variable2.VariableCode).limit(1).first()

        except Exception as e:
            print "Connection was unsuccessful ", e.message
            return False
        return True

    def testEngine1_1(self, connection_string):
        s = SessionFactory(connection_string, echo=False)
        try:
            # s.ms_test_Session().query(Variable1).limit(1).first()

            if 'mssql' in connection_string:
                s.ms_test_Session().execute("Select top 1 VariableCode From Variables")
            elif 'postgresql' in connection_string:
                s.psql_test_Session().execute('Select "VariableCode" From "Variables" Limit 1')
                # s.psql_test_Session().execute('Select "VariableNameCV" From "ODM2"."Variables" Limit 1')
            elif 'mysql' in connection_string:
                s.psql_test_Session().execute('Select variablecode From variables Limit 1')

        except Exception as e:
            print "Connection was unsuccessful ", e.message
            return False
        return True

    def buildConnDict(self, engine, address, db, user, password):
        line_dict = {}
        line_dict['engine'] = engine
        line_dict['user'] = user
        line_dict['password'] = password
        line_dict['address'] = address
        line_dict['db'] = db
        self._connections.append(line_dict)
        self._current_connection = self._connections[-1]
        return self.__buildConnectionString(line_dict)

    def getConnections(self):
        return self._connections

    def getCurrentConnection(self):
        return self._current_connection

    def addConnection(self, conn_dict):<commit_msg>Modified for sqlite a bit more<commit_after>from sqlalchemy.exc import SQLAlchemyError, DBAPIError
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from .ODM2.models import Variables as Variable2, change_schema
from .ODM1_1_1.models import Variable as Variable1


class SessionFactory():
    def __init__(self, connection_string, echo):
        if 'sqlite' in connection_string:
            self.engine = create_engine(connection_string, encoding='utf-8', echo=echo)
            self.test_engine = self.engine
        if 'mssql' in connection_string:
              self.engine = create_engine(connection_string, encoding='utf-8', echo=echo, pool_recycle=3600, pool_timeout=5,
                                    pool_size=20, max_overflow=0)
              self.test_engine = create_engine(connection_string, encoding='utf-8', echo=echo, pool_recycle=3600,
                                    pool_timeout=5, max_overflow=0, connect_args={'timeout': 1})

        elif 'postgresql' in connection_string or 'mysql' in connection_string:
            self.engine = create_engine(connection_string, encoding='utf-8', echo=echo, pool_recycle=3600, pool_timeout=5,
                                        pool_size=20,
                                        max_overflow=0)
            self.test_engine = create_engine(connection_string, encoding='utf-8', echo=echo, pool_recycle=3600,
                                              pool_timeout=5,
                                              max_overflow=0, connect_args={'connect_timeout': 1})


        # Create session maker

        self.Session = sessionmaker(bind=self.engine)
        self.test_Session = sessionmaker(bind=self.test_engine)


    def getSession(self):
        return self.Session()

    def __repr__(self):
        return "<SessionFactory('%s')>" % (self.engine)


class dbconnection():
    def __init__(self, debug=False):
        self.debug = debug
        self._connections = []
        self.version = 0
        self._connection_format = "%s+%s://%s:%s@%s/%s"

    @classmethod
    def createConnection(self, engine, address, db=None, user=None, password=None):
        if engine == 'sqlite':
            connection_string = engine +':///'+address
        else:
            connection_string = dbconnection.buildConnDict(dbconnection(), engine, address, db, user, password)
        # if self.testConnection(connection_string):
        if self.testEngine(connection_string):
            # print "sucess"
            return SessionFactory(connection_string, echo=False)
        else:
            return None

    @staticmethod
    def _getSchema(engine):
        from sqlalchemy.engine import reflection

        insp=reflection.Inspector.from_engine(engine)

        for name in insp.get_schema_names():
            if 'odm2'== name.lower():
                return name
        else:
            return insp.default_schema_name

    @classmethod
    def _setSchema(self, engine):

        s = self._getSchema(engine)

        print "orig", Variable2.__table__.schema
        print "New", s
        change_schema(s)

        print "set new", Variable2.__table__.schema

    @classmethod
    def testEngine(self, connection_string):
        s = SessionFactory(connection_string, echo=False)

        try:
            self._setSchema(s.test_engine)
            s.test_Session().query(Variable2.VariableCode).limit(1).first()
        except Exception as e:
            print "Connection was unsuccessful ", e.message
            return False
        return True

    def testEngine1_1(self, connection_string):
        s = SessionFactory(connection_string, echo=False)
        try:
            # s.ms_test_Session().query(Variable1).limit(1).first()
            s.test_Session.execute(Variable1).limit(1).first()
            '''
            if 'mssql' in connection_string:
                s.ms_test_Session().execute(Variable1).limit(1).first()
            elif 'postgresql' in connection_string:
                s.psql_test_Session().execute('Select "VariableCode" From "Variables" Limit 1')
                # s.psql_test_Session().execute('Select "VariableNameCV" From "ODM2"."Variables" Limit 1')
            elif 'mysql' in connection_string:
                s.psql_test_Session().execute('Select variablecode From variables Limit 1')
            '''

        except Exception as e:
            print "Connection was unsuccessful ", e.message
            return False
        return True

    def buildConnDict(self, engine, address, db, user, password):
        line_dict = {}
        line_dict['engine'] = engine
        line_dict['user'] = user
        line_dict['password'] = password
        line_dict['address'] = address
        line_dict['db'] = db
        self._connections.append(line_dict)
        self._current_connection = self._connections[-1]
        return self.__buildConnectionString(line_dict)

    def getConnections(self):
        return self._connections

    def getCurrentConnection(self):
        return self._current_connection

    def addConnection(self, conn_dict):
{'fraction_non_alphanumeric': 0.0941590154327017, 'fraction_numerical': 0.0114280132838445, 'mean_word_length': 2.346078431372549, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4005180', 'n_tokens_mistral': 2824, 'n_tokens_neox': 2676, 'n_words': 601}