Exemple #1
0
 def _default_select(self, table_name, condition=None):
     query = SqlQuery(
         table_name=table_name,
         columns=self.COLUMNS[table_name],
         datatypes=self.DATATYPES[table_name],
     ).where(condition)
     return self._do_db_select(query)
Exemple #2
0
 def migrate_data_table_0_1(self, table, columns, datatypes, attributes):
     """Migrate a single data table for v0 to v1."""
     c = self.session._do_db_select(SqlQuery(table, columns, datatypes))
     for row in c:
         cuds_idx = self.get_cuds_idx_0_1(row[-1])  # uuid is lasr element
         for col, attr, value in zip(columns, attributes, row):
             datatype = datatypes[col] or STR
             self.migrate_data_triple_0_1(attr, datatype, cuds_idx, value)
Exemple #3
0
    def _construct_query(self, pattern, table_name, object_datatype):
        q = SqlQuery(self.CUDS_TABLE,
                     columns=self.COLUMNS[self.CUDS_TABLE][1:],
                     datatypes=self.DATATYPES[self.CUDS_TABLE],
                     alias="ts").where(
                         JoinCondition(table_name, "s", "ts", "cuds_idx"))

        if table_name != self.TYPES_TABLE:
            q = q.join(self.ENTITIES_TABLE,
                       columns=self.COLUMNS[self.ENTITIES_TABLE][1:],
                       datatypes=self.DATATYPES[self.ENTITIES_TABLE],
                       alias="tp").where(
                           JoinCondition(table_name, "p", "tp", "entity_idx"))
        else:
            q = q.join(self.ENTITIES_TABLE,
                       columns=self.COLUMNS[self.ENTITIES_TABLE][1:],
                       datatypes=self.DATATYPES[self.ENTITIES_TABLE],
                       alias="to").where(
                           JoinCondition(table_name, "o", "to", "entity_idx"))

        if table_name == self.RELATIONSHIP_TABLE:
            q = q.join(self.CUDS_TABLE,
                       columns=self.COLUMNS[self.CUDS_TABLE][1:],
                       datatypes=self.DATATYPES[self.CUDS_TABLE],
                       alias="to").where(
                           JoinCondition(table_name, "o", "to", "cuds_idx"))

        cols, dtypes = [], {}
        if table_name.startswith(self.DATA_TABLE_PREFIX):
            cols, dtypes = ["o"], {"o": object_datatype}
        q = q.join(table_name, cols, dtypes)
        q = q.where(self._get_conditions(pattern, table_name, object_datatype))
        return q
Exemple #4
0
    def migrate_relations_0_1(self):
        """Migrate the relations from v0 to v1."""
        c = self.session._do_db_select(
            SqlQuery(
                "OSP_RELATIONSHIPS",
                ["origin", "target", "name"],
                {
                    "origin": "UID",
                    "target": "UID",
                    "name": STR
                },
            ))
        for origin, target, name in c:
            rel = get_entity(name)

            ns_idx = self.get_ns_idx_0_1(rel.namespace.get_iri())
            p = self.get_entity_idx_0_1(ns_idx, rel)
            s = self.get_cuds_idx_0_1(origin)
            o = self.get_cuds_idx_0_1(target)

            self.session._do_db_insert(
                "OSP_V1_RELATIONS",
                ["s", "p", "o"],
                [s, p, o],
                {
                    "s": INT,
                    "p": INT,
                    "o": INT
                },
            )

            if target == uuid.UUID(int=0):
                ns_idx = self.get_ns_idx_0_1(rel.inverse.namespace.get_iri())
                p = self.get_entity_idx_0_1(ns_idx, rel.inverse)
                self.session._do_db_insert(
                    "OSP_V1_RELATIONS",
                    ["s", "p", "o"],
                    [o, p, s],
                    {
                        "s": INT,
                        "p": INT,
                        "o": INT
                    },
                )
Exemple #5
0
    def migrate_master_0_1(self):
        """Migrate the OSP_MASTER table."""
        c = self.session._do_db_select(
            SqlQuery("OSP_MASTER", ["uid", "oclass"], {
                "uid": "UID",
                "oclass": STR
            }))
        for uid, oclass in c:
            oclass = get_entity(oclass) if oclass != "" else cuba.Wrapper
            ns_iri = str(oclass.namespace.get_iri())

            ns_idx = self.get_ns_idx_0_1(ns_iri)
            oclass_idx = self.get_entity_idx_0_1(ns_idx, oclass)
            cuds_idx = self.get_cuds_idx_0_1(uid)

            self.session._do_db_insert(
                "OSP_V1_TYPES",
                ["s", "o"],
                [cuds_idx, oclass_idx],
                {
                    "s": INT,
                    "o": INT
                },
            )
Exemple #6
0
    def test_sql_query(self):
        """Test the SqlQuery class."""
        q = SqlQuery("my_table", COLS, DTYPES, "alias")
        self.assertEqual(q.order, ["alias"])
        self.assertEqual(q.tables, {"alias": "my_table"})
        self.assertEqual(q._columns, {"alias": EXPANDED_COLS})
        self.assertEqual(q.datatypes, {"alias": EXPANDED_DTYPES})
        self.assertEqual(q.condition, None)

        # test WHERE
        c1 = EqualsCondition("my_table", "1", VALS[0], DTYPES["1"])
        c2 = EqualsCondition("my_table", "2", VALS[1], DTYPES["2"])
        c21 = EqualsCondition("my_table", "2___0", VALS[1][0],
                              EXPANDED_DTYPES["2___0"])
        c22 = EqualsCondition("my_table", "2___1", VALS[1][1],
                              EXPANDED_DTYPES["2___1"])
        c3 = EqualsCondition("my_table", "3", VALS[2], DTYPES["2"])
        c3 = AndCondition(c1, c2, c3)
        q.where(c1)
        self.assertEqual(q.condition, c1)
        q.condition = None
        q.where(c2)
        self.assertIsInstance(q.condition, AndCondition)
        self.assertEqual(len(q.condition.conditions), 2)
        self.assertEqual(q.condition, AndCondition(c21, c22))
        q.condition = None
        q.where(c1)
        q.where(c2)
        self.assertIsInstance(q.condition, AndCondition)
        self.assertEqual(len(q.condition.conditions), 3)
        self.assertEqual(q.condition, AndCondition(c1, c21, c22))
        q.condition = None
        q.where(c2)
        q.where(c1)
        self.assertIsInstance(q.condition, AndCondition)
        self.assertEqual(len(q.condition.conditions), 3)
        self.assertEqual(q.condition, AndCondition(c1, c21, c22))
        q.condition = None
        q.where(c1)
        q.where(c1)
        self.assertIsInstance(q.condition, AndCondition)
        self.assertEqual(len(q.condition.conditions), 1)
        self.assertEqual(q.condition, AndCondition(c1))

        # test JOIN
        c4 = JoinCondition("alias", "1", "alias2", "1")
        q.join("2ndTable", COLS, DTYPES, alias="alias2")
        self.assertEqual(q.order, ["alias", "alias2"])
        self.assertEqual(q.tables, {"alias": "my_table", "alias2": "2ndTable"})
        self.assertEqual(q._columns, {
            "alias": EXPANDED_COLS,
            "alias2": EXPANDED_COLS
        })
        self.assertEqual(q.datatypes, {
            "alias": EXPANDED_DTYPES,
            "alias2": EXPANDED_DTYPES
        })
        self.assertEqual(q.condition, AndCondition(c1))
        q.where(c4)
        self.assertEqual(q.condition, AndCondition(c1, c4))
Exemple #7
0
 def test_contract_vector_values(self):
     """Test the contract_vector_values method."""
     q = SqlQuery("table", EXPANDED_COLS, EXPANDED_DTYPES)
     r = contract_vector_values([EXPANDED_VALS], q)
     np.testing.assert_equal(next(r), VALS)