sqlfactory 1.0.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.
- sqlfactory/__init__.py +11 -0
- sqlfactory/condition/__init__.py +7 -0
- sqlfactory/condition/base.py +168 -0
- sqlfactory/condition/between.py +54 -0
- sqlfactory/condition/in_condition.py +126 -0
- sqlfactory/condition/like.py +49 -0
- sqlfactory/condition/simple.py +149 -0
- sqlfactory/delete/__init__.py +3 -0
- sqlfactory/delete/delete.py +64 -0
- sqlfactory/entities.py +213 -0
- sqlfactory/execute.py +158 -0
- sqlfactory/func/__init__.py +0 -0
- sqlfactory/func/agg.py +79 -0
- sqlfactory/func/base.py +37 -0
- sqlfactory/func/control.py +27 -0
- sqlfactory/func/datetime.py +637 -0
- sqlfactory/func/enc.py +177 -0
- sqlfactory/func/info.py +159 -0
- sqlfactory/func/misc.py +142 -0
- sqlfactory/func/numeric.py +338 -0
- sqlfactory/func/str.py +251 -0
- sqlfactory/insert/__init__.py +4 -0
- sqlfactory/insert/insert.py +164 -0
- sqlfactory/insert/values.py +16 -0
- sqlfactory/logger.py +5 -0
- sqlfactory/mixins/__init__.py +5 -0
- sqlfactory/mixins/limit.py +133 -0
- sqlfactory/mixins/order.py +81 -0
- sqlfactory/mixins/where.py +27 -0
- sqlfactory/select/__init__.py +6 -0
- sqlfactory/select/aliased.py +35 -0
- sqlfactory/select/column_list.py +70 -0
- sqlfactory/select/join.py +84 -0
- sqlfactory/select/select.py +200 -0
- sqlfactory/statement.py +114 -0
- sqlfactory/update/__init__.py +3 -0
- sqlfactory/update/update.py +151 -0
- sqlfactory-1.0.0.dist-info/LICENSE +21 -0
- sqlfactory-1.0.0.dist-info/METADATA +251 -0
- sqlfactory-1.0.0.dist-info/RECORD +41 -0
- sqlfactory-1.0.0.dist-info/WHEEL +4 -0
sqlfactory/__init__.py
ADDED
|
@@ -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
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""Base classes to implement SQL conditions."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from abc import ABC
|
|
6
|
+
from collections.abc import Iterable
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from ..statement import StatementWithArgs, ConditionalStatement, Statement
|
|
10
|
+
StatementOrColumn = str | Statement
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ConditionBase(StatementWithArgs, ConditionalStatement, ABC):
|
|
14
|
+
"""
|
|
15
|
+
Generic condition interface, that can be chained with other conditions using & or | operators. All condition
|
|
16
|
+
classes should inherit from this one, as there are checks through the library for instances of this class.
|
|
17
|
+
"""
|
|
18
|
+
def __and__(self, other: ConditionBase) -> And:
|
|
19
|
+
if isinstance(self, And):
|
|
20
|
+
out = self
|
|
21
|
+
else:
|
|
22
|
+
out = And(self)
|
|
23
|
+
|
|
24
|
+
if isinstance(other, And):
|
|
25
|
+
out.extend(other.sub_conditions)
|
|
26
|
+
else:
|
|
27
|
+
out.append(other)
|
|
28
|
+
|
|
29
|
+
return out
|
|
30
|
+
|
|
31
|
+
def __or__(self, other: ConditionBase) -> Or:
|
|
32
|
+
if isinstance(self, Or):
|
|
33
|
+
out = self
|
|
34
|
+
else:
|
|
35
|
+
out = Or(self)
|
|
36
|
+
|
|
37
|
+
if isinstance(other, Or):
|
|
38
|
+
out.extend(other.sub_conditions)
|
|
39
|
+
else:
|
|
40
|
+
out.append(other)
|
|
41
|
+
|
|
42
|
+
return out
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class Condition(ConditionBase):
|
|
46
|
+
"""
|
|
47
|
+
Generic RAW condition with optional arguments.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
def __init__(self, condition: str, *args: Any):
|
|
51
|
+
"""
|
|
52
|
+
:param condition: Condition (such as "`column` = %s")
|
|
53
|
+
:param args: Optional arguments used in condition.
|
|
54
|
+
"""
|
|
55
|
+
self.condition = condition
|
|
56
|
+
self._args = list(args)
|
|
57
|
+
|
|
58
|
+
def __str__(self):
|
|
59
|
+
return self.condition
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def args(self) -> list[Any]:
|
|
63
|
+
return self._args
|
|
64
|
+
|
|
65
|
+
def __bool__(self) -> bool:
|
|
66
|
+
return bool(self.condition)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class CompoundCondition(ConditionBase):
|
|
70
|
+
"""
|
|
71
|
+
Base class for joining multiple conditions together using specified operator. As there are only two operators
|
|
72
|
+
(AND and OR), this class is not meant to be used directly, but rather through And and Or classes.
|
|
73
|
+
"""
|
|
74
|
+
def __init__(self, operator: str, *conditions: ConditionBase | str):
|
|
75
|
+
"""
|
|
76
|
+
:param operator: Which operator to use for joining specific conditions.
|
|
77
|
+
:param conditions: Conditions to join using given operator.
|
|
78
|
+
"""
|
|
79
|
+
self.operator = operator
|
|
80
|
+
self._sub_conditions: list[ConditionBase] = [
|
|
81
|
+
Condition(condition) if isinstance(condition, str) else condition
|
|
82
|
+
for condition in conditions
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
def append(self, condition: ConditionBase | str, *args: Any) -> CompoundCondition:
|
|
86
|
+
"""
|
|
87
|
+
Append another condition to be joined.
|
|
88
|
+
:param condition: Condition to append
|
|
89
|
+
:param args: Optional arguments for given condition, if condition is passed as string.
|
|
90
|
+
"""
|
|
91
|
+
if isinstance(condition, str):
|
|
92
|
+
condition = Condition(condition, *args)
|
|
93
|
+
elif args:
|
|
94
|
+
raise AttributeError("*args can be used only for str conditions.")
|
|
95
|
+
|
|
96
|
+
self._sub_conditions.append(condition)
|
|
97
|
+
return self
|
|
98
|
+
|
|
99
|
+
def extend(self, conditions: Iterable[ConditionBase]):
|
|
100
|
+
"""
|
|
101
|
+
Extend condition with list of conditions.
|
|
102
|
+
:param conditions: Conditions to extend this condition with
|
|
103
|
+
"""
|
|
104
|
+
self._sub_conditions.extend(conditions)
|
|
105
|
+
return self
|
|
106
|
+
|
|
107
|
+
@property
|
|
108
|
+
def sub_conditions(self) -> list[ConditionBase]:
|
|
109
|
+
"""
|
|
110
|
+
Return filtered list of conditions, which are valid.
|
|
111
|
+
"""
|
|
112
|
+
return list(filter(bool, self._sub_conditions))
|
|
113
|
+
|
|
114
|
+
def __str__(self):
|
|
115
|
+
"""
|
|
116
|
+
Create SQL statement of joined conditions.
|
|
117
|
+
"""
|
|
118
|
+
sub_conditions = list(map(str, self.sub_conditions))
|
|
119
|
+
if sub_conditions:
|
|
120
|
+
return "(" + f" {self.operator} ".join(sub_conditions) + ")" if len(sub_conditions) > 1 else sub_conditions[0]
|
|
121
|
+
|
|
122
|
+
return "TRUE"
|
|
123
|
+
|
|
124
|
+
@property
|
|
125
|
+
def args(self) -> list[Any]:
|
|
126
|
+
"""
|
|
127
|
+
Return arguments from all sub conditions in correct order.
|
|
128
|
+
"""
|
|
129
|
+
out = []
|
|
130
|
+
|
|
131
|
+
for cond in self.sub_conditions:
|
|
132
|
+
out.extend(cond.args)
|
|
133
|
+
|
|
134
|
+
return out
|
|
135
|
+
|
|
136
|
+
def __bool__(self):
|
|
137
|
+
"""
|
|
138
|
+
Is this compound condition non-empty?
|
|
139
|
+
"""
|
|
140
|
+
return bool(self.sub_conditions)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class And(CompoundCondition):
|
|
144
|
+
"""
|
|
145
|
+
Compound condition joined by AND.
|
|
146
|
+
|
|
147
|
+
Usage:
|
|
148
|
+
>>> And(Equals("id", 1), Equals("name", "hello"))
|
|
149
|
+
>>> "(`id` = 1 AND `name` = 'hello')"
|
|
150
|
+
|
|
151
|
+
Of course, condition can be another compound condition:
|
|
152
|
+
>>> And(Equals("id", 1), Or(Equals("name", "hello"), Equals("name", "world")))
|
|
153
|
+
>>> "(`id` = 1 AND (`name` = 'hello' OR `name` = 'world'))"
|
|
154
|
+
"""
|
|
155
|
+
def __init__(self, *conditions: ConditionBase | str):
|
|
156
|
+
super().__init__("AND", *conditions)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class Or(CompoundCondition):
|
|
160
|
+
"""
|
|
161
|
+
Compound condition joined by OR.
|
|
162
|
+
|
|
163
|
+
Usage:
|
|
164
|
+
>>> Or(Equals("id", 1), Equals("name", "hello"))
|
|
165
|
+
>>> "(`id` = 1 OR `name` = 'hello')"
|
|
166
|
+
"""
|
|
167
|
+
def __init__(self, *conditions: ConditionBase | str):
|
|
168
|
+
super().__init__("OR", *conditions)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""BETWEEN condition generator"""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from .base import Condition, StatementOrColumn
|
|
6
|
+
from ..entities import Column
|
|
7
|
+
from ..statement import Statement, StatementWithArgs
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# pylint: disable=too-few-public-methods # As everything is handled by base classes.
|
|
11
|
+
class Between(Condition):
|
|
12
|
+
# pylint: disable=duplicate-code # It does not make sense to generalize two-row statement used on two places.
|
|
13
|
+
"""
|
|
14
|
+
Provides generation for following syntax:
|
|
15
|
+
|
|
16
|
+
- `column` BETWEEN <lower_bound> AND <upper_bound>
|
|
17
|
+
- `column` NOT BETWEEN <lower_bound> AND <upper_bound>
|
|
18
|
+
- <statement> BETWEEN <lower_bound> AND <upper_bound>
|
|
19
|
+
- <statement> NOT BETWEEN <lower_bound> AND <upper_bound>
|
|
20
|
+
|
|
21
|
+
"""
|
|
22
|
+
def __init__(
|
|
23
|
+
self, column: StatementOrColumn, lower_bound: Any | Statement, upper_bound: Any | Statement, *,
|
|
24
|
+
negative: bool = False
|
|
25
|
+
):
|
|
26
|
+
lower_bound_s = "%s"
|
|
27
|
+
upper_bound_s = "%s"
|
|
28
|
+
|
|
29
|
+
if not isinstance(column, Statement):
|
|
30
|
+
column = Column(column)
|
|
31
|
+
|
|
32
|
+
args = []
|
|
33
|
+
|
|
34
|
+
if isinstance(column, StatementWithArgs):
|
|
35
|
+
args.extend(column.args)
|
|
36
|
+
|
|
37
|
+
if isinstance(lower_bound, Statement):
|
|
38
|
+
lower_bound_s = str(lower_bound)
|
|
39
|
+
if isinstance(lower_bound, StatementWithArgs):
|
|
40
|
+
args.extend(lower_bound.args)
|
|
41
|
+
else:
|
|
42
|
+
args.append(lower_bound)
|
|
43
|
+
|
|
44
|
+
if isinstance(upper_bound, Statement):
|
|
45
|
+
upper_bound_s = str(upper_bound)
|
|
46
|
+
if isinstance(upper_bound, StatementWithArgs):
|
|
47
|
+
args.extend(upper_bound.args)
|
|
48
|
+
else:
|
|
49
|
+
args.append(upper_bound)
|
|
50
|
+
|
|
51
|
+
super().__init__(
|
|
52
|
+
f"{str(column)} {'NOT ' if negative else ''}BETWEEN {lower_bound_s} AND {upper_bound_s}",
|
|
53
|
+
*args
|
|
54
|
+
)
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""IN condition, used for checking whether column value is in given list of values."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Collection
|
|
4
|
+
from typing import overload, Any
|
|
5
|
+
|
|
6
|
+
from ..entities import Column
|
|
7
|
+
from ..statement import StatementWithArgs, Statement
|
|
8
|
+
from .base import Condition, StatementOrColumn
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# pylint: disable=too-few-public-methods # Everything is handled by super classes.
|
|
12
|
+
class In(Condition):
|
|
13
|
+
"""
|
|
14
|
+
IN condition:
|
|
15
|
+
|
|
16
|
+
- `column` IN (%s, %s, %s)
|
|
17
|
+
- <statement> IN (%s, %s, %s)
|
|
18
|
+
|
|
19
|
+
OR
|
|
20
|
+
|
|
21
|
+
- (`column1`, `column2`) IN ((%s, %s), (%s, %s), (%s, %s))
|
|
22
|
+
- (<statement>, `column`) IN ((%s, %s), (%s, %s), (%s, %s))
|
|
23
|
+
- (<statement>, <statement>) IN ((%s, %s), (%s, %s), (%s, %s))
|
|
24
|
+
- (`column`, <statement>) IN ((%s, %s), (%s, %s), (%s, %s))
|
|
25
|
+
|
|
26
|
+
Also supports comparing to None for single-column conditions (In("column", [1,2,3,None]) will work as expected).
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
@overload
|
|
30
|
+
def __init__(
|
|
31
|
+
self, columns: tuple[StatementOrColumn, ...], values: Collection[tuple[Any, ...]], *,
|
|
32
|
+
negative: bool = False
|
|
33
|
+
):
|
|
34
|
+
"""Provides type definition for statement (`column1`, `column2`) IN ((%s, %s), (%s, %s), (%s, %s))"""
|
|
35
|
+
|
|
36
|
+
@overload
|
|
37
|
+
def __init__(self, column: StatementOrColumn, values: Collection[Any], *, negative: bool = False):
|
|
38
|
+
"""Provides type definition for statement `column` IN (%s, %s, %s)"""
|
|
39
|
+
|
|
40
|
+
# pylint: disable=consider-using-f-string, too-many-branches # Yes, IN statement is rather complex.
|
|
41
|
+
def __init__(
|
|
42
|
+
self, column: StatementOrColumn | tuple[StatementOrColumn, ...],
|
|
43
|
+
values: Collection[Any | tuple[Any, ...]], *, negative: bool = False
|
|
44
|
+
):
|
|
45
|
+
"""
|
|
46
|
+
:param column: Column to compare, or tuple of columns for multi-column comparison.
|
|
47
|
+
:param values: Values to compare (list of values, or list of tuples of values).
|
|
48
|
+
:param negative: Whether to perform negative comparison (NOT IN)
|
|
49
|
+
"""
|
|
50
|
+
add_none = False
|
|
51
|
+
|
|
52
|
+
if None in values:
|
|
53
|
+
add_none = True
|
|
54
|
+
values = list(filter(lambda v: v is not None, values))
|
|
55
|
+
|
|
56
|
+
args = []
|
|
57
|
+
|
|
58
|
+
if isinstance(column, tuple):
|
|
59
|
+
column = tuple(Column(col) if not isinstance(col, Statement) else col for col in column)
|
|
60
|
+
elif not isinstance(column, Statement):
|
|
61
|
+
column = Column(column)
|
|
62
|
+
|
|
63
|
+
if values:
|
|
64
|
+
if isinstance(column, tuple):
|
|
65
|
+
for stmt in column:
|
|
66
|
+
if isinstance(stmt, StatementWithArgs):
|
|
67
|
+
args.extend(stmt.args)
|
|
68
|
+
|
|
69
|
+
for value_tuple in values:
|
|
70
|
+
for value in value_tuple:
|
|
71
|
+
if not isinstance(value, Statement):
|
|
72
|
+
args.append(value)
|
|
73
|
+
elif isinstance(value, StatementWithArgs):
|
|
74
|
+
args.extend(value.args)
|
|
75
|
+
|
|
76
|
+
super().__init__(
|
|
77
|
+
"({}) {} ({})".format(
|
|
78
|
+
", ".join(map(str, column)),
|
|
79
|
+
"IN" if not negative else "NOT IN ",
|
|
80
|
+
", ".join(["(" + ", ".join([
|
|
81
|
+
"%s" if not isinstance(value, Statement) else str(value)
|
|
82
|
+
for value in value_tuple
|
|
83
|
+
]) + ")" for value_tuple in values])
|
|
84
|
+
),
|
|
85
|
+
*args,
|
|
86
|
+
)
|
|
87
|
+
else:
|
|
88
|
+
in_stmt = "{} {} ({})".format(
|
|
89
|
+
str(column),
|
|
90
|
+
"IN" if not negative else "NOT IN",
|
|
91
|
+
", ".join(["%s" if not isinstance(value, Statement) else str(value) for value in values])
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
if isinstance(column, StatementWithArgs):
|
|
95
|
+
args.extend(column.args)
|
|
96
|
+
|
|
97
|
+
for value in values:
|
|
98
|
+
if isinstance(value, StatementWithArgs):
|
|
99
|
+
args.extend(value.args)
|
|
100
|
+
elif not isinstance(value, Statement):
|
|
101
|
+
args.append(value)
|
|
102
|
+
|
|
103
|
+
if add_none:
|
|
104
|
+
if isinstance(column, StatementWithArgs):
|
|
105
|
+
args.extend(column.args)
|
|
106
|
+
|
|
107
|
+
super().__init__(
|
|
108
|
+
f"({in_stmt} {'OR' if not negative else 'AND'} {str(column)} IS {'NOT ' if negative else ''}NULL)",
|
|
109
|
+
*args
|
|
110
|
+
)
|
|
111
|
+
else:
|
|
112
|
+
super().__init__(
|
|
113
|
+
in_stmt,
|
|
114
|
+
*args
|
|
115
|
+
)
|
|
116
|
+
elif add_none:
|
|
117
|
+
# This could happen only if there is just a one column, not multi-column statement.
|
|
118
|
+
if isinstance(column, StatementWithArgs):
|
|
119
|
+
args.extend(column.args)
|
|
120
|
+
|
|
121
|
+
super().__init__(
|
|
122
|
+
f"{str(column)} IS {'NOT ' if negative else ''}NULL",
|
|
123
|
+
*args
|
|
124
|
+
)
|
|
125
|
+
else:
|
|
126
|
+
super().__init__("FALSE" if not negative else "TRUE")
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""LIKE statement"""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from .base import Condition, StatementOrColumn
|
|
6
|
+
from ..entities import Column
|
|
7
|
+
from ..statement import Statement, StatementWithArgs
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Like(Condition):
|
|
11
|
+
"""
|
|
12
|
+
SQL LIKE statement
|
|
13
|
+
|
|
14
|
+
`column` LIKE %s
|
|
15
|
+
`column` NOT LIKE %s
|
|
16
|
+
<statement> LIKE %s
|
|
17
|
+
<statement> NOT LIKE %s
|
|
18
|
+
"""
|
|
19
|
+
def __init__(self, column: StatementOrColumn, value: Any | Statement, negative: bool = False):
|
|
20
|
+
args = []
|
|
21
|
+
|
|
22
|
+
if not isinstance(column, Statement):
|
|
23
|
+
column = Column(column)
|
|
24
|
+
|
|
25
|
+
if isinstance(column, StatementWithArgs):
|
|
26
|
+
args.extend(column.args)
|
|
27
|
+
|
|
28
|
+
if isinstance(value, StatementWithArgs):
|
|
29
|
+
args.extend(value.args)
|
|
30
|
+
else:
|
|
31
|
+
args.append(value)
|
|
32
|
+
|
|
33
|
+
if isinstance(value, Statement):
|
|
34
|
+
super().__init__(
|
|
35
|
+
f"{str(column)}{' NOT' if negative else ''} LIKE {str(value)}",
|
|
36
|
+
*args,
|
|
37
|
+
)
|
|
38
|
+
else:
|
|
39
|
+
super().__init__(
|
|
40
|
+
f"{str(column)}{' NOT' if negative else ''} LIKE %s",
|
|
41
|
+
*args
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
@staticmethod
|
|
45
|
+
def escape(s: str) -> str:
|
|
46
|
+
"""
|
|
47
|
+
Escape string for use in LIKE statement
|
|
48
|
+
"""
|
|
49
|
+
return s.replace("%", "%%").replace("_", "__")
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Simple binary comparison conditions."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from .base import Condition, StatementOrColumn
|
|
6
|
+
from ..statement import Statement, StatementWithArgs
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# pylint: disable=too-few-public-methods # As everything is handled in base classes.
|
|
10
|
+
class SimpleCondition(Condition):
|
|
11
|
+
# pylint: disable=duplicate-code # It does not make sense to generalize two-row statement used on two places.
|
|
12
|
+
"""
|
|
13
|
+
Simple condition comparing one column with given value, using specified operator.
|
|
14
|
+
"""
|
|
15
|
+
def __init__(self, column: StatementOrColumn, operator: str, value: Statement | Any):
|
|
16
|
+
"""
|
|
17
|
+
:param column: Column to compare (string or instance of Column) or Statement to use on left side of comparison.
|
|
18
|
+
:param operator: Operator to use for comparison.
|
|
19
|
+
:param value: Value to compare column value to (or statement to use on right side of comparison).
|
|
20
|
+
"""
|
|
21
|
+
if not isinstance(column, Statement):
|
|
22
|
+
# pylint: disable=import-outside-toplevel,cyclic-import
|
|
23
|
+
from ..entities import Column
|
|
24
|
+
column = Column(column)
|
|
25
|
+
|
|
26
|
+
args = []
|
|
27
|
+
|
|
28
|
+
if isinstance(column, StatementWithArgs):
|
|
29
|
+
args.extend(column.args)
|
|
30
|
+
|
|
31
|
+
if isinstance(value, StatementWithArgs):
|
|
32
|
+
args.extend(value.args)
|
|
33
|
+
|
|
34
|
+
elif not isinstance(value, Statement):
|
|
35
|
+
args.append(value)
|
|
36
|
+
|
|
37
|
+
if isinstance(value, Statement):
|
|
38
|
+
super().__init__(
|
|
39
|
+
f"{str(column)} {operator} {str(value)}",
|
|
40
|
+
*args
|
|
41
|
+
)
|
|
42
|
+
else:
|
|
43
|
+
super().__init__(
|
|
44
|
+
f"{str(column)} {operator} %s",
|
|
45
|
+
*args
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# pylint: disable=too-few-public-methods # As everything is handled in base classes.
|
|
50
|
+
class Equals(SimpleCondition):
|
|
51
|
+
"""
|
|
52
|
+
- `column` = <value>
|
|
53
|
+
- `column` IS NULL
|
|
54
|
+
- <statement> = <value>
|
|
55
|
+
- <statement> IS NULL
|
|
56
|
+
"""
|
|
57
|
+
def __init__(self, column: StatementOrColumn, value: Any | None | Statement):
|
|
58
|
+
"""
|
|
59
|
+
:param column: Column to compare (string or instance of Column) or Statement to use on left side of comparison.
|
|
60
|
+
:param value: Value to compare column value to (or statement to use on right side of comparison).
|
|
61
|
+
"""
|
|
62
|
+
if value is None:
|
|
63
|
+
super().__init__(column, "IS", value)
|
|
64
|
+
else:
|
|
65
|
+
super().__init__(column, "=", value)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# pylint: disable=too-few-public-methods # As everything is handled in base classes.
|
|
69
|
+
class NotEquals(SimpleCondition):
|
|
70
|
+
"""
|
|
71
|
+
- `column` != <value>
|
|
72
|
+
- <statement> != <value>
|
|
73
|
+
- `column` IS NOT NULL
|
|
74
|
+
- <statement> IS NOT NULL
|
|
75
|
+
"""
|
|
76
|
+
def __init__(self, column: StatementOrColumn, value: Any | None | Statement):
|
|
77
|
+
"""
|
|
78
|
+
:param column: Column to compare (string or instance of Column) or Statement to use on left side of comparison.
|
|
79
|
+
:param value: Value to compare column value to (or statement to use on right side of comparison).
|
|
80
|
+
"""
|
|
81
|
+
if value is None:
|
|
82
|
+
super().__init__(column, "IS NOT", value)
|
|
83
|
+
else:
|
|
84
|
+
super().__init__(column, "!=", value)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# pylint: disable=too-few-public-methods # As everything is handled in base classes.
|
|
88
|
+
class GreaterThanOrEquals(SimpleCondition):
|
|
89
|
+
"""
|
|
90
|
+
- `column` >= <value>
|
|
91
|
+
- <statement> >= <value>
|
|
92
|
+
"""
|
|
93
|
+
def __init__(self, column: StatementOrColumn, value: Any | Statement):
|
|
94
|
+
"""
|
|
95
|
+
:param column: Column to compare (string or instance of Column) or Statement to use on left side of comparison.
|
|
96
|
+
:param value: Value to compare column value to (or statement to use on right side of comparison).
|
|
97
|
+
"""
|
|
98
|
+
super().__init__(column, ">=", value)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# pylint: disable=too-few-public-methods # As everything is handled in base classes.
|
|
102
|
+
class GreaterThan(SimpleCondition):
|
|
103
|
+
"""
|
|
104
|
+
- `column` > <value>
|
|
105
|
+
- <statement> > <value>
|
|
106
|
+
"""
|
|
107
|
+
def __init__(self, column: StatementOrColumn, value: Any | Statement):
|
|
108
|
+
"""
|
|
109
|
+
:param column: Column to compare (string or instance of Column) or Statement to use on left side of comparison.
|
|
110
|
+
:param value: Value to compare column value to (or statement to use on right side of comparison).
|
|
111
|
+
"""
|
|
112
|
+
super().__init__(column, ">", value)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
# pylint: disable=too-few-public-methods # As everything is handled in base classes.
|
|
116
|
+
class LessThanOrEquals(SimpleCondition):
|
|
117
|
+
"""
|
|
118
|
+
- `column` <= <value>
|
|
119
|
+
- <statement> <= <value>
|
|
120
|
+
"""
|
|
121
|
+
def __init__(self, column: StatementOrColumn, value: Any | Statement):
|
|
122
|
+
"""
|
|
123
|
+
:param column: Column to compare (string or instance of Column) or Statement to use on left side of comparison.
|
|
124
|
+
:param value: Value to compare column value to (or statement to use on right side of comparison).
|
|
125
|
+
"""
|
|
126
|
+
super().__init__(column, "<=", value)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
# pylint: disable=too-few-public-methods # As everything is handled in base classes.
|
|
130
|
+
class LessThan(SimpleCondition):
|
|
131
|
+
"""
|
|
132
|
+
- `column` < <value>
|
|
133
|
+
- <statement> < <value>
|
|
134
|
+
"""
|
|
135
|
+
def __init__(self, column: StatementOrColumn, value: Any | Statement):
|
|
136
|
+
"""
|
|
137
|
+
:param column: Column to compare (string or instance of Column) or Statement to use on left side of comparison.
|
|
138
|
+
:param value: Value to compare column value to (or statement to use on right side of comparison).
|
|
139
|
+
"""
|
|
140
|
+
super().__init__(column, "<", value)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
# Convenient aliases for shorter code.
|
|
144
|
+
Eq = Equals
|
|
145
|
+
Ge = GreaterThanOrEquals
|
|
146
|
+
Gt = GreaterThan
|
|
147
|
+
Le = LessThanOrEquals
|
|
148
|
+
Lt = LessThan
|
|
149
|
+
Ne = NotEquals
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""DELETE statement builder"""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from ..entities import Table
|
|
6
|
+
from ..condition.base import ConditionBase
|
|
7
|
+
from ..execute import ExecutableStatementWithArgs
|
|
8
|
+
from ..mixins.limit import WithLimit, Limit
|
|
9
|
+
from ..mixins.order import WithOrder, OrderArg
|
|
10
|
+
from ..mixins.where import WithWhere
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# pylint: disable=too-many-ancestors # This is intentional, as this class is a combination of multiple mixins.
|
|
14
|
+
class Delete(ExecutableStatementWithArgs, WithWhere['Delete'], WithOrder['Delete'], WithLimit['Delete']):
|
|
15
|
+
"""
|
|
16
|
+
DELETE statement
|
|
17
|
+
|
|
18
|
+
>>> Delete("table", where=In("id", [1, 2, 3]))
|
|
19
|
+
>>> "DELETE FROM `table` WHERE `id` IN (1,2,3)"
|
|
20
|
+
"""
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
table: Table | str,
|
|
24
|
+
where: ConditionBase = None,
|
|
25
|
+
order: OrderArg = None,
|
|
26
|
+
limit: Limit = None
|
|
27
|
+
):
|
|
28
|
+
"""
|
|
29
|
+
:param table: Table to delete from
|
|
30
|
+
:param where: WHERE condition
|
|
31
|
+
:param order: Ordering of matched rows, usefull when limiting number of deleted rows.
|
|
32
|
+
:param limit: Limit number of deleted rows.
|
|
33
|
+
"""
|
|
34
|
+
super().__init__(where=where, order=order, limit=limit)
|
|
35
|
+
self.table = table if isinstance(table, Table) else Table(table)
|
|
36
|
+
|
|
37
|
+
def __str__(self):
|
|
38
|
+
"""Construct the DELETE statement."""
|
|
39
|
+
q = [f"DELETE FROM {str(self.table)}"]
|
|
40
|
+
|
|
41
|
+
if self._where:
|
|
42
|
+
q.append("WHERE")
|
|
43
|
+
q.append(str(self._where))
|
|
44
|
+
|
|
45
|
+
if self._order:
|
|
46
|
+
q.append(str(self._order))
|
|
47
|
+
|
|
48
|
+
if self._limit:
|
|
49
|
+
q.append(str(self._limit))
|
|
50
|
+
|
|
51
|
+
return " ".join(q)
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def args(self) -> list[Any]:
|
|
55
|
+
"""DELETE statement arguments."""
|
|
56
|
+
return (
|
|
57
|
+
(self._where.args if self._where else []) +
|
|
58
|
+
(self._order.args if self._order else []) +
|
|
59
|
+
(self._limit.args if self._limit else [])
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# Alias to provide better SQL compatibility
|
|
64
|
+
DELETE = Delete
|