PtokaX forum

Development Section => Your Developing Problems => Topic started by: [NL]Pur on 09 May, 2004, 16:07:13

Title: Standard database functions
Post by: [NL]Pur on 09 May, 2004, 16:07:13
I'm trying to build some standard database functions.
I've come up with the following functions, but maby i'm missing something critical stuff.

I'm not all to sure of the use of rawset, but i had weird experiance with tremove and tinsert.

So i wanna know what you think of it.



--credits:
-- function Serialize(tTable, sTableName, hFile, sTab) Author: RabidWombat Date: unknown

-- function UPDATE(tTable, PRIMARY_KEY, sField, sValue) Author: Pur Date: 9 may 04
-- function DELETE(tTable, PRIMARY_KEY) Author: Pur Date: 9 may 04
-- function INSERT(tTable, PRIMARY_KEY, tFields) Author: Pur Date: 9 may 04
-- function TRUNCATE_TABLE(tTable) Author: Pur Date: 9 may 04

reg = {

["nickName"] = {
["profile"] = "test1",
["password"] = "test1"
},
["pur"] = {
["profile"] = "test3",
["password"] = "test3"
 }

}

function Main()

UPDATE(reg, "nickName","profile","test2");
DELETE(reg, "pur");
INSERT(reg, "new", {["profile"]="test4",["password"]="test4"});
TRUNCATE_TABLE(reg);
fh = appendto("reg.txt");
Serialize(reg,"registered_Users",fh," ");
closefile(fh);

end


function UPDATE(tTable, PRIMARY_KEY, sField, sValue)
local tFields = rawget(tTable,PRIMARY_KEY);
rawset(tFields, sField, sValue);
end


function DELETE(tTable, PRIMARY_KEY)
rawset(tTable,PRIMARY_KEY,nil);
end


function INSERT(tTable, PRIMARY_KEY, tFields)
rawset(tTable, PRIMARY_KEY, tFields);
end


function TRUNCATE_TABLE(tTable)
for PRIMARY_KEY, tFields in tTable do
DELETE(tTable,PRIMARY_KEY);
end
end


function Serialize(tTable, sTableName, hFile, sTab)
sTab = sTab or "";
write(hFile, sTab..sTableName.." = {\n" );
for key, value in tTable do
local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key);
if(type(value) == "table") then
Serialize(value, sKey, hFile, sTab.."\t");
else
local sValue = (type(value) == "string") and format("%q",value) or tostring(value);
write(hFile, sTab.."\t"..sKey.." = "..sValue);
end
write(hFile, ",\n");
end
write(hFile, sTab.."}");
end

 
Title:
Post by: NotRabidWombat on 09 May, 2004, 19:03:44
My current plan was to use an actual database, MySQL, rather than try to make my own from Lua. This way is fine until you start having lots of entries. Then memory usage will sky rocket.

So I ended up with two options:

1) Write a small console application that could take arguments and update the database. This would only be insert, update and delete since there is no easy way to return select queries.

This would turn out like:
fuction NewUserConnected(curUser)
   local sArgLine = " NewUserConnected";
   sArgLine = sArgLine .. " nick=" .. curUser.sNick;
   sArgLine = sArgLine .. " ip=" .. curUser.IP;
   --etc
   execute("PtokaXDatabase.exe"..sArgLine);
end
This is only an example and far from the most efficient method.

2) Integrate Sql into lua and perform all queries directly through lua. I would prefer this method, but this will not be a pratical solution for PtokaX for a while.

-NotRabidWombat
Title:
Post by: [NL]Pur on 09 May, 2004, 20:00:31
the first option, the small console whould use exec,
when using exec the hub stops processing for a moment so that whould be a bad option.


the second option, well there is myLua available somewhere tho  i can't change the lua version of ptokax so it whouldn't get me anyware.


about the memory usage,
i was thinking of storing only the current users in memory and the offline users on harddisk, for every day or week an other stats file.
Title:
Post by: NotRabidWombat on 10 May, 2004, 05:52:10
"when using exec the hub stops processing for a moment so that whould be a bad option."

No, it will be fine. The executable could have several running processes so blocking at the command line will be insignificant.

As suggested by the proposal for the standard, the information we are recording should not be recorded into a text file.

"about the memory usage,
i was thinking of storing only the current users in memory and the offline users on harddisk, for every day or week an other stats file."
There is no purpose to have the statistical information in memory. Using the database for runtime data is still in discussion.

-NotRabidWombat
Title:
Post by: [NL]Pur on 10 May, 2004, 10:42:00
QuoteAs suggested by the proposal for the standard, the information we are recording should not be recorded into a text file.
 

ah, yes after reading the first post again i now remember again. Before i read it i thought it was only the structure you wanted to make standard.