-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_orm.py
135 lines (111 loc) · 4.23 KB
/
06_orm.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
# 需求 orm:类映射数据库中的一张表
import numbers
class Field:
pass
class IntField(Field):
def __init__(self, db_column, min_value=None, max_value=None):
self._value = None
self.min_value = min_value
self.max_value = max_value
self.db_column = db_column
if min_value is not None:
if not isinstance(min_value, numbers.Integral):
raise ValueError("int value need")
elif min_value < 0:
raise ValueError("positive value needed")
if max_value is not None:
if not isinstance(max_value, numbers.Integral):
raise ValueError("int value need")
elif max_value < 0:
raise ValueError("positive value needed")
if min_value is not None and max_value is not None:
if min_value > max_value:
raise ValueError("min_value must be smaller than max_value")
# 数据描述符
def __get__(self, instance, owner):
return self._value
def __set__(self, instance, value):
if not isinstance(value, numbers.Integral):
raise ValueError("int value need")
# 保存值
if value < 0:
raise ValueError("POSITIVE value needed")
if value < self.min_value or value > self.max_value:
raise ValueError("value must be between min_value and max_value")
# 如果instance.age 会死循环
self._value = value
def __delete__(self, instance):
pass
class CharField(Field):
def __init__(self, db_column, max_length=None):
self._value = None
self.db_column = db_column
self.max_length = max_length
if max_length is None:
raise ValueError("max_length needs to be specified")
def __get__(self, instance, owner):
return self._value
def __set__(self, instance, value):
if not isinstance(value, str):
raise ValueError("string value needed")
if len(value) > self.max_length:
raise ValueError("len of value excesses len of max_length")
self._value = value
class ModelMetaClass(type):
def __new__(cls, name, bases, attrs, *args, **kwargs):
if name == "BaseModel":
return super().__new__(cls, name, bases, attrs, *args, **kwargs)
fields = {}
for key, value in attrs.items():
# 判断是不是表中的列
if isinstance(value, Field):
fields[key] = value
attrs_meta = attrs.get("Meta", None)
_meta = {}
db_table = name.lower()
if attrs_meta is not None:
table = getattr(attrs_meta, "db_table", None)
if db_table is not None:
db_table = table
_meta["db_table"] = db_table
attrs["_meta"] = _meta
attrs["fields"] = fields
del attrs["Meta"]
return super().__new__(cls, name, bases, attrs, *args, **kwargs)
class BaseModel(metaclass=ModelMetaClass):
def __init__(self, *args, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
return super().__init__()
def save(self):
fields = []
values = []
for key, value in self.fields.items():
db_column = value.db_column
if db_column is None:
db_column = key.lower
fields.append(db_column)
value = getattr(self, key)
values.append(str(value))
# sql = 'insert user(name,age) value("danny",30)'
# .join 要同种类型
sql = 'insert {db_table}({fields}) values({values})'.format(db_table=self._meta["db_table"],
fields=",".join(fields), values=",".join(values))
pass
# class User(metaclass=ModelMetaClass):
class User(BaseModel):
# # 初始化让基类完成
# def __init__(self,name, age):
# pass
name = CharField(db_column="name", max_length=10)
age = IntField(db_column="age", min_value=1, max_value=100)
class Meta:
db_table = "user"
if __name__ == "__main__":
# 方法一
# user = User()
# user.name ="danny"
# user.age =26
# 方法二
user = User(name="danny2", age=11)
user.save()