sql-fragments 0.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sql_fragments-0.0.2/LICENSE +24 -0
- sql_fragments-0.0.2/PKG-INFO +156 -0
- sql_fragments-0.0.2/README.md +143 -0
- sql_fragments-0.0.2/pyproject.toml +27 -0
- sql_fragments-0.0.2/setup.cfg +4 -0
- sql_fragments-0.0.2/sql_fragments.egg-info/PKG-INFO +156 -0
- sql_fragments-0.0.2/sql_fragments.egg-info/SOURCES.txt +13 -0
- sql_fragments-0.0.2/sql_fragments.egg-info/dependency_links.txt +1 -0
- sql_fragments-0.0.2/sql_fragments.egg-info/top_level.txt +1 -0
- sql_fragments-0.0.2/sqlfragments/__init__.py +27 -0
- sql_fragments-0.0.2/sqlfragments/_fragments.py +683 -0
- sql_fragments-0.0.2/sqlfragments/py.typed +0 -0
- sql_fragments-0.0.2/tests/test_params.py +316 -0
- sql_fragments-0.0.2/tests/test_sqlalchemy.py +300 -0
- sql_fragments-0.0.2/tests/test_tokenize.py +112 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sql-fragments
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: SQL Fragments library for building SQL queries.
|
|
5
|
+
Author-email: Jennifer Taylor <dragonminded@dragonminded.com>
|
|
6
|
+
Maintainer-email: Jennifer Taylor <dragonminded@dragonminded.com>
|
|
7
|
+
License-Expression: Unlicense
|
|
8
|
+
Project-URL: Homepage, https://github.com/DragonMinded/sql-fragments
|
|
9
|
+
Requires-Python: >=3.11
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
SQL fragments are a way to keep chunks of SQL and any parameterized arguments together when building queries without an ORM. They take advantage of Python's ability to specify a literal string as an argument to ensure that any potential injection is flagged by the type checker. They include logically consistent ways of creating AND and OR filters for data fetching, sidestep the problem in MySQL and Postgres where an empty list in an IN query causes a syntax error, and provide an easy way to optionally provide fragments to a larger SQL statement. They also allow you to keep parameters local to the fragment of SQL that you are building.
|
|
15
|
+
|
|
16
|
+
Originally inspired by a similar library in PHP/Hack, I've used or built a version of this at the last several jobs I've worked at in various languages. Unfortunately, `mypy` does not currently support `LiteralString` type checking so this library cannot enable lint-time checks against possible SQL injection when using `mypy` as your type checking system. It's on you to only provide literal strings to the first parameter of `fragment()` in this case, but this is no different than using `text()` in sqlalchemy.
|
|
17
|
+
|
|
18
|
+
### API
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
def statement(sql: LiteralString, *args: object, **kwargs: object) -> "Statement":
|
|
22
|
+
...
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Parses a SQL query with optional specifiers, binding those specifiers to the arguments presented. Returns a `Statement` which can be used with the `%statement` or `%statementlist` specifiers in another statement or fragment. The `Statement` class also has a `to_sqlalchemy()` method which returns a tuple of SQLAlchemy-compatible SQL and a dictionary of bind parameters, suitable for passing to SQLAlchemy's `execute()` function.
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
def fragment(sql: LiteralString, *args: object, **kwargs: object) -> "Fragment":
|
|
29
|
+
...
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Parses a fragment of SQL with optional specifiers, binding those specifiers to the arguments presented. Returns a `Fragment` which can be used with the `%fragment`, `%fragmentlist`, `%andlist` or `%orlist` specifiers in another statement or fragment. Fragments and Statements both support the full gamut of format specifiers.
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
class FragmentException(Exception):
|
|
36
|
+
...
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Base exception type that all exceptions thrown when parsing SQL will originate from. Various exceptions exist for parsing exceptions that are nested underneath `ParseException` which is itself a subclass of `FragmentException`. Various exceptions exist for argument exceptions that are nested underneath `ArgumentException` which is itself a subclass of `FragmentException`. You should expect to get a subclass of `ParseException` if you provide SQL to either `statement()` or `fragment()` that contains an invalid or unparseable format specifier or specifier name. You should expect to get a subclass of `ArgumentException` if you provide invalid arguments for the given specifier, either as positional arguments or as kwargs to either `statement()` or `fragment()`.
|
|
40
|
+
|
|
41
|
+
### Format Specifiers
|
|
42
|
+
|
|
43
|
+
- `%table` - A table name. For simplicity, this expects the associated argument to be a string containing only alphanumeric characters and the characters `.` and `_`. Results in SQL that contains the table name escaped in back ticks.
|
|
44
|
+
- `%column` - A column name. For simplicity, this expects the associated argument to be a string containing only alphanumeric characters and the characters `.` and `_`. Results in SQL that contains the column name escaped in back ticks.
|
|
45
|
+
- `%columnlist` - A sequence of column names. Each entry in the sequence must conform to the same rules as `%column`. Note that since SQL is order-sensitive when specifying column names, this only takes sequence types, such as a list. Often used in tandem with `%valuelist` in insert statements.
|
|
46
|
+
- `%value` - A value, meant to be passed to the underlying SQL engine as a bind parameter. Can contain any valid python type that the underlying engine knows how to serialize, or `None`.
|
|
47
|
+
- `%valuelist` - A sequence of values. Each entry in the sequence must conform to the same rules as `%value`. Note that since SQL is order-sensitive when specifying lists of values, this only takes sequence types, such as a list. Often used in tandem with `%columnlist` in insert statements.
|
|
48
|
+
- `%fragment` - A `Fragment` obtained from a call to `fragment()`. Use this to compose SQL statements or fragments based on smaller fragments that were composed elsewhere. Note that the specifiers and arguments given to the fragment are respected, so this can be a way of decomposing large queries while still keeping locality of arguments to SQL fragments. Note that this format specifier can also accept `None` in which case it will output nothing. Use this for composing SQL statements with optional fragments.
|
|
49
|
+
- `%fragmentlist` - A sequence of fragments. Each entry in the sequence must conform to the same rules as `%fragment`. Note that much like individual fragments, an entry in this sequence can be `None` which means it will be skipped over when constructing the final SQL. Note that the argument itself cannot be `None`. Instead, use an empty list.
|
|
50
|
+
- `%statement` - A `Statement` obtained from a call to `statement()`. Use this to compose SQL statements or fragments based on complete inner statements that were composed elsewhere. Note that the specifiers and arguments given to the statement are respected, so this can be a way of decomposing large queries while still keeping locality of arguments to SQL statements. Note that this format specifier can also accept `None` in which case it will output nothing. Use this for composing SQL statements with optional inner statements. Note that if the statement itself is not `None`, the resulting SQL will get a semicolon appended to the end of the statement automatically.
|
|
51
|
+
- `%statementlist` - A sequence of statements. Each entry in the sequence must conform to the same rules as `%statement`. Note that much like individual statements, an entry in this sequence can be `None` which means it will be skipped over when constructing the final SQL. Note that the argument itself cannot be `None`. Instead, use an empty list. Note that each valid statement that gets inserted into the final SQL will have a semicolon appended to the end of the statement automatically.
|
|
52
|
+
- `%inlist` - An iterable of values intended to be used in `IN` queries. SQL `IN` queries are not order-specific, so this specifier takes any iterable, such as a list or a set. The values are passed to the underlying SQL engine as bind parameters. Note that if an empty iterable is provided, the SQL keyword `NULL` is instead emitted. This means it is safe to write queries such as `WHERE id IN (%inlist)` without checking to see if the list contains at least one element. **WARNING**: Substituting `NULL` can have the side effect of matching `NULL` in columns that are nullable, so it is best to use this to query groups of IDs or other columns that are not nullable.
|
|
53
|
+
- `%andlist` - An iterable of fragments that will be joined together with the SQL `AND` keyword. Each entry in the iterable must conform to the same rules as `%fragment`. SQL boolean logic is not order-specific so this specifier takes any iterable, such as a list of a set. Order is however respected for iterables that have a defined order. Much like `%fragment` individual entries can be `None` in which case they are filtered out before emitting the final SQL. If the resulting filtered iterable has no entries, the SQL keyword `TRUE` will instead be emitted. Use this to construct narrowing queries such as `SELECT * FROM table WHERE %andlist` where each fragment filters out one or more rows. Each additional fragment added to the list will further constrain the rows found, so for logical consistency specifying zero fragments should result in all rows being selected. The resulting SQL will parenthesize all fragments, so it is safe to use this in conjunction with fragments that include `%andlist` or `%orlist` specifiers or their own.
|
|
54
|
+
- `%orlist` - An iterable of fragments that will be joined together with the SQL `OR` keyword. Each entry in the iterable must conform to the same rules as `%fragment`. SQL boolean logic is not order-specific so this specifier takes any iterable, such as a list of a set. Order is however respected for iterables that have a defined order. Much like `%fragment` individual entries can be `None` in which case they are filtered out before emitting the final SQL. If the resulting filtered iterable has no entries, the SQL keyword `FALSE` will instead be emitted. Use this to construct widening queries such as `SELECT * FROM table WHERE %orlist` where each fragment matches one or more rows. Each additional fragment added to the list will further include rows, so for logical consistency specifying zero fragments should result in no rows being selected. The resulting SQL will parenthesize all fragments, so it is safe to use this in conjunction with fragments that include `%andlist` or `%orlist` specifiers of their own.
|
|
55
|
+
|
|
56
|
+
### Usage Examples
|
|
57
|
+
|
|
58
|
+
First up, we have a simple statement.
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from sqlfragments import statement
|
|
62
|
+
|
|
63
|
+
sql, params = statement("SELECT * FROM table").to_sqlalchemy()
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
As you might expect, this results in an identical SQL statement being emitted with an empty param object. These are suitable for passing to SQLAlchemy's `execute()` function similar to `session.execute(text(sql), params)`. On its own, this is fairly useless, but if your type checker supports `LiteralString` this will at least type check okay and prevent future modifications from introducing potential injection exploits.
|
|
67
|
+
|
|
68
|
+
Then, we have a slightly more complex query.
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from sqlfragments import statement
|
|
72
|
+
|
|
73
|
+
def insert(name: str) -> None:
|
|
74
|
+
sql, params = statement(
|
|
75
|
+
"INSERT INTO table (`name`) VALUES (%value)",
|
|
76
|
+
name,
|
|
77
|
+
).to_sqlalchemy()
|
|
78
|
+
session.execute(text(sql), params)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
This does exactly what it looks like as well. Note that we have a `%value` specifier. Under the hood, this will convert the SQL to a parameterized query and the name variable will be set as the parameter. Any type that is supported by SQLAlchemy can be used here as the parameter to the `%value` specifier. Because this uses bind parameters under the hood it is safe from various classes of SQL exploits.
|
|
82
|
+
|
|
83
|
+
Alternatively, you can use named specifiers.
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from sqlfragments import statement
|
|
87
|
+
|
|
88
|
+
def insert(name: str) -> None:
|
|
89
|
+
sql, params = statement(
|
|
90
|
+
"INSERT INTO table (`name`) VALUES (%value:val)",
|
|
91
|
+
val=name,
|
|
92
|
+
).to_sqlalchemy()
|
|
93
|
+
session.execute(text(sql), params)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
This doesn't have many clear advantages in the simple example presented. However, it allows you to re-use arguments for multiple specifier positions. Note that all specifiers can take an optional name, not just value specifiers.
|
|
97
|
+
|
|
98
|
+
Next, we have an example of an optional fragment.
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from sqlfragments import statement
|
|
102
|
+
|
|
103
|
+
def lookup(
|
|
104
|
+
name: Optional[str] = None,
|
|
105
|
+
limit: Optional[int] = None,
|
|
106
|
+
offset: Optional[int] = None,
|
|
107
|
+
) -> List[Dict[str, object]]:
|
|
108
|
+
namequery = None
|
|
109
|
+
limitfragment = None
|
|
110
|
+
offsetfragment = None
|
|
111
|
+
|
|
112
|
+
if name:
|
|
113
|
+
namequery = fragment("WHERE name = %value", name)
|
|
114
|
+
if limit and limit > 0:
|
|
115
|
+
limitfragment = fragment("LIMIT %value", limit)
|
|
116
|
+
if offset and offset >= 0:
|
|
117
|
+
offsetfragment = fragment("OFFSET %value", offset)
|
|
118
|
+
|
|
119
|
+
sql, params = statement(
|
|
120
|
+
"SELECT * FROM table %fragment:name %fragment:limit %fragment:offset",
|
|
121
|
+
name=namequery,
|
|
122
|
+
limit=limitfragment,
|
|
123
|
+
offset=offsetfragment,
|
|
124
|
+
)
|
|
125
|
+
return list(session.execute(text(sql), params).mappings())
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Note that while this is slightly contrived, it still shows off the power of optional fragments as well as format specifier names. Callers to lookup can pass as many or as few arguments as they want and the resulting SQL will be correct in all cases. You do not need to keep a separate SQL string and parameter map to pass to `session.execute()` and you don't have to worry about what order to concatenate SQL in. Bind parameters are still used under the hood for all values, meaning that this function is user-input safe.
|
|
129
|
+
|
|
130
|
+
Finally, we show off an example of building a more complex query from optional filters.
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from sqlfragments import statement
|
|
134
|
+
|
|
135
|
+
def filter(
|
|
136
|
+
*,
|
|
137
|
+
name: Optional[str] = None,
|
|
138
|
+
ids: Optional[Iterable[int]] = None,
|
|
139
|
+
minAge: Optional[int] = None,
|
|
140
|
+
maxAge: Optional[int] = None,
|
|
141
|
+
) -> Statement:
|
|
142
|
+
filters: List[Fragment] = []
|
|
143
|
+
|
|
144
|
+
if name:
|
|
145
|
+
filters.append(fragment("name = %value", name))
|
|
146
|
+
if ids:
|
|
147
|
+
filters.append(fragment("id IN (%inlist)", ids))
|
|
148
|
+
if minAge is not None:
|
|
149
|
+
filters.append(fragment("age >= %value", minAge))
|
|
150
|
+
if maxAge is not None:
|
|
151
|
+
filters.append(fragment("age <= %value", maxAge))
|
|
152
|
+
|
|
153
|
+
return statement("SELECT * FROM user WHERE %andlist", filters)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
This constructs a `filter()` function which returns a valid SQL statement that will select all rows which are constrained by zero or more of the provided filters. Note that this is logically consistent. If you specify no filters, you would expect no rows to be filtered out. Under the hood this makes a SQL statement like `SELECT * FROM user WHERE TRUE`. Each additional filter supplies additional constraints. You could select users from a list of IDs who are at least 18 years old, select users who are at most 16 with the name "John", or any other combination. Note also that when given, the ID list is inclusive, meaning if you passed a list of no IDs you would expect to get back no rows when you run the query.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
SQL fragments are a way to keep chunks of SQL and any parameterized arguments together when building queries without an ORM. They take advantage of Python's ability to specify a literal string as an argument to ensure that any potential injection is flagged by the type checker. They include logically consistent ways of creating AND and OR filters for data fetching, sidestep the problem in MySQL and Postgres where an empty list in an IN query causes a syntax error, and provide an easy way to optionally provide fragments to a larger SQL statement. They also allow you to keep parameters local to the fragment of SQL that you are building.
|
|
2
|
+
|
|
3
|
+
Originally inspired by a similar library in PHP/Hack, I've used or built a version of this at the last several jobs I've worked at in various languages. Unfortunately, `mypy` does not currently support `LiteralString` type checking so this library cannot enable lint-time checks against possible SQL injection when using `mypy` as your type checking system. It's on you to only provide literal strings to the first parameter of `fragment()` in this case, but this is no different than using `text()` in sqlalchemy.
|
|
4
|
+
|
|
5
|
+
### API
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
def statement(sql: LiteralString, *args: object, **kwargs: object) -> "Statement":
|
|
9
|
+
...
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Parses a SQL query with optional specifiers, binding those specifiers to the arguments presented. Returns a `Statement` which can be used with the `%statement` or `%statementlist` specifiers in another statement or fragment. The `Statement` class also has a `to_sqlalchemy()` method which returns a tuple of SQLAlchemy-compatible SQL and a dictionary of bind parameters, suitable for passing to SQLAlchemy's `execute()` function.
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
def fragment(sql: LiteralString, *args: object, **kwargs: object) -> "Fragment":
|
|
16
|
+
...
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Parses a fragment of SQL with optional specifiers, binding those specifiers to the arguments presented. Returns a `Fragment` which can be used with the `%fragment`, `%fragmentlist`, `%andlist` or `%orlist` specifiers in another statement or fragment. Fragments and Statements both support the full gamut of format specifiers.
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
class FragmentException(Exception):
|
|
23
|
+
...
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Base exception type that all exceptions thrown when parsing SQL will originate from. Various exceptions exist for parsing exceptions that are nested underneath `ParseException` which is itself a subclass of `FragmentException`. Various exceptions exist for argument exceptions that are nested underneath `ArgumentException` which is itself a subclass of `FragmentException`. You should expect to get a subclass of `ParseException` if you provide SQL to either `statement()` or `fragment()` that contains an invalid or unparseable format specifier or specifier name. You should expect to get a subclass of `ArgumentException` if you provide invalid arguments for the given specifier, either as positional arguments or as kwargs to either `statement()` or `fragment()`.
|
|
27
|
+
|
|
28
|
+
### Format Specifiers
|
|
29
|
+
|
|
30
|
+
- `%table` - A table name. For simplicity, this expects the associated argument to be a string containing only alphanumeric characters and the characters `.` and `_`. Results in SQL that contains the table name escaped in back ticks.
|
|
31
|
+
- `%column` - A column name. For simplicity, this expects the associated argument to be a string containing only alphanumeric characters and the characters `.` and `_`. Results in SQL that contains the column name escaped in back ticks.
|
|
32
|
+
- `%columnlist` - A sequence of column names. Each entry in the sequence must conform to the same rules as `%column`. Note that since SQL is order-sensitive when specifying column names, this only takes sequence types, such as a list. Often used in tandem with `%valuelist` in insert statements.
|
|
33
|
+
- `%value` - A value, meant to be passed to the underlying SQL engine as a bind parameter. Can contain any valid python type that the underlying engine knows how to serialize, or `None`.
|
|
34
|
+
- `%valuelist` - A sequence of values. Each entry in the sequence must conform to the same rules as `%value`. Note that since SQL is order-sensitive when specifying lists of values, this only takes sequence types, such as a list. Often used in tandem with `%columnlist` in insert statements.
|
|
35
|
+
- `%fragment` - A `Fragment` obtained from a call to `fragment()`. Use this to compose SQL statements or fragments based on smaller fragments that were composed elsewhere. Note that the specifiers and arguments given to the fragment are respected, so this can be a way of decomposing large queries while still keeping locality of arguments to SQL fragments. Note that this format specifier can also accept `None` in which case it will output nothing. Use this for composing SQL statements with optional fragments.
|
|
36
|
+
- `%fragmentlist` - A sequence of fragments. Each entry in the sequence must conform to the same rules as `%fragment`. Note that much like individual fragments, an entry in this sequence can be `None` which means it will be skipped over when constructing the final SQL. Note that the argument itself cannot be `None`. Instead, use an empty list.
|
|
37
|
+
- `%statement` - A `Statement` obtained from a call to `statement()`. Use this to compose SQL statements or fragments based on complete inner statements that were composed elsewhere. Note that the specifiers and arguments given to the statement are respected, so this can be a way of decomposing large queries while still keeping locality of arguments to SQL statements. Note that this format specifier can also accept `None` in which case it will output nothing. Use this for composing SQL statements with optional inner statements. Note that if the statement itself is not `None`, the resulting SQL will get a semicolon appended to the end of the statement automatically.
|
|
38
|
+
- `%statementlist` - A sequence of statements. Each entry in the sequence must conform to the same rules as `%statement`. Note that much like individual statements, an entry in this sequence can be `None` which means it will be skipped over when constructing the final SQL. Note that the argument itself cannot be `None`. Instead, use an empty list. Note that each valid statement that gets inserted into the final SQL will have a semicolon appended to the end of the statement automatically.
|
|
39
|
+
- `%inlist` - An iterable of values intended to be used in `IN` queries. SQL `IN` queries are not order-specific, so this specifier takes any iterable, such as a list or a set. The values are passed to the underlying SQL engine as bind parameters. Note that if an empty iterable is provided, the SQL keyword `NULL` is instead emitted. This means it is safe to write queries such as `WHERE id IN (%inlist)` without checking to see if the list contains at least one element. **WARNING**: Substituting `NULL` can have the side effect of matching `NULL` in columns that are nullable, so it is best to use this to query groups of IDs or other columns that are not nullable.
|
|
40
|
+
- `%andlist` - An iterable of fragments that will be joined together with the SQL `AND` keyword. Each entry in the iterable must conform to the same rules as `%fragment`. SQL boolean logic is not order-specific so this specifier takes any iterable, such as a list of a set. Order is however respected for iterables that have a defined order. Much like `%fragment` individual entries can be `None` in which case they are filtered out before emitting the final SQL. If the resulting filtered iterable has no entries, the SQL keyword `TRUE` will instead be emitted. Use this to construct narrowing queries such as `SELECT * FROM table WHERE %andlist` where each fragment filters out one or more rows. Each additional fragment added to the list will further constrain the rows found, so for logical consistency specifying zero fragments should result in all rows being selected. The resulting SQL will parenthesize all fragments, so it is safe to use this in conjunction with fragments that include `%andlist` or `%orlist` specifiers or their own.
|
|
41
|
+
- `%orlist` - An iterable of fragments that will be joined together with the SQL `OR` keyword. Each entry in the iterable must conform to the same rules as `%fragment`. SQL boolean logic is not order-specific so this specifier takes any iterable, such as a list of a set. Order is however respected for iterables that have a defined order. Much like `%fragment` individual entries can be `None` in which case they are filtered out before emitting the final SQL. If the resulting filtered iterable has no entries, the SQL keyword `FALSE` will instead be emitted. Use this to construct widening queries such as `SELECT * FROM table WHERE %orlist` where each fragment matches one or more rows. Each additional fragment added to the list will further include rows, so for logical consistency specifying zero fragments should result in no rows being selected. The resulting SQL will parenthesize all fragments, so it is safe to use this in conjunction with fragments that include `%andlist` or `%orlist` specifiers of their own.
|
|
42
|
+
|
|
43
|
+
### Usage Examples
|
|
44
|
+
|
|
45
|
+
First up, we have a simple statement.
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from sqlfragments import statement
|
|
49
|
+
|
|
50
|
+
sql, params = statement("SELECT * FROM table").to_sqlalchemy()
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
As you might expect, this results in an identical SQL statement being emitted with an empty param object. These are suitable for passing to SQLAlchemy's `execute()` function similar to `session.execute(text(sql), params)`. On its own, this is fairly useless, but if your type checker supports `LiteralString` this will at least type check okay and prevent future modifications from introducing potential injection exploits.
|
|
54
|
+
|
|
55
|
+
Then, we have a slightly more complex query.
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from sqlfragments import statement
|
|
59
|
+
|
|
60
|
+
def insert(name: str) -> None:
|
|
61
|
+
sql, params = statement(
|
|
62
|
+
"INSERT INTO table (`name`) VALUES (%value)",
|
|
63
|
+
name,
|
|
64
|
+
).to_sqlalchemy()
|
|
65
|
+
session.execute(text(sql), params)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
This does exactly what it looks like as well. Note that we have a `%value` specifier. Under the hood, this will convert the SQL to a parameterized query and the name variable will be set as the parameter. Any type that is supported by SQLAlchemy can be used here as the parameter to the `%value` specifier. Because this uses bind parameters under the hood it is safe from various classes of SQL exploits.
|
|
69
|
+
|
|
70
|
+
Alternatively, you can use named specifiers.
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from sqlfragments import statement
|
|
74
|
+
|
|
75
|
+
def insert(name: str) -> None:
|
|
76
|
+
sql, params = statement(
|
|
77
|
+
"INSERT INTO table (`name`) VALUES (%value:val)",
|
|
78
|
+
val=name,
|
|
79
|
+
).to_sqlalchemy()
|
|
80
|
+
session.execute(text(sql), params)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
This doesn't have many clear advantages in the simple example presented. However, it allows you to re-use arguments for multiple specifier positions. Note that all specifiers can take an optional name, not just value specifiers.
|
|
84
|
+
|
|
85
|
+
Next, we have an example of an optional fragment.
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from sqlfragments import statement
|
|
89
|
+
|
|
90
|
+
def lookup(
|
|
91
|
+
name: Optional[str] = None,
|
|
92
|
+
limit: Optional[int] = None,
|
|
93
|
+
offset: Optional[int] = None,
|
|
94
|
+
) -> List[Dict[str, object]]:
|
|
95
|
+
namequery = None
|
|
96
|
+
limitfragment = None
|
|
97
|
+
offsetfragment = None
|
|
98
|
+
|
|
99
|
+
if name:
|
|
100
|
+
namequery = fragment("WHERE name = %value", name)
|
|
101
|
+
if limit and limit > 0:
|
|
102
|
+
limitfragment = fragment("LIMIT %value", limit)
|
|
103
|
+
if offset and offset >= 0:
|
|
104
|
+
offsetfragment = fragment("OFFSET %value", offset)
|
|
105
|
+
|
|
106
|
+
sql, params = statement(
|
|
107
|
+
"SELECT * FROM table %fragment:name %fragment:limit %fragment:offset",
|
|
108
|
+
name=namequery,
|
|
109
|
+
limit=limitfragment,
|
|
110
|
+
offset=offsetfragment,
|
|
111
|
+
)
|
|
112
|
+
return list(session.execute(text(sql), params).mappings())
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Note that while this is slightly contrived, it still shows off the power of optional fragments as well as format specifier names. Callers to lookup can pass as many or as few arguments as they want and the resulting SQL will be correct in all cases. You do not need to keep a separate SQL string and parameter map to pass to `session.execute()` and you don't have to worry about what order to concatenate SQL in. Bind parameters are still used under the hood for all values, meaning that this function is user-input safe.
|
|
116
|
+
|
|
117
|
+
Finally, we show off an example of building a more complex query from optional filters.
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from sqlfragments import statement
|
|
121
|
+
|
|
122
|
+
def filter(
|
|
123
|
+
*,
|
|
124
|
+
name: Optional[str] = None,
|
|
125
|
+
ids: Optional[Iterable[int]] = None,
|
|
126
|
+
minAge: Optional[int] = None,
|
|
127
|
+
maxAge: Optional[int] = None,
|
|
128
|
+
) -> Statement:
|
|
129
|
+
filters: List[Fragment] = []
|
|
130
|
+
|
|
131
|
+
if name:
|
|
132
|
+
filters.append(fragment("name = %value", name))
|
|
133
|
+
if ids:
|
|
134
|
+
filters.append(fragment("id IN (%inlist)", ids))
|
|
135
|
+
if minAge is not None:
|
|
136
|
+
filters.append(fragment("age >= %value", minAge))
|
|
137
|
+
if maxAge is not None:
|
|
138
|
+
filters.append(fragment("age <= %value", maxAge))
|
|
139
|
+
|
|
140
|
+
return statement("SELECT * FROM user WHERE %andlist", filters)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
This constructs a `filter()` function which returns a valid SQL statement that will select all rows which are constrained by zero or more of the provided filters. Note that this is logically consistent. If you specify no filters, you would expect no rows to be filtered out. Under the hood this makes a SQL statement like `SELECT * FROM user WHERE TRUE`. Each additional filter supplies additional constraints. You could select users from a list of IDs who are at least 18 years old, select users who are at most 16 with the name "John", or any other combination. Note also that when given, the ID list is inclusive, meaning if you passed a list of no IDs you would expect to get back no rows when you run the query.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools >= 77.0.3"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sql-fragments"
|
|
7
|
+
version = "0.0.2"
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
authors = [
|
|
10
|
+
{name = "Jennifer Taylor", email = "dragonminded@dragonminded.com"},
|
|
11
|
+
]
|
|
12
|
+
maintainers = [
|
|
13
|
+
{name = "Jennifer Taylor", email = "dragonminded@dragonminded.com"},
|
|
14
|
+
]
|
|
15
|
+
description = "SQL Fragments library for building SQL queries."
|
|
16
|
+
readme = "README.md"
|
|
17
|
+
license = "Unlicense"
|
|
18
|
+
license-files = ["LICENSE"]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Homepage = "https://github.com/DragonMinded/sql-fragments"
|
|
22
|
+
|
|
23
|
+
[tool.setuptools]
|
|
24
|
+
packages = ["sqlfragments"]
|
|
25
|
+
|
|
26
|
+
[tool.setuptools.package-data]
|
|
27
|
+
"pkgname" = ["py.typed"]
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sql-fragments
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: SQL Fragments library for building SQL queries.
|
|
5
|
+
Author-email: Jennifer Taylor <dragonminded@dragonminded.com>
|
|
6
|
+
Maintainer-email: Jennifer Taylor <dragonminded@dragonminded.com>
|
|
7
|
+
License-Expression: Unlicense
|
|
8
|
+
Project-URL: Homepage, https://github.com/DragonMinded/sql-fragments
|
|
9
|
+
Requires-Python: >=3.11
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
SQL fragments are a way to keep chunks of SQL and any parameterized arguments together when building queries without an ORM. They take advantage of Python's ability to specify a literal string as an argument to ensure that any potential injection is flagged by the type checker. They include logically consistent ways of creating AND and OR filters for data fetching, sidestep the problem in MySQL and Postgres where an empty list in an IN query causes a syntax error, and provide an easy way to optionally provide fragments to a larger SQL statement. They also allow you to keep parameters local to the fragment of SQL that you are building.
|
|
15
|
+
|
|
16
|
+
Originally inspired by a similar library in PHP/Hack, I've used or built a version of this at the last several jobs I've worked at in various languages. Unfortunately, `mypy` does not currently support `LiteralString` type checking so this library cannot enable lint-time checks against possible SQL injection when using `mypy` as your type checking system. It's on you to only provide literal strings to the first parameter of `fragment()` in this case, but this is no different than using `text()` in sqlalchemy.
|
|
17
|
+
|
|
18
|
+
### API
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
def statement(sql: LiteralString, *args: object, **kwargs: object) -> "Statement":
|
|
22
|
+
...
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Parses a SQL query with optional specifiers, binding those specifiers to the arguments presented. Returns a `Statement` which can be used with the `%statement` or `%statementlist` specifiers in another statement or fragment. The `Statement` class also has a `to_sqlalchemy()` method which returns a tuple of SQLAlchemy-compatible SQL and a dictionary of bind parameters, suitable for passing to SQLAlchemy's `execute()` function.
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
def fragment(sql: LiteralString, *args: object, **kwargs: object) -> "Fragment":
|
|
29
|
+
...
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Parses a fragment of SQL with optional specifiers, binding those specifiers to the arguments presented. Returns a `Fragment` which can be used with the `%fragment`, `%fragmentlist`, `%andlist` or `%orlist` specifiers in another statement or fragment. Fragments and Statements both support the full gamut of format specifiers.
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
class FragmentException(Exception):
|
|
36
|
+
...
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Base exception type that all exceptions thrown when parsing SQL will originate from. Various exceptions exist for parsing exceptions that are nested underneath `ParseException` which is itself a subclass of `FragmentException`. Various exceptions exist for argument exceptions that are nested underneath `ArgumentException` which is itself a subclass of `FragmentException`. You should expect to get a subclass of `ParseException` if you provide SQL to either `statement()` or `fragment()` that contains an invalid or unparseable format specifier or specifier name. You should expect to get a subclass of `ArgumentException` if you provide invalid arguments for the given specifier, either as positional arguments or as kwargs to either `statement()` or `fragment()`.
|
|
40
|
+
|
|
41
|
+
### Format Specifiers
|
|
42
|
+
|
|
43
|
+
- `%table` - A table name. For simplicity, this expects the associated argument to be a string containing only alphanumeric characters and the characters `.` and `_`. Results in SQL that contains the table name escaped in back ticks.
|
|
44
|
+
- `%column` - A column name. For simplicity, this expects the associated argument to be a string containing only alphanumeric characters and the characters `.` and `_`. Results in SQL that contains the column name escaped in back ticks.
|
|
45
|
+
- `%columnlist` - A sequence of column names. Each entry in the sequence must conform to the same rules as `%column`. Note that since SQL is order-sensitive when specifying column names, this only takes sequence types, such as a list. Often used in tandem with `%valuelist` in insert statements.
|
|
46
|
+
- `%value` - A value, meant to be passed to the underlying SQL engine as a bind parameter. Can contain any valid python type that the underlying engine knows how to serialize, or `None`.
|
|
47
|
+
- `%valuelist` - A sequence of values. Each entry in the sequence must conform to the same rules as `%value`. Note that since SQL is order-sensitive when specifying lists of values, this only takes sequence types, such as a list. Often used in tandem with `%columnlist` in insert statements.
|
|
48
|
+
- `%fragment` - A `Fragment` obtained from a call to `fragment()`. Use this to compose SQL statements or fragments based on smaller fragments that were composed elsewhere. Note that the specifiers and arguments given to the fragment are respected, so this can be a way of decomposing large queries while still keeping locality of arguments to SQL fragments. Note that this format specifier can also accept `None` in which case it will output nothing. Use this for composing SQL statements with optional fragments.
|
|
49
|
+
- `%fragmentlist` - A sequence of fragments. Each entry in the sequence must conform to the same rules as `%fragment`. Note that much like individual fragments, an entry in this sequence can be `None` which means it will be skipped over when constructing the final SQL. Note that the argument itself cannot be `None`. Instead, use an empty list.
|
|
50
|
+
- `%statement` - A `Statement` obtained from a call to `statement()`. Use this to compose SQL statements or fragments based on complete inner statements that were composed elsewhere. Note that the specifiers and arguments given to the statement are respected, so this can be a way of decomposing large queries while still keeping locality of arguments to SQL statements. Note that this format specifier can also accept `None` in which case it will output nothing. Use this for composing SQL statements with optional inner statements. Note that if the statement itself is not `None`, the resulting SQL will get a semicolon appended to the end of the statement automatically.
|
|
51
|
+
- `%statementlist` - A sequence of statements. Each entry in the sequence must conform to the same rules as `%statement`. Note that much like individual statements, an entry in this sequence can be `None` which means it will be skipped over when constructing the final SQL. Note that the argument itself cannot be `None`. Instead, use an empty list. Note that each valid statement that gets inserted into the final SQL will have a semicolon appended to the end of the statement automatically.
|
|
52
|
+
- `%inlist` - An iterable of values intended to be used in `IN` queries. SQL `IN` queries are not order-specific, so this specifier takes any iterable, such as a list or a set. The values are passed to the underlying SQL engine as bind parameters. Note that if an empty iterable is provided, the SQL keyword `NULL` is instead emitted. This means it is safe to write queries such as `WHERE id IN (%inlist)` without checking to see if the list contains at least one element. **WARNING**: Substituting `NULL` can have the side effect of matching `NULL` in columns that are nullable, so it is best to use this to query groups of IDs or other columns that are not nullable.
|
|
53
|
+
- `%andlist` - An iterable of fragments that will be joined together with the SQL `AND` keyword. Each entry in the iterable must conform to the same rules as `%fragment`. SQL boolean logic is not order-specific so this specifier takes any iterable, such as a list of a set. Order is however respected for iterables that have a defined order. Much like `%fragment` individual entries can be `None` in which case they are filtered out before emitting the final SQL. If the resulting filtered iterable has no entries, the SQL keyword `TRUE` will instead be emitted. Use this to construct narrowing queries such as `SELECT * FROM table WHERE %andlist` where each fragment filters out one or more rows. Each additional fragment added to the list will further constrain the rows found, so for logical consistency specifying zero fragments should result in all rows being selected. The resulting SQL will parenthesize all fragments, so it is safe to use this in conjunction with fragments that include `%andlist` or `%orlist` specifiers or their own.
|
|
54
|
+
- `%orlist` - An iterable of fragments that will be joined together with the SQL `OR` keyword. Each entry in the iterable must conform to the same rules as `%fragment`. SQL boolean logic is not order-specific so this specifier takes any iterable, such as a list of a set. Order is however respected for iterables that have a defined order. Much like `%fragment` individual entries can be `None` in which case they are filtered out before emitting the final SQL. If the resulting filtered iterable has no entries, the SQL keyword `FALSE` will instead be emitted. Use this to construct widening queries such as `SELECT * FROM table WHERE %orlist` where each fragment matches one or more rows. Each additional fragment added to the list will further include rows, so for logical consistency specifying zero fragments should result in no rows being selected. The resulting SQL will parenthesize all fragments, so it is safe to use this in conjunction with fragments that include `%andlist` or `%orlist` specifiers of their own.
|
|
55
|
+
|
|
56
|
+
### Usage Examples
|
|
57
|
+
|
|
58
|
+
First up, we have a simple statement.
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from sqlfragments import statement
|
|
62
|
+
|
|
63
|
+
sql, params = statement("SELECT * FROM table").to_sqlalchemy()
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
As you might expect, this results in an identical SQL statement being emitted with an empty param object. These are suitable for passing to SQLAlchemy's `execute()` function similar to `session.execute(text(sql), params)`. On its own, this is fairly useless, but if your type checker supports `LiteralString` this will at least type check okay and prevent future modifications from introducing potential injection exploits.
|
|
67
|
+
|
|
68
|
+
Then, we have a slightly more complex query.
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from sqlfragments import statement
|
|
72
|
+
|
|
73
|
+
def insert(name: str) -> None:
|
|
74
|
+
sql, params = statement(
|
|
75
|
+
"INSERT INTO table (`name`) VALUES (%value)",
|
|
76
|
+
name,
|
|
77
|
+
).to_sqlalchemy()
|
|
78
|
+
session.execute(text(sql), params)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
This does exactly what it looks like as well. Note that we have a `%value` specifier. Under the hood, this will convert the SQL to a parameterized query and the name variable will be set as the parameter. Any type that is supported by SQLAlchemy can be used here as the parameter to the `%value` specifier. Because this uses bind parameters under the hood it is safe from various classes of SQL exploits.
|
|
82
|
+
|
|
83
|
+
Alternatively, you can use named specifiers.
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from sqlfragments import statement
|
|
87
|
+
|
|
88
|
+
def insert(name: str) -> None:
|
|
89
|
+
sql, params = statement(
|
|
90
|
+
"INSERT INTO table (`name`) VALUES (%value:val)",
|
|
91
|
+
val=name,
|
|
92
|
+
).to_sqlalchemy()
|
|
93
|
+
session.execute(text(sql), params)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
This doesn't have many clear advantages in the simple example presented. However, it allows you to re-use arguments for multiple specifier positions. Note that all specifiers can take an optional name, not just value specifiers.
|
|
97
|
+
|
|
98
|
+
Next, we have an example of an optional fragment.
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from sqlfragments import statement
|
|
102
|
+
|
|
103
|
+
def lookup(
|
|
104
|
+
name: Optional[str] = None,
|
|
105
|
+
limit: Optional[int] = None,
|
|
106
|
+
offset: Optional[int] = None,
|
|
107
|
+
) -> List[Dict[str, object]]:
|
|
108
|
+
namequery = None
|
|
109
|
+
limitfragment = None
|
|
110
|
+
offsetfragment = None
|
|
111
|
+
|
|
112
|
+
if name:
|
|
113
|
+
namequery = fragment("WHERE name = %value", name)
|
|
114
|
+
if limit and limit > 0:
|
|
115
|
+
limitfragment = fragment("LIMIT %value", limit)
|
|
116
|
+
if offset and offset >= 0:
|
|
117
|
+
offsetfragment = fragment("OFFSET %value", offset)
|
|
118
|
+
|
|
119
|
+
sql, params = statement(
|
|
120
|
+
"SELECT * FROM table %fragment:name %fragment:limit %fragment:offset",
|
|
121
|
+
name=namequery,
|
|
122
|
+
limit=limitfragment,
|
|
123
|
+
offset=offsetfragment,
|
|
124
|
+
)
|
|
125
|
+
return list(session.execute(text(sql), params).mappings())
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Note that while this is slightly contrived, it still shows off the power of optional fragments as well as format specifier names. Callers to lookup can pass as many or as few arguments as they want and the resulting SQL will be correct in all cases. You do not need to keep a separate SQL string and parameter map to pass to `session.execute()` and you don't have to worry about what order to concatenate SQL in. Bind parameters are still used under the hood for all values, meaning that this function is user-input safe.
|
|
129
|
+
|
|
130
|
+
Finally, we show off an example of building a more complex query from optional filters.
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from sqlfragments import statement
|
|
134
|
+
|
|
135
|
+
def filter(
|
|
136
|
+
*,
|
|
137
|
+
name: Optional[str] = None,
|
|
138
|
+
ids: Optional[Iterable[int]] = None,
|
|
139
|
+
minAge: Optional[int] = None,
|
|
140
|
+
maxAge: Optional[int] = None,
|
|
141
|
+
) -> Statement:
|
|
142
|
+
filters: List[Fragment] = []
|
|
143
|
+
|
|
144
|
+
if name:
|
|
145
|
+
filters.append(fragment("name = %value", name))
|
|
146
|
+
if ids:
|
|
147
|
+
filters.append(fragment("id IN (%inlist)", ids))
|
|
148
|
+
if minAge is not None:
|
|
149
|
+
filters.append(fragment("age >= %value", minAge))
|
|
150
|
+
if maxAge is not None:
|
|
151
|
+
filters.append(fragment("age <= %value", maxAge))
|
|
152
|
+
|
|
153
|
+
return statement("SELECT * FROM user WHERE %andlist", filters)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
This constructs a `filter()` function which returns a valid SQL statement that will select all rows which are constrained by zero or more of the provided filters. Note that this is logically consistent. If you specify no filters, you would expect no rows to be filtered out. Under the hood this makes a SQL statement like `SELECT * FROM user WHERE TRUE`. Each additional filter supplies additional constraints. You could select users from a list of IDs who are at least 18 years old, select users who are at most 16 with the name "John", or any other combination. Note also that when given, the ID list is inclusive, meaning if you passed a list of no IDs you would expect to get back no rows when you run the query.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
sql_fragments.egg-info/PKG-INFO
|
|
5
|
+
sql_fragments.egg-info/SOURCES.txt
|
|
6
|
+
sql_fragments.egg-info/dependency_links.txt
|
|
7
|
+
sql_fragments.egg-info/top_level.txt
|
|
8
|
+
sqlfragments/__init__.py
|
|
9
|
+
sqlfragments/_fragments.py
|
|
10
|
+
sqlfragments/py.typed
|
|
11
|
+
tests/test_params.py
|
|
12
|
+
tests/test_sqlalchemy.py
|
|
13
|
+
tests/test_tokenize.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sqlfragments
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from ._fragments import (
|
|
2
|
+
FragmentException,
|
|
3
|
+
ParseException,
|
|
4
|
+
InvalidSpecifier,
|
|
5
|
+
InvalidName,
|
|
6
|
+
ArgumentException,
|
|
7
|
+
MissingArgument,
|
|
8
|
+
InvalidArgument,
|
|
9
|
+
Fragment,
|
|
10
|
+
fragment,
|
|
11
|
+
Statement,
|
|
12
|
+
statement,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"FragmentException",
|
|
17
|
+
"ParseException",
|
|
18
|
+
"InvalidSpecifier",
|
|
19
|
+
"InvalidName",
|
|
20
|
+
"ArgumentException",
|
|
21
|
+
"MissingArgument",
|
|
22
|
+
"InvalidArgument",
|
|
23
|
+
"Fragment",
|
|
24
|
+
"fragment",
|
|
25
|
+
"Statement",
|
|
26
|
+
"statement",
|
|
27
|
+
]
|