-
Notifications
You must be signed in to change notification settings - Fork 6
/
model.lua
221 lines (175 loc) · 6.04 KB
/
model.lua
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
local fun = require'orm.func'
local cache = require'orm.cache'
local table_concat = table.concat
local setmetatable = setmetatable
local rawget = rawget
local assert = assert
local type = type
local function define_model(DB, Query, table_name)
assert(type(table_name) == 'string', 'table name required')
table_name = DB.escape_identity(table_name)
local _init_model = function(Model)
local attrs
local conf = DB.config()
local cache_key = table_concat({'orm', conf.host, conf.port, conf.database, table_name}, '^')
local data, stale = cache:get(cache_key)
if not data then
attrs = DB.get_schema(table_name)
cache:set(cache_key, fun.table_clone(attrs), conf.expires)
else
attrs = fun.table_clone(data)
end
assert(attrs, 'initializing model failed')
assert(attrs.__pk__, 'primary key required')
local pk = attrs.__pk__
attrs.__pk__ = nil
local function filter_attrs(params)
return fun.kmap(function(k, v)
if type(k) == 'number' then
return k, v
elseif attrs[k] ~= nil then
return k, v
end
end, params)
end
local function pop_models(ok, rows)
if not ok then return ok, rows end
return ok, fun.map(function(row)
local model = Model.new(row, true)
model:trigger('AfterFind')
return model
end, rows)
end
local function query()
return Query():from(table_name)
end
Model.find = function()
local q = query()
getmetatable(q).__call = function(self)
if self._state == 'select' then
return pop_models(self:exec())
end
return self:exec()
end
return q
end
Model.group = function(expr, cond, ...)
local q = query():select(expr .. ' AS group__res')
if cond then q:where(cond, ...) end
local ok, res = q()
if ok and #res > 0 then
return res[1].group__res
end
return nil
end
Model.count = function(cond, ...)
return Model.group('COUNT(*)', cond, ...)
end
Model.find_all = function(cond, ...)
return Model.find():where(cond, ...)()
end
Model.find_one = function(cond, ...)
local ok, records = Model.find():where(cond, ...):limit(1)()
if ok then records = records[1] end
return ok, records
end
Model.update_where = function(set, cond, ...)
return query():update():where(cond, ...):set(set)()
end
Model.delete_where = function(cond, ...)
return query():delete():where(cond, ...)()
end
Model.__index = function(self, key)
if Model[key] then
return Model[key]
else
return self.__attrs__[key]
end
end
Model.__newindex = function(self, k, v)
if attrs[k] ~= nil then
self.__attrs__[k] = v
self.__dirty_attrs__[k] = true
end
end
function Model:set_dirty(attr)
self.__dirty_attrs__[attr] = true
end
function Model:get_dirty_attrs()
local count = 0
local res = fun.kmap(function(k, v)
count = count + 1
return k, self.__attrs__[k]
end, self.__dirty_attrs__)
return res, count
end
function Model:save()
if self[pk] then -- update
self:trigger('BeforeSave')
local res = "no dirty attributes"
local ok = false
local dirty_attrs, count = self:get_dirty_attrs()
if count > 0 then
ok, res = query():update():where(pk .. ' = ?d ', self[pk]):set(dirty_attrs)()
if ok then
self:set_none_dirty()
end
end
return ok, res
else -- insert
self:trigger('BeforeSave')
local ok, res = query():insert():values(self.__attrs__)()
if ok then
self[pk] = res.insert_id
self:set_none_dirty()
self.__is_new__ = false
return ok, res
else
return false, res
end
end
end
function Model:set_none_dirty()
self.__dirty_attrs__ = {}
end
function Model:delete()
assert(self[pk], 'primary key ['.. pk .. '] required')
return query():delete():where(pk .. '= ?d', self[pk])()
end
function Model:load(data)
if type(data) == 'table' then
fun.kmap(function(k, v)
self[k] = v
end, data)
end
end
function Model:trigger(event, ...)
local method = Model['on'..event]
if type(method) == 'function' then
return method(self, ...)
end
end
function Model:is_new()
return self.__is_new__
end
Model.new = function(data, not_dirty)
local instance = { __attrs__ = {}, __dirty_attrs__ = {} , __is_new__ = true }
setmetatable(instance, Model)
instance:load(data)
if not_dirty then
instance:set_none_dirty()
-- while loading from db, records are not new
instance.__is_new__ = false
end
return instance
end
setmetatable(Model, nil)
end
return setmetatable({}, {
__index = function(self, key)
_init_model(self)
return rawget(self, key)
end
})
end
return define_model