half-orm 1.1.0__tar.gz → 1.2.1__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.
- {half_orm-1.1.0/half_orm.egg-info → half_orm-1.2.1}/PKG-INFO +11 -2
- {half_orm-1.1.0 → half_orm-1.2.1}/README.md +10 -1
- half_orm-1.2.1/half_orm/_predicate.py +56 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/field.py +66 -9
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/fkey.py +17 -4
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/model.py +171 -16
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/pg_meta.py +36 -13
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/relation.py +144 -5
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/relation_factory.py +1 -1
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/testing.py +16 -35
- half_orm-1.2.1/half_orm/transaction.py +473 -0
- half_orm-1.2.1/half_orm/version.txt +1 -0
- {half_orm-1.1.0 → half_orm-1.2.1/half_orm.egg-info}/PKG-INFO +11 -2
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm.egg-info/SOURCES.txt +1 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/pyproject.toml +1 -1
- half_orm-1.1.0/half_orm/transaction.py +0 -236
- half_orm-1.1.0/half_orm/version.txt +0 -1
- {half_orm-1.1.0 → half_orm-1.2.1}/AUTHORS +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/LICENSE +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/__init__.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/__main__.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/cli.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/cli_utils.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/field_errors.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/hotest.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/migrations/BREAKING_CHANGES-1.0.0.md +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/migrations/__init__.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/model_errors.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/null.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/py.typed +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/relation_errors.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/sql_adapter.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/sql_ast.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm/utils.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm.egg-info/dependency_links.txt +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm.egg-info/entry_points.txt +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm.egg-info/requires.txt +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/half_orm.egg-info/top_level.txt +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/setup.cfg +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/test/test_cli.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/test/test_main.py +0 -0
- {half_orm-1.1.0 → half_orm-1.2.1}/test/test_sql_ast.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: half_orm
|
|
3
|
-
Version: 1.1
|
|
3
|
+
Version: 1.2.1
|
|
4
4
|
Summary: A database-first ORM for PostgreSQL
|
|
5
5
|
Author-email: Joël Maïzi <joel.maizi@collorg.org>
|
|
6
6
|
License-Expression: GPL-3.0-or-later
|
|
@@ -81,7 +81,9 @@ with your data. No migrations, no code generation.
|
|
|
81
81
|
**The central idea:** a `Relation` object is a **predicate**. It describes the
|
|
82
82
|
logical condition that rows must satisfy to belong to the relation. Its
|
|
83
83
|
*extension* — the set of rows currently satisfying the predicate in the
|
|
84
|
-
database — is what you read, update, or delete.
|
|
84
|
+
database — is what you read, update, or delete. Why a predicate rather than an
|
|
85
|
+
object mapped to a row is argued in
|
|
86
|
+
[A Relation Is a Predicate, Not an Object](https://half-orm.github.io/half-orm/dev/essays/relation-as-predicate/).
|
|
85
87
|
|
|
86
88
|
## Key features
|
|
87
89
|
|
|
@@ -164,6 +166,13 @@ Author(id=alice['id']).ho_delete()
|
|
|
164
166
|
|
|
165
167
|
- [Learn halfORM in half an hour](https://half-orm.github.io/half-orm/dev/half-an-hour/)
|
|
166
168
|
- [API Reference](https://half-orm.github.io/half-orm/dev/api/relation/)
|
|
169
|
+
- [Why database-first](https://half-orm.github.io/half-orm/dev/essays/) — the argument
|
|
170
|
+
behind the design, in six essays
|
|
171
|
+
- [Breaking changes](https://half-orm.github.io/half-orm/latest/breaking-changes/) — and
|
|
172
|
+
what to do coming from a 0.x release, all of which were published as Beta
|
|
173
|
+
- [Security](https://half-orm.github.io/half-orm/latest/security/) — advisories by
|
|
174
|
+
release line, and how to report a vulnerability
|
|
175
|
+
- [What halfORM defends, and what it cannot](https://half-orm.github.io/half-orm/latest/security-model/)
|
|
167
176
|
|
|
168
177
|
## Extensions
|
|
169
178
|
|
|
@@ -52,7 +52,9 @@ with your data. No migrations, no code generation.
|
|
|
52
52
|
**The central idea:** a `Relation` object is a **predicate**. It describes the
|
|
53
53
|
logical condition that rows must satisfy to belong to the relation. Its
|
|
54
54
|
*extension* — the set of rows currently satisfying the predicate in the
|
|
55
|
-
database — is what you read, update, or delete.
|
|
55
|
+
database — is what you read, update, or delete. Why a predicate rather than an
|
|
56
|
+
object mapped to a row is argued in
|
|
57
|
+
[A Relation Is a Predicate, Not an Object](https://half-orm.github.io/half-orm/dev/essays/relation-as-predicate/).
|
|
56
58
|
|
|
57
59
|
## Key features
|
|
58
60
|
|
|
@@ -135,6 +137,13 @@ Author(id=alice['id']).ho_delete()
|
|
|
135
137
|
|
|
136
138
|
- [Learn halfORM in half an hour](https://half-orm.github.io/half-orm/dev/half-an-hour/)
|
|
137
139
|
- [API Reference](https://half-orm.github.io/half-orm/dev/api/relation/)
|
|
140
|
+
- [Why database-first](https://half-orm.github.io/half-orm/dev/essays/) — the argument
|
|
141
|
+
behind the design, in six essays
|
|
142
|
+
- [Breaking changes](https://half-orm.github.io/half-orm/latest/breaking-changes/) — and
|
|
143
|
+
what to do coming from a 0.x release, all of which were published as Beta
|
|
144
|
+
- [Security](https://half-orm.github.io/half-orm/latest/security/) — advisories by
|
|
145
|
+
release line, and how to report a vulnerability
|
|
146
|
+
- [What halfORM defends, and what it cannot](https://half-orm.github.io/half-orm/latest/security-model/)
|
|
138
147
|
|
|
139
148
|
## Extensions
|
|
140
149
|
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
"""Canonicalisation of the predicate structure returned by
|
|
5
|
+
:meth:`~half_orm.relation.Relation.ho_where_display`.
|
|
6
|
+
|
|
7
|
+
Kept in a dependency-free leaf module so that both
|
|
8
|
+
:mod:`half_orm.relation` and :mod:`half_orm.testing` can use it without
|
|
9
|
+
either importing the other, and so that importing the testing helpers
|
|
10
|
+
does not pull in the database driver.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
__all__ = ['canonical_predicate']
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def canonical_predicate(node):
|
|
17
|
+
"""Return a hashable, alias-free, canonical form of a predicate node.
|
|
18
|
+
|
|
19
|
+
The form produced is a nested tuple, so it can be used as a dictionary
|
|
20
|
+
key or a set member. Relation aliases (``r{ho_id}``), which differ
|
|
21
|
+
between instances of the same query, are dropped, and the operands of
|
|
22
|
+
the commutative operators (``or``, ``and``) are ordered, so that
|
|
23
|
+
``A | B`` and ``B | A`` canonicalise identically. ``and not`` is not
|
|
24
|
+
commutative and keeps its operand order.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
node (dict | None): a node as returned by
|
|
28
|
+
:meth:`~half_orm.relation.Relation.ho_where_display`.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
tuple | None: the canonical form, or ``None`` for an unconstrained
|
|
32
|
+
predicate. Note that ``None`` says nothing about *which* relation
|
|
33
|
+
is unconstrained; callers needing to tell two unconstrained
|
|
34
|
+
relations apart must tag the result with the relation itself, as
|
|
35
|
+
:meth:`~half_orm.relation.Relation.ho_structural_key` does.
|
|
36
|
+
"""
|
|
37
|
+
if node is None:
|
|
38
|
+
return None
|
|
39
|
+
op = node.get('operator')
|
|
40
|
+
if op == 'neg':
|
|
41
|
+
return ('neg', canonical_predicate(node['operand']))
|
|
42
|
+
if op:
|
|
43
|
+
left = canonical_predicate(node['left'])
|
|
44
|
+
right = canonical_predicate(node.get('right'))
|
|
45
|
+
if op in ('or', 'and'):
|
|
46
|
+
return (op, *sorted([left, right], key=repr))
|
|
47
|
+
return (op, left, right) # 'and not' is not commutative
|
|
48
|
+
tables = frozenset(node.get('tables', set()))
|
|
49
|
+
constraints = tuple(sorted(
|
|
50
|
+
(c['relation'][0], c['field'], c['comp'], str(c['value']))
|
|
51
|
+
for c in node.get('constraints', [])
|
|
52
|
+
))
|
|
53
|
+
leaf = (tables, constraints)
|
|
54
|
+
# A negated compound arrives as a 'neg' node; a negated leaf carries the
|
|
55
|
+
# flag instead. Both canonicalise to the same shape.
|
|
56
|
+
return ('neg', leaf) if node.get('neg') else leaf
|
|
@@ -10,6 +10,7 @@ import warnings
|
|
|
10
10
|
import yaml
|
|
11
11
|
from collections.abc import Iterable
|
|
12
12
|
from half_orm.null import NULL
|
|
13
|
+
from half_orm.pg_meta import quote_ident
|
|
13
14
|
from half_orm.sql_adapter import SQL_ADAPTER
|
|
14
15
|
from half_orm.sql_ast import FieldExpr
|
|
15
16
|
|
|
@@ -93,6 +94,32 @@ def check_comparator(comp: str) -> str:
|
|
|
93
94
|
f"'=', '!=', '<', '>=', '@>', '@@'.")
|
|
94
95
|
|
|
95
96
|
|
|
97
|
+
# A type name PostgreSQL reads the same bare or quoted. Anything else -- an
|
|
98
|
+
# upper-case letter, a space, a quote, a semicolon -- is quoted before it is
|
|
99
|
+
# written into a cast.
|
|
100
|
+
_BARE_TYPE_NAME_RE = re.compile(r'^[a-z_][a-z0-9_]*$')
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _cast_to(sql_type: str) -> str:
|
|
104
|
+
"""Render the ``::type`` cast applied to a bound value.
|
|
105
|
+
|
|
106
|
+
The type name is read from the catalog and interpolated, no parameter
|
|
107
|
+
being able to stand for it. PostgreSQL lets a user-defined type be called
|
|
108
|
+
anything -- ``create type "int4); drop table t; --"`` is legal -- so a
|
|
109
|
+
name that is not a plain lower-case identifier is quoted, and can then
|
|
110
|
+
only ever be read as a name. Plain ones are left bare, which keeps the
|
|
111
|
+
statements for every built-in type exactly as they were.
|
|
112
|
+
"""
|
|
113
|
+
if _BARE_TYPE_NAME_RE.match(sql_type):
|
|
114
|
+
return f'::{sql_type}'
|
|
115
|
+
return f'::{quote_ident(sql_type)}'
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# What IS / IS NOT may be followed by. They take a keyword, not a value, so
|
|
119
|
+
# none of this can be bound as a parameter.
|
|
120
|
+
_IS_OPERANDS = {True: 'TRUE', False: 'FALSE'}
|
|
121
|
+
|
|
122
|
+
|
|
96
123
|
class Expr:
|
|
97
124
|
"""A raw SQL expression for use with :meth:`Field.set`.
|
|
98
125
|
|
|
@@ -313,9 +340,17 @@ class Field():
|
|
|
313
340
|
select. Otherwise, returns the field name quoted with ".
|
|
314
341
|
"""
|
|
315
342
|
ho_id = f'r{ho_id}'
|
|
343
|
+
# quote_ident, not a bare pair of quotes: a column may be called
|
|
344
|
+
# `a"b`, and the quote inside it would otherwise close the identifier.
|
|
316
345
|
if query == 'select':
|
|
317
|
-
return f'{ho_id}.
|
|
318
|
-
return
|
|
346
|
+
return f'{ho_id}.{quote_ident(self.__name)}'
|
|
347
|
+
return quote_ident(self.__name)
|
|
348
|
+
|
|
349
|
+
def __is_operand(self):
|
|
350
|
+
"""The keyword following IS / IS NOT for the current value."""
|
|
351
|
+
if self.__value is NULL:
|
|
352
|
+
return 'NULL'
|
|
353
|
+
return _IS_OPERANDS[self.__value]
|
|
319
354
|
|
|
320
355
|
def _where_repr(self, query, ho_id):
|
|
321
356
|
"""Returns the SQL representation of the field for the where clause
|
|
@@ -329,9 +364,11 @@ class Field():
|
|
|
329
364
|
comp = 'in'
|
|
330
365
|
cast = ''
|
|
331
366
|
if self.__value != NULL and not isiterable:
|
|
332
|
-
cast =
|
|
367
|
+
cast = _cast_to(self.__sql_type)
|
|
333
368
|
rhs = _OPERATOR_RHS_TEMPLATES.get((comp, self.__sql_type), f'{comp_str}{cast}')
|
|
334
|
-
if
|
|
369
|
+
if comp in ('is', 'is not'):
|
|
370
|
+
where_repr = f"{self.__praf(query, ho_id)} {comp} {self.__is_operand()}"
|
|
371
|
+
elif col_is_array and comp == '=':
|
|
335
372
|
where_repr = f'{comp_str} = ANY({self.__praf(query, ho_id)})'
|
|
336
373
|
elif not self.unaccent or not self._is_text_like:
|
|
337
374
|
where_repr = f"{self.__praf(query, ho_id)} {comp} {rhs}"
|
|
@@ -366,12 +403,21 @@ class Field():
|
|
|
366
403
|
isiterable = type(self.__value) in {tuple, list, set}
|
|
367
404
|
col_is_array = self.__sql_type[0] == '_'
|
|
368
405
|
comp = self._comp()
|
|
406
|
+
if comp in ('is', 'is not'):
|
|
407
|
+
# A keyword, written into the statement: `IS %s` is not SQL.
|
|
408
|
+
return FieldExpr(
|
|
409
|
+
column=self.__praf(query, ho_id),
|
|
410
|
+
comp=comp,
|
|
411
|
+
placeholder='',
|
|
412
|
+
value=None,
|
|
413
|
+
col_ref=self.__is_operand(),
|
|
414
|
+
)
|
|
369
415
|
if comp == '=' and isiterable:
|
|
370
416
|
comp = 'in'
|
|
371
417
|
is_array_any = col_is_array and comp == '='
|
|
372
418
|
cast = ''
|
|
373
419
|
if not is_array_any and self.__value != NULL and not isiterable:
|
|
374
|
-
cast =
|
|
420
|
+
cast = _cast_to(self.__sql_type)
|
|
375
421
|
placeholder = _OPERATOR_RHS_TEMPLATES.get((comp, self.__sql_type), f'{comp_str}{cast}')
|
|
376
422
|
return FieldExpr(
|
|
377
423
|
column=self.__praf(query, ho_id),
|
|
@@ -407,7 +453,8 @@ class Field():
|
|
|
407
453
|
``=``. Supported comparators: ``=``, ``!=``, ``<``, ``<=``,
|
|
408
454
|
``>``, ``>=``, ``like``, ``not like``, ``ilike``, ``not ilike``,
|
|
409
455
|
``similar to``, ``not similar to``, ``in``, ``not in``, ``is``,
|
|
410
|
-
``is not
|
|
456
|
+
``is not`` (with ``NULL``, ``True`` or ``False`` only),
|
|
457
|
+
``is distinct from``, ``is not distinct from``, and
|
|
411
458
|
any operator spelled with PostgreSQL's operator characters —
|
|
412
459
|
including those added by extensions, such as ``@@``, ``@>``,
|
|
413
460
|
``&&`` or pgvector's ``<->``.
|
|
@@ -496,6 +543,15 @@ class Field():
|
|
|
496
543
|
value = tuple(value)
|
|
497
544
|
if value is NULL and comp not in {'is', 'is not'}:
|
|
498
545
|
raise ValueError("comp should be 'is' or 'is not' with NULL value!")
|
|
546
|
+
if comp in {'is', 'is not'} and value is not NULL \
|
|
547
|
+
and not isinstance(value, bool):
|
|
548
|
+
# Whatever the value, the condition used to be rendered as
|
|
549
|
+
# `IS NULL`: ('is', True) silently selected the rows where the
|
|
550
|
+
# column was NULL instead of those where it was true.
|
|
551
|
+
raise ValueError(
|
|
552
|
+
f"'{comp}' takes NULL, True or False, got {value!r}. Use "
|
|
553
|
+
"'is distinct from' / 'is not distinct from' to compare "
|
|
554
|
+
"with a value.")
|
|
499
555
|
self.__is_set = True
|
|
500
556
|
self.__value = value
|
|
501
557
|
self.__comp = comp
|
|
@@ -552,9 +608,10 @@ class Field():
|
|
|
552
608
|
|
|
553
609
|
def _comp(self):
|
|
554
610
|
"Returns the comparator associated to the value."
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
611
|
+
# Doubled wherever it appears, not only when it is the whole
|
|
612
|
+
# operator: pg_trgm's `%>` and `<%` reached psycopg as a malformed
|
|
613
|
+
# placeholder.
|
|
614
|
+
return self.__comp.replace('%', '%%')
|
|
558
615
|
|
|
559
616
|
@property
|
|
560
617
|
def _relation(self):
|
|
@@ -11,7 +11,7 @@ FKs. They are exposed as attributes on relation instances and used in two ways:
|
|
|
11
11
|
* **``.set(rel)``** to add a JOIN condition on the owning relation.
|
|
12
12
|
"""
|
|
13
13
|
|
|
14
|
-
from half_orm.pg_meta import normalize_fqrn, normalize_qrn
|
|
14
|
+
from half_orm.pg_meta import normalize_fqrn, normalize_qrn, quote_ident, unquote_ident
|
|
15
15
|
from half_orm import utils
|
|
16
16
|
|
|
17
17
|
def _would_create_cycle(from_rel, to_rel):
|
|
@@ -42,10 +42,17 @@ class FKey:
|
|
|
42
42
|
class Post(blog.get_relation_class('blog.post')):
|
|
43
43
|
Fkeys = {
|
|
44
44
|
'author_fk': 'post_author_id_fkey',
|
|
45
|
-
'comment_rfk': '
|
|
45
|
+
'comment_rfk': '_reverse_fkey_blog_blog_comment_post_id',
|
|
46
46
|
}
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
+
A direct FK carries the constraint name PostgreSQL gave it. A reverse FK
|
|
50
|
+
has no constraint of its own, so one is built from the referencing side:
|
|
51
|
+
``_reverse_fkey_<database>_<schema>_<table>_<field>``. The database name
|
|
52
|
+
is part of it, which is why ``blog`` appears twice above -- the database
|
|
53
|
+
and the schema happen to share a name. It is not a typo, and dropping the
|
|
54
|
+
component yields a name no relation answers to.
|
|
55
|
+
|
|
49
56
|
Use a FK attribute in two ways:
|
|
50
57
|
|
|
51
58
|
* **Call it** (``post.author_fk()``) to navigate: returns the related
|
|
@@ -72,7 +79,10 @@ class FKey:
|
|
|
72
79
|
self.__confdeltype = confdeltype
|
|
73
80
|
self.__fk_fqrn = fk_sfqrn
|
|
74
81
|
self.__fields_names = fields
|
|
75
|
-
|
|
82
|
+
# Unquoted first: navigating a key hands the reverse one names that
|
|
83
|
+
# are already quoted. Then quoted properly, a bare pair of quotes
|
|
84
|
+
# leaving a quote inside the name free to close the identifier.
|
|
85
|
+
self.__fields = [quote_ident(unquote_ident(name)) for name in fields]
|
|
76
86
|
|
|
77
87
|
def __get_rel(self, fqtn):
|
|
78
88
|
"""Returns the relation class referenced by fqtn.
|
|
@@ -216,7 +226,10 @@ class FKey:
|
|
|
216
226
|
if from_._qrn == orig_rel._qrn:
|
|
217
227
|
from_id = orig_rel_id
|
|
218
228
|
from_fields = (f'{from_id}.{name}' for name in self.__fields)
|
|
219
|
-
|
|
229
|
+
# Quoted here: the names read from the catalog arrive bare, and
|
|
230
|
+
# went into the join condition as they were.
|
|
231
|
+
to_fields = (f'{to_id}.{quote_ident(unquote_ident(name))}'
|
|
232
|
+
for name in self.__fk_names)
|
|
220
233
|
bounds = " and ".join(
|
|
221
234
|
[f'{a} = {b}' for a, b in zip(to_fields, from_fields)])
|
|
222
235
|
return f"({bounds})"
|
|
@@ -262,8 +262,9 @@ class Model:
|
|
|
262
262
|
never written to the logs, in any mode — but extensions do:
|
|
263
263
|
half_orm_dev makes a server read-only when it is set.
|
|
264
264
|
|
|
265
|
-
``crud_only`` refuses :meth:`execute_query
|
|
266
|
-
:meth:`
|
|
265
|
+
``crud_only`` refuses :meth:`execute_query`,
|
|
266
|
+
:meth:`execute_function`, :meth:`call_procedure` and their async
|
|
267
|
+
variants, leaving only the Relation CRUD methods.
|
|
267
268
|
It is a guard rail against accidents, not a privilege boundary:
|
|
268
269
|
use ``GRANT``/``REVOKE`` to actually restrict what a role can do.
|
|
269
270
|
scope (str | None): package name used to resolve registered subclasses.
|
|
@@ -287,8 +288,15 @@ class Model:
|
|
|
287
288
|
MalformedConfigFile: if ``name`` is missing from the file.
|
|
288
289
|
psycopg.OperationalError: if the database connection fails.
|
|
289
290
|
"""
|
|
290
|
-
|
|
291
|
-
|
|
291
|
+
# Models and relation classes are shared between the Model objects that
|
|
292
|
+
# reach the same database *as the same role*: (host, port, dbname, user),
|
|
293
|
+
# read off the connection. They used to be shared by database name alone,
|
|
294
|
+
# so a second Model opened on the same database under another role -- a
|
|
295
|
+
# read-only one next to an administrative one -- was handed the classes of
|
|
296
|
+
# the first, and ran its queries through the first one's connection.
|
|
297
|
+
__deja_vu = {} # identity -> Model
|
|
298
|
+
__registries = {} # identity -> {dbname: {fqrn: relation class}}
|
|
299
|
+
__first_seen = {} # dbname -> Model, for the _deja_vu() lookup by name
|
|
292
300
|
__sql_trace = False
|
|
293
301
|
def __init__(self, config_file: None, scope: str=None, with_half_orm_meta=False):
|
|
294
302
|
self.__connection_params = {}
|
|
@@ -298,9 +306,44 @@ class Model:
|
|
|
298
306
|
self.__thread_local = threading.local()
|
|
299
307
|
self.__with_half_orm_meta = _normalize_with_half_orm_meta(with_half_orm_meta)
|
|
300
308
|
self.__schema_generation = 0
|
|
309
|
+
self.__read_sets = {}
|
|
301
310
|
self.__aconn = None
|
|
311
|
+
self.__identity = None
|
|
302
312
|
self.__connect()
|
|
303
313
|
|
|
314
|
+
@property
|
|
315
|
+
def _classes_(self):
|
|
316
|
+
"""The relation classes registered for this Model's connection identity.
|
|
317
|
+
|
|
318
|
+
``{dbname: {(dbname, schema, relation): class}}``, shared by every
|
|
319
|
+
Model connected to the same database as the same role, and by no
|
|
320
|
+
other.
|
|
321
|
+
"""
|
|
322
|
+
return Model.__registries.setdefault(self.__identity, {})
|
|
323
|
+
|
|
324
|
+
def __set_identity(self, conn):
|
|
325
|
+
"""Record who this Model is connected as, and where.
|
|
326
|
+
|
|
327
|
+
Taken from the connection rather than from the configuration: an
|
|
328
|
+
omitted host, port or user is resolved by libpq, and only the result
|
|
329
|
+
says whether two Models may answer for one another.
|
|
330
|
+
|
|
331
|
+
``reconnect(config_file)`` may change the role. The classes already
|
|
332
|
+
generated stay bound to this Model -- that is what reconnecting a
|
|
333
|
+
Model means -- so their registry follows it, and the identity it
|
|
334
|
+
leaves stops designating it.
|
|
335
|
+
"""
|
|
336
|
+
info = conn.info
|
|
337
|
+
identity = (info.host, info.port, info.dbname, info.user)
|
|
338
|
+
previous = self.__identity
|
|
339
|
+
if previous is not None and previous != identity \
|
|
340
|
+
and Model.__deja_vu.get(previous) is self:
|
|
341
|
+
del Model.__deja_vu[previous]
|
|
342
|
+
registry = Model.__registries.pop(previous, None)
|
|
343
|
+
if registry is not None and identity not in Model.__deja_vu:
|
|
344
|
+
Model.__registries[identity] = registry
|
|
345
|
+
self.__identity = identity
|
|
346
|
+
|
|
304
347
|
def has_extension(self, name: str) -> bool:
|
|
305
348
|
"""True if the PostgreSQL extension `name` is installed on this database.
|
|
306
349
|
|
|
@@ -421,14 +464,17 @@ class Model:
|
|
|
421
464
|
conn.adapters.register_dumper(Field, FieldDumper)
|
|
422
465
|
conn.adapters.register_dumper(dict, JsonbDumper)
|
|
423
466
|
self.__pg_meta = pg_meta.PgMeta(conn, self.__with_half_orm_meta, reload)
|
|
467
|
+
self.__set_identity(conn)
|
|
424
468
|
if reload:
|
|
425
469
|
self.__schema_generation += 1
|
|
470
|
+
self.__read_sets = {}
|
|
426
471
|
self._classes_[self._dbname] = {}
|
|
427
|
-
|
|
472
|
+
Model.__deja_vu[self.__identity] = self
|
|
473
|
+
Model.__first_seen[self.__dbname] = self
|
|
428
474
|
self.__thread_local.conn = conn
|
|
429
475
|
self.__thread_local.schema_generation = self.__schema_generation
|
|
430
|
-
|
|
431
|
-
|
|
476
|
+
Model.__deja_vu.setdefault(self.__identity, self)
|
|
477
|
+
Model.__first_seen.setdefault(self.__dbname, self)
|
|
432
478
|
|
|
433
479
|
reconnect = __connect
|
|
434
480
|
|
|
@@ -554,7 +600,7 @@ class Model:
|
|
|
554
600
|
# Preferred: subclass and register
|
|
555
601
|
@register
|
|
556
602
|
class Author(blog.get_relation_class('blog.author')):
|
|
557
|
-
Fkeys = {'post_rfk': '
|
|
603
|
+
Fkeys = {'post_rfk': '_reverse_fkey_blog_blog_post_author_id'}
|
|
558
604
|
```
|
|
559
605
|
|
|
560
606
|
"""
|
|
@@ -570,17 +616,19 @@ class Model:
|
|
|
570
616
|
if len(parts) < 2:
|
|
571
617
|
raise model_errors.MissingSchemaInName(relation_name)
|
|
572
618
|
schema, table = '.'.join(parts[:-1]), parts[-1]
|
|
573
|
-
return factory({'fqrn': (self.__dbname, schema, table), 'model':
|
|
619
|
+
return factory({'fqrn': (self.__dbname, schema, table), 'model': Model.__deja_vu[self.__identity], 'fields_aliases':fields_aliases})
|
|
574
620
|
|
|
575
621
|
|
|
576
622
|
@staticmethod
|
|
577
623
|
def _deja_vu(dbname):
|
|
578
624
|
"""Returns None if the database hasn't been loaded yet.
|
|
579
|
-
Otherwise, it returns the Model object
|
|
580
|
-
|
|
581
|
-
|
|
625
|
+
Otherwise, it returns the first Model object loaded for that name.
|
|
626
|
+
|
|
627
|
+
A name does not say which server or which role: relation classes are
|
|
628
|
+
shared per connection identity (see ``_classes_``), and this lookup
|
|
629
|
+
is not what binds them to a Model.
|
|
582
630
|
"""
|
|
583
|
-
return Model.
|
|
631
|
+
return Model.__first_seen.get(dbname)
|
|
584
632
|
|
|
585
633
|
@property
|
|
586
634
|
def __dbname(self):
|
|
@@ -670,12 +718,20 @@ class Model:
|
|
|
670
718
|
f"Query execution failed:\nquery: {query}\n"
|
|
671
719
|
f"values: {_describe_values(values)}\n")
|
|
672
720
|
|
|
721
|
+
# See _execute_query: a connection lost inside a transaction is not
|
|
722
|
+
# replaced behind the caller's back, the retry would commit on its own.
|
|
723
|
+
in_transaction = self.__aconn is not None and not self.__aconn.autocommit
|
|
673
724
|
if self.__aconn is not None and self.__aconn.closed:
|
|
725
|
+
if in_transaction:
|
|
726
|
+
raise psycopg.OperationalError(
|
|
727
|
+
"the connection was lost inside a transaction")
|
|
674
728
|
await self.aconnect()
|
|
675
729
|
try:
|
|
676
730
|
cursor = self._aconnection.cursor(row_factory=dict_row)
|
|
677
731
|
await cursor.execute(query, values)
|
|
678
732
|
except (psycopg.OperationalError, psycopg.InterfaceError):
|
|
733
|
+
if in_transaction:
|
|
734
|
+
raise
|
|
679
735
|
await self.aconnect()
|
|
680
736
|
try:
|
|
681
737
|
cursor = self._aconnection.cursor(row_factory=dict_row)
|
|
@@ -741,6 +797,68 @@ class Model:
|
|
|
741
797
|
"Connection closed. Call model.reconnect() to re-establish.")
|
|
742
798
|
return conn
|
|
743
799
|
|
|
800
|
+
__READ_SET_QUERY = """
|
|
801
|
+
WITH RECURSIVE root AS (
|
|
802
|
+
SELECT (quote_ident(%s) || '.' || quote_ident(%s))::regclass AS oid
|
|
803
|
+
), reads(oid) AS (
|
|
804
|
+
SELECT oid FROM root
|
|
805
|
+
UNION
|
|
806
|
+
SELECT x.oid
|
|
807
|
+
FROM reads r
|
|
808
|
+
CROSS JOIN LATERAL (
|
|
809
|
+
SELECT d.refobjid AS oid
|
|
810
|
+
FROM pg_rewrite rw
|
|
811
|
+
JOIN pg_depend d ON d.objid = rw.oid
|
|
812
|
+
AND d.classid = 'pg_rewrite'::regclass
|
|
813
|
+
AND d.refclassid = 'pg_class'::regclass
|
|
814
|
+
AND d.refobjid <> r.oid
|
|
815
|
+
WHERE rw.ev_class = r.oid
|
|
816
|
+
UNION ALL
|
|
817
|
+
SELECT i.inhrelid
|
|
818
|
+
FROM pg_inherits i
|
|
819
|
+
WHERE i.inhparent = r.oid
|
|
820
|
+
AND NOT (%s AND r.oid = (SELECT oid FROM root))
|
|
821
|
+
) x
|
|
822
|
+
)
|
|
823
|
+
SELECT n.nspname AS schema, c.relname AS name
|
|
824
|
+
FROM reads r
|
|
825
|
+
JOIN pg_class c ON c.oid = r.oid
|
|
826
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
827
|
+
WHERE c.relkind IN ('r', 'p', 'm', 'f')
|
|
828
|
+
"""
|
|
829
|
+
|
|
830
|
+
def _read_set(self, schema, relname, only=False):
|
|
831
|
+
"""Returns the physical relations a read of ``schema.relname`` touches.
|
|
832
|
+
|
|
833
|
+
A view contributes the relations it is defined over, recursively, and
|
|
834
|
+
a table contributes its inheritance children, also recursively, since
|
|
835
|
+
a query without ``ONLY`` returns their rows too. The two expansions
|
|
836
|
+
compose: a view over an inherited table reaches the children of that
|
|
837
|
+
table. Views themselves are left out of the result, having no rows of
|
|
838
|
+
their own.
|
|
839
|
+
|
|
840
|
+
The answer depends only on the schema, so it is computed once per
|
|
841
|
+
``(schema, relname, only)`` and dropped by
|
|
842
|
+
``reconnect(reload=True)``.
|
|
843
|
+
|
|
844
|
+
Args:
|
|
845
|
+
schema (str): schema name.
|
|
846
|
+
relname (str): relation name.
|
|
847
|
+
only (bool): ``True`` when the read uses ``ONLY``, which excludes
|
|
848
|
+
the inheritance children of this relation, though not those
|
|
849
|
+
reached through a view it is defined over.
|
|
850
|
+
|
|
851
|
+
Returns:
|
|
852
|
+
frozenset[str]: ``'schema.name'`` for each physical relation read.
|
|
853
|
+
"""
|
|
854
|
+
key = (schema, relname, only)
|
|
855
|
+
if key not in self.__read_sets:
|
|
856
|
+
self.__read_sets[key] = frozenset(
|
|
857
|
+
f"{row['schema']}.{row['name']}"
|
|
858
|
+
for row in self.execute_query(
|
|
859
|
+
self.__READ_SET_QUERY, (schema, relname, only)))
|
|
860
|
+
return self.__read_sets[key]
|
|
861
|
+
|
|
744
862
|
def _fields_metadata(self, sfqrn):
|
|
745
863
|
"Proxy to PgMeta.fields_meta"
|
|
746
864
|
return self.__pg_meta.fields_meta(self.__dbname, sfqrn)
|
|
@@ -789,13 +907,23 @@ class Model:
|
|
|
789
907
|
def _execute_query(self, query, values=None, mogrify=False):
|
|
790
908
|
"""Internal query executor — no crud_only check. Called by Relation.__execute."""
|
|
791
909
|
values = self._unwrap_values(values)
|
|
792
|
-
|
|
910
|
+
conn = self._connection
|
|
911
|
+
# Read before the query runs: once the connection is lost there is
|
|
912
|
+
# no asking it whether a transaction was open.
|
|
913
|
+
in_transaction = not conn.autocommit
|
|
914
|
+
cursor = conn.cursor(row_factory=dict_row)
|
|
793
915
|
try:
|
|
794
916
|
if mogrify or self.sql_trace:
|
|
795
917
|
client_cur = ClientCursor(self._connection)
|
|
796
918
|
print(client_cur.mogrify(query, values))
|
|
797
919
|
cursor.execute(query, values)
|
|
798
920
|
except (psycopg.OperationalError, psycopg.InterfaceError):
|
|
921
|
+
if in_transaction:
|
|
922
|
+
# The statements already sent went with the connection.
|
|
923
|
+
# Replaying this one on a fresh connection would run it in
|
|
924
|
+
# autocommit mode -- committed on its own, outside the
|
|
925
|
+
# transaction the caller believes it is part of.
|
|
926
|
+
raise
|
|
799
927
|
self.ping()
|
|
800
928
|
cursor = self._connection.cursor(row_factory=dict_row)
|
|
801
929
|
cursor.execute(query, values)
|
|
@@ -808,6 +936,12 @@ class Model:
|
|
|
808
936
|
raise exc
|
|
809
937
|
return cursor
|
|
810
938
|
|
|
939
|
+
def __check_not_crud_only(self):
|
|
940
|
+
"""Refuse direct SQL when the connection is declared `crud_only`."""
|
|
941
|
+
if self._crud_only:
|
|
942
|
+
raise PermissionError(
|
|
943
|
+
"Direct SQL execution is disabled (crud_only = true in connection config).")
|
|
944
|
+
|
|
811
945
|
def execute_query(self, query, values=None, mogrify=False):
|
|
812
946
|
"""Execute a raw SQL query. *Executes SQL.*
|
|
813
947
|
|
|
@@ -845,12 +979,16 @@ class Model:
|
|
|
845
979
|
|
|
846
980
|
Raises:
|
|
847
981
|
RuntimeError: if both ``*args`` and ``**kwargs`` are provided.
|
|
982
|
+
PermissionError: if ``crud_only`` is set in the connection configuration.
|
|
848
983
|
ValueError: if the name or a named parameter is not a SQL
|
|
849
984
|
identifier. The name is interpolated into the statement,
|
|
850
985
|
unlike the arguments, which are bound.
|
|
851
986
|
"""
|
|
852
987
|
if bool(args) and bool(kwargs):
|
|
853
988
|
raise RuntimeError("You can't mix args and kwargs with the execute_function method!")
|
|
989
|
+
# As the async variant always did, going through aexecute_query: a
|
|
990
|
+
# function is arbitrary SQL, not one of the Relation CRUD methods.
|
|
991
|
+
self.__check_not_crud_only()
|
|
854
992
|
fct_name = _check_qualified_name(fct_name, 'function name')
|
|
855
993
|
_check_named_params(kwargs, 'execute_function')
|
|
856
994
|
cursor = self._connection.cursor(row_factory=dict_row)
|
|
@@ -877,12 +1015,14 @@ class Model:
|
|
|
877
1015
|
|
|
878
1016
|
Raises:
|
|
879
1017
|
RuntimeError: if both ``*args`` and ``**kwargs`` are provided.
|
|
1018
|
+
PermissionError: if ``crud_only`` is set in the connection configuration.
|
|
880
1019
|
ValueError: if the name or a named parameter is not a SQL
|
|
881
1020
|
identifier. The name is interpolated into the statement,
|
|
882
1021
|
unlike the arguments, which are bound.
|
|
883
1022
|
"""
|
|
884
1023
|
if bool(args) and bool(kwargs):
|
|
885
1024
|
raise RuntimeError("You can't mix args and kwargs with the call_procedure method!")
|
|
1025
|
+
self.__check_not_crud_only()
|
|
886
1026
|
proc_name = _check_qualified_name(proc_name, 'procedure name')
|
|
887
1027
|
_check_named_params(kwargs, 'call_procedure')
|
|
888
1028
|
if kwargs:
|
|
@@ -1062,11 +1202,26 @@ class Model:
|
|
|
1062
1202
|
# imported modules, so get_relation_class() alone could go
|
|
1063
1203
|
# stale. Fall back to it only when there's no generated
|
|
1064
1204
|
# package to import from, or its conventional class is missing.
|
|
1205
|
+
module_path = f'{package_name}.{module_name}'
|
|
1206
|
+
if not all(part.isidentifier() for part in module_path.split('.')):
|
|
1207
|
+
# As in _import_class: nothing importable can be named
|
|
1208
|
+
# this, and these names come out of the database. A
|
|
1209
|
+
# schema called `..x` would otherwise turn the import
|
|
1210
|
+
# below into a relative one climbing out of the package.
|
|
1211
|
+
yield self.get_relation_class(module_name), relation[0]
|
|
1212
|
+
continue
|
|
1065
1213
|
try:
|
|
1066
1214
|
class_name = pg_meta.camel_case(relation[1][-1])
|
|
1067
|
-
module = importlib.import_module(
|
|
1215
|
+
module = importlib.import_module(module_path)
|
|
1068
1216
|
yield getattr(module, class_name), relation[0]
|
|
1069
|
-
except
|
|
1217
|
+
except ModuleNotFoundError as exc:
|
|
1218
|
+
if not _module_is_absent(exc, module_path):
|
|
1219
|
+
# The module is there; something it imports is not.
|
|
1220
|
+
# That is a defect in the caller's own code, which
|
|
1221
|
+
# falling back to the generated class would hide.
|
|
1222
|
+
raise
|
|
1223
|
+
yield self.get_relation_class(module_name), relation[0]
|
|
1224
|
+
except AttributeError:
|
|
1070
1225
|
yield self.get_relation_class(module_name), relation[0]
|
|
1071
1226
|
|
|
1072
1227
|
@property
|