-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqi_exporter.lua
225 lines (190 loc) · 6.86 KB
/
sqi_exporter.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
221
222
223
224
225
// DO NOT TOUCH OR FILE WILL BREAK
SQE = SQE or {}
SQE.Config = SQE.Config or {}
SQE.Connected = false
SQE.Functions = {
["string"] = {
convertFunC = function(arg)
return !isstring(arg) and tostring(arg) or arg
end
},
["int"] = {
convertFunc = function(arg)
return !isnumber(arg) and tonumber(arg) or arg
end
},
["boolean"] = {
convertFunc = function(arg)
return !isbool(arg) and tobool(arg) or arg
end
},
["varchar"] = {
convertFunc = function(arg)
return !isstring(arg) and tostring(arg) or arg
end
}
}
require("mysqloo")
// DO NOT TOUCH OR FILE WILL BREAK
/*
SQLite Exporter - Config
*/
// MySQL database credentials goes here
SQE.Config.SqlInfos = {
["hostname"] = "",
["username"] = "",
["password"] = "",
["database"] = "",
["port"] = 3306
}
// Table to target - string
SQE.Config.Table = ""
// Columns to use when creating table - table of string
SQE.Config.Columns = {
// Example: "amount int",
// Example: "steamID64 varchar(20) NOT NULL",
}
// Specific condition to use when fetching data from SQLite - false or string
SQE.Config.Condition = ""
/*
SQLiter Exporter - End of config
*/
/*
MAIN LOGIC
*/
// Function - Fetch data contained in SQLite targetted table
function SQE.FetchSQLite()
if (!isstring(SQE.Config.Table) or #SQE.Config.Table <= 0) then
print("[SQLite Exporter] Please, indicate a valid table to target!")
return
end
if (!SQE.Config.Condition) then
print("[SQLite Exporter] No condition were given, proceeding.")
end
local fetchQuery = sql.Query("SELECT * FROM " .. SQE.Config.Table .. " " .. (SQE.Config.Condition or "") .. ";")
if (!fetchQuery) then
print("[SQLite Exporter] ERROR", sql.LastError())
return
else
print("[SQLite Exporter] Fetched " .. SQE.Config.Table .. " table correctly, " .. #fetchQuery .. " rows received, processing..")
SQE.ExportMySQL(fetchQuery)
end
end
// Function - Insert fetched data from SQLite database to remote database
function SQE.ExportMySQL(data)
// Localize stuff cuz that doesn't hurt
local table, string = table, string
// Shouldn't happen but who knows
if (!data or #data <= 0) then
print("[SQLite Exporter] No data passed to the function, stopping.")
return
end
// Shouldn't happen but who knows
if (SQE.Database == nil) then
SQE.InitializeConnection()
end
/*
Create table in remote database if it doesn't exist yet
*/
local createQuery = SQE.Database:query([[ CREATE TABLE IF NOT EXISTS ]] .. SQE.Config.Table .. [[ ( ]]
.. table.concat(SQE.Config.Columns, ", ") ..
[[ ) ]])
function createQuery:onError(q, err)
print("[SQLite Exporter] " .. err)
return
end
function createQuery:onSuccess()
print("[SQLite Exporter] Successfully created " .. SQE.Config.Table .. " table!")
end
createQuery:start()
/*
This will allows us to determine function to use based on each column expected value type
*/
local columnNames, columnTypes, availableTypes = {}, {}, { "int", "string", "boolean", "varchar" }
local function FetchColumnDetails(index, column)
// Fetch column name
local name = string.Explode(" ", column)
columnNames[index] = name[1]
// Fetch type per column and assign it
for k, cType in ipairs(availableTypes) do
local startPos = string.find(column, cType)
if (startPos == nil) then continue end
columnTypes[index] = cType
end
end
for _, cms in ipairs(SQE.Config.Columns) do
FetchColumnDetails(_, cms)
end
/*
Formats a [k] = v table to an inline (v1, v2, v3) row that we can later use in an INSERT query as VALUES
dataAsValues{} will contain all data formatted as bracket groups separated by a comma
*/
local dataAsValues = {}
local function FormatEndRow(row)
// Initialize stuff we use
// @pos represents the current column
local tbl, pos = {}, 1
// Loop through the non-numerical SQLite data and formats it as a numerical table
for k, v in pairs(row) do
if (SQE.Functions[columnTypes[pos]]) then
v = SQE.Functions[columnTypes[pos]].convertFunc(v)
end
pos = pos + 1
if (type(v) != "string") then
table.insert(tbl, v)
continue
end
v = "\'" .. v .. "\'"
table.insert(tbl, v)
end
// Return created numerical table as a string - v1, v2, v3, etc..
return table.concat(tbl, ", ")
end
for k, v in pairs(data) do
// Needed to create bracket groups and insert multiple values
local entry = { "(", ")," }
local formattedRow = FormatEndRow(v)
// Insert the values between ( ),
table.insert(entry, 2, formattedRow)
// This is needed so that we remove the , from last bracket group
// If you don't do this, query will error as it expects another group of values to be added
if (next(data, k) == nil) then entry[#entry] = ")" end
// Insert into table containint all bracket groups
table.insert(dataAsValues, table.concat(entry, " "))
end
/*
FINAL STEP: Insert our formatted data into remote database, and print the status
*/
local insertQuery = SQE.Database:query("INSERT INTO " .. SQE.Config.Table .. "(" .. table.concat(columnNames, ", ") .. ") VALUES " .. table.concat(dataAsValues, "") .. ";")
function insertQuery:onSuccess()
print("[SQLite Exporter] Export is done, you can remove the file now!")
end
function insertQuery:onError(q, err)
print("[SQLite Exporter] " .. err)
return
end
insertQuery:start()
end
// Function - Starts connection to remote SQL database if Mysqloo module exists
function SQE.InitializeConnection()
if (!mysqloo) then
return print("[SQLite Exporter] ERROR: make sure that mysqloo module is properly installed.")
end
SQE.Database = mysqloo.connect(SQE.Config.SqlInfos["hostname"], SQE.Config.SqlInfos["username"], SQE.Config.SqlInfos["password"], SQE.Config.SqlInfos["database"], { ["port"] = SQE.Config.SqlInfos["port"] })
function SQE.Database:onConnected()
print("[SQLite Exporter] Connected to MySQL database!")
SQE.Connected = true
SQE.FetchSQLite()
end
function SQE.Database:onConnectionFailed(err)
print("[SQL Exporter] Cannot connect to database:\n" .. err)
return
end
SQE.Database:connect()
end
// Launch the whole export process once all entities have been loaded
hook.Add("InitPostEntity", "SQE.LaunchExport", function()
if (!SQE.Connected) then
SQE.InitializeConnection()
end
end)