View previous topic :: View next topic |
Author |
Message |
Firya'nis
Joined: 21 Sep 2009 Posts: 3
|
Posted: Thu Oct 08, 2009 16:59 Post subject: doesTableExist(string TableName) function in nwnx_demo_mysql |
|
|
Hello, everybody
I'm doing a persistent quest journal for our french persistent RP module... it works and actually, i'm trying to simplify the installation process by creating the requested table automatically when the module is loaded the first time after importing the system in the toolset.
I had a look in the nwnx_demo_mysql module to learn how to create requested tables from within a module... but I don't understand something in the following function and hope someone can help me to understand...
Code: | int doesTableExist(string tablename)
{
SQLExecDirect("SELECT count(*) FROM " + tablename);
if (SQLFetch() != SQL_SUCCESS)
return FALSE;
string val = SQLGetData(1);
if (val == "0" || StringToInt(val) > 0)
return TRUE;
return FALSE;
} |
I understand everything except the :
Code: | if (val == "0" || StringToInt(val) > 0) |
I have two questions about it...
1. Why do we compare the same variable with a string and then again with an integer after casting the variable to an integer ?
2. As both tests are separated by a ||, why do we return TRUE when the variable is "0" or > 0... shouldn't it be :
Code: | if (val != "0" || StringToInt(val) > 0) |
Thank you for your time. _________________ Firya'nis |
|
Back to top |
|
|
Zebranky
Joined: 04 Jun 2006 Posts: 415
|
Posted: Thu Oct 08, 2009 19:30 Post subject: |
|
|
StringToInt() returns 0 if it can't parse the string into an int. Therefore, if val were originally "Table does not exist" (or whatever error message might be returned), StringToInt(val) would still return 0, but it would actually be an error condition. A table with 0 rows is a perfectly valid condition, though. _________________ Win32 SVN builds: http://www.mercuric.net/nwn/nwnx/
<Fluffy-Kooshy> NWNx plugin is to this as nuclear warheads are to getting rid of fire ants.
<ThriWork> whenever I hear nwn extender, I think what does NWN need a penis extender for? |
|
Back to top |
|
|
Firya'nis
Joined: 21 Sep 2009 Posts: 3
|
Posted: Fri Oct 09, 2009 0:17 Post subject: |
|
|
Thanks a lot, i think i got it...
It's the only way to check if the table exists or not because :
If val == "0", it means val is 0 and the table exists.
If StringToInt(val) > 0, it means val is greater than 0 and the table exists
If StringToInt(val) == 0, it means val can't be casted to an integer and so, it is not "0", it is not "1"... it is something like "there is no table and therefore, no return value".
Correct ? _________________ Firya'nis |
|
Back to top |
|
|
Zebranky
Joined: 04 Jun 2006 Posts: 415
|
Posted: Fri Oct 09, 2009 2:44 Post subject: |
|
|
Firya'nis wrote: | If StringToInt(val) == 0, it means val can't be casted to an integer and so, it is not "0", it is not "1"... it is something like "there is no table and therefore, no return value". |
Close, except that this can ALSO mean it actually is "0". It's ambiguous, and there's no way to know for sure besides checking the string. _________________ Win32 SVN builds: http://www.mercuric.net/nwn/nwnx/
<Fluffy-Kooshy> NWNx plugin is to this as nuclear warheads are to getting rid of fire ants.
<ThriWork> whenever I hear nwn extender, I think what does NWN need a penis extender for? |
|
Back to top |
|
|
|