Ejemplo n.º 1
0
    def _configure_pks(self):
        self.tables = sql_util.find_tables(self.mapped_table)

        self._pks_by_table = {}
        self._cols_by_table = {}

        all_cols = sqlalchemy_util.column_set(chain(*[col.proxy_set for col in self._columntoproperty]))

        pk_cols = sqlalchemy_util.column_set(c for c in all_cols if c.primary_key)
        # identify primary key columns which are also mapped by this mapper.
        tables = set(self.tables + [self.mapped_table])
        self._all_tables.update(tables)
        self._cols_by_table[self.mapped_table] = all_cols
        primary_key = [c for c in all_cols if c.name in self._primary_key_argument]
        self._pks_by_table[self.mapped_table] = primary_key

        self.primary_key = tuple(primary_key)
        self._log("Identified primary key columns: %s", primary_key)

        # determine cols that aren't expressed within our tables; mark these
        # as "read only" properties which are refreshed upon INSERT/UPDATE
        self._readonly_props = set(
            self._columntoproperty[col]
            for col in self._columntoproperty
            if self._columntoproperty[col] not in self._identity_key_props
            and (not hasattr(col, "table") or col.table not in self._cols_by_table)
        )
Ejemplo n.º 2
0
    def _configure_pks(self):
        self.tables = sql_util.find_tables(self.mapped_table)

        self._pks_by_table = {}
        self._cols_by_table = {}

        all_cols = util.column_set(chain(*[
            col.proxy_set for col in
            self._columntoproperty]))

        pk_cols = util.column_set(c for c in all_cols if c.primary_key)
        # identify primary key columns which are also mapped by this mapper.
        tables = set(self.tables + [self.mapped_table])
        self._all_tables.update(tables)
        self._cols_by_table[self.mapped_table] = all_cols
        primary_key = [c for c in all_cols if c.name in self._primary_key_argument]
        self._pks_by_table[self.mapped_table] = primary_key

        self.primary_key = tuple(primary_key)
        self._log("Identified primary key columns: %s", primary_key)

        # determine cols that aren't expressed within our tables; mark these
        # as "read only" properties which are refreshed upon INSERT/UPDATE
        self._readonly_props = set(
            self._columntoproperty[col]
            for col in self._columntoproperty
            if self._columntoproperty[col] not in self._identity_key_props and
            (not hasattr(col, 'table') or
                col.table not in self._cols_by_table))
Ejemplo n.º 3
0
    def get_dbname(self, mapper, clause):
        if mapper is not None:
            info = getattr(mapper.mapped_table, 'info', {})
            bind_key = info.get('bind_key')
            if bind_key:
                return bind_key
        if clause is not None:
            from sqlalchemy.sql import util
            is_match = False
            for t in util.find_tables(clause, include_crud=True):
                # print t,'------------',self.table_dbname_map
                if t in self.table_binds:
                    is_match = True
                    dn = self.table_binds[t]
                    break
            if is_match:
                return dn if dn is not None else "hrms"

        if read_or_write.__dict__.has_key(
                'db_name') and read_or_write.__dict__['db_name'] is not None:
            db_name = read_or_write.__dict__['db_name']
            print '---------------@read(' + str(
                db_name
            ) + ') has deprecated,you can easy use @read()----------'
            if db_name:
                return db_name

        return "hrms"
Ejemplo n.º 4
0
    def test_column_element_no_visit(self):
        class MyElement(ColumnElement):
            pass

        eq_(
            sql_util.find_tables(MyElement(), check_columns=True),
            []
        )
Ejemplo n.º 5
0
    def get_bind(self, mapper=None, clause=None):
        # todo by terry
        # 优化流程
        if mapper is clause is None:
            if self.bind:
                return self.bind
            else:
                raise UnboundExecutionError(
                    "This session is not bound to a single Engine or "
                    "Connection, and no context was provided to locate "
                    "a binding.")

        c_mapper = mapper is not None and getattr(orm_base, '_class_to_mapper')(mapper) or None

        # manually bound?
        if self.__binds:
            if c_mapper:
                _name = repr(c_mapper.mapped_table)
                _name_list = _name.split(':')
                if len(_name_list) > 1:
                    _tag = c_mapper.class_.__get_version__()
                    for t in self.__tags:
                        if contain_version(_tag, t):
                            _name = ':'.join([_name_list[0], t])
                            break
                if _name in self.__metadata__:
                    return self.__metadata__[_name]
                if repr(c_mapper.mapped_table) in self.__binds:
                    return self.__binds[repr(c_mapper.mapped_table)]

            # 非ORM不支持版本控制
            if clause is not None:
                for t in sql_util.find_tables(clause, include_crud=True):
                    if repr(t) in self.__binds:
                        return self.__binds[repr(t)]
                    elif str(t) in self.__binds:
                        return self.__binds[str(t)]

        if self.bind:
            return self.bind

        if isinstance(clause, ClauseElement) and clause.bind:
            return clause.bind

        if c_mapper and c_mapper.mapped_table.bind:
            return c_mapper.mapped_table.bind
        context = []
        if mapper is not None:
            context.append('mapper %s' % c_mapper)
        if clause is not None:
            context.append('SQL expression')
        raise UnboundExecutionError(
            "Could not locate a bind configured on %s or this Session" % (
                ', '.join(context)))
Ejemplo n.º 6
0
    def test_find_tables_selectable(self):
        metadata = MetaData()
        common = Table(
            "common",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", Integer),
            Column("extra", String(45)),
        )

        subset_select = select([common.c.id, common.c.data]).alias()

        eq_(set(sql_util.find_tables(subset_select)), {common})
Ejemplo n.º 7
0
    def test_find_tables_aliases(self):
        metadata = MetaData()
        common = Table(
            "common",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", Integer),
            Column("extra", String(45)),
        )

        calias = common.alias()
        subset_select = select([common.c.id, calias.c.data]).subquery()

        eq_(
            set(sql_util.find_tables(subset_select, include_aliases=True)),
            {common, calias, subset_select},
        )
Ejemplo n.º 8
0
    def test_column_element_no_visit(self):
        class MyElement(ColumnElement):
            _traverse_internals = []

        eq_(sql_util.find_tables(MyElement(), check_columns=True), [])
Ejemplo n.º 9
0
def find_selectable_dependencies(selectable: Selectable) -> List[Table]:
    """Find the tables used in a select query."""

    return (table for table in set(find_tables(selectable)))