summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/assignmapper.py
blob: 07ba95a694ae8d176e3853ea2174db9e18aa4e76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from sqlalchemy import mapper, util
import types

def monkeypatch_query_method(ctx, class_, name):
    def do(self, *args, **kwargs):
        query = class_.mapper.query(session=ctx.current)
        return getattr(query, name)(*args, **kwargs)
    setattr(class_, name, classmethod(do))

def monkeypatch_objectstore_method(ctx, class_, name):
    def do(self, *args, **kwargs):
        session = ctx.current
	if name == "flush":
		# flush expects a list of objects
		self = [self]
        return getattr(session, name)(self, *args, **kwargs)
    setattr(class_, name, do)
    
def assign_mapper(ctx, class_, *args, **kwargs):
    kwargs.setdefault("is_primary", True)
    if not isinstance(getattr(class_, '__init__'), types.MethodType):
        def __init__(self, **kwargs):
             for key, value in kwargs.items():
                 setattr(self, key, value)
        class_.__init__ = __init__
    extension = kwargs.pop('extension', None)
    if extension is not None:
        extension = util.to_list(extension)
        extension.append(ctx.mapper_extension)
    else:
        extension = ctx.mapper_extension
    m = mapper(class_, extension=extension, *args, **kwargs)
    class_.mapper = m
    for name in ['get', 'select', 'select_by', 'selectone', 'get_by', 'join_to', 'join_via']:
        monkeypatch_query_method(ctx, class_, name)
    for name in ['flush', 'delete', 'expire', 'refresh', 'expunge', 'merge', 'update', 'save_or_update']:
        monkeypatch_objectstore_method(ctx, class_, name)