-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextend_example.py
59 lines (39 loc) · 1.25 KB
/
extend_example.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from mapper import ObjectMapper
class Person(object):
def __init__(self):
self.name = 'john'
self.age = 22
#this properties are ignored
self._some_information = 'Not saved in table'
self.__other_stuff = 'Also ignored'
class PersonMapper(ObjectMapper):
"""
This class overrides get_table and get_id methods to
customize the ObjectMapper according to the tables I have.
Usage demo and tests
>>> p = Person()
>>> m = PersonMapper(p)
>>> m.insert()
("INSERT INTO people(age,name) VALUES ('?','?')", '22', 'john')
>>> m.get_by_id(id=1)
('SELECT age, name FROM people WHERE id_people = ?', 1)
>>> m.get_all()
'SELECT age, name FROM people'
>>> m.delete(id=1)
('DELETE FROM people WHERE id_people = ?', 1)
>>> m.update(id=1)
("UPDATE people SET age = '?', name = '?' WHERE id_people = ?", '22', 'john', 1)
"""
def get_table(self):
"""
Returns the table name
"""
return "people"
def get_id(self):
"""
Returns the id field name
"""
return "id_people"
if __name__ == '__main__':
import doctest
doctest.testmod()