Author Topic: Standard database functions  (Read 1693 times)

0 Members and 1 Guest are viewing this topic.

[NL]Pur

  • Guest
Standard database functions
« 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(tTablesTableNamehFilesTab)
	
	
AuthorRabidWombat Dateunknown

-- function UPDATE(tTablePRIMARY_KEYsFieldsValue)
	
	
AuthorPur Date9 may 04
-- function DELETE(tTablePRIMARY_KEY)
	
	
	
	
AuthorPur Date9 may 04
-- function INSERT(tTablePRIMARY_KEYtFields)
	
	
AuthorPur Date9 may 04
-- function TRUNCATE_TABLE(tTable)
	
	
	
	
AuthorPur Date9 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(tTablePRIMARY_KEYsFieldsValue)
	
	

	
	
local tFields rawget(tTable,PRIMARY_KEY);
	

	
	
rawset(tFieldssFieldsValue);
	
end


	
function 
DELETE(tTablePRIMARY_KEY)
	
	
	
	

	
	
rawset(tTable,PRIMARY_KEY,nil);
	
end


	
function 
INSERT(tTablePRIMARY_KEYtFields)
	
	
rawset(tTablePRIMARY_KEYtFields);
	
end


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


	
function 
Serialize(tTablesTableNamehFilesTab)
	
	

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


PtokaX forum

Standard database functions
« on: 09 May, 2004, 16:07:13 »

Offline NotRabidWombat

  • Lord
  • ***
  • Posts: 384
  • Karma: +2/-0
(No subject)
« Reply #1 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:
Code: [Select]
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


I like childish behavior. Maybe this post will be deleted next.

[NL]Pur

  • Guest
(No subject)
« Reply #2 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.

Offline NotRabidWombat

  • Lord
  • ***
  • Posts: 384
  • Karma: +2/-0
(No subject)
« Reply #3 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


I like childish behavior. Maybe this post will be deleted next.

[NL]Pur

  • Guest
(No subject)
« Reply #4 on: 10 May, 2004, 10:42:00 »
Quote
As 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.

PtokaX forum

(No subject)
« Reply #4 on: 10 May, 2004, 10:42:00 »