Skip to content

Updating querpy #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ae3dad8
Update README.md
ftrotter Jul 17, 2022
ea08c25
print function has parens now
ftrotter Jul 17, 2022
7240dcf
manually importing zip
ftrotter Jul 17, 2022
2a17ff6
now its 3
ftrotter Jul 17, 2022
2826797
newline
ftrotter Jul 17, 2022
2c7b693
Merge pull request #1 from ftrotter/ft_port_to_python_3
ftrotter Jul 17, 2022
a415ded
documented two regex
ftrotter Jul 18, 2022
ec8a7ed
learned what the underlying purpose of the query components are and d…
ftrotter Jul 18, 2022
ed6b25a
documentation lists an add command but then shows the results of an o…
ftrotter Jul 18, 2022
1300fcd
remove those tabs
ftrotter Jul 18, 2022
190c9d9
correct build_join example in README
ftrotter Jul 18, 2022
71e6239
remove build_join as a seperate function
ftrotter Jul 18, 2022
f77c461
account for static functions in test
ftrotter Jul 18, 2022
9f69295
use build_join as static function
ftrotter Jul 18, 2022
3366a72
Merge pull request #2 from ftrotter/ft_doc
ftrotter Jul 18, 2022
8fffa42
added create table function
ftrotter Jul 18, 2022
dd17d07
Merge pull request #3 from ftrotter/ft_create
ftrotter Jul 18, 2022
90e1a8d
remove additional print
ftrotter Jul 18, 2022
cde45b8
adding group by to test
ftrotter Jul 18, 2022
6a140cd
pretty print is wonky, but LIMIT works
ftrotter Jul 18, 2022
5a550a1
just use l for LIMIT
ftrotter Jul 18, 2022
daefae3
create table, group by and limit examples in the README
ftrotter Jul 18, 2022
6b11d7e
adding order by and notes
ftrotter Aug 4, 2022
3341a65
testing order by
ftrotter Aug 4, 2022
7099dfc
porting Shawns changes back!
ftrotter Feb 8, 2023
cc42da0
Thanks to @dmahone1 for finding this bug and providing the initial fix
ftrotter Apr 19, 2023
aed45a2
Merge branch 'master' of https://github.com/ftrotter/querpy
ftrotter Apr 19, 2023
e0b21c6
handle objects that know how to become strings
ftrotter Nov 23, 2024
ede04ee
adding recent changes
ftrotter Nov 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
The Query class is intended to provide a high level interface for
building/editing SQL query strings.

This library should now support python 3.



Example usage:
```python
>>> from querpy import Query
Expand All @@ -11,7 +15,7 @@ Example usage:
>>> new_query.s += ['col1', 'col2', 'col3'] # can take lists
>>> new_query.s += 'col4' # can take single strings
>>> new_query.w += 'col1 = 1' # can also take a list (separated by AND)
>>> new_query.w &= 'col2 IS NULL' # handles &= and |= operators
>>> new_query.w |= 'col2 IS NULL' # handles &= and |= operators
>>> print new_query
SELECT
col1,
Expand Down Expand Up @@ -71,12 +75,11 @@ Suppose you want to extend your query by joining to another table and adding col
col1 = 1
OR col2 IS NULL
```
While this works, we are returning to the land of long strings. We can do the same thing (n.b. we'll LEFT JOIN this time) using the build_join helper function to make the join step more readable and modular:
While this works, we are returning to the land of long strings. We can do the same thing (n.b. we'll LEFT JOIN this time) using the Query.build_join helper function to make the join step more readable and modular:
```python
>>> from querpy import build_join
>>> new_query.j.clear()
>>> new_query.join_type = 'LEFT'
>>> new_query.j += build_join('ex_db.dbo.new_tbl nt', 'tbl.id', 'nt.id', 'tbl.city', 'nt.city')
>>> new_query.j += Query.build_join('ex_db.dbo.new_tbl nt', 'tbl.id', 'nt.id', 'tbl.city', 'nt.city')
>>> new_query.join_type = '' # set back to regular join
>>> new_query
SELECT
Expand All @@ -94,4 +97,33 @@ When your query string is ready to be passed to the function that will execute t
>>> new_query.statement
SELECT col2, nt.id FROM ex_db.dbo.ex_table tbl LEFT JOIN ex_db.dbo.new_tbl nt ON tbl.id = nt.id AND tbl.city = nt.city WHERE col1 = 1 OR col2 IS NULL
```

You can also prepend a CREATE TABLE <> AS to the SQL
```python
>>> new_query.j += 'ex_db.dbo.new_tbl nt ON tbl.id = nt.id'
>>> new_query.s += 'nt.id'
>>> new_query.g += 'col1'
>>> new_query.l += ' 10, 100'
>>> new_query.ci += ' db_name.tbl_name '
>>> new_query
CREATE TABLE db_name.tbl_name AS
SELECT
col1,
nt.id
FROM
ex_db.dbo.ex_table tbl
JOIN ex_db.dbo.new_tbl nt ON tbl.id = nt.id
WHERE
col1 = 1
OR col2 IS NULL
GROUP BY
col1
LIMIT 10, 100
```


Suppose you want to extend your query by joining to another table and adding columns from this table:


NOTE: the SQL constructed is **not** validated.

18 changes: 12 additions & 6 deletions doc_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,38 @@
">>> new_query.s += 'col4' # can take single strings",
">>> new_query.w += 'col1 = 1' # can also take a list",
">>> new_query.w |= 'col2 IS NULL' # handles &= and |= operators",
">>> print new_query",
">>> print(new_query)",
">>> new_query",
">>> new_query.s.clear() # clear SELECT component",
">>> new_query.s += 'col1'",
">>> new_query",
">>> new_query.s[0] = 'col2'",
">>> print new_query.s",
">>> print(new_query.s)",
">>> new_query.j += 'ex_db.dbo.new_tbl nt ON tbl.id = nt.id'",
">>> new_query.s += 'nt.id'",
">>> new_query",
">>> from querpy import build_join",
">>> new_query.j.clear()",
">>> new_query.join_type = 'LEFT'",
">>> new_query.j += build_join('ex_db.dbo.new_tbl nt', 'tbl.id', 'nt.id',"
">>> new_query.j += Query.build_join('ex_db.dbo.new_tbl nt', 'tbl.id', 'nt.id',"
" 'tbl.city', 'nt.city')",
">>> new_query.join_type = '' # set back to regular join",
">>> new_query.ci += 'thisDB.thatTable' # set back to regular join",
">>> new_query.l += ' 10, 100' ",
">>> new_query.g += 'col1' ",
">>> new_query.g += 'col3' ",
">>> new_query.o += 'col1' ",
">>> new_query.o += 'col3' ",
">>> new_query",
">>> new_query",
">>> new_query.statement",
]

def main():

for c in commands:
print c
print(c)
if c in ('>>> new_query', '>>> new_query.statement'):
print eval(c[4:])
print(eval(c[4:]))
else:
exec(c[4:])

Expand Down
3 changes: 1 addition & 2 deletions doc_example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ SELECT
WHERE
col1 = 1
OR col2 IS NULL
>>> from querpy import build_join
>>> new_query.j.clear()
>>> new_query.join_type = 'LEFT'
>>> new_query.j += build_join('ex_db.dbo.new_tbl nt', 'tbl.id', 'nt.id', 'tbl.city', 'nt.city')
>>> new_query.j += Query.build_join('ex_db.dbo.new_tbl nt', 'tbl.id', 'nt.id', 'tbl.city', 'nt.city')
>>> new_query.join_type = '' # set back to regular join
>>> new_query
SELECT
Expand Down
Loading