-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.s.lua
112 lines (87 loc) · 2.74 KB
/
example.s.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
--[[
_
| |
_ __ _____ _____ _ __ ___ __| |
| '_ \ / _ \ \ /\ / / _ \ '__/ _ \/ _` |
| |_) | (_) \ V V / __/ | | __/ (_| |
| .__/ \___/ \_/\_/ \___|_| \___|\__,_|
| |
| |__ _ _
| '_ \| | | | https://github.com/sanyisasha
| |_) | |_| | @Author SaSha <Molnár Sándor>
|_.__/ \__, |
_____/ / _____ _
/ ______/ / _____| |
| (___ __ _| (___ | |__ __ _
\___ \ / _` |\___ \| '_ \ / _` |
____) | (_| |____) | | | | (_| |
|_____/ \__,_|_____/|_| |_|\__,_|
]]
--[[EXAMPLE SQL
CREATE TABLE `test` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT , `name` VARCHAR(255) NOT NULL , `value` VARCHAR(255) NOT NULL , `created_at` DATETIME(0) NOT NULL , `updated_at` DATETIME(0) NOT NULL , PRIMARY KEY (`id`)) ENGINE = MyISAM;
]]
-- DATABASE EXAMPLES
-- Insert a row
--[[
Query():insert('test', {
name = 'testName',
value = 'testValue',
created_at = '2020-01-01 00:00:00',
updated_at = '2020-01-01 00:00:00',
})
]]
-- Update a row
Query():update('test', {
name = "updatedTestName"
}, {
id = 1
})
-- Select all rows
local test = Query():select():from('test'):all()
if not test[1] then -- all() returns array. If [1] is nil, means it's empty.
outputDebugString('[Query] The result was empty.')
else
for i,v in pairs(test) do
outputDebugString(v['name'])
end
end
-- Advanced query
local test = Query():select({'id', 'name', 'value'}):from('test'):where({value = 'testValue'})
-- You can separate, or make condition extra querys.
-- For an example, only add :orderBy() when a condition is true
if true then
test = test:orderBy({
id = Query.SORT_ASC
})
end
test = test:one()
if not test then
outputDebugString('[Query] The result was empty.')
else
outputDebugString(test['value'])
end
-- ACTIVERECORD EXAMPLES
-- Get a single row
local test = Test():findOne(1)
outputDebugString(test.name..': '..test.value)
-- Change some data
test.value = 'Updated With AR'
test:save() -- If the value is not changed, then it don't do query, just return true.
test = test:refresh()
outputDebugString(test.value)
-- Advanced find
-- You can debug your SQL. If debug true, it will debug the whole SQL query it.
local q = Test():getQuery():debug(true):where({name = 'updatedTestName'})
local test = Test():loadByQuery(q:one())
if test then
outputDebugString('Advanced find: '..test.id)
test:testMethod()
else
outputDebugString('Test data not found.', 1)
end
tests = Test():findAll({
{'>=', 'id', '5'}
})
for i,v in pairs(tests) do
outputDebugString('id: '..v.id)
end