📄 default.py
字号:
# engine/default.py# Copyright (C) 2005, 2006, 2007, 2008 Michael Bayer mike_mp@zzzcomputing.com## This module is part of SQLAlchemy and is released under# the MIT License: http://www.opensource.org/licenses/mit-license.php"""Default implementations of per-dialect sqlalchemy.engine classes.These are semi-private implementation classes which are only of importanceto database dialect authors; dialects will usually use the classes hereas the base class for their own corresponding classes."""import re, randomfrom sqlalchemy.engine import basefrom sqlalchemy.sql import compiler, expressionAUTOCOMMIT_REGEXP = re.compile(r'\s*(?:UPDATE|INSERT|CREATE|DELETE|DROP|ALTER)', re.I | re.UNICODE)SELECT_REGEXP = re.compile(r'\s*SELECT', re.I | re.UNICODE)class DefaultDialect(base.Dialect): """Default implementation of Dialect""" schemagenerator = compiler.SchemaGenerator schemadropper = compiler.SchemaDropper statement_compiler = compiler.DefaultCompiler preparer = compiler.IdentifierPreparer defaultrunner = base.DefaultRunner supports_alter = True supports_unicode_statements = False max_identifier_length = 9999 supports_sane_rowcount = True supports_sane_multi_rowcount = True preexecute_pk_sequences = False supports_pk_autoincrement = True dbapi_type_map = {} def __init__(self, convert_unicode=False, assert_unicode=False, encoding='utf-8', default_paramstyle='named', paramstyle=None, dbapi=None, **kwargs): self.convert_unicode = convert_unicode self.assert_unicode = assert_unicode self.encoding = encoding self.positional = False self._ischema = None self.dbapi = dbapi if paramstyle is not None: self.paramstyle = paramstyle elif self.dbapi is not None: self.paramstyle = self.dbapi.paramstyle else: self.paramstyle = default_paramstyle self.positional = self.paramstyle in ('qmark', 'format', 'numeric') self.identifier_preparer = self.preparer(self) # preexecute_sequences was renamed preexecute_pk_sequences. If a # subclass has the older property, proxy the new name to the subclass's # property. # TODO: remove @ 0.5.0 if (hasattr(self, 'preexecute_sequences') and isinstance(getattr(type(self), 'preexecute_pk_sequences'), bool)): setattr(type(self), 'preexecute_pk_sequences', property(lambda s: s.preexecute_sequences, doc=( "Proxy to deprecated preexecute_sequences attribute."))) def create_execution_context(self, connection, **kwargs): return DefaultExecutionContext(self, connection, **kwargs) def type_descriptor(self, typeobj): """Provide a database-specific ``TypeEngine`` object, given the generic object which comes from the types module. Subclasses will usually use the ``adapt_type()`` method in the types module to make this job easy.""" if type(typeobj) is type: typeobj = typeobj() return typeobj def oid_column_name(self, column): return None def do_begin(self, connection): """Implementations might want to put logic here for turning autocommit on/off, etc. """ pass def do_rollback(self, connection): """Implementations might want to put logic here for turning autocommit on/off, etc. """ connection.rollback() def do_commit(self, connection): """Implementations might want to put logic here for turning autocommit on/off, etc. """ connection.commit() def create_xid(self): """Create a random two-phase transaction ID. This id will be passed to do_begin_twophase(), do_rollback_twophase(), do_commit_twophase(). Its format is unspecified.""" return "_sa_%032x" % random.randint(0,2**128) def do_savepoint(self, connection, name): connection.execute(expression.SavepointClause(name)) def do_rollback_to_savepoint(self, connection, name): connection.execute(expression.RollbackToSavepointClause(name)) def do_release_savepoint(self, connection, name): connection.execute(expression.ReleaseSavepointClause(name)) def do_executemany(self, cursor, statement, parameters, context=None): cursor.executemany(statement, parameters) def do_execute(self, cursor, statement, parameters, context=None): cursor.execute(statement, parameters) def is_disconnect(self, e): return Falseclass DefaultExecutionContext(base.ExecutionContext): def __init__(self, dialect, connection, compiled=None, statement=None, parameters=None): self.dialect = dialect self._connection = self.root_connection = connection self.compiled = compiled self.engine = connection.engine if compiled is not None: # compiled clauseelement. process bind params, process table defaults, # track collections used by ResultProxy to target and process results self.processors = dict([ (key, value) for key, value in [( compiled.bind_names[bindparam], bindparam.bind_processor(self.dialect) ) for bindparam in compiled.bind_names] if value is not None ]) self.result_map = compiled.result_map if not dialect.supports_unicode_statements: self.statement = unicode(compiled).encode(self.dialect.encoding) else: self.statement = unicode(compiled) self.isinsert = compiled.isinsert self.isupdate = compiled.isupdate if isinstance(compiled.statement, expression._TextClause): self.returns_rows = self.returns_rows_text(self.statement) self.should_autocommit = compiled.statement._autocommit or self.should_autocommit_text(self.statement) else: self.returns_rows = self.returns_rows_compiled(compiled) self.should_autocommit = getattr(compiled.statement, '_autocommit', False) or self.should_autocommit_compiled(compiled) if not parameters: self.compiled_parameters = [compiled.construct_params()] self.executemany = False else: self.compiled_parameters = [compiled.construct_params(m) for m in parameters] self.executemany = len(parameters) > 1 self.cursor = self.create_cursor() self.__process_defaults() self.parameters = self.__convert_compiled_params(self.compiled_parameters) elif statement is not None: # plain text statement. self.result_map = None self.parameters = self.__encode_param_keys(parameters) self.executemany = len(parameters) > 1 if isinstance(statement, unicode) and not dialect.supports_unicode_statements: self.statement = statement.encode(self.dialect.encoding) else: self.statement = statement self.isinsert = self.isupdate = False self.cursor = self.create_cursor() self.returns_rows = self.returns_rows_text(statement) self.should_autocommit = self.should_autocommit_text(statement) else: # no statement. used for standalone ColumnDefault execution. self.statement = None self.isinsert = self.isupdate = self.executemany = self.returns_rows = self.should_autocommit = False self.cursor = self.create_cursor()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -