sqlfactory 1.0.0__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.
- sqlfactory-1.0.0/LICENSE +21 -0
- sqlfactory-1.0.0/PKG-INFO +251 -0
- sqlfactory-1.0.0/README.md +231 -0
- sqlfactory-1.0.0/pyproject.toml +40 -0
- sqlfactory-1.0.0/sqlfactory/__init__.py +11 -0
- sqlfactory-1.0.0/sqlfactory/condition/__init__.py +7 -0
- sqlfactory-1.0.0/sqlfactory/condition/base.py +168 -0
- sqlfactory-1.0.0/sqlfactory/condition/between.py +54 -0
- sqlfactory-1.0.0/sqlfactory/condition/in_condition.py +126 -0
- sqlfactory-1.0.0/sqlfactory/condition/like.py +49 -0
- sqlfactory-1.0.0/sqlfactory/condition/simple.py +149 -0
- sqlfactory-1.0.0/sqlfactory/delete/__init__.py +3 -0
- sqlfactory-1.0.0/sqlfactory/delete/delete.py +64 -0
- sqlfactory-1.0.0/sqlfactory/entities.py +213 -0
- sqlfactory-1.0.0/sqlfactory/execute.py +158 -0
- sqlfactory-1.0.0/sqlfactory/func/__init__.py +0 -0
- sqlfactory-1.0.0/sqlfactory/func/agg.py +79 -0
- sqlfactory-1.0.0/sqlfactory/func/base.py +37 -0
- sqlfactory-1.0.0/sqlfactory/func/control.py +27 -0
- sqlfactory-1.0.0/sqlfactory/func/datetime.py +637 -0
- sqlfactory-1.0.0/sqlfactory/func/enc.py +177 -0
- sqlfactory-1.0.0/sqlfactory/func/info.py +159 -0
- sqlfactory-1.0.0/sqlfactory/func/misc.py +142 -0
- sqlfactory-1.0.0/sqlfactory/func/numeric.py +338 -0
- sqlfactory-1.0.0/sqlfactory/func/str.py +251 -0
- sqlfactory-1.0.0/sqlfactory/insert/__init__.py +4 -0
- sqlfactory-1.0.0/sqlfactory/insert/insert.py +164 -0
- sqlfactory-1.0.0/sqlfactory/insert/values.py +16 -0
- sqlfactory-1.0.0/sqlfactory/logger.py +5 -0
- sqlfactory-1.0.0/sqlfactory/mixins/__init__.py +5 -0
- sqlfactory-1.0.0/sqlfactory/mixins/limit.py +133 -0
- sqlfactory-1.0.0/sqlfactory/mixins/order.py +81 -0
- sqlfactory-1.0.0/sqlfactory/mixins/where.py +27 -0
- sqlfactory-1.0.0/sqlfactory/select/__init__.py +6 -0
- sqlfactory-1.0.0/sqlfactory/select/aliased.py +35 -0
- sqlfactory-1.0.0/sqlfactory/select/column_list.py +70 -0
- sqlfactory-1.0.0/sqlfactory/select/join.py +84 -0
- sqlfactory-1.0.0/sqlfactory/select/select.py +200 -0
- sqlfactory-1.0.0/sqlfactory/statement.py +114 -0
- sqlfactory-1.0.0/sqlfactory/update/__init__.py +3 -0
- sqlfactory-1.0.0/sqlfactory/update/update.py +151 -0
sqlfactory-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [year] [fullname]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: sqlfactory
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Convenient classes for building SQL queries in Python. Main purpose of this library is to ease construction of SQL queries in Python code. It is not an ORM (and don't intend to be), just a plain SQL query builder with syntax as similar to actual SQL as possible.
|
|
5
|
+
Author: Michal Kuchta
|
|
6
|
+
Author-email: niximor@gmail.com
|
|
7
|
+
Requires-Python: >=3.11,<4.0
|
|
8
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: SQL
|
|
16
|
+
Classifier: Topic :: Database
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# SQLFactory
|
|
21
|
+
|
|
22
|
+

|
|
23
|
+

|
|
24
|
+

|
|
25
|
+
|
|
26
|
+
A zero-dependency SQL builder library!
|
|
27
|
+
|
|
28
|
+
Convenient classes for building SQL queries in Python. Main purpose of this library is to ease construction of SQL
|
|
29
|
+
queries in Python code. It is not an ORM (and don't intend to be), just a plain SQL query builder with syntax as
|
|
30
|
+
similar to actual SQL as possible.
|
|
31
|
+
|
|
32
|
+
Let's have look at few examples, because examples tell much more than a thousand words:
|
|
33
|
+
|
|
34
|
+
TL;DR:
|
|
35
|
+
```python
|
|
36
|
+
sql = Select("column1", "column2", table="books").where(Eq("column1", "value") & In("column2", [1, 2, 3]))
|
|
37
|
+
|
|
38
|
+
# Will do the args magic for you
|
|
39
|
+
sql.execute(cursor)
|
|
40
|
+
|
|
41
|
+
# OR
|
|
42
|
+
cursor.execute(
|
|
43
|
+
str(sql), # Produces SQL string with value placeholders as %s
|
|
44
|
+
sql.args # Returns arguments for the placeholders
|
|
45
|
+
)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
All the following examples are self-contained. You can copy-paste them into your Python code and they will work.
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
Let's see how to build a SELECT statement like this:
|
|
53
|
+
|
|
54
|
+
```sql
|
|
55
|
+
SELECT column1, column2, column3 FROM books WHERE column1 = 'value' AND column2 IN (1, 2, 3);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
All the following examples are equivalent and produce the same SQL query:
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from sqlfactory import SELECT, Table, Select, And, Eq, In
|
|
62
|
+
|
|
63
|
+
# The most naive and most explicit approach
|
|
64
|
+
Select("column1", "column2", "column3", table="books", where=And(Eq("column1", "value"), In("column2", [1, 2, 3])))
|
|
65
|
+
|
|
66
|
+
# A little more like a SQL:
|
|
67
|
+
SELECT("column1", "column2", "column3", table="books")
|
|
68
|
+
.WHERE(Eq("column1", "value") & In("column2", [1, 2, 3]))
|
|
69
|
+
|
|
70
|
+
# A little more like a python, but still SQL:
|
|
71
|
+
books = Table("books")
|
|
72
|
+
SELECT(books.column1, books.column2, books.column3, table=books)
|
|
73
|
+
.WHERE((books.column1 == "value") & In(books.column2, [1, 2, 3]))
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
Inserts are simple, too:
|
|
79
|
+
|
|
80
|
+
```sql
|
|
81
|
+
INSERT INTO books (column1, column2, column3) VALUES ('value1', 'value2', 'value3'), ('value4', 'value5', 'value6');
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from sqlfactory import Insert, INSERT, Table
|
|
86
|
+
|
|
87
|
+
Insert.into("books")("column1", "column2", "column3").VALUES(
|
|
88
|
+
("value1", "value2", "value3"),
|
|
89
|
+
("value4", "value5", "value6")
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
# Of course, you can use Table object as well
|
|
93
|
+
books = Table("books")
|
|
94
|
+
INSERT.INTO(books)(books.column1, books.column2, books.column3).VALUES(
|
|
95
|
+
("value1", "value2", "value3"),
|
|
96
|
+
("value4", "value5", "value6")
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
# The INTO is not necessary, you can call INSERT constructor directly:
|
|
100
|
+
INSERT("books")("column1", "column2", "column3").VALUES(
|
|
101
|
+
("value1", "value2", "value3"),
|
|
102
|
+
("value4", "value5", "value6")
|
|
103
|
+
)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
Even updates (and in fact deletes, too):
|
|
109
|
+
|
|
110
|
+
```sql
|
|
111
|
+
UPDATE books SET column1 = 'value1', column2 = 'value2' WHERE column3 = 'value3';
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from sqlfactory import Update, Table, Eq
|
|
116
|
+
|
|
117
|
+
Update("books")
|
|
118
|
+
.set("column1", "value1")
|
|
119
|
+
.set("column2", "value2")
|
|
120
|
+
.where(Eq("column3", "value3"))
|
|
121
|
+
|
|
122
|
+
# Of course, you can use Table object as well
|
|
123
|
+
books = Table("books")
|
|
124
|
+
Update(books)
|
|
125
|
+
.set(books.column1, "value1")
|
|
126
|
+
.set(books.column2, "value2")
|
|
127
|
+
.where(books.column3 == "value3")
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
It might seem strange to have so many ways to do the same thing, but it's up to you to choose the one that fits your
|
|
131
|
+
style the best. The library is designed to be as flexible as possible. You can mix and match different styles in the same
|
|
132
|
+
codebase, or even in the same query, if you want, as long as it makes sense to you.
|
|
133
|
+
|
|
134
|
+
By leveraging Python code in constructing SQL, you can use all sorts of Python features to make building SQL an ease.
|
|
135
|
+
Consider list comprehensions for IN statement, building of complex WHERE clauses, dynamic select columns, call UPDATE
|
|
136
|
+
only if anything has changed, ... All of that and much more can be done without the hassle of building complex strings
|
|
137
|
+
together.
|
|
138
|
+
|
|
139
|
+
Let's have a look at a few more practical examples:
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from sqlfactory import Select, In, Direction, Eq, Column, SelectColumn
|
|
143
|
+
from dataclasses import dataclass
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@dataclass
|
|
147
|
+
class Book:
|
|
148
|
+
id: int
|
|
149
|
+
title: str
|
|
150
|
+
author: str
|
|
151
|
+
year: str
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def select_books_by_authors(c: DictCursor, authors: list[str], book_properties: set[str] = None, offset: int = 0,
|
|
155
|
+
limit: int = 10):
|
|
156
|
+
"""
|
|
157
|
+
Returns books written by specific authors. Returns list of books paginated by specified offset and limit, ordered
|
|
158
|
+
by book title and author name.
|
|
159
|
+
"""
|
|
160
|
+
|
|
161
|
+
if book_properties is None:
|
|
162
|
+
book_properties = {"title", "author", "year"}
|
|
163
|
+
|
|
164
|
+
property_column = {
|
|
165
|
+
"title": SelectColumn("books.title", alias="title"),
|
|
166
|
+
"author": SelectColumn("authors.name", alias="author"),
|
|
167
|
+
"year": SelectColumn("books.year", alias="year")
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
select = (
|
|
171
|
+
# Map dataclass attributes to SQL columns by using mapping table.
|
|
172
|
+
Select(*[property_column[book_property] for book_property in book_properties], table="books")
|
|
173
|
+
|
|
174
|
+
# As Eq expects firt argument to be column and second argument to be value, we need to provide hint, that
|
|
175
|
+
# authors.id is a column, not a value.
|
|
176
|
+
.join("authors", on=Eq("books.author", Column("authors.id")))
|
|
177
|
+
|
|
178
|
+
# In is intelligent, it will work even when authors list is empty (will produce False, which in turn will
|
|
179
|
+
# return empty result, as no author has been matched).
|
|
180
|
+
.where(In("authors.name", authors))
|
|
181
|
+
|
|
182
|
+
# Multiple ORDER BY columns is supported
|
|
183
|
+
.order_by("title", Direction.ASC)
|
|
184
|
+
.order_by("authors.name", Direction.ASC)
|
|
185
|
+
|
|
186
|
+
# Limit and offset are supported as well
|
|
187
|
+
.limit(offset, limit)
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
select.execute(c)
|
|
191
|
+
return [Book(**row) for row in c.fetchall()]
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
from sqlfactory import Update, Eq
|
|
196
|
+
from dataclasses import dataclass
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
@dataclass
|
|
200
|
+
class BookUpdate:
|
|
201
|
+
id: int
|
|
202
|
+
title: str = None
|
|
203
|
+
author: str = None
|
|
204
|
+
year: int = None
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def update_books(c: Cursor, books: list[BookUpdate]):
|
|
208
|
+
"""Update multiple books at once. Attributes that has None value won't be modified at all."""
|
|
209
|
+
for book in books:
|
|
210
|
+
update = Update("books", where=Eq("id", book.id))
|
|
211
|
+
|
|
212
|
+
if book.title is not None:
|
|
213
|
+
update.set("title", book.title)
|
|
214
|
+
if book.author is not None:
|
|
215
|
+
update.set("author", book.author)
|
|
216
|
+
if book.year is not None:
|
|
217
|
+
update.set("year", book.year)
|
|
218
|
+
|
|
219
|
+
# It can even be done as one-liner, but it gets ugly pretty quickly, so it's not recommended for readability:
|
|
220
|
+
# list(map(update.set, [(attr, getattr(book, attr)) for attr in ["title", "author", "year"] if getattr(book, attr) is not None]))
|
|
221
|
+
|
|
222
|
+
# Will be executed only if any of the columns should be updated.
|
|
223
|
+
update.execute(c)
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Installation
|
|
227
|
+
|
|
228
|
+
Just install it from PyPi and use:
|
|
229
|
+
|
|
230
|
+
```shell
|
|
231
|
+
pip install py-SQLBuilder
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## Maturity
|
|
235
|
+
|
|
236
|
+
This library is still very new, but grew from multiple projects where it gradually evolved. So it is already used in
|
|
237
|
+
production environment successfully. But as always, bugs are expected to be found. On the other hand, the library
|
|
238
|
+
contains large test suite with 100% code coverage, so it should be quite stable. If you find any bug, please report it.
|
|
239
|
+
|
|
240
|
+
Implemented SQL features are not complete set of what SQL offers, they are added as-needed. Feel free to open a merge
|
|
241
|
+
request if you find missing feature that you need.
|
|
242
|
+
|
|
243
|
+
As we are mainly targeting MySQL / MariaDB, there are some extra features that are on top of SQL standard, that are
|
|
244
|
+
implemented in the builder. But the library should work with any database that supports standard SQL, when you won't
|
|
245
|
+
use features that are extensions to the SQL standard.
|
|
246
|
+
|
|
247
|
+
## Contributing
|
|
248
|
+
|
|
249
|
+
Contributions are always welcome. Just open an issue or a merge request. Keep in mind, that this is not an ORM. So no
|
|
250
|
+
sessions, no transactions, no lazy loading, no relations, no schema management, no migrations, no database creation.
|
|
251
|
+
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# SQLFactory
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
A zero-dependency SQL builder library!
|
|
8
|
+
|
|
9
|
+
Convenient classes for building SQL queries in Python. Main purpose of this library is to ease construction of SQL
|
|
10
|
+
queries in Python code. It is not an ORM (and don't intend to be), just a plain SQL query builder with syntax as
|
|
11
|
+
similar to actual SQL as possible.
|
|
12
|
+
|
|
13
|
+
Let's have look at few examples, because examples tell much more than a thousand words:
|
|
14
|
+
|
|
15
|
+
TL;DR:
|
|
16
|
+
```python
|
|
17
|
+
sql = Select("column1", "column2", table="books").where(Eq("column1", "value") & In("column2", [1, 2, 3]))
|
|
18
|
+
|
|
19
|
+
# Will do the args magic for you
|
|
20
|
+
sql.execute(cursor)
|
|
21
|
+
|
|
22
|
+
# OR
|
|
23
|
+
cursor.execute(
|
|
24
|
+
str(sql), # Produces SQL string with value placeholders as %s
|
|
25
|
+
sql.args # Returns arguments for the placeholders
|
|
26
|
+
)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
All the following examples are self-contained. You can copy-paste them into your Python code and they will work.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
Let's see how to build a SELECT statement like this:
|
|
34
|
+
|
|
35
|
+
```sql
|
|
36
|
+
SELECT column1, column2, column3 FROM books WHERE column1 = 'value' AND column2 IN (1, 2, 3);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
All the following examples are equivalent and produce the same SQL query:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from sqlfactory import SELECT, Table, Select, And, Eq, In
|
|
43
|
+
|
|
44
|
+
# The most naive and most explicit approach
|
|
45
|
+
Select("column1", "column2", "column3", table="books", where=And(Eq("column1", "value"), In("column2", [1, 2, 3])))
|
|
46
|
+
|
|
47
|
+
# A little more like a SQL:
|
|
48
|
+
SELECT("column1", "column2", "column3", table="books")
|
|
49
|
+
.WHERE(Eq("column1", "value") & In("column2", [1, 2, 3]))
|
|
50
|
+
|
|
51
|
+
# A little more like a python, but still SQL:
|
|
52
|
+
books = Table("books")
|
|
53
|
+
SELECT(books.column1, books.column2, books.column3, table=books)
|
|
54
|
+
.WHERE((books.column1 == "value") & In(books.column2, [1, 2, 3]))
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
Inserts are simple, too:
|
|
60
|
+
|
|
61
|
+
```sql
|
|
62
|
+
INSERT INTO books (column1, column2, column3) VALUES ('value1', 'value2', 'value3'), ('value4', 'value5', 'value6');
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from sqlfactory import Insert, INSERT, Table
|
|
67
|
+
|
|
68
|
+
Insert.into("books")("column1", "column2", "column3").VALUES(
|
|
69
|
+
("value1", "value2", "value3"),
|
|
70
|
+
("value4", "value5", "value6")
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Of course, you can use Table object as well
|
|
74
|
+
books = Table("books")
|
|
75
|
+
INSERT.INTO(books)(books.column1, books.column2, books.column3).VALUES(
|
|
76
|
+
("value1", "value2", "value3"),
|
|
77
|
+
("value4", "value5", "value6")
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# The INTO is not necessary, you can call INSERT constructor directly:
|
|
81
|
+
INSERT("books")("column1", "column2", "column3").VALUES(
|
|
82
|
+
("value1", "value2", "value3"),
|
|
83
|
+
("value4", "value5", "value6")
|
|
84
|
+
)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
Even updates (and in fact deletes, too):
|
|
90
|
+
|
|
91
|
+
```sql
|
|
92
|
+
UPDATE books SET column1 = 'value1', column2 = 'value2' WHERE column3 = 'value3';
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from sqlfactory import Update, Table, Eq
|
|
97
|
+
|
|
98
|
+
Update("books")
|
|
99
|
+
.set("column1", "value1")
|
|
100
|
+
.set("column2", "value2")
|
|
101
|
+
.where(Eq("column3", "value3"))
|
|
102
|
+
|
|
103
|
+
# Of course, you can use Table object as well
|
|
104
|
+
books = Table("books")
|
|
105
|
+
Update(books)
|
|
106
|
+
.set(books.column1, "value1")
|
|
107
|
+
.set(books.column2, "value2")
|
|
108
|
+
.where(books.column3 == "value3")
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
It might seem strange to have so many ways to do the same thing, but it's up to you to choose the one that fits your
|
|
112
|
+
style the best. The library is designed to be as flexible as possible. You can mix and match different styles in the same
|
|
113
|
+
codebase, or even in the same query, if you want, as long as it makes sense to you.
|
|
114
|
+
|
|
115
|
+
By leveraging Python code in constructing SQL, you can use all sorts of Python features to make building SQL an ease.
|
|
116
|
+
Consider list comprehensions for IN statement, building of complex WHERE clauses, dynamic select columns, call UPDATE
|
|
117
|
+
only if anything has changed, ... All of that and much more can be done without the hassle of building complex strings
|
|
118
|
+
together.
|
|
119
|
+
|
|
120
|
+
Let's have a look at a few more practical examples:
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from sqlfactory import Select, In, Direction, Eq, Column, SelectColumn
|
|
124
|
+
from dataclasses import dataclass
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@dataclass
|
|
128
|
+
class Book:
|
|
129
|
+
id: int
|
|
130
|
+
title: str
|
|
131
|
+
author: str
|
|
132
|
+
year: str
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def select_books_by_authors(c: DictCursor, authors: list[str], book_properties: set[str] = None, offset: int = 0,
|
|
136
|
+
limit: int = 10):
|
|
137
|
+
"""
|
|
138
|
+
Returns books written by specific authors. Returns list of books paginated by specified offset and limit, ordered
|
|
139
|
+
by book title and author name.
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
if book_properties is None:
|
|
143
|
+
book_properties = {"title", "author", "year"}
|
|
144
|
+
|
|
145
|
+
property_column = {
|
|
146
|
+
"title": SelectColumn("books.title", alias="title"),
|
|
147
|
+
"author": SelectColumn("authors.name", alias="author"),
|
|
148
|
+
"year": SelectColumn("books.year", alias="year")
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
select = (
|
|
152
|
+
# Map dataclass attributes to SQL columns by using mapping table.
|
|
153
|
+
Select(*[property_column[book_property] for book_property in book_properties], table="books")
|
|
154
|
+
|
|
155
|
+
# As Eq expects firt argument to be column and second argument to be value, we need to provide hint, that
|
|
156
|
+
# authors.id is a column, not a value.
|
|
157
|
+
.join("authors", on=Eq("books.author", Column("authors.id")))
|
|
158
|
+
|
|
159
|
+
# In is intelligent, it will work even when authors list is empty (will produce False, which in turn will
|
|
160
|
+
# return empty result, as no author has been matched).
|
|
161
|
+
.where(In("authors.name", authors))
|
|
162
|
+
|
|
163
|
+
# Multiple ORDER BY columns is supported
|
|
164
|
+
.order_by("title", Direction.ASC)
|
|
165
|
+
.order_by("authors.name", Direction.ASC)
|
|
166
|
+
|
|
167
|
+
# Limit and offset are supported as well
|
|
168
|
+
.limit(offset, limit)
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
select.execute(c)
|
|
172
|
+
return [Book(**row) for row in c.fetchall()]
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from sqlfactory import Update, Eq
|
|
177
|
+
from dataclasses import dataclass
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@dataclass
|
|
181
|
+
class BookUpdate:
|
|
182
|
+
id: int
|
|
183
|
+
title: str = None
|
|
184
|
+
author: str = None
|
|
185
|
+
year: int = None
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def update_books(c: Cursor, books: list[BookUpdate]):
|
|
189
|
+
"""Update multiple books at once. Attributes that has None value won't be modified at all."""
|
|
190
|
+
for book in books:
|
|
191
|
+
update = Update("books", where=Eq("id", book.id))
|
|
192
|
+
|
|
193
|
+
if book.title is not None:
|
|
194
|
+
update.set("title", book.title)
|
|
195
|
+
if book.author is not None:
|
|
196
|
+
update.set("author", book.author)
|
|
197
|
+
if book.year is not None:
|
|
198
|
+
update.set("year", book.year)
|
|
199
|
+
|
|
200
|
+
# It can even be done as one-liner, but it gets ugly pretty quickly, so it's not recommended for readability:
|
|
201
|
+
# list(map(update.set, [(attr, getattr(book, attr)) for attr in ["title", "author", "year"] if getattr(book, attr) is not None]))
|
|
202
|
+
|
|
203
|
+
# Will be executed only if any of the columns should be updated.
|
|
204
|
+
update.execute(c)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Installation
|
|
208
|
+
|
|
209
|
+
Just install it from PyPi and use:
|
|
210
|
+
|
|
211
|
+
```shell
|
|
212
|
+
pip install py-SQLBuilder
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Maturity
|
|
216
|
+
|
|
217
|
+
This library is still very new, but grew from multiple projects where it gradually evolved. So it is already used in
|
|
218
|
+
production environment successfully. But as always, bugs are expected to be found. On the other hand, the library
|
|
219
|
+
contains large test suite with 100% code coverage, so it should be quite stable. If you find any bug, please report it.
|
|
220
|
+
|
|
221
|
+
Implemented SQL features are not complete set of what SQL offers, they are added as-needed. Feel free to open a merge
|
|
222
|
+
request if you find missing feature that you need.
|
|
223
|
+
|
|
224
|
+
As we are mainly targeting MySQL / MariaDB, there are some extra features that are on top of SQL standard, that are
|
|
225
|
+
implemented in the builder. But the library should work with any database that supports standard SQL, when you won't
|
|
226
|
+
use features that are extensions to the SQL standard.
|
|
227
|
+
|
|
228
|
+
## Contributing
|
|
229
|
+
|
|
230
|
+
Contributions are always welcome. Just open an issue or a merge request. Keep in mind, that this is not an ORM. So no
|
|
231
|
+
sessions, no transactions, no lazy loading, no relations, no schema management, no migrations, no database creation.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "sqlfactory"
|
|
3
|
+
version = "v1.0.0"
|
|
4
|
+
description = "Convenient classes for building SQL queries in Python. Main purpose of this library is to ease construction of SQL queries in Python code. It is not an ORM (and don't intend to be), just a plain SQL query builder with syntax as similar to actual SQL as possible."
|
|
5
|
+
authors = ["Michal Kuchta <niximor@gmail.com>"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
packages = [{include = "sqlfactory"}]
|
|
8
|
+
classifiers = [
|
|
9
|
+
"Programming Language :: Python :: 3",
|
|
10
|
+
"Development Status :: 5 - Production/Stable",
|
|
11
|
+
"Intended Audience :: Developers",
|
|
12
|
+
"Topic :: Database",
|
|
13
|
+
"Topic :: Software Development :: Libraries",
|
|
14
|
+
"Programming Language :: SQL",
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage = "https://gitlab.com/gcm-cz/sqlfactory"
|
|
21
|
+
Repository = "https://gitlab.com/gcm-cz/sqlfactory"
|
|
22
|
+
Issues = "https://gitlab.com/gcm-cz/sqlfactory/-/issues"
|
|
23
|
+
|
|
24
|
+
[tool.poetry.dependencies]
|
|
25
|
+
python = "^3.11"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
[tool.poetry.group.dev.dependencies]
|
|
29
|
+
pytest = "^8.1.1"
|
|
30
|
+
pytest-asyncio = "^0.23.6"
|
|
31
|
+
pylint = "^3.1.0"
|
|
32
|
+
mypy = "^1.9.0"
|
|
33
|
+
pytest-cov = "^5.0.0"
|
|
34
|
+
|
|
35
|
+
[build-system]
|
|
36
|
+
requires = ["poetry-core"]
|
|
37
|
+
build-backend = "poetry.core.masonry.api"
|
|
38
|
+
|
|
39
|
+
[tool.pylint.'MESSAGES CONTROL']
|
|
40
|
+
max-line-length = 130
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Main SQLBuilder module. All public classes are exported from here."""
|
|
2
|
+
|
|
3
|
+
from .condition import *
|
|
4
|
+
from .delete import *
|
|
5
|
+
from .insert import *
|
|
6
|
+
from .mixins import *
|
|
7
|
+
from .select import *
|
|
8
|
+
from .update import *
|
|
9
|
+
|
|
10
|
+
from .entities import Table, Column
|
|
11
|
+
from .statement import Statement, StatementWithArgs, Raw
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""Conditions for WHERE, ON, HAVING clauses in SQL statements."""
|
|
2
|
+
|
|
3
|
+
from .base import ConditionBase, Condition, And, Or
|
|
4
|
+
from .between import Between
|
|
5
|
+
from .in_condition import In
|
|
6
|
+
from .like import Like
|
|
7
|
+
from .simple import Equals, NotEquals, GreaterThan, GreaterThanOrEquals, LessThan, LessThanOrEquals, Eq, Ne, Gt, Ge, Lt, Le
|