コード例 #1
0
ファイル: sql_query.py プロジェクト: dwiel/graphcore
    def __call__(self, **kwargs):
        if set(self.input_mapping.keys()) != set(kwargs.keys()):
            raise ValueError('input mapping keys {} != kwargs keys {}'.format(
                self.input_mapping.keys(), kwargs.keys()
            ))

        # compose where query
        where = self.where.copy()
        for k, v in kwargs.items():
            where[self.input_mapping[k]] = v

        sql, vals = sql_query_dict.select(
            self.tables, self.selects, where, limit=self.limit,
            param_style=self.param_style
        )

        ret = self.driver(sql, vals)

        if self.one_column:
            ret = [row[0] for row in ret]

        if self.first:
            if len(ret):
                ret = ret[0]
            else:
                raise NoResult()

        return ret
コード例 #2
0
ファイル: test.py プロジェクト: PlotWatt/sql_query_dict
def test_mysql_with_gt_lt():
    SQL, vals = sql_query_dict.select(
        't', 'z', {'x><': (10, 30), 'y': 1}
    )
    # easiest way to handle both orderings of the clauses
    assert SQL in (
        "SELECT z FROM t WHERE  (y = %s)  AND  ((x > %s) AND (x < %s)) ",
        "SELECT z FROM t WHERE  ((x > %s) AND (x < %s))  AND  (y = %s) ",
    )
    assert vals in (
        [1, 10, 30],
        [10, 30, 1],
    )