def _init_ddl(cls, dialect, connection, dbapi_connection, compiled_ddl): self = cls.__new__(cls) self.root_connection = connection self._dbapi_connection = dbapi_connection self.dialect = connection.dialect self.compiled = compiled = compiled_ddl self.isddl = True self.execution_options = compiled.execution_options if connection._execution_options: self.execution_options = dict(self.execution_options) self.execution_options.update(connection._execution_options) if not dialect.supports_unicode_statements: self.unicode_statement = util.text_type(compiled) self.statement = dialect._encoder(self.unicode_statement)[0] else: self.statement = self.unicode_statement = util.text_type(compiled) self.cursor = self.create_cursor() self.compiled_parameters = [] if dialect.positional: self.parameters = [dialect.execute_sequence_format()] else: self.parameters = [{}] self.statement = compiled_ddl.string return self
def assert_raises_message(except_cls, msg, callable_, *args, **kwargs): try: callable_(*args, **kwargs) assert False, "Callable did not raise an exception" except except_cls as e: assert re.search(msg, util.text_type(e), re.UNICODE), "%r !~ %s" % (msg, e) print(util.text_type(e).encode('utf-8'))
def has_table(self, connection, table_name, schema=None): # seems like case gets folded in pg_class... if schema is None: cursor = connection.execute( sql.text( "select name " "from sys.tables " "where system = false " "and type = 0 " "and name=:name", bindparams=[ sql.bindparam('name', util.text_type(table_name), type_=sqltypes.Unicode)] ) ) else: cursor = connection.execute( sql.text( "SELECT tables.name " "FROM sys.tables, sys.schemas " "WHERE tables.system = FALSE " "AND tables.schema_id = schemas.id " "AND type = 0 " "AND tables.name = :name " "AND schemas.name = :schema", bindparams=[ sql.bindparam('name', util.text_type(table_name), type_=sqltypes.Unicode), sql.bindparam('schema', util.text_type(schema), type_=sqltypes.Unicode)] ) ) return bool(cursor.first())
def assert_compile(self, clause, result, params=None, checkparams=None, dialect=None, checkpositional=None, use_default_dialect=False, allow_dialect_select=False, literal_binds=False): if use_default_dialect: dialect = default.DefaultDialect() elif allow_dialect_select: dialect = None else: if dialect is None: dialect = getattr(self, '__dialect__', None) if dialect is None: dialect = config.db.dialect elif dialect == 'default': dialect = default.DefaultDialect() elif isinstance(dialect, util.string_types): dialect = url.URL(dialect).get_dialect()() kw = {} compile_kwargs = {} if params is not None: kw['column_keys'] = list(params) if literal_binds: compile_kwargs['literal_binds'] = True if isinstance(clause, orm.Query): context = clause._compile_context() context.statement.use_labels = True clause = context.statement if compile_kwargs: kw['compile_kwargs'] = compile_kwargs c = clause.compile(dialect=dialect, **kw) param_str = repr(getattr(c, 'params', {})) if util.py3k: param_str = param_str.encode('utf-8').decode('ascii', 'ignore') print(("\nSQL String:\n" + util.text_type(c) + param_str).encode('utf-8')) else: print("\nSQL String:\n" + util.text_type(c).encode('utf-8') + param_str) cc = re.sub(r'[\n\t]', '', util.text_type(c)) eq_(cc, result, "%r != %r on dialect %r" % (cc, result, dialect)) if checkparams is not None: eq_(c.construct_params(params), checkparams) if checkpositional is not None: p = c.construct_params(params) eq_(tuple([p[x] for x in c.positiontup]), checkpositional)
def _get_column_info(self, name, type_, nullable, default, primary_key): match = re.match(r"(\w+)(\(.*?\))?", type_) if match: coltype = match.group(1) args = match.group(2) else: coltype = "VARCHAR" args = "" try: coltype = self.ischema_names[coltype] if args is not None: args = re.findall(r"(\d+)", args) coltype = coltype(*[int(a) for a in args]) except KeyError: util.warn("Did not recognize type '%s' of column '%s'" % (coltype, name)) coltype = sqltypes.NullType() if default is not None: default = util.text_type(default) return { "name": name, "type": coltype, "nullable": nullable, "default": default, "autoincrement": default is None, "primary_key": primary_key, }
def _get_column_info(self, name, type_, nullable, default, primary_key): match = re.match(r'(\w+)(\(.*?\))?', type_) if match: coltype = match.group(1) args = match.group(2) else: coltype = "VARCHAR" args = '' try: coltype = self.ischema_names[coltype] if args is not None: args = re.findall(r'(\d+)', args) coltype = coltype(*[int(a) for a in args]) except KeyError: util.warn("Did not recognize type '%s' of column '%s'" % (coltype, name)) coltype = sqltypes.NullType() if default is not None: default = util.text_type(default) return { 'name': name, 'type': coltype, 'nullable': nullable, 'default': default, 'autoincrement': default is None, 'primary_key': primary_key }
def _bindparam_requires_quotes(self, value): """Return True if the given identifier requires quoting.""" lc_value = value.lower() return (lc_value in self.reserved_words or value[0] in self.illegal_initial_characters or not self.legal_characters.match(util.text_type(value)) )
def assert_compile(self, clause, result, params=None, checkparams=None, dialect=None, checkpositional=None, use_default_dialect=False, allow_dialect_select=False): if use_default_dialect: dialect = default.DefaultDialect() elif dialect == None and not allow_dialect_select: dialect = getattr(self, '__dialect__', None) if dialect == 'default': dialect = default.DefaultDialect() elif dialect is None: dialect = config.db.dialect elif isinstance(dialect, basestring): dialect = create_engine("%s://" % dialect).dialect kw = {} if params is not None: kw['column_keys'] = params.keys() if isinstance(clause, orm.Query): context = clause._compile_context() context.statement.use_labels = True clause = context.statement c = clause.compile(dialect=dialect, **kw) param_str = repr(getattr(c, 'params', {})) if util.py3k: param_str = param_str.encode('utf-8').decode('ascii', 'ignore') print(("\nSQL String:\n" + util.text_type(c) + param_str).encode('utf-8')) else: print(("\nSQL String:\n" + util.text_type(c).encode('utf-8') + param_str)) cc = re.sub(r'[\n\t]', '', util.text_type(c)) eq_(cc, result, "%r != %r on dialect %r" % (cc, result, dialect)) if checkparams is not None: eq_(c.construct_params(params), checkparams) if checkpositional is not None: p = c.construct_params(params) eq_(tuple([p[x] for x in c.positiontup]), checkpositional)
def _init_compiled_prepared(cls, dialect, connection, dbapi_connection, compiled, parameters): self = cls.__new__(cls) self.root_connection = connection self._dbapi_connection = dbapi_connection self.dialect = connection.dialect self.compiled = compiled # this should be caught in the engine before # we get here assert compiled.can_execute self.execution_options = compiled.execution_options.union( connection._execution_options) self.result_column_struct = (compiled._result_columns, compiled._ordered_columns, compiled._textual_ordered_columns) self.unicode_statement = util.text_type(compiled) if not dialect.supports_unicode_statements: self.statement = self.unicode_statement.encode( self.dialect.encoding) else: self.statement = self.unicode_statement self.isinsert = compiled.isinsert self.isupdate = compiled.isupdate self.isdelete = compiled.isdelete self.is_text = compiled.isplaintext self.executemany = False self.cursor = self.create_cursor() if self.isinsert or self.isupdate or self.isdelete: self.is_crud = True self._is_explicit_returning = bool(compiled.statement._returning) self._is_implicit_returning = bool( compiled.returning and not compiled.statement._returning) if self.dialect.positional: self.parameters = [dialect.execute_sequence_format()] else: self.parameters = [{}] self.compiled_parameters = [{}] return self
def _get_foreign_table_names(self, connection, schema=None, **kw): log.debug("-->") if schema is None: schema = self._get_current_schema_name(connection) if not self.is_system_in_lowercase(): schema = self.denormalize_name(schema) result = connection.execute( sql.text( "select tablename as name from _v_table where objid > 200000 and schema = :schema and relkind = 'f' " ).bindparams( sql.bindparam("schema", util.text_type(schema), type_=sqltypes.Unicode))) table_names = [r[0] for r in result] return table_names
def __init__(self, left, right, astext=False, opstring=None, result_type=None): self._astext = astext if opstring is None: if hasattr(right, '__iter__') and \ not isinstance(right, util.string_types): opstring = "#>" right = "{%s}" % ( ", ".join(util.text_type(elem) for elem in right)) else: opstring = "->" self._json_opstring = opstring operator = custom_op(opstring, precedence=5) right = default_comparator._check_literal( left, operator, right) super(JSONElement, self).__init__( left, right, operator, type_=result_type)
def count(self, **kwargs): db = dataset.connect( settings.db_url, engine_kwargs={'connect_args': { 'check_same_thread': False }}) if self.name in db.tables: table = db.get_table(self.name) result = table.count( TextClause( util.text_type(" AND ".join([ "{}={}".format(i, kwargs[i]) if isinstance( kwargs[i], (int, float)) else '{}="{}"'.format(i, kwargs[i]) for i in kwargs ])))) return result else: return False
def _execute_scalar(self, stmt): return super(OracleExecutionContext_cx_oracle_with_unicode, self).\ _execute_scalar(util.text_type(stmt))
def __init__(self, *arg, **kw): OracleExecutionContext_cx_oracle.__init__(self, *arg, **kw) self.statement = util.text_type(self.statement)
def _bindparam_requires_quotes(self, value): """Return True if the given identifier requires quoting.""" lc_value = value.lower() return (lc_value in self.reserved_words or value[0] in self.illegal_initial_characters or not self.legal_characters.match(util.text_type(value)))
def _init_compiled(cls, dialect, connection, dbapi_connection, compiled, parameters): """Initialize execution context for a Compiled construct.""" self = cls.__new__(cls) self.root_connection = connection self._dbapi_connection = dbapi_connection self.dialect = connection.dialect self.compiled = compiled # this should be caught in the engine before # we get here assert compiled.can_execute self.execution_options = compiled.execution_options.union( connection._execution_options) self.result_column_struct = ( compiled._result_columns, compiled._ordered_columns, compiled._textual_ordered_columns) self.unicode_statement = util.text_type(compiled) if not dialect.supports_unicode_statements: self.statement = self.unicode_statement.encode( self.dialect.encoding) else: self.statement = self.unicode_statement self.isinsert = compiled.isinsert self.isupdate = compiled.isupdate self.isdelete = compiled.isdelete self.is_text = compiled.isplaintext if not parameters: self.compiled_parameters = [compiled.construct_params()] else: self.compiled_parameters = \ [compiled.construct_params(m, _group_number=grp) for grp, m in enumerate(parameters)] self.executemany = len(parameters) > 1 self.cursor = self.create_cursor() if self.isinsert or self.isupdate or self.isdelete: self.is_crud = True self._is_explicit_returning = bool(compiled.statement._returning) self._is_implicit_returning = bool( compiled.returning and not compiled.statement._returning) if self.compiled.insert_prefetch or self.compiled.update_prefetch: if self.executemany: self._process_executemany_defaults() else: self._process_executesingle_defaults() processors = compiled._bind_processors # Convert the dictionary of bind parameter values # into a dict or list to be sent to the DBAPI's # execute() or executemany() method. parameters = [] if dialect.positional: for compiled_params in self.compiled_parameters: param = [] for key in self.compiled.positiontup: if key in processors: param.append(processors[key](compiled_params[key])) else: param.append(compiled_params[key]) parameters.append(dialect.execute_sequence_format(param)) else: encode = not dialect.supports_unicode_statements for compiled_params in self.compiled_parameters: if encode: param = dict( ( dialect._encoder(key)[0], processors[key](compiled_params[key]) if key in processors else compiled_params[key] ) for key in compiled_params ) else: param = dict( ( key, processors[key](compiled_params[key]) if key in processors else compiled_params[key] ) for key in compiled_params ) parameters.append(param) self.parameters = dialect.execute_sequence_format(parameters) self.statement = compiled return self
def assert_compile(self, clause, result, params=None, checkparams=None, dialect=None, checkpositional=None, check_prefetch=None, use_default_dialect=False, allow_dialect_select=False, literal_binds=False, schema_translate_map=None): if use_default_dialect: dialect = default.DefaultDialect() elif allow_dialect_select: dialect = None else: if dialect is None: dialect = getattr(self, '__dialect__', None) if dialect is None: dialect = config.db.dialect elif dialect == 'default': dialect = default.DefaultDialect() elif dialect == 'default_enhanced': dialect = default.StrCompileDialect() elif isinstance(dialect, util.string_types): dialect = url.URL(dialect).get_dialect()() kw = {} compile_kwargs = {} if schema_translate_map: kw['schema_translate_map'] = schema_translate_map if params is not None: kw['column_keys'] = list(params) if literal_binds: compile_kwargs['literal_binds'] = True if isinstance(clause, orm.Query): context = clause._compile_context() context.statement.use_labels = True clause = context.statement elif isinstance(clause, orm.persistence.BulkUD): with mock.patch.object(clause, "_execute_stmt") as stmt_mock: clause.exec_() clause = stmt_mock.mock_calls[0][1][0] if compile_kwargs: kw['compile_kwargs'] = compile_kwargs c = clause.compile(dialect=dialect, **kw) param_str = repr(getattr(c, 'params', {})) if util.py3k: param_str = param_str.encode('utf-8').decode('ascii', 'ignore') print(("\nSQL String:\n" + util.text_type(c) + param_str).encode('utf-8')) else: print("\nSQL String:\n" + util.text_type(c).encode('utf-8') + param_str) cc = re.sub(r'[\n\t]', '', util.text_type(c)) eq_(cc, result, "%r != %r on dialect %r" % (cc, result, dialect)) if checkparams is not None: eq_(c.construct_params(params), checkparams) if checkpositional is not None: p = c.construct_params(params) eq_(tuple([p[x] for x in c.positiontup]), checkpositional) if check_prefetch is not None: eq_(c.prefetch, check_prefetch)
def _init_compiled( cls, dialect, connection, dbapi_connection, compiled, parameters ): """Initialize execution context for a Compiled construct.""" self = cls.__new__(cls) self.root_connection = connection self._dbapi_connection = dbapi_connection self.dialect = connection.dialect self.compiled = compiled # this should be caught in the engine before # we get here assert compiled.can_execute self.execution_options = compiled.execution_options.union( connection._execution_options ) self.result_column_struct = ( compiled._result_columns, compiled._ordered_columns, compiled._textual_ordered_columns, ) self.unicode_statement = util.text_type(compiled) if not dialect.supports_unicode_statements: self.statement = self.unicode_statement.encode(self.dialect.encoding) else: self.statement = self.unicode_statement self.isinsert = compiled.isinsert self.isupdate = compiled.isupdate self.isdelete = compiled.isdelete self.is_text = compiled.isplaintext if not parameters: self.compiled_parameters = [compiled.construct_params()] else: self.compiled_parameters = [ compiled.construct_params(m, _group_number=grp) for grp, m in enumerate(parameters) ] self.executemany = len(parameters) > 1 self.cursor = self.create_cursor() if self.isinsert or self.isupdate or self.isdelete: self.is_crud = True self._is_explicit_returning = bool(compiled.statement._returning) self._is_implicit_returning = bool( compiled.returning and not compiled.statement._returning ) if self.compiled.insert_prefetch or self.compiled.update_prefetch: if self.executemany: self._process_executemany_defaults() else: self._process_executesingle_defaults() processors = compiled._bind_processors # Convert the dictionary of bind parameter values # into a dict or list to be sent to the DBAPI's # execute() or executemany() method. parameters = [] if dialect.positional: for compiled_params in self.compiled_parameters: param = [] for key in self.compiled.positiontup: if key in processors: param.append(processors[key](compiled_params[key])) else: param.append(compiled_params[key]) parameters.append(dialect.execute_sequence_format(param)) else: encode = not dialect.supports_unicode_statements for compiled_params in self.compiled_parameters: if encode: param = dict( ( dialect._encoder(key)[0], processors[key](compiled_params[key]) if key in processors else compiled_params[key], ) for key in compiled_params ) else: param = dict( ( key, processors[key](compiled_params[key]) if key in processors else compiled_params[key], ) for key in compiled_params ) parameters.append(param) self.parameters = dialect.execute_sequence_format(parameters) self.statement = compiled return self
def assert_compile(self, clause, result, params=None, checkparams=None, dialect=None, checkpositional=None, check_prefetch=None, use_default_dialect=False, allow_dialect_select=False, literal_binds=False, schema_translate_map=None): if use_default_dialect: dialect = default.DefaultDialect() elif allow_dialect_select: dialect = None else: if dialect is None: dialect = getattr(self, '__dialect__', None) if dialect is None: dialect = config.db.dialect elif dialect == 'default': dialect = default.DefaultDialect() elif dialect == 'default_enhanced': dialect = default.StrCompileDialect() elif isinstance(dialect, util.string_types): dialect = url.URL(dialect).get_dialect()() kw = {} compile_kwargs = {} if schema_translate_map: kw['schema_translate_map'] = schema_translate_map if params is not None: kw['column_keys'] = list(params) if literal_binds: compile_kwargs['literal_binds'] = True if isinstance(clause, orm.Query): context = clause._compile_context() context.statement.use_labels = True clause = context.statement elif isinstance(clause, orm.persistence.BulkUD): with mock.patch.object(clause, "_execute_stmt") as stmt_mock: clause.exec_() clause = stmt_mock.mock_calls[0][1][0] if compile_kwargs: kw['compile_kwargs'] = compile_kwargs c = clause.compile(dialect=dialect, **kw) param_str = repr(getattr(c, 'params', {})) if util.py3k: param_str = param_str.encode('utf-8').decode('ascii', 'ignore') print( ("\nSQL String:\n" + util.text_type(c) + param_str).encode('utf-8')) else: print( "\nSQL String:\n" + util.text_type(c).encode('utf-8') + param_str) cc = re.sub(r'[\n\t]', '', util.text_type(c)) eq_(cc, result, "%r != %r on dialect %r" % (cc, result, dialect)) if checkparams is not None: eq_(c.construct_params(params), checkparams) if checkpositional is not None: p = c.construct_params(params) eq_(tuple([p[x] for x in c.positiontup]), checkpositional) if check_prefetch is not None: eq_(c.prefetch, check_prefetch)
def process(value): assert isinstance(value, util.collections_abc.Sequence) if super_proc: return [super_proc(util.text_type(elem)) for elem in value] else: return [util.text_type(elem) for elem in value]
def test_random_compiled(dialect, expected): from mixer.backend.sqlalchemy import random compiled = random().compile(dialect=dialect) assert text_type(compiled) == expected
def assert_compile( self, clause, result, params=None, checkparams=None, dialect=None, checkpositional=None, check_prefetch=None, use_default_dialect=False, allow_dialect_select=False, literal_binds=False, schema_translate_map=None, ): if use_default_dialect: dialect = default.DefaultDialect() elif allow_dialect_select: dialect = None else: if dialect is None: dialect = getattr(self, "__dialect__", None) if dialect is None: dialect = config.db.dialect elif dialect == "default": dialect = default.DefaultDialect() elif isinstance(dialect, util.string_types): dialect = url.URL(dialect).get_dialect()() kw = {} compile_kwargs = {} if schema_translate_map: kw["schema_translate_map"] = schema_translate_map if params is not None: kw["column_keys"] = list(params) if literal_binds: compile_kwargs["literal_binds"] = True if isinstance(clause, orm.Query): context = clause._compile_context() context.statement.use_labels = True clause = context.statement if compile_kwargs: kw["compile_kwargs"] = compile_kwargs c = clause.compile(dialect=dialect, **kw) param_str = repr(getattr(c, "params", {})) if util.py3k: param_str = param_str.encode("utf-8").decode("ascii", "ignore") print(("\nSQL String:\n" + util.text_type(c) + param_str).encode("utf-8")) else: print("\nSQL String:\n" + util.text_type(c).encode("utf-8") + param_str) cc = re.sub(r"[\n\t]", "", util.text_type(c)) eq_(cc, result, "%r != %r on dialect %r" % (cc, result, dialect)) if checkparams is not None: eq_(c.construct_params(params), checkparams) if checkpositional is not None: p = c.construct_params(params) eq_(tuple([p[x] for x in c.positiontup]), checkpositional) if check_prefetch is not None: eq_(c.prefetch, check_prefetch)