Difference between revisions of "User:LepreKhaun Resident/Json Database 2 Chat"

From Second Life Wiki
Jump to navigation Jump to search
(Added more functionality, can be used for regular backups of data now.)
m
Line 3: Line 3:
Let's hope you have the following code in place to retrieve the database, so you can update and debug your code and then reset everything properly. Otherwise, you'll have lost your entire database. Ouch!
Let's hope you have the following code in place to retrieve the database, so you can update and debug your code and then reset everything properly. Otherwise, you'll have lost your entire database. Ouch!


Can also (now! lol) be used just to obtain a back up of your database at times.
Can also ('''now!''' lol) be used just to obtain a back up of your database at times.


<pre>// Where myDataBase is a global Json text string
<pre>// Where myDataBase is a global Json text string
Line 30: Line 30:
}</pre>
}</pre>


After calling this function (possibly as a response to a button within your admin menu- "ChatDB", or a chatted command that's listened for on some obscure negative channel that is masked for the owner's key), the object will chat out the database for you.
After calling this function (possibly as a response to a button within your admin menu- "ChatDB", or a chatted command that's listened for on some obscure channel that is masked for the owner's key), the object will chat out the database for you.


All that remains now is to copy&paste this output to a text document, remove the inserted new lines, "[time stamp] Object:" bits and put double quotes around it all. Then have the following global initialization line in your rewritten code:
All that remains now is to copy&paste this output to a text document, remove the inserted new lines, "[time stamp] Object:" bits and put double quotes around it all. Then have the following global initialization line in your rewritten code:
<pre>string myDataBase = llUnescapeURL('your cleaned and double quote enclosed chat output');</pre>
<pre>string myDataBase = llUnescapeURL('your cleaned and double quote enclosed chat output');</pre>


NOTE: When using Json text for a database, you should check free memory after each insertion of new data into it. Only by doing so will you be able to realize that things may be getting a bit too tight for comfort within your program. At that point, you need to consider a way to either shorten the length of your database, possible by going with (shudder) one character "Key"s, making a note in comments of what Key "Z" stands for, deleting older or no longer necessary records and/or transferring the entire database into a stand alone script with minimal functionality and having a link message protocol worked out for retrieval, insertion, deletion and updating the records.
'''NOTE:''' When using Json text for a database, you should check free memory after each insertion of new data into it. Only by doing so will you be able to realize that things may be getting a wee bit too tight for comfort within your program.
 
At that point, you need to consider a way to either shorten the length of your database, possible by going with (shudder) one character "Key"s (making a note in comments of what Key "Z" stands for) deleting older or no longer necessary records and/or transferring the entire database into a stand alone script with minimal functionality and having a link message protocol worked out for retrieval, insertion, deletion and updating the records.

Revision as of 23:06, 29 August 2013

You may find at some point that you've written an application with a Json text database within it that is 10k or so in size and now you need to update the code, perhaps to add some new record fields you realize you require. Ohoh!

Let's hope you have the following code in place to retrieve the database, so you can update and debug your code and then reset everything properly. Otherwise, you'll have lost your entire database. Ouch!

Can also (now! lol) be used just to obtain a back up of your database at times.

// Where myDataBase is a global Json text string
// function jsonDb2Chat ();
// Version 1.5 (8/29/2013 - added a necessary conversion of DB back to original!)
// Version 1.0
// By LepreKhaun 8/28/2013

jsonDb2Chat () {
	integer count = 0;
	
	// Needed to keep internal quotes and escape characters intact
	// Be *certain* you have the available memory needed for this inflation!
	myDataBase = llEscapeURL(myDataBase);
	
	// Keeping in mind chat can only output 1024 single-byte characters at a time:
	integer totalChunks = (integer)(llStringLength (myDataBase)/1022);
	llOwnerSay ("Chatting your database.\n\n");
	while (count < totalChunks) {
		llOwnerSay ("\n" + llGetSubString (myDataBase, count*1022, ((++count)*1022)-1));
	}
	llOwnerSay ("\n" +  llGetSubString(myDataBase, count*1022, -1));
	// convert DB back so this can be used to simply retrieve a back up at times
	myDataBase = llUnescapeURL(myDataBase); 

}

After calling this function (possibly as a response to a button within your admin menu- "ChatDB", or a chatted command that's listened for on some obscure channel that is masked for the owner's key), the object will chat out the database for you.

All that remains now is to copy&paste this output to a text document, remove the inserted new lines, "[time stamp] Object:" bits and put double quotes around it all. Then have the following global initialization line in your rewritten code:

string myDataBase = llUnescapeURL('your cleaned and double quote enclosed chat output');

NOTE: When using Json text for a database, you should check free memory after each insertion of new data into it. Only by doing so will you be able to realize that things may be getting a wee bit too tight for comfort within your program.

At that point, you need to consider a way to either shorten the length of your database, possible by going with (shudder) one character "Key"s (making a note in comments of what Key "Z" stands for) deleting older or no longer necessary records and/or transferring the entire database into a stand alone script with minimal functionality and having a link message protocol worked out for retrieval, insertion, deletion and updating the records.