Esempio n. 1
0
    def initialize(self, ctx):

        super(TableMapper, self).initialize(ctx)

        if self._uses_table:

            if (self.entity == None):
                raise Exception("No entity defined for %s" % self)
            if (self.connection == None):
                raise Exception("No connection defined for %s" % self)

            ctx.comp.initialize(self.entity)
            ctx.comp.initialize(self.connection)

            self._sqltable = CachedSQLTable()
            self._sqltable.name = self.table
            self._sqltable.connection = self.connection

            # Assert that the sqltable is clean
            #if (len(self._sqltable.columns) != 0): raise AssertionError("SQLTable '%s' columns shall be empty!" % self._sqltable.name)

        # If lookup_cols is a string, split by commas
        if (isinstance(self.lookup_cols, basestring)):
            self.lookup_cols = [
                key.strip() for key in self.lookup_cols.split(",")
            ]

        Mappings.includes(ctx, self.mappings)
        for mapping in self.mappings:
            try:
                if (not "entity" in mapping):
                    mapping["entity"] = self.entity
            except TypeError as e:
                raise Exception(
                    "Could not initialize mapping '%s' of '%s': %s" %
                    (mapping, self, e))

        if self._uses_table:

            mappings = self._mappings(ctx)
            for mapping in mappings:
                logger.debug("%s adding column from OLAP mapping: %s" %
                             (self, mapping))
                self._sqltable.columns.append({
                    "name": mapping["column"],
                    "type": mapping["type"],
                    "pk": mapping["pk"]
                })

            # If no key, use pk()
            if (self.lookup_cols == None):
                pk = self.pk(ctx)
                if ((pk == None) or (pk["type"] == "AutoIncrement")):
                    raise Exception(
                        "No lookup cols defined for %s (use lookup_cols=[...])"
                        % self)
                self.lookup_cols = [pk["name"]]

            ctx.comp.initialize(self._sqltable)
Esempio n. 2
0
    def initialize(self, ctx):

        super().initialize(ctx)

        if (self.entity == None):
            raise Exception("No entity defined for %s" % self)

        ctx.comp.initialize(self.entity)

        # Apply a caching layer
        # TODO: shall at least be optional, also, columns are referenced to the backed table
        # another option is that everybody that wants caching adds the wrapper, or maybe that
        # tables natively support caching.
        if self._uses_table:
            self._sqltable = CachedSQLTable(sqltable=self.sqltable)
        #self._sqltable = self.sqltable

        # Assert that the sqltable is clean
        #if (len(self._sqltable.columns) != 0): raise AssertionError("SQLTable '%s' columns shall be empty!" % self._sqltable.name)

        # If lookup_cols is a string, split by commas
        if (isinstance(self.lookup_cols, str)):
            self.lookup_cols = [
                key.strip() for key in self.lookup_cols.split(",")
            ]

        #Mappings.includes(ctx, self.mappings)
        for mapping in self.mappings:
            try:
                if mapping.path is None:
                    raise Exception("Mapping entity is None: %s" % self)
            except TypeError as e:
                raise Exception(
                    "Could not initialize mapping '%s' of '%s': %s" %
                    (mapping, self, e))

        if self._uses_table:

            # If no key, use pk()
            if self.lookup_cols is None:
                pk = self.pk(ctx)
                if (pk is None) or (pk.sqlcolumn.type == "AutoIncrement"):
                    #logger.warning("No lookup cols defined for %s", self)   # else
                    raise Exception("No lookup cols defined for %s" % self)
                self.lookup_cols = [pk]

            ctx.comp.initialize(self._sqltable)