-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
129 lines (111 loc) · 2.61 KB
/
server.lua
File metadata and controls
129 lines (111 loc) · 2.61 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
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
importCache = nil
importTimer = nil
function import()
if not importCache then
local classFile = fileOpen("classlib.lua",true)
local classStr = fileRead(classFile,fileGetSize(classFile))
fileClose(classFile)
importCache = classStr
importTimer = setTimer(function() importCache = nil end,1000,1) --Clear cache
end
return importCache
end
fnc,err = loadstring(import())
if err then print(err) return end
fnc()
--------------------Custom Class
class "account" {
uid = "uint32",
username = "char[32]",
password = "char[256]",
email = "varchar[256]",
created_at = "datetime",
constructor = function(self,data)
if type(data) ~= "table" then return end
for k,v in pairs(data) do
self[k] = v
end
end;
}
class "vehicle" {
id = "uint32;primary",
model = "uint32;foreign account(`model`)",
x = "float",
y = "float",
z = "float",
Create = function(self)
self.element = createVehicle(self.model,self.x,self.y,self.z)
end;
Save = function(self)
self.x,self.y,self.z = getElementPosition(self.element)
end;
}
-- 创建数据库连接
db = morm:Open("sqlite","test.db")
-- 使用AutoMigrate自动创建或更新表结构
db:AutoMigrate("account", account)
db:AutoMigrate("vehicle", vehicle)
-- 创建新账户
local newAccount = account{
uid = 1,
username = "testuser",
password = "testpass",
email = "test@example.com",
created_at = "2023-01-01 00:00:00"
}
db:Create(newAccount):Query()
-- 查询账户
local acc = account{
uid = 1
}
db:Find(acc):Query(-1, function(self, successCount)
if successCount > 0 then
print("找到账户: " .. acc.username)
else
print("未找到账户")
end
end)
-- 更新账户
acc.username = "updateduser"
db:Update(acc):Query()
-- 使用OrderBy和Limit查询
db:Select("*"):From("account"):OrderBy("uid", "DESC"):Limit(10):Query()
-- 事务处理示例
db:BeginTransaction()
local account1 = account{
uid = 2,
username = "user2",
password = "pass2",
email = "user2@example.com",
created_at = "2023-01-02 00:00:00"
}
local account2 = account{
uid = 3,
username = "user3",
password = "pass3",
email = "user3@example.com",
created_at = "2023-01-03 00:00:00"
}
db:Create(account1):Query()
db:Create(account2):Query()
db:Commit()
-- 添加新列
db:Add("account", "last_login", "datetime"):Query()
-- 修改列定义
db:Modify("account", "email", "varchar[512]"):Query()
-- 删除列
-- db:Drop("account", "last_login"):Query()
veh = vehicle{
id = 1;
}
db:Create(vehicle):Query()
db:Find(veh):Query(-1,function()
veh:Create()
end)
setTimer(function()
veh:Save()
db:Update(veh):Query(-1)
end,5000,1)
iprint(veh)
--查询
--db:Select("*"):From("account"):Where("uid",123):Query()