-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.py
135 lines (102 loc) · 2.36 KB
/
examples.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# pip install dori-orm
from dori_orm import DB, ResultConfig
from dori_orm.operators import AND, OR, NOT
from dori_orm import columns
class Person(DB):
name = columns.Text(nullable=False)
family = columns.Text(nullable=False)
age = columns.Integer()
phone = columns.Integer()
salary = columns.Real(default=100_000)
class School(DB):
name = columns.VarChar(nullable=False)
created_at = columns.Date()
address = columns.Text()
students_count = columns.SmallInt(default=300)
class Student(DB):
person = columns.ForeignKey(Person)
school = columns.ForeignKey(School)
# class_name = columns.VarChar()
gpa = columns.TinyInt(default=20)
p1 = Person(
name='Mohammad',
family='Dori',
age=20,
phone=1234567890,
salary=110_000,
)
p2 = Person(
name='John',
family='Gits',
age=30,
phone=1234567890,
)
print(p1.age)
print(Person.all())
print(Person.get('id', 'name', 'family'))
print(Person.filter(id=1, name='Mohammad'))
not_mohammad = Person.filter(name__n='Mohammad')
print(not_mohammad)
print(not_mohammad.count())
print(not_mohammad.first())
print(not_mohammad.last())
for row in not_mohammad:
print(row.name)
# row.remove()
print(p1)
p1.update(name='Salar')
print(p1)
print(Person.count())
print(Person.first())
print(Person.last())
print(Person.max('salary'))
print(Person.min('salary'))
print(Person.sum('salary'))
print(Person.avg('salary'))
print(Person.all(config=ResultConfig(order_by='id', reverse=True)))
print(Person.get(
config=ResultConfig(
limit=5
)
))
# id__lt, id__lte, id__gt, id__gte, id__n
print(Person.filter(id__lte=5))
print(Person.filter(id__between=(2, 8)))
print(Person.filter(
name__like='Mo%',
config=ResultConfig(
limit=2,
order_by='age',
)
))
rows = Person.filter(
OR(id=2, name='Ali')
)
print()
print(rows)
rows = Person.filter(
NOT(AND(id=2, name='Ali'))
)
print()
print(rows.count())
rows = Person.filter(
OR(OR(name='Ali', id=2), OR(salary=10, age=20))
)
print()
print(rows.count())
print(Person.queries())
school1 = School(
name='The Sample School',
created_at='2002-01-04',
address='1600 Amphitheatre Parkway in Mountain View, California',
)
print(school1)
student = Student(
person=p1,
school=school1,
# class_name='A3',
gpa=10,
)
print(school1.id)
print(p1.id)
print(student)