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 
 
doesTableExist(string TableName) function in nwnx_demo_mysql

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Technical support
View previous topic :: View next topic  
Author Message
Firya'nis



Joined: 21 Sep 2009
Posts: 3

PostPosted: Thu Oct 08, 2009 16:59    Post subject: doesTableExist(string TableName) function in nwnx_demo_mysql Reply with quote

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... Smile

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
View user's profile Send private message
Zebranky



Joined: 04 Jun 2006
Posts: 415

PostPosted: Thu Oct 08, 2009 19:30    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Firya'nis



Joined: 21 Sep 2009
Posts: 3

PostPosted: Fri Oct 09, 2009 0:17    Post subject: Reply with quote

Thanks a lot, i think i got it... Smile

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 ? Smile
_________________
Firya'nis
Back to top
View user's profile Send private message
Zebranky



Joined: 04 Jun 2006
Posts: 415

PostPosted: Fri Oct 09, 2009 2:44    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Technical support 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