I'm working on a lua account manager but the moment i started making the account creator script i noticed a bad pattern.
^ When executing a query everything has to be on one line, in this case its only 6 columns and it looks semi-decent, but can you guess what will happen when i edit the player table?
This is obviously going to look ugly if we don't do something about it, so i rolled a small function to format queries, instead it should look like this:
^ I don't know about you but i find that very pleasant, looks easier to read at least, avoids long lines and having to concatenate with ".."
db.formatQuery
^ When executing a query everything has to be on one line, in this case its only 6 columns and it looks semi-decent, but can you guess what will happen when i edit the player table?
This is obviously going to look ugly if we don't do something about it, so i rolled a small function to format queries, instead it should look like this:
^ I don't know about you but i find that very pleasant, looks easier to read at least, avoids long lines and having to concatenate with ".."
db.formatQuery
PHP:
function db.formatQuery(statement, values)
if statement and values then
local query = statement
local key = {}
local value = {}
for l in ipairs(values) do
key[l] = "`"..values[l][1].."`"
value[l] = values[l][2]
if type(value[l]) == "number" then
value[l]=""..value[l]..""
else
value[l]="`"..value[l].."`"
end
end
key = table.concat(key, ",")
value = table.concat(value, ",")
return query.." ("..key..") VALUES ("..value..")"
end
end
PHP:
local hash = sha1(password)
statement = "INSERT INTO `accounts`"
values = {{"id", account},
{"password", hash},
{"email", ""},
{"premend", 0},
{"blocked", 0},
{"warnings", 0}}
query = db.formatQuery(statement, values)
db.executeQuery(query)