Skip to content

Commit 3c350e8

Browse files
authored
Merge pull request #2 from swiftly-solution/feature/swiftly-v1.4.0
update(core): to QueryBuilder
2 parents ae1faf6 + 5e09b04 commit 3c350e8

File tree

2 files changed

+52
-62
lines changed

2 files changed

+52
-62
lines changed

plugins/tags/modules/commands.lua

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,21 @@ local consoleCommands = {
2323
"Syntax: sw_tags add <identifier> <tag> <color> <name_color> <msg_color> <scoreboard (0/1)>")
2424
end
2525

26-
27-
local addRowQuery =
28-
"INSERT INTO @tablename VALUES ('@identifier', '@tag', '@color', '@name_color', '@msg_color', @scoreboard)"
29-
local addRowParams = {
30-
["tablename"] = config:Fetch("tags.database.tablesname.tags"),
31-
["identifier"] = identifier,
32-
["tag"] = tag,
33-
["color"] = color,
34-
["name_color"] = name_color,
35-
["msg_color"] = msg_color,
36-
["scoreboard"] = scoreboard
37-
}
38-
39-
db:QueryParams(addRowQuery, addRowParams, function(err, result)
26+
db:QueryBuilder():Table(tostring(config:Fetch("tags.database.tablesname.tags") or "sw_tags")):Insert(
27+
{
28+
identifier = identifier,
29+
tag = tag,
30+
color = color,
31+
name_color = name_color,
32+
msg_color = msg_color,
33+
scoreboard = scoreboard
34+
}
35+
):Execute(function (err, result)
4036
if #err > 0 then
4137
return print("{DARKRED} ERROR: {DEFAULT}" .. err)
4238
end
4339
ReplyToCommand(playerid, config:Fetch("tags.prefix"), FetchTranslation("tags.added"):gsub("{ID}", identifier))
44-
ReloadTags()
40+
ReloadTags()
4541
end)
4642
end,
4743

@@ -58,19 +54,13 @@ local consoleCommands = {
5854
FetchTranslation("tags.not_exists"):gsub("{ID}", identifier))
5955
end
6056

61-
local removeRowQuery = "DELETE FROM @tablename WHERE identifier = '@identifier' LIMIT 1;"
62-
local removeRowParams = {
63-
["tablename"] = config:Fetch("tags.database.tablesname.tags"),
64-
["identifier"] = identifier
65-
}
66-
67-
db:QueryParams(removeRowQuery, removeRowParams, function(err, result)
57+
db:QueryBuilder():Table(tostring(config:Fetch("tags.database.tablesname.tags") or "sw_tags")):Delete():Where('identifier', '=', identifier):Execute(function (err, result)
6858
if #err > 0 then
6959
return print("{DARKRED} ERROR: {DEFAULT}" .. err)
7060
end
7161
ReplyToCommand(playerid, config:Fetch("tags.prefix"),
7262
FetchTranslation("tags.removed"):gsub("{ID}", identifier))
73-
ReloadTags()
63+
ReloadTags()
7464
end)
7565
end,
7666

@@ -89,28 +79,39 @@ local consoleCommands = {
8979

9080
local option = args[3]
9181

92-
if option == "tag" and option ~= "color" and option ~= "name_color" and option ~= "msg_color" and option ~= "scoreboard" then
82+
if option ~= "tag" and option ~= "color" and option ~= "name_color" and option ~= "msg_color" and option ~= "scoreboard" then
9383
return ReplyToCommand(playerid, config:Fetch("tags.prefix"),
9484
"Syntax: sw_tags edit <identifier> <tag/color/name_color/msg_color/scoreboard> <value>)")
9585
end
9686
local value = args[4]
9787

88+
local qb = db:QueryBuilder():Table(tostring(config:Fetch("tags.database.tablesname.tags") or "sw_tags")):Where('identifier', '=', identifier)
9889

99-
local updateRowQuery = "UPDATE @tablename SET @option = '@value' WHERE identifier = '@identifier'";
100-
local updateRowParams = {
101-
["tablename"] = config:Fetch("tags.database.tablesname.tags"),
102-
["option"] = option,
103-
["value"] = value,
104-
["identifier"] = identifier
105-
}
90+
switch(option, {
91+
["tag"] = function ()
92+
qb:Update({tag = value})
93+
end,
94+
["color"] = function ()
95+
qb:Update({color = value})
96+
end,
97+
["name_color"] = function ()
98+
qb:Update({name_color = value})
99+
end,
100+
["msg_color"] = function ()
101+
qb:Update({msg_color = value})
102+
end,
103+
["scoreboard"] = function ()
104+
qb:Update({scoreboard = value})
105+
end,
106+
})
106107

107-
db:QueryParams(updateRowQuery, updateRowParams, function(err, result)
108+
qb:Execute(function (err, result)
108109
if #err > 0 then
109110
return print("{DARKRED} ERROR: {DEFAULT}" .. err)
110111
end
111112
ReplyToCommand(playerid, config:Fetch("tags.prefix"),
112113
FetchTranslation("tags.updated"):gsub("{ID}", identifier))
113-
ReloadTags()
114+
ReloadTags()
114115
end)
115116
end,
116117

@@ -194,7 +195,6 @@ local playerCommands = {
194195
end
195196
}
196197

197-
198198
commands:Register("tags", function(playerid, args, argc, silent, prefix)
199199
if playerid < 0 then
200200
if argc < 1 then

plugins/tags/modules/tagsloader.lua

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@ AddEventHandler("OnPluginStart", function(event)
22
db = Database(tostring(config:Fetch("tags.database.connection")))
33
if not db:IsConnected() then return EventResult.Continue end
44

5-
local createTableQuery = [[
6-
CREATE TABLE IF NOT EXISTS @tablename (
7-
identifier VARCHAR(255) PRIMARY KEY,
8-
tag VARCHAR(255) NOT NULL,
9-
color VARCHAR(255) NOT NULL,
10-
name_color VARCHAR(255) NOT NULL,
11-
msg_color VARCHAR(255) NOT NULL,
12-
scoreboard TINYINT(1) NOT NULL DEFAULT 0
13-
);
14-
]]
15-
16-
local createTableParams = {
17-
["tablename"] = config:Fetch("tags.database.tablesname.tags"),
18-
}
19-
20-
db:QueryParams(createTableQuery, createTableParams, function(err, result)
21-
if #err > 0 then
22-
return print("{DARKRED} ERROR: {DEFAULT}" .. err)
23-
end
24-
TagsLoader()
25-
end)
5+
db:QueryBuilder():Table(tostring(config:Fetch("tags.database.tablesname.tags") or "sw_tags"))
6+
:Create({
7+
identifier = "string|max:255|primary",
8+
tag = "string|max:255",
9+
color = "string|max:255",
10+
name_color = "string|max:255",
11+
msg_color = "string|max:255",
12+
scoreboard = "boolean"
13+
})
14+
:Execute(function (err, result)
15+
if #err > 0 then
16+
return print("{DARKRED} ERROR: {DEFAULT}" .. err)
17+
end
18+
TagsLoader()
19+
end)
2620
return EventResult.Continue
2721
end)
2822

@@ -34,12 +28,7 @@ function TagsLoader(cb)
3428
Tags = {}
3529
TagsIndexMap = {}
3630

37-
local selectTableQuery = "SELECT * from @tablename"
38-
local selectTableParams = {
39-
["tablename"] = config:Fetch("tags.database.tablesname.tags")
40-
}
41-
42-
db:QueryParams(selectTableQuery, selectTableParams, function (err, result)
31+
db:QueryBuilder():Table(tostring(config:Fetch("tags.database.tablesname.tags")) or "sw_tags"):Select({'*'}):Execute(function (err, result)
4332
if #err > 0 then
4433
return print("{DARKRED} ERROR: {DEFAULT}" .. err)
4534
end
@@ -55,9 +44,10 @@ function TagsLoader(cb)
5544
end
5645

5746
local out, _ = FetchTranslation("tags.loaded"):gsub("{COUNT}", #Tags)
58-
print(out)
47+
print(out)
5948
end)
6049

50+
6151
end
6252

6353
function ReloadTags()

0 commit comments

Comments
 (0)