-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_old.py
97 lines (79 loc) · 2.82 KB
/
example_old.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
import datetime
from pydal import DAL, Field
from typedal.helpers import utcnow
db = DAL("sqlite:memory")
### basic examples
db.define_table("person",
Field("name", "string"),
Field("age", "integer", default=18),
Field("nicknames", "list:string")
)
db.define_table("pet",
Field("name", "string"),
Field("owners", "list:reference person")
)
db.person.insert(name="Henk", age=44, nicknames=["Henkie", "Gekke Henk"])
db.person.insert(name="Ingrid", age=47, nicknames=[])
henk = db(db.person.name == "Henk").select().first()
ingrid = db(db.person.name == "Ingrid").select().first()
print(henk, ingrid)
db.pet.insert(name="Max", owners=[henk, ingrid])
max = db.pet(name="Max")
print(max)
people = db(db.person.id > 0).select()
for person in people:
print(person.name)
### example with all possible field types;
db.define_table("other_table")
db.define_table("all_fields",
Field("string", "string"),
Field("text", "text"),
Field("blob", "blob"),
Field("boolean", "boolean"),
Field("integer", "integer"),
Field("double", "double"),
Field("decimal", "decimal(2,3)"),
Field("date", "date"),
Field("time", "time"),
Field("datetime", "datetime"),
Field("password", "password"),
Field("upload", "upload", uploadfield="upload_data"),
Field("upload_data", "blob"),
Field("reference", "reference other_table"),
Field("list_string", "list:string"),
Field("list_integer", "list:integer"),
Field("list_reference", "list:reference other_table"),
Field("json", "json"),
Field("bigint", "bigint"),
# The big-id and, big-reference are only supported by some of the database engines and are experimental.
)
now = utcnow()
db.other_table.insert()
db.other_table.insert()
other1 = db.other_table(id=1)
other2 = db.other_table(id=2)
with open('example_old.py', 'rb') as stream:
db.all_fields.insert(
string="hi",
text="hi but longer",
blob=b"\x23",
boolean=True,
integer=1,
double=1.11111111111111111111111111111,
decimal=1.11111111111111111111111111111,
date=now.date(),
time=now.time(),
datetime=now,
password="secret",
upload=stream,
upload_data=stream.read(),
reference=other1,
list_string=["hi", "there"],
list_integer=[1, 2],
list_reference=[other1, other2],
json={'hi': 'there'},
bigint=42,
)
row = db.all_fields(string="hi")
for field in row:
print(field, type(row[field]))