Standard database functions
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Standard database functions

Started by [NL]Pur, 09 May, 2004, 16:07:13

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

[NL]Pur

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
	


NotRabidWombat

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


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

[NL]Pur

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.

NotRabidWombat

"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

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.

SMF spam blocked by CleanTalk