python-sqlite-orm icon indicating copy to clipboard operation
python-sqlite-orm copied to clipboard

Query builder

Open fernandojunior opened this issue 9 years ago • 1 comments

fernandojunior avatar Mar 27 '16 11:03 fernandojunior

I am currently implementing a query builder in my fork here

It is a WIP and the update() method is unimplemented, and there are no tests/docs yet

Current Syntax

Test Table

class Test(db.Model):

    one = int
    two = int
    three = int
    four = int

    def __init__(self, one, two, three, four):
        self.one = one
        self.two = two
        self.three = three
        self.four = four

Test(1, 0, 0, 0).save()
Test(0, 1, 1, 0).save()
Test(1, 1, 1, 0).save()
Test(0, 1, 1, 0).save()

SELECT

# SELECT * FROM user where one = 1 OR two = 1;
rows = QueryBuilder(Test).filter(one = 1, two=1, boolean=QueryBuilder.OR).limit(3).select()
{'four': 0, 'id': 1, 'three': 0, 'two': 0, 'one': 1}
{'four': 0, 'id': 2, 'three': 1, 'two': 1, 'one': 0}
{'four': 0, 'id': 3, 'three': 1, 'two': 1, 'one': 1}
# SELECT one, two FROM user where one = 1 AND two = 2;
rows = QueryBuilder(Test).filter(one = 1, two=1, boolean=QueryBuilder.AND).select("one","two")
{'two': 1, 'one': 1}
{'two': 1, 'one': 1}

DELETE

# DELETE from Test WHERE one = 1 and two = 1;
QueryBuilder(Test).filter(one = 1, two = 1, boolean=QueryBuilder.AND).delete()

aberkowitz avatar Oct 31 '16 23:10 aberkowitz