DefPixel 0.1.3__73-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.
- DefPixel/DataAccessObject/Configuration.py +15 -0
- DefPixel/DataAccessObject/Connection.py +25 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Adapters/Array.py +16 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Adapters/JavaScriptObjectNotation.py +74 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Adapters/__init__.py +51 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Column.py +34 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Compiler.py +161 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Configuration.py +116 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Connection.py +138 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Constraint.py +18 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Constraints/Check.py +23 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Constraints/Columns.py +24 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Constraints/ForeignKey.py +56 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Constraints/PrimaryKey.py +21 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Constraints/Unique.py +25 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Constraints/__init__.py +13 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Context.py +48 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Cursor.py +123 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Executors/Create.py +181 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Executors/Delete.py +31 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Executors/Drop.py +53 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Executors/Get.py +24 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Executors/Insert.py +98 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Executors/Query.py +37 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Executors/Select.py +122 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Executors/Update.py +57 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Executors/__init__.py +19 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Expr.py +11 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Exprs/Common.py +180 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Exprs/Compare.py +87 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Exprs/Condition.py +155 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Exprs/JavaScriptObjectNotation.py +53 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Exprs/Operator.py +42 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Exprs/String.py +48 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Exprs/__init__.py +75 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Function.py +11 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Functions/Common.py +142 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Functions/DateTime.py +219 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Functions/JavaScriptObjectNotation.py +100 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Functions/Sequence.py +55 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Functions/__init__.py +91 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Index.py +135 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Joins/FullOuter.py +15 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Joins/Inner.py +15 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Joins/Join.py +41 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Joins/LeftOuter.py +15 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Joins/RightOuter.py +15 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Joins/__init__.py +15 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Mapping.py +359 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Orders.py +59 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Pool.py +84 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Schema.py +28 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Sequence.py +55 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Session.py +84 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Type.py +113 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Types/Array.py +61 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Types/Binary.py +34 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Types/Bool.py +29 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Types/DateTime.py +120 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Types/JavaScriptObjectNotation.py +52 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Types/Numeric.py +151 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Types/Size.py +13 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Types/String.py +80 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/Types/__init__.py +76 -0
- DefPixel/DataAccessObject/Implements/PostgreSQL/__init__.py +85 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Configuration.py +16 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Connection.py +96 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Constraint.py +9 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Constraints/ForeignKey.py +16 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Constraints/PrimaryKey.py +15 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Constraints/Unique.py +25 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Constraints/__init__.py +11 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Context.py +36 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Cursor.py +87 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Executors/Create.py +115 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Executors/Delete.py +40 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Executors/Drop.py +34 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Executors/Get.py +24 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Executors/Insert.py +59 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Executors/Select.py +127 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Executors/Update.py +44 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Executors/__init__.py +19 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Expr.py +77 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Exprs/Alias.py +25 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Exprs/Is.py +42 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Exprs/__init__.py +13 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Filters/Compare.py +84 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Filters/Operator.py +42 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Filters/String.py +34 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Filters/__init__.py +30 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Function.py +19 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Functions/Common.py +52 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Functions/Condition.py +31 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Functions/__init__.py +16 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Index.py +23 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Joins/FullOuter.py +19 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Joins/Inner.py +19 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Joins/Join.py +39 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Joins/LeftOuter.py +19 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Joins/RightOuter.py +19 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Joins/__init__.py +13 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Mapping.py +303 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Orders/Ascend.py +28 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Orders/Descend.py +28 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Orders/__init__.py +9 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Session.py +60 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Type.py +75 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Types/Binary.py +28 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Types/DateTime.py +42 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Types/Numeric.py +49 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Types/String.py +28 -0
- DefPixel/DataAccessObject/Implements/Sqlite/Types/__init__.py +34 -0
- DefPixel/DataAccessObject/Implements/Sqlite/__init__.py +39 -0
- DefPixel/DataAccessObject/Implements/__init__.py +1 -0
- DefPixel/DataAccessObject/Properties/Database/Context.py +33 -0
- DefPixel/DataAccessObject/Properties/Database/Cursor.py +27 -0
- DefPixel/DataAccessObject/Properties/Database/Database.py +48 -0
- DefPixel/DataAccessObject/Properties/Database/Executor.py +38 -0
- DefPixel/DataAccessObject/Properties/Database/Executors.py +31 -0
- DefPixel/DataAccessObject/Properties/Database/Fetch.py +37 -0
- DefPixel/DataAccessObject/Properties/Database/Filter.py +21 -0
- DefPixel/DataAccessObject/Properties/Database/Run.py +39 -0
- DefPixel/DataAccessObject/Properties/Database/Session.py +36 -0
- DefPixel/DataAccessObject/Properties/Database/__init__.py +25 -0
- DefPixel/DataAccessObject/Properties/Dataclass.py +25 -0
- DefPixel/DataAccessObject/Properties/Document/Document.py +36 -0
- DefPixel/DataAccessObject/Properties/Document/__init__.py +7 -0
- DefPixel/DataAccessObject/Properties/Index/Index.py +48 -0
- DefPixel/DataAccessObject/Properties/Index/__init__.py +7 -0
- DefPixel/DataAccessObject/Properties/KeyValue/KeyValue.py +44 -0
- DefPixel/DataAccessObject/Properties/KeyValue/__init__.py +7 -0
- DefPixel/DataAccessObject/Properties/__init__.py +15 -0
- DefPixel/DataAccessObject/__init__.py +9 -0
- DefPixel/Validator/Pattern.py +36 -0
- DefPixel/Validator/Patterns/Array.py +30 -0
- DefPixel/Validator/Patterns/Common.py +92 -0
- DefPixel/Validator/Patterns/Compare.py +180 -0
- DefPixel/Validator/Patterns/Condition.py +60 -0
- DefPixel/Validator/Patterns/DataObject.py +85 -0
- DefPixel/Validator/Patterns/DateTime.py +111 -0
- DefPixel/Validator/Patterns/Number.py +43 -0
- DefPixel/Validator/Patterns/Object.py +150 -0
- DefPixel/Validator/Patterns/Size.py +110 -0
- DefPixel/Validator/Patterns/String.py +85 -0
- DefPixel/Validator/Patterns/Type.py +350 -0
- DefPixel/Validator/Patterns/__init__.py +137 -0
- DefPixel/Validator/Validate.py +29 -0
- DefPixel/Validator/Validator.py +30 -0
- DefPixel/Validator/__init__.py +11 -0
- DefPixel/__init__.py +0 -0
- defpixel-0.1.3.dist-info/METADATA +179 -0
- defpixel-0.1.3.dist-info/RECORD +155 -0
- defpixel-0.1.3.dist-info/WHEEL +6 -0
- defpixel-0.1.3.dist-info/licenses/LICENSE +674 -0
- defpixel-0.1.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from DefPixel.DataAccessObject.Properties.Database import (
|
|
4
|
+
Executor,
|
|
5
|
+
Fetch,
|
|
6
|
+
)
|
|
7
|
+
from ..Mapping import (
|
|
8
|
+
identifier,
|
|
9
|
+
inspect_model,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
__all__ = (
|
|
13
|
+
'Insert',
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Insert(Executor, Fetch):
|
|
18
|
+
"""Map field names to columns and bind validated values."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, o):
|
|
21
|
+
self.mapping = inspect_model(o, 'table')
|
|
22
|
+
self.kwargs = {}
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
def values(self, **kwargs):
|
|
26
|
+
self.kwargs.update(self.mapping.values(kwargs))
|
|
27
|
+
return self
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def query(self):
|
|
31
|
+
if not self.kwargs:
|
|
32
|
+
return 'INSERT INTO {} DEFAULT VALUES RETURNING *'.format(
|
|
33
|
+
identifier(self.mapping.name),
|
|
34
|
+
)
|
|
35
|
+
return 'INSERT INTO {}({}) VALUES({}) RETURNING *'.format(
|
|
36
|
+
identifier(self.mapping.name),
|
|
37
|
+
', '.join(identifier(key) for key in self.kwargs),
|
|
38
|
+
', '.join(['?'] * len(self.kwargs)),
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def args(self):
|
|
43
|
+
return tuple(self.kwargs.values())
|
|
44
|
+
|
|
45
|
+
def fetch(
|
|
46
|
+
self,
|
|
47
|
+
cursor,
|
|
48
|
+
filter=None,
|
|
49
|
+
fetch=None,
|
|
50
|
+
):
|
|
51
|
+
rows = cursor.rows()
|
|
52
|
+
if not rows:
|
|
53
|
+
return None
|
|
54
|
+
row = rows[0]
|
|
55
|
+
if filter is not None:
|
|
56
|
+
row = filter(row)
|
|
57
|
+
if fetch is not None:
|
|
58
|
+
return inspect_model(fetch).restore(row)
|
|
59
|
+
return row
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from DefPixel.DataAccessObject.Properties.Database import (
|
|
4
|
+
Executor,
|
|
5
|
+
Fetch,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
from ..Mapping import (
|
|
9
|
+
identifier,
|
|
10
|
+
inspect_model,
|
|
11
|
+
)
|
|
12
|
+
from ..Expr import Expr
|
|
13
|
+
|
|
14
|
+
__all__ = (
|
|
15
|
+
'Select',
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Select(Executor, Fetch):
|
|
20
|
+
"""Select query for an explicitly mapped dataclass."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, o):
|
|
23
|
+
self.mapping = inspect_model(o)
|
|
24
|
+
self.joins = ()
|
|
25
|
+
self.conds = ()
|
|
26
|
+
self.grps = ()
|
|
27
|
+
self.havs = ()
|
|
28
|
+
self.ords = ()
|
|
29
|
+
self.vals = ()
|
|
30
|
+
self.offset = 0
|
|
31
|
+
self.size = None
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
def join(self, *args):
|
|
35
|
+
self.joins = args
|
|
36
|
+
return self
|
|
37
|
+
|
|
38
|
+
def where(self, *args):
|
|
39
|
+
self.conds = args
|
|
40
|
+
return self
|
|
41
|
+
|
|
42
|
+
def groupBy(self, *args):
|
|
43
|
+
self.grps = args
|
|
44
|
+
return self
|
|
45
|
+
|
|
46
|
+
def having(self, *args):
|
|
47
|
+
self.havs = args
|
|
48
|
+
return self
|
|
49
|
+
|
|
50
|
+
def orderBy(self, *args):
|
|
51
|
+
self.ords = args
|
|
52
|
+
return self
|
|
53
|
+
|
|
54
|
+
def limit(
|
|
55
|
+
self,
|
|
56
|
+
size=None,
|
|
57
|
+
offset=0,
|
|
58
|
+
):
|
|
59
|
+
if size is not None and (type(size) is not int or size < 0):
|
|
60
|
+
raise ValueError('size must be a nonnegative integer')
|
|
61
|
+
if type(offset) is not int or offset < 0:
|
|
62
|
+
raise ValueError('offset must be a nonnegative integer')
|
|
63
|
+
if size is None and offset:
|
|
64
|
+
raise ValueError('offset requires size')
|
|
65
|
+
self.size = size
|
|
66
|
+
self.offset = offset
|
|
67
|
+
return self
|
|
68
|
+
|
|
69
|
+
def values(self, *args):
|
|
70
|
+
self.vals = args
|
|
71
|
+
return self
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def query(self):
|
|
75
|
+
columns = self.vals or tuple(self.mapping.columns.values())
|
|
76
|
+
sql = 'SELECT {} FROM {}'.format(
|
|
77
|
+
', '.join(str(value) for value in columns),
|
|
78
|
+
identifier(self.mapping.name),
|
|
79
|
+
)
|
|
80
|
+
if self.joins:
|
|
81
|
+
sql += ' ' + ' '.join(str(value) for value in self.joins)
|
|
82
|
+
for keyword, values, separator in (
|
|
83
|
+
('WHERE', self.conds, ' AND '),
|
|
84
|
+
('GROUP BY', self.grps, ', '),
|
|
85
|
+
('HAVING', self.havs, ' AND '),
|
|
86
|
+
('ORDER BY', self.ords, ', '),
|
|
87
|
+
):
|
|
88
|
+
if values:
|
|
89
|
+
sql += ' {} {}'.format(keyword, separator.join(str(value) for value in values))
|
|
90
|
+
if self.size is not None:
|
|
91
|
+
sql += ' LIMIT ? OFFSET ?'
|
|
92
|
+
return sql
|
|
93
|
+
|
|
94
|
+
@property
|
|
95
|
+
def args(self):
|
|
96
|
+
columns = self.vals or tuple(self.mapping.columns.values())
|
|
97
|
+
values = (
|
|
98
|
+
columns,
|
|
99
|
+
self.joins,
|
|
100
|
+
self.conds,
|
|
101
|
+
self.grps,
|
|
102
|
+
self.havs,
|
|
103
|
+
self.ords,
|
|
104
|
+
)
|
|
105
|
+
args = tuple(
|
|
106
|
+
value
|
|
107
|
+
for group in values
|
|
108
|
+
for expression in group
|
|
109
|
+
for value in Expr.parameters_for(expression)
|
|
110
|
+
)
|
|
111
|
+
if self.size is not None:
|
|
112
|
+
args += (self.size, self.offset)
|
|
113
|
+
return args
|
|
114
|
+
|
|
115
|
+
def fetch(
|
|
116
|
+
self,
|
|
117
|
+
cursor,
|
|
118
|
+
filter=None,
|
|
119
|
+
fetch=None,
|
|
120
|
+
):
|
|
121
|
+
mapping = inspect_model(fetch) if fetch is not None else None
|
|
122
|
+
result = []
|
|
123
|
+
for row in cursor.rows():
|
|
124
|
+
if filter is not None:
|
|
125
|
+
row = filter(row)
|
|
126
|
+
result.append(mapping.restore(row) if mapping is not None else row)
|
|
127
|
+
return result if result else None
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from .Insert import Insert
|
|
4
|
+
from ..Expr import Expr
|
|
5
|
+
from ..Mapping import identifier
|
|
6
|
+
|
|
7
|
+
__all__ = (
|
|
8
|
+
'Update',
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Update(Insert):
|
|
13
|
+
"""Update query preserving set and where chaining."""
|
|
14
|
+
|
|
15
|
+
def __init__(self, o):
|
|
16
|
+
super().__init__(o)
|
|
17
|
+
self.conds = ()
|
|
18
|
+
return
|
|
19
|
+
|
|
20
|
+
def set(self, **kwargs):
|
|
21
|
+
return self.values(**kwargs)
|
|
22
|
+
|
|
23
|
+
def where(self, *args):
|
|
24
|
+
self.conds = args
|
|
25
|
+
return self
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def query(self):
|
|
29
|
+
if not self.kwargs:
|
|
30
|
+
raise ValueError('No columns are specified for update')
|
|
31
|
+
return 'UPDATE {} SET {}{} RETURNING *'.format(
|
|
32
|
+
identifier(self.mapping.name),
|
|
33
|
+
', '.join('{}=?'.format(identifier(key)) for key in self.kwargs),
|
|
34
|
+
' WHERE ' + ' AND '.join(str(cond) for cond in self.conds) if self.conds else '',
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def args(self):
|
|
39
|
+
where = tuple(
|
|
40
|
+
value
|
|
41
|
+
for condition in self.conds
|
|
42
|
+
for value in Expr.parameters_for(condition)
|
|
43
|
+
)
|
|
44
|
+
return super().args + where
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from .Create import Create
|
|
4
|
+
from .Drop import Drop
|
|
5
|
+
from .Insert import Insert
|
|
6
|
+
from .Select import Select
|
|
7
|
+
from .Update import Update
|
|
8
|
+
from .Delete import Delete
|
|
9
|
+
from .Get import Get
|
|
10
|
+
|
|
11
|
+
__all__ = (
|
|
12
|
+
'Create',
|
|
13
|
+
'Drop',
|
|
14
|
+
'Insert',
|
|
15
|
+
'Select',
|
|
16
|
+
'Update',
|
|
17
|
+
'Delete',
|
|
18
|
+
'Get',
|
|
19
|
+
)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from abc import (
|
|
4
|
+
ABC,
|
|
5
|
+
ABCMeta,
|
|
6
|
+
abstractmethod,
|
|
7
|
+
)
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
from math import isfinite
|
|
10
|
+
|
|
11
|
+
from .Function import Function
|
|
12
|
+
from .Type import Type
|
|
13
|
+
|
|
14
|
+
__all__ = (
|
|
15
|
+
'Expr'
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Expr(ABC, metaclass=ABCMeta):
|
|
20
|
+
"""Filter Interface"""
|
|
21
|
+
|
|
22
|
+
def encode(self, o):
|
|
23
|
+
return self.literal(o)
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def parameters(self):
|
|
27
|
+
return ()
|
|
28
|
+
|
|
29
|
+
@staticmethod
|
|
30
|
+
def bind(value):
|
|
31
|
+
if value is None or isinstance(value, (str, bytes)):
|
|
32
|
+
return value
|
|
33
|
+
if isinstance(value, datetime):
|
|
34
|
+
return value.isoformat()
|
|
35
|
+
if isinstance(value, bool):
|
|
36
|
+
return int(value)
|
|
37
|
+
if isinstance(value, (int, float)):
|
|
38
|
+
if isinstance(value, float) and not isfinite(value):
|
|
39
|
+
raise ValueError('SQL values require finite numbers')
|
|
40
|
+
return value
|
|
41
|
+
raise TypeError('Unsupported SQL value type: {}'.format(type(value).__name__))
|
|
42
|
+
|
|
43
|
+
@staticmethod
|
|
44
|
+
def expression(value):
|
|
45
|
+
if isinstance(value, (Expr, Function, Type)):
|
|
46
|
+
return str(value)
|
|
47
|
+
return '?'
|
|
48
|
+
|
|
49
|
+
@staticmethod
|
|
50
|
+
def parameters_for(value, bind=False):
|
|
51
|
+
if isinstance(value, (Expr, Function)):
|
|
52
|
+
return value.parameters
|
|
53
|
+
if isinstance(value, Type):
|
|
54
|
+
return ()
|
|
55
|
+
return (Expr.bind(value),) if bind else ()
|
|
56
|
+
|
|
57
|
+
@staticmethod
|
|
58
|
+
def literal(value):
|
|
59
|
+
if value is None:
|
|
60
|
+
return 'NULL'
|
|
61
|
+
if isinstance(value, str):
|
|
62
|
+
return "'{}'".format(value.replace("'", "''"))
|
|
63
|
+
if isinstance(value, datetime):
|
|
64
|
+
return Expr.literal(value.isoformat())
|
|
65
|
+
if isinstance(value, bytes):
|
|
66
|
+
return "X'{}'".format(value.hex())
|
|
67
|
+
if isinstance(value, bool):
|
|
68
|
+
return '1' if value else '0'
|
|
69
|
+
if isinstance(value, (int, float)):
|
|
70
|
+
if isinstance(value, float) and not isfinite(value):
|
|
71
|
+
raise ValueError('SQL literals require finite numbers')
|
|
72
|
+
return str(value)
|
|
73
|
+
raise TypeError('Unsupported SQL literal type: {}'.format(type(value).__name__))
|
|
74
|
+
|
|
75
|
+
@abstractmethod
|
|
76
|
+
def __str__(self):
|
|
77
|
+
raise NotImplementedError('{} must be implemented __str__'.format(self.__class__.__name__))
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from ..Expr import Expr
|
|
4
|
+
from ..Type import Type
|
|
5
|
+
|
|
6
|
+
__all__ = (
|
|
7
|
+
'Alias'
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Alias(Expr):
|
|
12
|
+
def __init__(self, col: Type, name: str):
|
|
13
|
+
self.col = col
|
|
14
|
+
self.name = name
|
|
15
|
+
return
|
|
16
|
+
|
|
17
|
+
@property
|
|
18
|
+
def parameters(self):
|
|
19
|
+
return self.parameters_for(self.col)
|
|
20
|
+
|
|
21
|
+
def __str__(self):
|
|
22
|
+
return '{} AS {}'.format(
|
|
23
|
+
str(self.col),
|
|
24
|
+
self.name,
|
|
25
|
+
)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from typing import Union
|
|
4
|
+
|
|
5
|
+
from ..Expr import Expr
|
|
6
|
+
from ..Function import Function
|
|
7
|
+
from ..Type import Type
|
|
8
|
+
|
|
9
|
+
__all__ = (
|
|
10
|
+
'IsNull',
|
|
11
|
+
'IsNotNull',
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class IsNull(Expr):
|
|
16
|
+
def __init__(self, col: Union[Type, Function, Expr]):
|
|
17
|
+
self.col = col
|
|
18
|
+
return
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def parameters(self):
|
|
22
|
+
return self.parameters_for(self.col)
|
|
23
|
+
|
|
24
|
+
def __str__(self):
|
|
25
|
+
return '{} IS NULL'.format(
|
|
26
|
+
str(self.col),
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class IsNotNull(Expr):
|
|
31
|
+
def __init__(self, col: Union[Type, Function, Expr]):
|
|
32
|
+
self.col = col
|
|
33
|
+
return
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def parameters(self):
|
|
37
|
+
return self.parameters_for(self.col)
|
|
38
|
+
|
|
39
|
+
def __str__(self):
|
|
40
|
+
return '{} IS NOT NULL'.format(
|
|
41
|
+
str(self.col),
|
|
42
|
+
)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from ..Expr import Expr
|
|
4
|
+
|
|
5
|
+
from ..Type import Type
|
|
6
|
+
from ..Function import Function
|
|
7
|
+
|
|
8
|
+
from typing import Union, Any
|
|
9
|
+
|
|
10
|
+
__all__ = (
|
|
11
|
+
'Equal',
|
|
12
|
+
'NotEqual',
|
|
13
|
+
'GreaterThan',
|
|
14
|
+
'GreaterThanOrEqual',
|
|
15
|
+
'LessThanOrEqual',
|
|
16
|
+
'LessThan',
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Compare(Expr):
|
|
21
|
+
operator = ''
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
col: Union[str, Type, Function, Expr],
|
|
26
|
+
other: Union[Any, Type, Function],
|
|
27
|
+
) -> None:
|
|
28
|
+
self.col = col
|
|
29
|
+
self.value = other
|
|
30
|
+
self.other = (
|
|
31
|
+
str(other) if isinstance(other, (Type, Function))
|
|
32
|
+
else self.bind(other)
|
|
33
|
+
)
|
|
34
|
+
return
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def parameters(self):
|
|
38
|
+
return (
|
|
39
|
+
self.parameters_for(self.col)
|
|
40
|
+
+ self.parameters_for(self.value, bind=True)
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
def __str__(self):
|
|
44
|
+
return '{} {} {}'.format(
|
|
45
|
+
str(self.col),
|
|
46
|
+
self.operator,
|
|
47
|
+
self.expression(self.value),
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class Equal(Compare):
|
|
52
|
+
"""SQL equality expression."""
|
|
53
|
+
|
|
54
|
+
operator = '='
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class NotEqual(Compare):
|
|
58
|
+
"""SQL inequality expression."""
|
|
59
|
+
|
|
60
|
+
operator = '!='
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class GreaterThanOrEqual(Compare):
|
|
64
|
+
"""SQL greater-than-or-equal expression."""
|
|
65
|
+
|
|
66
|
+
operator = '>='
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class GreaterThan(Compare):
|
|
70
|
+
"""SQL greater-than expression."""
|
|
71
|
+
|
|
72
|
+
operator = '>'
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class LessThanOrEqual(Compare):
|
|
76
|
+
"""SQL less-than-or-equal expression."""
|
|
77
|
+
|
|
78
|
+
operator = '<='
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class LessThan(Compare):
|
|
82
|
+
"""SQL less-than expression."""
|
|
83
|
+
|
|
84
|
+
operator = '<'
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from ..Expr import Expr
|
|
4
|
+
|
|
5
|
+
__all__ = (
|
|
6
|
+
'And',
|
|
7
|
+
'Or',
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class And(Expr):
|
|
12
|
+
def __init__(self, *args) -> None:
|
|
13
|
+
self.args = args
|
|
14
|
+
return
|
|
15
|
+
|
|
16
|
+
def __str__(self):
|
|
17
|
+
return ' AND '.join([str(arg) for arg in self.args])
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def parameters(self):
|
|
21
|
+
return tuple(
|
|
22
|
+
value
|
|
23
|
+
for arg in self.args
|
|
24
|
+
for value in self.parameters_for(arg)
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Or(Expr):
|
|
29
|
+
def __init__(self, *args) -> None:
|
|
30
|
+
self.args = args
|
|
31
|
+
return
|
|
32
|
+
|
|
33
|
+
def __str__(self):
|
|
34
|
+
return ' OR '.join([str(arg) for arg in self.args])
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def parameters(self):
|
|
38
|
+
return tuple(
|
|
39
|
+
value
|
|
40
|
+
for arg in self.args
|
|
41
|
+
for value in self.parameters_for(arg)
|
|
42
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from typing import Union
|
|
4
|
+
|
|
5
|
+
from ..Expr import Expr
|
|
6
|
+
from ..Function import Function
|
|
7
|
+
from ..Type import Type
|
|
8
|
+
|
|
9
|
+
__all__ = (
|
|
10
|
+
'Like'
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Like(Expr):
|
|
15
|
+
"""SQL LIKE expression."""
|
|
16
|
+
|
|
17
|
+
def __init__(self, col: Union[Type, Function, Expr], other) -> None:
|
|
18
|
+
self.col = col
|
|
19
|
+
self.other = other
|
|
20
|
+
self.bind(other)
|
|
21
|
+
return
|
|
22
|
+
|
|
23
|
+
def __str__(self):
|
|
24
|
+
return '{} LIKE {}'.format(
|
|
25
|
+
str(self.col),
|
|
26
|
+
'?',
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def parameters(self):
|
|
31
|
+
return (
|
|
32
|
+
self.parameters_for(self.col)
|
|
33
|
+
+ (self.bind(self.other),)
|
|
34
|
+
)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from .Operator import (
|
|
4
|
+
And,
|
|
5
|
+
Or,
|
|
6
|
+
)
|
|
7
|
+
from .Compare import (
|
|
8
|
+
Equal,
|
|
9
|
+
NotEqual,
|
|
10
|
+
GreaterThan,
|
|
11
|
+
GreaterThanOrEqual,
|
|
12
|
+
LessThan,
|
|
13
|
+
LessThanOrEqual,
|
|
14
|
+
)
|
|
15
|
+
from .String import (
|
|
16
|
+
Like,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
__all__ = (
|
|
21
|
+
'And',
|
|
22
|
+
'Or',
|
|
23
|
+
'Equal',
|
|
24
|
+
'NotEqual',
|
|
25
|
+
'Like',
|
|
26
|
+
'GreaterThan',
|
|
27
|
+
'GreaterThanOrEqual',
|
|
28
|
+
'LessThan',
|
|
29
|
+
'LessThanOrEqual',
|
|
30
|
+
)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from abc import ABCMeta, abstractmethod
|
|
4
|
+
|
|
5
|
+
__all__ = (
|
|
6
|
+
'Function'
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Function(metaclass=ABCMeta):
|
|
11
|
+
"""Function Interface"""
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
def parameters(self):
|
|
15
|
+
return ()
|
|
16
|
+
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def __str__(self):
|
|
19
|
+
raise NotImplementedError('{} must be implemented __str__'.format(self.__class__.__name__))
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from typing import Union
|
|
4
|
+
|
|
5
|
+
from ..Expr import Expr
|
|
6
|
+
from ..Function import Function
|
|
7
|
+
from ..Type import Type
|
|
8
|
+
|
|
9
|
+
__all__ = (
|
|
10
|
+
'Count',
|
|
11
|
+
'Sum',
|
|
12
|
+
'Average',
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Count(Function):
|
|
17
|
+
def __init__(self, col: Union[Type, Function, Expr]):
|
|
18
|
+
self.col = col
|
|
19
|
+
return
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def parameters(self):
|
|
23
|
+
return Expr.parameters_for(self.col)
|
|
24
|
+
|
|
25
|
+
def __str__(self):
|
|
26
|
+
return 'COUNT({})'.format(str(self.col))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Sum(Function):
|
|
30
|
+
def __init__(self, col: Union[Type, Function, Expr]):
|
|
31
|
+
self.col = col
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def parameters(self):
|
|
36
|
+
return Expr.parameters_for(self.col)
|
|
37
|
+
|
|
38
|
+
def __str__(self):
|
|
39
|
+
return 'SUM({})'.format(str(self.col))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class Average(Function):
|
|
43
|
+
def __init__(self, col: Union[Type, Function, Expr]):
|
|
44
|
+
self.col = col
|
|
45
|
+
return
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def parameters(self):
|
|
49
|
+
return Expr.parameters_for(self.col)
|
|
50
|
+
|
|
51
|
+
def __str__(self):
|
|
52
|
+
return 'AVG({})'.format(str(self.col))
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from typing import Union
|
|
4
|
+
|
|
5
|
+
from ..Function import Function
|
|
6
|
+
from ..Expr import Expr
|
|
7
|
+
from ..Type import Type
|
|
8
|
+
|
|
9
|
+
__all__ = (
|
|
10
|
+
'IfNull'
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class IfNull(Function):
|
|
15
|
+
def __init__(self, attr: Union[Type, Function, Expr], value: any):
|
|
16
|
+
self.attr = attr
|
|
17
|
+
self.value = value
|
|
18
|
+
return
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def parameters(self):
|
|
22
|
+
return (
|
|
23
|
+
Expr.parameters_for(self.attr)
|
|
24
|
+
+ Expr.parameters_for(self.value, bind=True)
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
def __str__(self):
|
|
28
|
+
return 'IFNULL({}, {})'.format(
|
|
29
|
+
str(self.attr),
|
|
30
|
+
Expr.expression(self.value),
|
|
31
|
+
)
|