logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
[suggestion-optimisation] SQLEncodeSpecialChar

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Development
View previous topic :: View next topic  
Author Message
caloup



Joined: 29 Sep 2006
Posts: 59
Location: albi (france)

PostPosted: Sat Nov 25, 2006 0:04    Post subject: [suggestion-optimisation] SQLEncodeSpecialChar Reply with quote

Hello !

Garrath a member of the french community in neverwinternights2.fr
offer to replace this :
Quote:

// Des problèmes peuvent apparaître avec les commandes SQL si les variable ou leurs valeurs n’ont //qu’un seul apostrophe dans leur nom
// Ces fonctions vont remplacer ce guillemet par un caractère tilde (~)

string SQLEncodeSpecialChars(string sString)

{
if (FindSubString(sString, "'") == -1) // Si aucun de trouvé
return sString;

int i;
string sReturn = "";
string sChar;

// Fait une boucle sur chaque caractères et remplace les caractères spéciaux
//Ajoute letter par letters en remplaçant les caractères spéciaux jusqu’à obtenir la phrase
for (i = 0; i < GetStringLength(sString); i++)

{
sChar = GetSubString(sString, i, 1);
if (sChar == "'")
sReturn += "~";
else
sReturn += sChar;

}
return sReturn;

}

string SQLDecodeSpecialChars(string sString)

{
if (FindSubString(sString, "~") == -1) // Si aucun de trouvé
return sString;

int i;
string sReturn = "";
string sChar;

// Fait une boucle sur chaque caractères et remplace les caractères spéciaux
for (i = 0; i < GetStringLength(sString); i++)

{
sChar = GetSubString(sString, i, 1);
if (sChar == "~")
sReturn += "'";
else
sReturn += sChar;

}
return sReturn;

}


with that :

Quote:

// Des problèmes peuvent apparaître avec les commandes SQL si les variable ou leurs valeurs n’ont //qu’un seul apostrophe dans leur nom
// Ces fonctions vont remplacer ce guillemet par un caractère tilde (~)

string SQLEncodeSpecialChars(string sString)

{
if (FindSubString(sString, "'") == -1) // Si aucun de trouvé
return sString;

int i;
string sReturn = "";
string sChar;

// Fait une boucle sur chaque caractères et remplace les caractères spéciaux
//Ajoute letter par letters en remplaçant les caractères spéciaux jusqu’à obtenir la phrase
for (i = 0; i < GetStringLength(sString); i++)

{
sChar = GetSubString(sString, i, 1);
if (sChar == "'")
sReturn += sChar+sChar;
else
sReturn += sChar;

}
return sReturn;

}


So you don't have to do SQLDecodeSpecialChar after that...

(note : comment are in french because i have translated them...for french community)
Back to top
View user's profile Send private message
Grinning Fool



Joined: 12 Feb 2005
Posts: 264

PostPosted: Sat Nov 25, 2006 0:52    Post subject: Reply with quote

You'll also want to assign the result of GetStringLength to a temporary int -- otherwise I believe it will be re-invoked and a variable possibly re-created on the stack every time the loop iterates.
_________________
Khalidine, a NWN2 persistent world

Looking for volunteers.
Back to top
View user's profile Send private message
caloup



Joined: 29 Sep 2006
Posts: 59
Location: albi (france)

PostPosted: Sun Nov 26, 2006 8:54    Post subject: so... Reply with quote

So we could write :

Quote:

// Des problèmes peuvent apparaître avec les commandes SQL si les variable ou leurs valeurs n’ont //qu’un seul apostrophe dans leur nom
// Ces fonctions vont remplacer ce guillemet par un caractère tilde (~)

string SQLEncodeSpecialChars(string sString)

{
if (FindSubString(sString, "'") == -1) // Si aucun de trouvé
return sString;

int i;
string sReturn = "";
string sChar;
int iLength=GetStringLength(sString);

// Fait une boucle sur chaque caractères et remplace les caractères spéciaux
//Ajoute letter par letters en remplaçant les caractères spéciaux jusqu’à obtenir la phrase
for (i = 0; i <iLength ; i++)

{
sChar = GetSubString(sString, i, 1);
if (sChar == "'")
sReturn += sChar+sChar;
else
sReturn += sChar;

}
return sReturn;

}
Back to top
View user's profile Send private message
Asmodae



Joined: 07 Jan 2005
Posts: 55

PostPosted: Wed Nov 29, 2006 20:41    Post subject: Reply with quote

Depending on NWN2 and the new versions of MySQL this shouldn't be necessary.

Any looping through the string is a performance hit, and ideally would like to be avoided.
I made a topic about this in the NWNX2 forums here:
http://www.nwnx.org/phpBB2/viewtopic.php?t=30&highlight=encode

there are a few suggestions on how to get and wrap the line with the double quotes before sending it to the Database, in which case there's no need for any encoding/decoding at all.



- Asmodae
_________________
Nepenthe - An NWN2 Persistant World, coming to a planet near you. http://www.nepentheonline.com
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Papillon
x-man


Joined: 28 Dec 2004
Posts: 1060
Location: Germany

PostPosted: Thu Nov 30, 2006 10:21    Post subject: Reply with quote

Yep, and I haven't forgotten about this thread. Escaping the special chars is on my list of things to do.
_________________
Papillon
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Grinning Fool



Joined: 12 Feb 2005
Posts: 264

PostPosted: Thu Nov 30, 2006 17:59    Post subject: Reply with quote

I think I'll suggest using a separate 'cache' object if that's the solution used for special characters. The special char variable should be the only thing store on it. The reason for this is that all local variables are stored on objects in linked list format. The more things we put on a single object (like the module), the longer it takes to access them.
_________________
Khalidine, a NWN2 persistent world

Looking for volunteers.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Development All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group