def set_transaction_mode(provider, connection, cache): assert not cache.in_transaction db_session = cache.db_session if db_session is not None and db_session.ddl: cursor = connection.cursor() cursor.execute("SHOW VARIABLES LIKE 'foreign_key_checks'") fk = cursor.fetchone() if fk is not None: fk = (fk[1] == 'ON') if fk: sql = 'SET foreign_key_checks = 0' if core.debug: core.log_orm(sql) cursor.execute(sql) cache.saved_fk_state = bool(fk) cache.in_transaction = True cache.immediate = True if db_session is not None and db_session.serializable: cursor = connection.cursor() sql = 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE' if core.debug: core.log_orm(sql) cursor.execute(sql) cache.in_transaction = True
def set_transaction_mode(provider, connection, optimistic): if optimistic: if core.debug: core.log_orm('SET AUTOCOMMIT = ON') connection.autocommit = True else: if core.debug: core.log_orm('SET TRANSACTION ISOLATION LEVEL READ COMMITTED') connection.set_isolation_level(extensions.ISOLATION_LEVEL_READ_COMMITTED)
def connect(pool): if pool.con is None: if core.debug: log_orm('GET NEW CONNECTION') pool.con = pool.dbapi_module.connect(*pool.args, **pool.kwargs) if 'client_encoding' not in pool.kwargs: pool.con.set_client_encoding('UTF8') elif core.debug: log_orm('GET CONNECTION FROM THE LOCAL POOL') return pool.con
def create(table, provider, connection, created_tables=None): try: dbschema.Table.create(table, provider, connection, created_tables) except DatabaseError, e: if 'already exists' not in e.args[0]: raise if core.debug: log_orm('ALREADY EXISTS: %s' % e.args[0]) log_orm('ROLLBACK') provider.rollback(connection)
def set_transaction_mode(provider, connection, optimistic): if optimistic: if core.debug: core.log_orm('SET AUTOCOMMIT = ON') connection.autocommit = True else: if core.debug: core.log_orm('SET TRANSACTION ISOLATION LEVEL READ COMMITTED') connection.set_isolation_level( extensions.ISOLATION_LEVEL_READ_COMMITTED)
def wrapper(*args, **kw): try: return func(*args, **kw) except: if core.debug: import traceback msg = traceback.format_exc() log_orm(msg) raise
def connect(pool): pid = os.getpid() if pool.pid != pid: pool.forked_pools.append((pool.cx_pool, pool.pid)) pool.cx_pool = cx_Oracle.SessionPool(**pool.kwargs) pool.pid = os.getpid() if core.local.debug: log_orm('GET CONNECTION') con = pool.cx_pool.acquire() con.outputtypehandler = output_type_handler return con, True
def set_transaction_mode(provider, connection, cache): assert not cache.in_transaction db_session = cache.db_session if db_session is not None and db_session.serializable: cursor = connection.cursor() sql = 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE' if core.debug: log_orm(sql) cursor.execute(sql) cache.immediate = True if db_session is not None and (db_session.serializable or db_session.ddl): cache.in_transaction = True
def release(provider, connection, cache=None): if cache is not None: db_session = cache.db_session if db_session is not None and db_session.ddl and cache.saved_fk_state: try: cursor = connection.cursor() sql = 'SET foreign_key_checks = 1' if core.debug: log_orm(sql) cursor.execute(sql) except: provider.pool.drop(connection) raise DBAPIProvider.release(provider, connection, cache)
def release(provider, connection, cache=None): if cache is not None: db_session = cache.db_session if db_session is not None and db_session.ddl and cache.saved_fk_state: try: cursor = connection.cursor() sql = 'PRAGMA foreign_keys = true' if core.debug: log_orm(sql) cursor.execute(sql) except: provider.pool.drop(connection) raise DBAPIProvider.release(provider, connection, cache)
def create(table, provider, connection, created_tables=None): commands = table.get_create_commands(created_tables) for i, sql in enumerate(commands): if core.debug: log_sql(sql) cursor = connection.cursor() try: provider.execute(cursor, sql) except DatabaseError, e: if e.exceptions[0].args[0].code == 955: if core.debug: log_orm('ALREADY EXISTS: %s' % e.args[0].message) if not i: if len(commands) > 1: log_orm('SKIP FURTHER DDL COMMANDS FOR TABLE %s\n' % table.name) return else: raise
def set_transaction_mode(provider, connection, cache): assert not cache.in_transaction db_session = cache.db_session if db_session is not None and db_session.ddl: cache.immediate = False if cache.immediate and connection.autocommit: connection.autocommit = False if core.local.debug: log_orm('SWITCH FROM AUTOCOMMIT TO TRANSACTION MODE') elif not cache.immediate and not connection.autocommit: connection.autocommit = True if core.local.debug: log_orm('SWITCH TO AUTOCOMMIT MODE') if db_session is not None and (db_session.serializable or db_session.ddl): cache.in_transaction = True
def connect(pool): con = pool.con if con is not None: if core.debug: core.log_orm('GET CONNECTION FROM THE LOCAL POOL') return con filename = pool.filename if filename != ':memory:' and not pool.create_db and not os.path.exists(filename): throw(IOError, "Database file is not found: %r" % filename) if core.debug: log_orm('GET NEW CONNECTION') pool.con = con = sqlite.connect(filename, isolation_level=None) con.text_factory = _text_factory con.create_function('power', 2, pow) con.create_function('rand', 0, random) if sqlite.sqlite_version_info >= (3, 6, 19): con.execute('PRAGMA foreign_keys = true') return con
def set_transaction_mode(provider, connection, cache): assert not cache.in_transaction db_session = cache.db_session if db_session is not None and db_session.ddl: cursor = connection.cursor() cursor.execute("SHOW VARIABLES LIKE 'foreign_key_checks'") fk = cursor.fetchone() if fk is not None: fk = (fk[1] == 'ON') if fk: sql = 'SET foreign_key_checks = 0' if core.local.debug: log_orm(sql) cursor.execute(sql) cache.saved_fk_state = bool(fk) cache.in_transaction = True cache.immediate = True if db_session is not None and db_session.serializable: cursor = connection.cursor() sql = 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE' if core.local.debug: log_orm(sql) cursor.execute(sql) cache.in_transaction = True
def set_transaction_mode(provider, connection, cache): assert not cache.in_transaction if cache.immediate: provider.transaction_lock.acquire() try: cursor = connection.cursor() db_session = cache.db_session if db_session is not None and db_session.ddl: cursor.execute('PRAGMA foreign_keys') fk = cursor.fetchone() if fk is not None: fk = fk[0] if fk: sql = 'PRAGMA foreign_keys = false' if core.debug: log_orm(sql) cursor.execute(sql) cache.saved_fk_state = bool(fk) assert cache.immediate if cache.immediate: sql = 'BEGIN IMMEDIATE TRANSACTION' if core.debug: log_orm(sql) cursor.execute(sql) cache.in_transaction = True elif core.debug: log_orm('SWITCH TO AUTOCOMMIT MODE') finally: if cache.immediate and not cache.in_transaction: provider.transaction_lock.release()
def set_transaction_mode(provider, connection, cache): assert not cache.in_transaction if cache.immediate and connection.autocommit: connection.autocommit = False if core.debug: log_orm('SWITCH FROM AUTOCOMMIT TO TRANSACTION MODE') db_session = cache.db_session if db_session is not None and db_session.serializable: cursor = connection.cursor() sql = 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE' if core.debug: log_orm(sql) cursor.execute(sql) elif not cache.immediate and not connection.autocommit: connection.autocommit = True if core.debug: log_orm('SWITCH TO AUTOCOMMIT MODE') if db_session is not None and (db_session.serializable or db_session.ddl): cache.in_transaction = True
def set_transaction_mode(provider, connection, cache): assert not cache.in_transaction cursor = connection.cursor() db_session = cache.db_session if db_session is not None and db_session.ddl: cursor.execute('PRAGMA foreign_keys') fk = cursor.fetchone() if fk is not None: fk = fk[0] if fk: sql = 'PRAGMA foreign_keys = false' if core.debug: log_orm(sql) cursor.execute(sql) cache.saved_fk_state = bool(fk) cache.in_transaction = True if cache.immediate: sql = 'BEGIN IMMEDIATE TRANSACTION' if core.debug: log_orm(sql) cursor.execute(sql) cache.in_transaction = True elif core.debug: log_orm('SWITCH TO AUTOCOMMIT MODE')
def rollback(self): if core.debug: core.log_orm('ROLLBACK') self.conn.query('rollback')
def start_optimistic_save(provider, connection): if core.debug: core.log_orm('SET TRANSACTION ISOLATION LEVEL READ COMMITTED') connection.set_isolation_level( extensions.ISOLATION_LEVEL_READ_COMMITTED)
def connect(pool): if core.debug: log_orm('GET CONNECTION') con = pool._pool.acquire() con.outputtypehandler = output_type_handler return con
def start_optimistic_save(provider, connection): if core.debug: core.log_orm('SET TRANSACTION ISOLATION LEVEL READ COMMITTED') connection.set_isolation_level(extensions.ISOLATION_LEVEL_READ_COMMITTED)
def commit(self): if core.debug: core.log_orm('COMMIT') self.conn.query('commit')