[Gambas-devel] Change to gb.db.odbc to use ODBC Connection Strings.

ML d4t4full at ...176...
Fri Aug 28 13:21:18 CEST 2015


*On 2015-08-27 15:06, Benoît Minisini wrote:*
> *Le 27/08/2015 20:03, Benoît Minisini a écrit :*
>> *Le 27/08/2015 19:14, zxMarce a écrit :*
>>> Benoît,
>>> No problem, I know you're not the author of the ODBC component.
>>> Precisely, one of my modifications was adding an IF to call either 
>>> SQLConnect (normal method so far) or SQLDriverConnect; this is the 
>>> new call, and does not use .User, .Password nor .Name, as they can 
>>> all be set in the connection string.
>>> I added, then, an IF and a FUNCTION. This is the IF, in main.c's 
>>> open_database:
>>> ... And this is the function that new IF calls:
>>> Thanks,
>>> zxMarce.
>> Please post directly to the developer mailing-list. nabble.com makes 
>> your mail unreadable by removing pictures.
>> Or don't post pictures. Post the code as text!
> If your function (is_host_a_connstring) returns a boolean value, you 
> can use 'bool' as return datatype.
> If you are testing a boolean value, writing that:
>     if (xxx == TRUE)
> is not needed. Just write:
>     if (xxx)
> Regards,
Wow! I did not know Nabble removed text blocks from mails. I now also 
see the missing segments in my mails. I used 'Raw Text' blocks to post 
the code.
Anyway, for post completeness, the missing if was as follows:

     if (is_host_a_connstring(desc->host) == TRUE)
     {
         /* zxMarce: Connect to Database (desc->host is an ODBC 
Connection String) */
         retcode = SQLDriverConnect(odbc->odbcHandle, 0, (SQLCHAR *) 
desc->host, SQL_NTS, 0, 0, 0, SQL_DRIVER_NOPROMPT);
         /* Example - ODBC-ConnString, all one line (must assign this to 
the Connection.Host property in
            Gambas code and then call Connection.Open):
"Driver=<driverSectionNameInODBCInst.Ini>;
                          TDS_Version=<useNormally'7.2';
                          Server=<serverNameOrIP>;
                          Port=<serverTcpPort>;
                          Database=<defaultDatabase>;
                          UId=<userName>;
                          Pwd=<password>>"
         */
     } else {
         /* Connect to Database (desc->host is an ODBC Data Source Name) */
         retcode = SQLConnect(odbc->odbcHandle, (SQLCHAR *) desc->host, 
SQL_NTS, (SQLCHAR *) desc->user, SQL_NTS, (SQLCHAR *) desc->password, 
SQL_NTS);
     }

And the missing helper function was:

     /*
     Internal function to check if the .Host property is actually an 
ODBC connection string.
     ODBC ConnStrings have one or more "ParamName=ParamValue" pairs, 
delimited by semicolons.
     The function helps the component know whether to call SQLConnect 
(when a host/DSN),
     or SQLDriverConnect (when a ConnString).
     zxMarce, 20150814
     */
     static int is_host_a_connstring(char *host_or_cs)
     {

       int length = strlen(host_or_cs);
       bool connstrCharFound = FALSE;
       int counter;
       char curChar;

       for (counter = 0; counter < length; counter++)
       {
         curChar = host_or_cs[counter];
         if ((curChar == '=') | (curChar == ';'))
         {
           connstrCharFound = TRUE;
           break;
         }
       }

       return connstrCharFound;

     }

Now I will change the function type from INT to BOOL and reform the IF. 
Thanks for the tip!

Regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/devel/attachments/20150828/4dd6d100/attachment.html>


More information about the Devel mailing list