View previous topic :: View next topic |
Author |
Message |
caloup
Joined: 29 Sep 2006 Posts: 59 Location: albi (france)
|
Posted: Sat Nov 25, 2006 0:04 Post subject: [suggestion-optimisation] SQLEncodeSpecialChar |
|
|
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 |
|
|
Grinning Fool
Joined: 12 Feb 2005 Posts: 264
|
Posted: Sat Nov 25, 2006 0:52 Post subject: |
|
|
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 |
|
|
caloup
Joined: 29 Sep 2006 Posts: 59 Location: albi (france)
|
Posted: Sun Nov 26, 2006 8:54 Post subject: so... |
|
|
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 |
|
|
Asmodae
Joined: 07 Jan 2005 Posts: 55
|
Posted: Wed Nov 29, 2006 20:41 Post subject: |
|
|
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 |
|
|
Papillon x-man
Joined: 28 Dec 2004 Posts: 1060 Location: Germany
|
Posted: Thu Nov 30, 2006 10:21 Post subject: |
|
|
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 |
|
|
Grinning Fool
Joined: 12 Feb 2005 Posts: 264
|
Posted: Thu Nov 30, 2006 17:59 Post subject: |
|
|
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 |
|
|
|