def replacement_traverse(obj, opts, replace): """clone the given expression structure, allowing element replacement by a given replacement function.""" cloned = util.column_dict() stop_on = util.column_set(opts.get('stop_on', [])) def clone(element): newelem = replace(element) if newelem is not None: stop_on.add(newelem) return newelem if element not in cloned: cloned[element] = element._clone() return cloned[element] obj = clone(obj) stack = [obj] while stack: t = stack.pop() if t in stop_on: continue t._copy_internals(clone=clone) for c in t.get_children(**opts): stack.append(c) return obj
def replacement_traverse(obj, opts, replace): """clone the given expression structure, allowing element replacement by a given replacement function.""" cloned = util.column_dict() stop_on = util.column_set(opts.get('stop_on', [])) def clone(elem, **kw): if elem in stop_on or \ 'no_replacement_traverse' in elem._annotations: return elem else: newelem = replace(elem) if newelem is not None: stop_on.add(newelem) return newelem else: if elem not in cloned: cloned[elem] = newelem = elem._clone() newelem._copy_internals(clone=clone, **kw) return cloned[elem] if obj is not None: obj = clone(obj, **opts) return obj
def _deep_annotate(element, annotations, exclude=None): """Deep copy the given ClauseElement, annotating each element with the given annotations dictionary. Elements within the exclude collection will be cloned but not annotated. """ cloned = util.column_dict() def clone(elem): # check if element is present in the exclude list. # take into account proxying relationships. if elem in cloned: return cloned[elem] elif exclude and \ hasattr(elem, 'proxy_set') and \ elem.proxy_set.intersection(exclude): newelem = elem._clone() elif annotations != elem._annotations: newelem = elem._annotate(annotations) else: newelem = elem newelem._copy_internals(clone=clone) cloned[elem] = newelem return newelem if element is not None: element = clone(element) return element
def __init__(self, selectable, equivalents=None, include=None, exclude=None, adapt_on_names=False): self.__traverse_options__ = {'stop_on':[selectable]} self.selectable = selectable self.include = include self.exclude = exclude self.equivalents = util.column_dict(equivalents or {}) self.adapt_on_names = adapt_on_names
def cloned_traverse(obj, opts, visitors): """clone the given expression structure, allowing modifications by visitors.""" cloned = util.column_dict() def clone(element): if element not in cloned: cloned[element] = element._clone() return cloned[element] obj = clone(obj) stack = [obj] while stack: t = stack.pop() if t in cloned: continue t._copy_internals(clone=clone) meth = visitors.get(t.__visit_name__, None) if meth: meth(t) for c in t.get_children(**opts): stack.append(c) return obj
def construct_params(self, params=None): """return a dictionary of bind parameter keys and values""" if params: params = util.column_dict(params) pd = {} for bindparam, name in self.bind_names.iteritems(): for paramname in (bindparam.key, bindparam.shortname, name): if paramname in params: pd[name] = params[paramname] break else: if util.callable(bindparam.value): pd[name] = bindparam.value() else: pd[name] = bindparam.value return pd else: pd = {} for bindparam in self.bind_names: if util.callable(bindparam.value): pd[self.bind_names[bindparam]] = bindparam.value() else: pd[self.bind_names[bindparam]] = bindparam.value return pd
def construct_params(self, params=None): """return a dictionary of bind parameter keys and values""" if params: params = util.column_dict(params) pd = {} for bindparam, name in self.bind_names.iteritems(): for paramname in (bindparam, bindparam.key, bindparam.shortname, name): if paramname in params: pd[name] = params[paramname] break else: if util.callable(bindparam.value): pd[name] = bindparam.value() else: pd[name] = bindparam.value return pd else: pd = {} for bindparam in self.bind_names: if util.callable(bindparam.value): pd[self.bind_names[bindparam]] = bindparam.value() else: pd[self.bind_names[bindparam]] = bindparam.value return pd
def __init__(self, dialect, statement, column_keys=None, inline=False, **kwargs): """Construct a new ``DefaultCompiler`` object. dialect Dialect to be used statement ClauseElement to be compiled column_keys a list of column names to be compiled into an INSERT or UPDATE statement. """ engine.Compiled.__init__(self, dialect, statement, column_keys, **kwargs) # compile INSERT/UPDATE defaults/sequences inlined (no pre-execute) self.inline = inline or getattr(statement, 'inline', False) # a dictionary of bind parameter keys to _BindParamClause instances. self.binds = {} # a dictionary of _BindParamClause instances to "compiled" names that are # actually present in the generated SQL self.bind_names = util.column_dict() # stack which keeps track of nested SELECT statements self.stack = [] # relates label names in the final SQL to # a tuple of local column/label name, ColumnElement object (if any) and TypeEngine. # ResultProxy uses this for type processing and column targeting self.result_map = {} # true if the paramstyle is positional self.positional = self.dialect.positional if self.positional: self.positiontup = [] self.bindtemplate = BIND_TEMPLATES[self.dialect.paramstyle] # an IdentifierPreparer that formats the quoting of identifiers self.preparer = self.dialect.identifier_preparer self.label_length = self.dialect.label_length or self.dialect.max_identifier_length # a map which tracks "anonymous" identifiers that are # created on the fly here self.anon_map = util.PopulateDict(self._process_anon) # a map which tracks "truncated" names based on dialect.label_length # or dialect.max_identifier_length self.truncated_names = {}
def __init__(self, class_, *columns, **kwargs): if 'comparator' in kwargs: util.warn_deprecated("The 'comparator' argument to CompositeProperty is deprecated. Use comparator_factory.") kwargs['comparator_factory'] = kwargs['comparator'] super(CompositeProperty, self).__init__(*columns, **kwargs) self._col_position_map = util.column_dict((c, i) for i, c in enumerate(columns)) self.composite_class = class_ self.strategy_class = strategies.CompositeColumnLoader
def __init__(self, class_, *columns, **kwargs): if "comparator" in kwargs: util.warn_deprecated( "The 'comparator' argument to CompositeProperty is deprecated. Use comparator_factory." ) kwargs["comparator_factory"] = kwargs["comparator"] super(CompositeProperty, self).__init__(*columns, **kwargs) self._col_position_map = util.column_dict((c, i) for i, c in enumerate(columns)) self.composite_class = class_ self.strategy_class = strategies.CompositeColumnLoader
def __init__(self, dialect, statement, column_keys=None, inline=False, **kwargs): """Construct a new ``DefaultCompiler`` object. dialect Dialect to be used statement ClauseElement to be compiled column_keys a list of column names to be compiled into an INSERT or UPDATE statement. """ engine.Compiled.__init__(self, dialect, statement, **kwargs) self.column_keys = column_keys # compile INSERT/UPDATE defaults/sequences inlined (no pre-execute) self.inline = inline or getattr(statement, 'inline', False) # a dictionary of bind parameter keys to _BindParamClause instances. self.binds = {} # a dictionary of _BindParamClause instances to "compiled" names that are # actually present in the generated SQL self.bind_names = util.column_dict() # stack which keeps track of nested SELECT statements self.stack = [] # relates label names in the final SQL to # a tuple of local column/label name, ColumnElement object (if any) and TypeEngine. # ResultProxy uses this for type processing and column targeting self.result_map = {} # true if the paramstyle is positional self.positional = self.dialect.positional if self.positional: self.positiontup = [] self.bindtemplate = BIND_TEMPLATES[self.dialect.paramstyle] # an IdentifierPreparer that formats the quoting of identifiers self.preparer = self.dialect.identifier_preparer self.label_length = self.dialect.label_length or self.dialect.max_identifier_length # a map which tracks "anonymous" identifiers that are # created on the fly here self.anon_map = util.PopulateDict(self._process_anon) # a map which tracks "truncated" names based on dialect.label_length # or dialect.max_identifier_length self.truncated_names = {}
def _create_lazy_clause(cls, prop, reverse_direction=False): binds = util.column_dict() lookup = util.column_dict() equated_columns = util.column_dict() if reverse_direction and not prop.secondaryjoin: for l, r in prop.local_remote_pairs: _list = lookup.setdefault(r, []) _list.append((r, l)) equated_columns[l] = r else: for l, r in prop.local_remote_pairs: _list = lookup.setdefault(l, []) _list.append((l, r)) equated_columns[r] = l def col_to_bind(col): if col in lookup: for tobind, equated in lookup[col]: if equated in binds: return None if col not in binds: binds[col] = sql.bindparam(None, None, type_=col.type) return binds[col] return None lazywhere = prop.primaryjoin if not prop.secondaryjoin or not reverse_direction: lazywhere = visitors.replacement_traverse(lazywhere, {}, col_to_bind) if prop.secondaryjoin is not None: secondaryjoin = prop.secondaryjoin if reverse_direction: secondaryjoin = visitors.replacement_traverse( secondaryjoin, {}, col_to_bind) lazywhere = sql.and_(lazywhere, secondaryjoin) bind_to_col = dict((binds[col].key, col) for col in binds) return (lazywhere, bind_to_col, equated_columns)
def _create_lazy_clause(cls, prop, reverse_direction=False): binds = util.column_dict() lookup = util.column_dict() equated_columns = util.column_dict() if reverse_direction and prop.secondaryjoin is None: for l, r in prop.local_remote_pairs: _list = lookup.setdefault(r, []) _list.append((r, l)) equated_columns[l] = r else: for l, r in prop.local_remote_pairs: _list = lookup.setdefault(l, []) _list.append((l, r)) equated_columns[r] = l def col_to_bind(col): if col in lookup: for tobind, equated in lookup[col]: if equated in binds: return None if col not in binds: binds[col] = sql.bindparam(None, None, type_=col.type) return binds[col] return None lazywhere = prop.primaryjoin if prop.secondaryjoin is None or not reverse_direction: lazywhere = visitors.replacement_traverse( lazywhere, {}, col_to_bind) if prop.secondaryjoin is not None: secondaryjoin = prop.secondaryjoin if reverse_direction: secondaryjoin = visitors.replacement_traverse( secondaryjoin, {}, col_to_bind) lazywhere = sql.and_(lazywhere, secondaryjoin) bind_to_col = dict((binds[col].key, col) for col in binds) return lazywhere, bind_to_col, equated_columns
def __init__(self, selectable, equivalents=None, include=None, exclude=None): self.__traverse_options__ = { 'column_collections': False, 'stop_on': [selectable] } self.selectable = selectable self.include = include self.exclude = exclude self.equivalents = util.column_dict(equivalents or {})
def _deep_deannotate(element): """Deep copy the given element, removing all annotations.""" cloned = util.column_dict() def clone(elem): if elem not in cloned: newelem = elem._deannotate() newelem._copy_internals(clone=clone) cloned[elem] = newelem return cloned[elem] if element is not None: element = clone(element) return element
def cloned_traverse(obj, opts, visitors): """clone the given expression structure, allowing modifications by visitors.""" cloned = util.column_dict() stop_on = util.column_set(opts.get('stop_on', [])) def clone(elem): if elem in stop_on: return elem else: if elem not in cloned: cloned[elem] = newelem = elem._clone() newelem._copy_internals(clone=clone) meth = visitors.get(newelem.__visit_name__, None) if meth: meth(newelem) return cloned[elem] if obj is not None: obj = clone(obj) return obj
def __init__(self, selectable, equivalents=None, include=None, exclude=None): self.__traverse_options__ = {"stop_on": [selectable]} self.selectable = selectable self.include = include self.exclude = exclude self.equivalents = util.column_dict(equivalents or {})