-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
59 lines (45 loc) · 1.67 KB
/
Copy pathindex.py
File metadata and controls
59 lines (45 loc) · 1.67 KB
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 arnelify_orm import MySQL
from arnelify_orm import MySQLQuery
def main() -> int:
res: list[dict] = []
db: MySQL = MySQL({
"ORM_MAX_CONNECTIONS": 10,
"ORM_HOST": "mysql",
"ORM_NAME": "test",
"ORM_USER": "root",
"ORM_PASS": "pass",
"ORM_PORT": 3306
})
db.connect()
print("Connected.")
db.foreignKeyChecks(False)
db.dropTable("users")
db.dropTable("posts")
db.foreignKeyChecks(True)
def users(query: MySQLQuery):
query.column("id", "BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY")
query.column("email", "VARCHAR(255) UNIQUE", None)
query.column("created_at", "DATETIME", "CURRENT_TIMESTAMP")
query.column("updated_at", "DATETIME", None)
db.createTable("users", users)
def posts(query: MySQLQuery):
query.column("id", "BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY")
query.column("user_id", "BIGINT UNSIGNED", None)
query.column("contents", "VARCHAR(2048)", None)
query.column("created_at", "DATETIME", "CURRENT_TIMESTAMP")
query.column("updated_at", "DATETIME", "CURRENT_TIMESTAMP")
query.index("INDEX", ["user_id"])
query.reference("user_id", "users", "id", ["ON DELETE CASCADE"])
db.createTable("posts", posts)
res = db.table("users").insert({ "email": "email@example.com" })
insert: str = db.toJson(res)
print(f"last inserted id: {insert}")
res = db.table("users").select(["id", "email"]).where("id", 1).limit(1)
select: str = db.toJson(res)
print(f"Inserted row: {select}")
db.table("users").update({ "email": "user@example.com" }).where("id", 1).exec()
db.table("users").delete_().where("id", 1).limit(1)
db.close()
print("Closed.")
if __name__ == "__main__":
main()