Exemplo n.º 1
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))
Exemplo n.º 2
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