sqlqb 0.1.0__py3-none-any.whl

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.
sqlqb/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .select import Select
2
+
3
+ __all__ = ["Select"]
sqlqb/select.py ADDED
@@ -0,0 +1,80 @@
1
+ import io
2
+
3
+
4
+ class Select:
5
+ def __init__(self, *args):
6
+ self.__columns = list(args)
7
+ self.__table = None
8
+ self.__wheres = []
9
+ self.__params = []
10
+ self.__joins = []
11
+ self.__orderby = []
12
+ self.__groupby = []
13
+ self.__limit = None
14
+ self.__offset = None
15
+
16
+ def Columns(self, *columns) -> "Select":
17
+ self.__columns = list(columns)
18
+ return self
19
+
20
+ def From(self, table: str) -> "Select":
21
+ self.__table = table
22
+ return self
23
+
24
+ def Join(self, join: str, *args) -> "Select":
25
+ self.__joins.append(join)
26
+ self.__params += args
27
+ return self
28
+
29
+ def Where(self, condition: str, *args) -> "Select":
30
+ self.__wheres.append(condition)
31
+ self.__params += args
32
+ return self
33
+
34
+ def OrderBy(self, column: str, direction="ASC") -> "Select":
35
+ self.__orderby.append((column, direction))
36
+ return self
37
+
38
+ def GroupBy(self, *columns) -> "Select":
39
+ self.__groupby += columns
40
+ return self
41
+
42
+ def Limit(self, limit: int) -> "Select":
43
+ self.__limit = limit
44
+ return self
45
+
46
+ def Offset(self, offset: int) -> "Select":
47
+ self.__offset = offset
48
+ return self
49
+
50
+ @property
51
+ def params(self) -> list:
52
+ return self.__params
53
+
54
+ @property
55
+ def sql(self) -> str:
56
+ sql = io.StringIO()
57
+ sql.write("SELECT ")
58
+ sql.write(", ".join(self.__columns or "*"))
59
+ sql.write(" FROM ")
60
+ sql.write(self.__table)
61
+ if self.__joins:
62
+ sql.write(" ")
63
+ sql.write(" ".join(self.__joins))
64
+ if self.__wheres:
65
+ sql.write(" WHERE ")
66
+ sql.write(" AND ".join(self.__wheres))
67
+ if self.__groupby:
68
+ sql.write(" GROUP BY ")
69
+ sql.write(", ".join(self.__groupby))
70
+ if self.__orderby:
71
+ sql.write(" ORDER BY ")
72
+ sql.write(", ".join([f"{column} {order}" for column, order in self.__orderby]))
73
+ if self.__limit is not None:
74
+ sql.write(f" LIMIT {self.__limit}")
75
+ if self.__offset is not None:
76
+ sql.write(f" OFFSET {self.__offset}")
77
+ return sql.getvalue()
78
+
79
+ def __str__(self) -> str:
80
+ return self.sql
@@ -0,0 +1,5 @@
1
+ Metadata-Version: 2.4
2
+ Name: sqlqb
3
+ Version: 0.1.0
4
+ Summary: A simple sql query builder
5
+ Requires-Python: >=3.12
@@ -0,0 +1,5 @@
1
+ sqlqb/__init__.py,sha256=z0wMQrM88DsU15rutGZ7SvEAlKgOGGDRVEgf0SI-zZ4,49
2
+ sqlqb/select.py,sha256=xzkD2pzAWiTpjbdCIn9AAYYQQXDmK72oKsIbSh01-QM,2243
3
+ sqlqb-0.1.0.dist-info/METADATA,sha256=cJiv1-7EXOzPNsaf2qla5e0NW8T_FDnWLuUaeQn_TTs,109
4
+ sqlqb-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
5
+ sqlqb-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any