G:/ScriptBasic/source/extensions/odbc/interface.c File Reference

#include <sql.h>
#include <sqlext.h>
#include <stdio.h>
#include "../../basext.h"

Go to the source code of this file.

Data Structures

struct  _odbcHANDLE
struct  _odbcOBJECT

Defines

#define BYTE_TYPE_ALREADY_DEFINED   1
#define ODBC_ERROR_NOCN   0x00081001
#define ODBC_ERROR_BDCN   0x00081002
#define ODBC_ERROR_CREF   0x00081003
#define ODBC_ERROR_EXEC   0x00081004
#define ODBC_ERROR_NORS   0x00081005
#define ODBC_ERROR_LVAL   0x00081006
#define GET_DB_HANDLE
#define CONFLEN   100
#define CONFROOT   "odbc.connections."
#define MAXKL   20

Typedefs

typedef _odbcHANDLE odbcHANDLE
typedef _odbcHANDLEpodbcHANDLE
typedef _odbcOBJECT odbcOBJECT
typedef _odbcOBJECTpodbcOBJECT

Functions

besSUB_ERRMSG switch (iError)
besEND besVERSION_NEGOTIATE return (int)
INITLOCK if (iFirst)
 if (besMODULEPOINTER==NULL)
 SQLFreeHandle (SQL_HANDLE_ENV, p->hEnv)
static int _GetData (pSupportTable pSt, LEFTVALUE Lval, podbcHANDLE q, long i, long j)
 besFUNCTION (odbc_fetcharray)
 if (!(SQL_SUCCEEDED(ret)))
 for (i=0;i< q->num_fields;i++)
 LONGVALUE (besRETURNVALUE)
besEND besFUNCTION (odbc_fetchhash)
besEND besFUNCTION (odbc_affected_rows)
 besDEREFERENCE (Argument)
 if (!Argument) return EX_ERROR_TOO_FEW_ARGUMENTS
 if (if ret==SQL_ERROR)
 if (Argument)
 if (pszDataSourceName)
 if (pH==NULL)
 strcpy (szConfigPath, CONFROOT)
 if (STRLEN(Argument) > CONFLEN-strlen(CONFROOT)-MAXKL) return ODBC_ERROR_BDCN
 memcpy (pszConf, STRINGVALUE(Argument), STRLEN(Argument))
 strcpy (pszConf,"dsn")
 strcpy (pszConf,"user")
 strcpy (pszConf,"password")
 if (pH->hStmt=NULL==pszDataSourceName||NULL==pszUser||NULL==pszPassword)

Variables

return Unknown error
return lTC
besEND besSUB_START podbcOBJECT p
SQLRETURN ret
INITUNLO besMODULEPOINTER = besALLOC(sizeof(odbcOBJECT))
 return
Lval = besNEWARRAY(0,q->num_fields-1)
if Lval return COMMAND_ERROR_MEMORY_LOW
 ret = SQLFetch(q->hStmt)
 besALLOC_RETURN_LONG
 ret = SQLFetch(q->hStmt)
Lval = besNEWARRAY(0,2*q->num_fields-1)
if Lval return COMMAND_ERROR_MEMORY_LOW
 besALLOC_RETURN_LONG
 Argument = besARGUMENT(2)
 Argument = besCONVERT2STRING(Argument)
 ret = SQLAllocHandle(SQL_HANDLE_STMT,q->hConn,&(q->hStmt))
else pszDataSourceName = NULL
 Argument = besARGUMENT(2)
else pszUser = NULL
 Argument = besARGUMENT(3)
else pszPassword = NULL
 ret
pH hStmt = NULL
SQLRETURN ret
char * pszDataSourceName
char * pszUser
char * pszPassword
podbcOBJECT p
char szConfigPath [CONFLEN]
char * pszConf
char * pszCname
podbcHANDLE pH
 p = (podbcOBJECT)besMODULEPOINTER
 pH = besALLOC(sizeof(odbcHANDLE))
else return ODBC_ERROR_NOCN
 pszConf = szConfigPath + strlen(szConfigPath)
 pszConf = STRLEN(Argument)
pszConf = '.'
 pszDataSourceName = besCONFIG(szConfigPath)
 pszUser = besCONFIG(szConfigPath)
 pszPassword = besCONFIG(szConfigPath)


Define Documentation

#define BYTE_TYPE_ALREADY_DEFINED   1
 

Definition at line 15 of file interface.c.

#define CONFLEN   100
 

Definition at line 766 of file interface.c.

#define CONFROOT   "odbc.connections."
 

Definition at line 767 of file interface.c.

#define GET_DB_HANDLE
 

Value:

p = (podbcOBJECT)besMODULEPOINTER;\
  Argument = besARGUMENT(1);\
  besDEREFERENCE(Argument);\
  if( ! Argument )return EX_ERROR_TOO_FEW_ARGUMENTS;\
  Argument = besCONVERT2LONG(Argument);\
  q = besHandleGetPointer(p->HandleArray,LONGVALUE(Argument));\
  if( q == NULL )return COMMAND_ERROR_ARGUMENT_RANGE;

Definition at line 198 of file interface.c.

#define MAXKL   20
 

Definition at line 768 of file interface.c.

#define ODBC_ERROR_BDCN   0x00081002
 

Definition at line 81 of file interface.c.

Referenced by switch().

#define ODBC_ERROR_CREF   0x00081003
 

Definition at line 83 of file interface.c.

Referenced by switch().

#define ODBC_ERROR_EXEC   0x00081004
 

Definition at line 85 of file interface.c.

Referenced by _GetData(), and switch().

#define ODBC_ERROR_LVAL   0x00081006
 

Definition at line 89 of file interface.c.

Referenced by switch().

#define ODBC_ERROR_NOCN   0x00081001
 

ODBC Database Connection Module

This module implements ODBC interface to connect to relational databases. Using this module you can use SQL RDBMS implementations from Windows/Unix/Linux platform independant of the actual database system.

Though this module does not deliver all functions of the ODBC system it does implement most of the functions that are needed to write SQL based applications. You can connect to ODBC databases, disconnect, issue SQL commands and retrieve the data resulted by the queries.

To use this module from a BASIC program you have to include the file T<odbc.bas> with the line

=verbatim import odbc.bas =noverbatim

to have all the neccessary function defintiions. This file and the neccessary T<odbc.dll> (or T<odbc.so>) are installed in the include and in the modules directories on Windows.

A typical BASIC application using this module first calls R<Connect> or R<RealConnect> to get a connection to the database. Then the program calls R<Query> to execute SQL queries, altering the database and R<FetchHash> and/or R<FetchArray> to get the result of queries. Finally the program optionally calls R<Close> to close the connection.

(Calling R<Close> is optional. R<Close> is automatically called for each opened database connection when the interpreter finishes.)

The underlying ODBC system layer implements connection pooling on process level. Thus there is no need to use the ScriptBasic resource pooling module as it is done by the MySQL module. (By the time I write this the ScriptBasic resource pool support module is still experimental, and so is the MySQL module interface utilizing it.)

When the ODBC module is initialized the module requests the underlying ODBC layer to perform process level connection pooling. The module also implements the multi-thread interface functions that allow the module to remain in memory if used in multi-thread application.

The module raises module specific errors. For more information see R<Error>.

Definition at line 79 of file interface.c.

Referenced by switch().

#define ODBC_ERROR_NORS   0x00081005
 

Definition at line 87 of file interface.c.

Referenced by switch().


Typedef Documentation

typedef struct _odbcHANDLE odbcHANDLE
 

typedef struct _odbcOBJECT odbcOBJECT
 

typedef struct _odbcHANDLE * podbcHANDLE
 

typedef struct _odbcOBJECT * podbcOBJECT
 


Function Documentation

static int _GetData pSupportTable  pSt,
LEFTVALUE  Lval,
podbcHANDLE  q,
long  i,
long  j
[static]
 

Definition at line 208 of file interface.c.

References COMMAND_ERROR_MEMORY_LOW, COMMAND_ERROR_SUCCESS, memcpy(), NULL, ODBC_ERROR_EXEC, q, and STRINGVALUE().

besDEREFERENCE Argument   ) 
 

besEND besFUNCTION odbc_affected_rows   ) 
 

=section AffectedRows =H AffectedRows

=verbatim R = ODBC::AffectedRows(DB) =noverbatim

Use this function to get the number of rows affected by a previous T<UPDATE>, T<DELETE> or T<INSERT> command. The function returns undefined number in case the last executed was none of the above command types.

The underlying ODBC function this function calls is T<SQLRowCount>.

Definition at line 471 of file interface.c.

References NULL, q, and SQLFreeHandle().

besEND besFUNCTION odbc_fetchhash   ) 
 

=section FetchHash =H FetchHash

=verbatim R = ODBC::FetchHash(DB,arr) =noverbatim

Use this function to fetch one row from the results after a successfully executed T<SELECT> statement. The first argument to the function is T<DB> the database handle. The second argument should be a variable. This variable will hold the hash-array containing the data of the actual row. The array is indexed from zero to T<2*n-1> where T<n> is the number of columns in the result set. Every even element (starting with index zero) will hold the name of a column and the next odd element of the array will hold the value of the column. This is according to the storage strategy of ScriptBasic for hashes.

After the successful execution of this function you will be able to access the value of the column named T<"column"> with the syntax T<arr{"column"}>. Note however that column names in SQL are usually case insensitive, but ScriptBasic hash indexing is case sensitive.

Use consecutive calls to this fucntion to get the rows of the result one after the other. You can mix consecutive calls to T<FetchHash> and R<FetchArray>.

The function returns T<-1> (TRUE) if fetching a row of data was successful or T<0> (FALSE) if there are no more rows in the dataset. In the latter case T<arr> will be T<undef>.

See also R<FetchArray>.

This function calls the ODBC functions T<SQLFetch>, T<SQLDescribeCol>, T<SQLGetData>.

Definition at line 357 of file interface.c.

References besRETURNVALUE, and COMMAND_ERROR_SUCCESS.

besFUNCTION odbc_fetcharray   ) 
 

=section FetchArray =H FetchArray

=verbatim R = ODBC::FetchArray(DB,arr) =noverbatim

Use this function to fetch one row from the results after a successfully executed T<SELECT> statement. The first argument to the function is T<DB> the database handle. The second argument should be a variable. This variable will hold the array containing the data of the actual row. The array is indexed from zero to T<n-1> where T<n> is the number of columns in the result set.

Use consecutive calls to this fucntion to get the rwos of the result one after the other. You can mix consecutive calls to R<FetchHash> and T<FetchArray>.

The function returns T<-1> (TRUE) if fetching a row of data was successful or T<0> (FALSE) if there are no more rows in the dataset. In the latter case T<arr> will be T<undef>.

See also R<FetchHash>

This function calls the ODBC functions T<SQLFetch>, T<SQLDescribeCol>, T<SQLGetData>.

Definition at line 275 of file interface.c.

References besRETURNVALUE, and COMMAND_ERROR_SUCCESS.

for i  = 0 ; i < q->num_fields ; i++  ) 
 

Definition at line 318 of file interface.c.

if pH->hStmt NULL = pszDataSourceName || NULL == pszUser || NULL == pszPassword  ) 
 

Definition at line 810 of file interface.c.

if STRLEN(Argument ,
CONFLEN-strlen(CONFROOT)-  MAXKL
 

if pH  = NULL  ) 
 

Definition at line 776 of file interface.c.

if pszDataSourceName   ) 
 

Definition at line 693 of file interface.c.

if Argument   ) 
 

Definition at line 675 of file interface.c.

References Argument, besCONVERT2ZCHAR(), and pszUser.

if if  ret = = SQL_ERROR  ) 
 

Definition at line 566 of file interface.c.

if Argument  ) 
 

Definition at line 1846 of file string.c.

if (SQL_SUCCEEDED(ret))  ) 
 

Definition at line 312 of file interface.c.

References besALLOC_RETURN_LONG, besRETURNVALUE, COMMAND_ERROR_SUCCESS, and LONGVALUE().

if besMODULEPOINTER  = NULL  ) 
 

Definition at line 170 of file interface.c.

INITLOCK if iFirst   ) 
 

Definition at line 159 of file interface.c.

References NULL.

LONGVALUE besRETURNVALUE   ) 
 

memcpy pszConf  ,
STRINGVALUE(Argument ,
STRLEN(Argument
 

besEND besVERSION_NEGOTIATE return int   ) 
 

Definition at line 122 of file interface.c.

SQLFreeHandle SQL_HANDLE_ENV  ,
p->  hEnv
 

Referenced by besFUNCTION().

strcpy pszConf  ,
"password" 
 

strcpy pszConf  ,
"user" 
 

strcpy pszConf  ,
"dsn" 
 

strcpy szConfigPath  ,
CONFROOT 
 

besSUB_ERRMSG switch iError   ) 
 

=H Error Codes

The module raises error in case when there is some problem with the underlying database handling. The error codes the module may raise:

=itemize =item 0x00081001 there is no such connection name =item 0x00081002 there is some problem with the connection name. This is an error in the configuration file. =item 0x00081003 the connection was refused by the database. =item 0x00081004 query execution error. =item 0x00081005 the program tries to call R<FetchArray> or R<Fetchhash> without a preceeding query execution. =item 0x00081006 the second argument of a fetch does not evaluate to left value. =noitemize

Definition at line 110 of file interface.c.

References ODBC_ERROR_BDCN, ODBC_ERROR_CREF, ODBC_ERROR_EXEC, ODBC_ERROR_LVAL, ODBC_ERROR_NOCN, and ODBC_ERROR_NORS.


Variable Documentation

Argument = besARGUMENT(3)
 

Definition at line 681 of file interface.c.

Argument = besARGUMENT(2)
 

Definition at line 673 of file interface.c.

Argument = besCONVERT2STRING(Argument)
 

Definition at line 563 of file interface.c.

Argument = besARGUMENT(2)
 

Definition at line 559 of file interface.c.

besALLOC_RETURN_LONG
 

Definition at line 452 of file interface.c.

besALLOC_RETURN_LONG
 

Definition at line 323 of file interface.c.

INITUNLO besMODULEPOINTER = besALLOC(sizeof(odbcOBJECT))
 

Definition at line 169 of file interface.c.

if Lval return COMMAND_ERROR_MEMORY_LOW
 

Definition at line 412 of file interface.c.

if Lval return COMMAND_ERROR_MEMORY_LOW
 

Definition at line 308 of file interface.c.

return Unknown error
 

Definition at line 118 of file interface.c.

pH hStmt = NULL
 

Definition at line 692 of file interface.c.

return lTC
 

Definition at line 151 of file interface.c.

* Lval = besNEWARRAY(0,2*q->num_fields-1)
 

Definition at line 411 of file interface.c.

* Lval = besNEWARRAY(0,q->num_fields-1)
 

Definition at line 307 of file interface.c.

else return ODBC_ERROR_NOCN
 

Definition at line 786 of file interface.c.

p = (podbcOBJECT)besMODULEPOINTER
 

Definition at line 773 of file interface.c.

podbcOBJECT p
 

Definition at line 765 of file interface.c.

besEND besSUB_START podbcOBJECT p
 

Definition at line 155 of file interface.c.

pH = besALLOC(sizeof(odbcHANDLE))
 

Definition at line 775 of file interface.c.

podbcHANDLE pH
 

Definition at line 771 of file interface.c.

char* pszCname
 

Definition at line 770 of file interface.c.

* pszConf = '.'
 

Definition at line 795 of file interface.c.

pszConf = STRLEN(Argument)
 

Definition at line 794 of file interface.c.

pszConf = szConfigPath + strlen(szConfigPath)
 

Definition at line 791 of file interface.c.

char * pszConf
 

Definition at line 769 of file interface.c.

pszDataSourceName = besCONFIG(szConfigPath)
 

Definition at line 799 of file interface.c.

char* pszDataSourceName
 

Definition at line 764 of file interface.c.

else pszDataSourceName = NULL
 

Definition at line 670 of file interface.c.

pszPassword = besCONFIG(szConfigPath)
 

Definition at line 807 of file interface.c.

char * pszPassword
 

Definition at line 764 of file interface.c.

else pszPassword = NULL
 

Definition at line 685 of file interface.c.

pszUser = besCONFIG(szConfigPath)
 

Definition at line 803 of file interface.c.

char * pszUser
 

Definition at line 764 of file interface.c.

else pszUser = NULL
 

Definition at line 677 of file interface.c.

SQLRETURN ret
 

Definition at line 762 of file interface.c.

ret
 

Initial value:

 SQLConnect(pH->hConn,
                   pszDataSourceName,(SQLSMALLINT)strlen(pszDataSourceName),
                   pszUser,(SQLSMALLINT)strlen(pszUser),
                   pszPassword,(SQLSMALLINT)strlen(pszPassword))

Definition at line 688 of file interface.c.

ret = SQLAllocHandle(SQL_HANDLE_STMT,q->hConn,&(q->hStmt))
 

Definition at line 565 of file interface.c.

ret = SQLFetch(q->hStmt)
 

Definition at line 404 of file interface.c.

ret = SQLFetch(q->hStmt)
 

Definition at line 311 of file interface.c.

SQLRETURN ret
 

Definition at line 156 of file interface.c.

return
 

Definition at line 195 of file interface.c.

char szConfigPath[CONFLEN]
 

Definition at line 769 of file interface.c.


Generated on Sun Mar 12 23:56:36 2006 for ScriptBasic by  doxygen 1.4.6-NO