-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.lua
61 lines (49 loc) · 1.32 KB
/
test.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
require 'middleclass'
Point = class('Point')
function Point:initialize(x,y)
self.x = x
self.y = y
end
function Point:__tostring()
return 'Point: [' .. tostring(self.x) .. ', ' .. tostring(self.y) .. ']'
end
p1 = Point(100, 200)
p2 = Point(35, -10)
print(p1)
print(p2)
ActiveRecord = class('ActiveRecord')
function ActiveRecord.static:getCount()
print("SELECT COUNT(*) FROM " .. self.tableName)
end
function ActiveRecord.static:find(klass, id)
--local record = orm.selectOne(self.tableName, 'id', id)
local record = {id = id, name = 'rabbit'}
local result = klass:new(record)
return result
end
function ActiveRecord:printName(params)
print("Name: " .. self.name)
end
User = class('User', ActiveRecord)
User.static.tableName = 'users'
function User:initialize(u)
for k,v in pairs(u) do
self[k] = v
end
--self.id = u.id
--self.name = u.name
end
Animal = class('Animal', ActiveRecord)
Animal.static.tableName = 'animals'
print('User tablename: ' .. User.tableName)
User.static:getCount()
print('Animal tablename: ' .. Animal.tableName)
Animal.static:getCount()
local u = User:new({id = 1, name = 'rabbit'})
print("id: " .. u.id)
print("name: " .. u.name)
u:printName()
u1 = User.static:find(User, 1)
print("CLASS OF u1: " .. u1.class.name)
a1 = Animal.static:find(Animal, 1)
print("CLASS OF a1: " .. a1.class.name)