G:/ScriptBasic/source/extensions/mysql/interface.c

Go to the documentation of this file.
00001 /* mysqlinterf.c interface to MySQL for ScriptBasic
00002 
00003 NTLIBS: mysqlclient.lib ws2_32.lib advapi32.lib
00004 UXLIBS: -lmysqlclient
00005 
00006 Disabled SHUTDOWN - JRS 3/3/2006
00007 */
00008 #if WIN32
00009 #include <windows.h>
00010 #endif
00011 #include <stdio.h>
00012 #include <string.h>
00013 
00014 #include <mysql/mysql.h>
00015 #include <mysql/errmsg.h>
00016 
00017 /* set this macro to 1 if you use MySQL 3.23.29 or newer 
00018    set it to 0 if you want to compile this interface for an older version of MySQL
00019 */
00020 #ifndef VERSION323
00021 #define VERSION323 1
00022 #endif
00023 
00024 #include "../../basext.h"
00025 
00026 typedef struct _mymysqlHANDLE {
00027   MYSQL *hSQL;
00028   MYSQL_RES *result;
00029   int num_fields;
00030   struct _mymysqlHANDLE *next,*prev;
00031   } mymysqlHANDLE, *pmymysqlHANDLE;
00032 
00033 typedef struct _myOBJECT {
00034   void *HandleArray;
00035   pmymysqlHANDLE first;
00036   } myOBJECT, *pmyOBJECT;
00037 
00038 static int err_conv_table[] = {
00039   CR_CONN_HOST_ERROR,
00040   CR_CONNECTION_ERROR,
00041   CR_IPSOCK_ERROR,
00042   CR_OUT_OF_MEMORY,
00043   CR_SOCKET_CREATE_ERROR,
00044   CR_UNKNOWN_HOST,
00045   CR_VERSION_ERROR,
00046   0
00047   };
00048 
00049 #define MAXERR 7
00050 
00051 static char * (errtxt[]) = {
00052   "Connection to host error",
00053   "Connection error",
00054   "IP socket error",
00055   "Out of memory error",
00056   "Socket create error",
00057   "Unknown host error",
00058   "Version error",
00059   NULL
00060   };
00061 
00062 static int convert_error(int iMyError){
00063   int i;
00064 
00065   for( i=0 ; err_conv_table[i] ; i++ )
00066     if( err_conv_table[i] == iMyError )return 0x00080002+i;
00067   return 0x00080001;
00068   }
00069 
00070 /* Left value argument is needed for the command */
00071 #define MYSQL_ERROR_LVAL 0x00081001
00072 /* Data seek without alive result set. */
00073 #define MYSQL_ERROR_SEEK 0x00081002
00074 /* Data seek offset is out of range */
00075 #define MYSQL_ERROR_SEKO 0x00081003
00076 /* fecth without a valid result set */
00077 #define MYSQL_ERROR_NORS 0x00081004
00078 /* no defined connection name or connection is not defined */
00079 #define MYSQL_ERROR_NOCN 0x00081005
00080 /* the connectionname is too long */
00081 #define MYSQL_ERROR_BDCN 0x00081006
00082 
00083 besSUB_ERRMSG
00084   if( iError < 0x80000+MAXERR && iError > 0x80000 )
00085     return errtxt[iError-0x80000];
00086   else
00087     return NULL;
00088 besEND
00089 
00090 besVERSION_NEGOTIATE
00091   return (int)INTERFACE_VERSION;
00092 besEND
00093 
00094 
00095 besDLL_MAIN
00096 
00097 besSUB_PROCESS_START
00098 besEND
00099 
00100 besSUB_PROCESS_FINISH
00101 besEND
00102 
00103 besSUB_START
00104   pmyOBJECT p;
00105 
00106   besMODULEPOINTER = besALLOC(sizeof(myOBJECT));
00107   if( besMODULEPOINTER == NULL )return COMMAND_ERROR_MEMORY_LOW;
00108   p = (pmyOBJECT)besMODULEPOINTER;
00109   p->first = NULL; /* list of opened mySQL myhandles */
00110   p->HandleArray = NULL;
00111   return 0;
00112 besEND
00113 
00114 besSUB_FINISH
00115   pmyOBJECT p;
00116   pmymysqlHANDLE q;
00117 
00118   p = (pmyOBJECT)besMODULEPOINTER;
00119   if( p != NULL ){
00120     for( q = p->first ; q ; q = q->next ){
00121       mysql_close(q->hSQL);
00122       }
00123     besHandleDestroyHandleArray(p->HandleArray);
00124     }
00125   return 0;
00126 besEND
00127 
00128 #define GET_DB_HANDLE \
00129   p = (pmyOBJECT)besMODULEPOINTER;\
00130   Argument = besARGUMENT(1);\
00131   besDEREFERENCE(Argument);\
00132   if( Argument == NULL )return EX_ERROR_TOO_FEW_ARGUMENTS;\
00133   q = besHandleGetPointer(p->HandleArray,besGETLONGVALUE(Argument));\
00134   if( q == NULL )return COMMAND_ERROR_ARGUMENT_RANGE;
00135 
00136 
00137 
00138 besFUNCTION(mys_affected_rows)
00139   VARIABLE Argument;
00140   pmymysqlHANDLE q;
00141   pmyOBJECT p;
00142 
00143   GET_DB_HANDLE
00144 
00145   besALLOC_RETURN_LONG;
00146   LONGVALUE(besRETURNVALUE) = (long)mysql_affected_rows(q->hSQL);
00147 besEND
00148 
00149 besFUNCTION(mys_get_client_info)
00150   char *pszClientInfo;
00151 
00152   pszClientInfo = mysql_get_client_info();
00153   if( pszClientInfo ){
00154     besALLOC_RETURN_STRING(strlen(pszClientInfo));
00155     memcpy(STRINGVALUE(besRETURNVALUE),pszClientInfo,STRLEN(besRETURNVALUE));
00156     }else besRETURNVALUE = NULL;
00157 besEND
00158 
00159 besFUNCTION(mys_get_host_info)
00160   VARIABLE Argument;
00161   char *pszHostInfo;
00162   pmymysqlHANDLE q;
00163   pmyOBJECT p;
00164 
00165   GET_DB_HANDLE
00166 
00167   pszHostInfo = mysql_get_host_info(q->hSQL);
00168   if( pszHostInfo ){
00169     besALLOC_RETURN_STRING(strlen(pszHostInfo));
00170     memcpy(STRINGVALUE(besRETURNVALUE),pszHostInfo,STRLEN(besRETURNVALUE));
00171     }else besRETURNVALUE = NULL;
00172 besEND
00173 
00174 besFUNCTION(mys_get_server_info)
00175   VARIABLE Argument;
00176   char *pszServerInfo;
00177   pmymysqlHANDLE q;
00178   pmyOBJECT p;
00179 
00180   GET_DB_HANDLE
00181 
00182   pszServerInfo = mysql_get_server_info(q->hSQL);
00183   if( pszServerInfo ){
00184     besALLOC_RETURN_STRING(strlen(pszServerInfo));
00185     memcpy(STRINGVALUE(besRETURNVALUE),pszServerInfo,STRLEN(besRETURNVALUE));
00186     }else besRETURNVALUE = NULL;
00187 besEND
00188 
00189 besFUNCTION(mys_stat)
00190   VARIABLE Argument;
00191   char *pszStat;
00192   pmymysqlHANDLE q;
00193   pmyOBJECT p;
00194 
00195   GET_DB_HANDLE
00196 
00197   pszStat = mysql_stat(q->hSQL);
00198   if( pszStat ){
00199     besALLOC_RETURN_STRING(strlen(pszStat));
00200     memcpy(STRINGVALUE(besRETURNVALUE),pszStat,STRLEN(besRETURNVALUE));
00201     }else besRETURNVALUE = NULL;
00202 besEND
00203 
00204 besFUNCTION(mys_thread_id)
00205   VARIABLE Argument;
00206   pmymysqlHANDLE q;
00207   pmyOBJECT p;
00208 
00209   GET_DB_HANDLE
00210 
00211   besALLOC_RETURN_LONG;
00212   LONGVALUE(besRETURNVALUE) = 0; //mysql_thread_id(q->hSQL);
00213 besEND
00214 
00215 
00216 besFUNCTION(mys_info)
00217   VARIABLE Argument;
00218   char *pszInfo;
00219   pmymysqlHANDLE q;
00220   pmyOBJECT p;
00221 
00222   GET_DB_HANDLE
00223 
00224   pszInfo = NULL; //mysql_info(q->hSQL);
00225   if( pszInfo ){
00226     besALLOC_RETURN_STRING(strlen(pszInfo));
00227     memcpy(STRINGVALUE(besRETURNVALUE),pszInfo,STRLEN(besRETURNVALUE));
00228     }else besRETURNVALUE = NULL;
00229 besEND
00230 
00231 besFUNCTION(mys_get_proto_info)
00232   VARIABLE Argument;
00233   pmymysqlHANDLE q;
00234   unsigned int uProtoInfo;
00235   pmyOBJECT p;
00236 
00237   GET_DB_HANDLE
00238 
00239   uProtoInfo  = mysql_get_proto_info(q->hSQL);
00240   besALLOC_RETURN_LONG;
00241   LONGVALUE(besRETURNVALUE) = uProtoInfo;
00242 besEND
00243 
00244 besFUNCTION(mys_insert_id)
00245   VARIABLE Argument;
00246   pmymysqlHANDLE q;
00247   unsigned int uInsertId;
00248   pmyOBJECT p;
00249 
00250   GET_DB_HANDLE
00251 
00252   uInsertId  = 0;//(unsigned long)mysql_insert_id(q->hSQL);
00253   besALLOC_RETURN_LONG;
00254   LONGVALUE(besRETURNVALUE) = uInsertId;
00255 besEND
00256 
00257 besFUNCTION(mys_kill)
00258   VARIABLE Argument;
00259   pmymysqlHANDLE q;
00260   unsigned long pid;
00261   pmyOBJECT p;
00262 
00263   GET_DB_HANDLE
00264 
00265   Argument = besARGUMENT(2);
00266   besDEREFERENCE(Argument);
00267   if( ! Argument )return EX_ERROR_TOO_FEW_ARGUMENTS;
00268   Argument = besCONVERT2LONG(Argument);
00269   pid = LONGVALUE(Argument);
00270 
00271   mysql_kill(q->hSQL,pid);
00272 
00273   besRETURNVALUE= NULL;
00274 besEND
00275 
00276 besFUNCTION(mys_error)
00277   VARIABLE Argument;
00278   pmymysqlHANDLE q;
00279   char *pszError;
00280   pmyOBJECT p;
00281 
00282   GET_DB_HANDLE
00283 
00284   pszError = mysql_error(q->hSQL);
00285 
00286   besALLOC_RETURN_STRING(strlen(pszError));
00287   memcpy(STRINGVALUE(besRETURNVALUE),pszError,STRLEN(besRETURNVALUE));
00288 besEND
00289 
00290 besFUNCTION(mys_ping)
00291   VARIABLE Argument;
00292   pmymysqlHANDLE q;
00293   pmyOBJECT p;
00294 
00295   GET_DB_HANDLE
00296 
00297   besALLOC_RETURN_LONG;
00298   LONGVALUE(besRETURNVALUE) = mysql_ping(q->hSQL) ? 0 : -1;
00299 
00300 besEND
00301 
00302 besFUNCTION(mys_real_escape_string)
00303   VARIABLE Argument;
00304   pmymysqlHANDLE q;
00305   char *pszTo;
00306   unsigned long lLen;
00307   pmyOBJECT p;
00308 
00309   GET_DB_HANDLE
00310 
00311   Argument = besARGUMENT(2);
00312   besDEREFERENCE(Argument);
00313   if( ! Argument )return EX_ERROR_TOO_FEW_ARGUMENTS;
00314   Argument = besCONVERT2STRING(Argument);
00315 
00316   pszTo = besALLOC( 2*STRLEN(Argument) + 1 );
00317   if( pszTo == NULL )return COMMAND_ERROR_MEMORY_LOW;
00318 #if VERSION323
00319   lLen = mysql_real_escape_string(q->hSQL,pszTo,STRINGVALUE(Argument),STRLEN(Argument));
00320 
00321   besALLOC_RETURN_STRING(lLen);
00322   memcpy(STRINGVALUE(besRETURNVALUE),pszTo,STRLEN(besRETURNVALUE));
00323   besFREE(pszTo);
00324 #else
00325   besRETURNVALUE = NULL;
00326 #endif
00327 
00328 besEND
00329 
00330 besFUNCTION(mys_select_db)
00331   VARIABLE Argument;
00332   pmymysqlHANDLE q;
00333   char *pszDb;
00334   pmyOBJECT p;
00335 
00336   GET_DB_HANDLE
00337 
00338   Argument = besARGUMENT(2);
00339   besDEREFERENCE(Argument);
00340   if( ! Argument )return EX_ERROR_TOO_FEW_ARGUMENTS;
00341   Argument = besCONVERT2STRING(Argument);
00342   besCONVERT2ZCHAR(Argument,pszDb);
00343 
00344   mysql_select_db(q->hSQL,pszDb);
00345   besFREE(pszDb);
00346   besRETURNVALUE = NULL;
00347 besEND
00348 
00349 besFUNCTION(mys_shutdown)
00350 /*
00351   VARIABLE Argument;
00352   pmymysqlHANDLE q;
00353   pmyOBJECT p;
00354 
00355   GET_DB_HANDLE
00356 
00357   mysql_shutdown(q->hSQL);
00358 */
00359   besRETURNVALUE = NULL;
00360 besEND
00361 
00362 besFUNCTION(mys_data_seek)
00363   VARIABLE Argument;
00364   pmymysqlHANDLE q;
00365   unsigned long lOffset,lLimit;
00366   pmyOBJECT p;
00367 
00368   GET_DB_HANDLE
00369 
00370   besRETURNVALUE = NULL;
00371   Argument = besARGUMENT(2);
00372   if( Argument == NULL ){
00373     lOffset = 0L;
00374     }else{
00375     Argument = besCONVERT2LONG(Argument);
00376     lOffset = LONGVALUE(Argument);
00377     }
00378 
00379   if( q->result ){
00380     lLimit = (unsigned long)mysql_num_rows(q->result);
00381     if( 0 <= lOffset && lOffset < lLimit )
00382       mysql_data_seek(q->result,(my_ulonglong)lOffset);
00383     else
00384       return MYSQL_ERROR_SEKO;
00385     }
00386   else
00387     return MYSQL_ERROR_SEEK;
00388 
00389 besEND
00390 
00391 besFUNCTION(mys_change_user)
00392   VARIABLE Argument;
00393   pmymysqlHANDLE q;
00394   char *pszUser,*pszPassword,*pszDB;
00395   int iError;
00396   pmyOBJECT p;
00397 
00398   GET_DB_HANDLE
00399 
00400   /* Get the user */
00401   Argument = besARGUMENT(2);
00402   besDEREFERENCE(Argument);
00403   if( Argument ){
00404     besCONVERT2ZCHAR(Argument,pszUser);
00405   }else pszUser = NULL;
00406 
00407 
00408   /* Get the password */
00409   Argument = besARGUMENT(3);
00410   besDEREFERENCE(Argument);
00411   if( Argument ){
00412     besCONVERT2ZCHAR(Argument,pszPassword);
00413   }else pszPassword = NULL;
00414 
00415   /* Get the dbname */
00416   Argument = besARGUMENT(4);
00417   besDEREFERENCE(Argument);
00418   if( Argument ){
00419     besCONVERT2ZCHAR(Argument,pszDB);
00420   }else pszDB = NULL;
00421 
00422 #if VERSION323
00423   iError = mysql_change_user(q->hSQL,pszUser,pszPassword,pszDB);
00424 #else
00425   iError = -1;
00426 #endif
00427 
00428   if( pszUser       )besFREE(pszUser);
00429   if( pszPassword   )besFREE(pszPassword);
00430   if( pszDB         )besFREE(pszDB);
00431 
00432   besRETURNVALUE = NULL;
00433   if( iError )return convert_error(iError); else return COMMAND_ERROR_SUCCESS;
00434 besEND
00435 
00436 besFUNCTION(mys_character_set_name)
00437   VARIABLE Argument;
00438   pmymysqlHANDLE q;
00439   const char *pszCharacterSetName;
00440   pmyOBJECT p;
00441 
00442   GET_DB_HANDLE
00443 
00444 #if VERSION323
00445   pszCharacterSetName = mysql_character_set_name(q->hSQL);
00446 
00447   besALLOC_RETURN_STRING(strlen(pszCharacterSetName));
00448 
00449   memcpy(STRINGVALUE(besRETURNVALUE),pszCharacterSetName,STRLEN(besRETURNVALUE));
00450 #else
00451   besRETURNVALUE = NULL;
00452 #endif
00453 
00454 besEND
00455 
00456 besFUNCTION(mys_fetcharray)
00457   VARIABLE Argument;
00458   LEFTVALUE Lval;
00459   pmymysqlHANDLE q;
00460   int i;
00461   unsigned long *lengths;
00462   MYSQL_ROW row;
00463   unsigned int numfields;
00464   unsigned long __refcount_;
00465   pmyOBJECT p;
00466 
00467   GET_DB_HANDLE
00468 
00469   besRETURNVALUE = NULL;
00470   /* db , array */
00471   if( besARGNR < 2 )return EX_ERROR_TOO_FEW_ARGUMENTS;
00472 
00473   if( q->result == NULL )return MYSQL_ERROR_NORS;
00474 
00475   Argument = besARGUMENT(2);
00476 
00477   besLEFTVALUE(Argument,Lval);
00478   if( ! Lval )return MYSQL_ERROR_LVAL;
00479 
00480   besRELEASE(*Lval);
00481   *Lval = NULL; /* to be safe it is undef until we fill in some new value */
00482 
00483   /* get the number of fields in the result */
00484   numfields = mysql_num_fields(q->result);
00485   if( numfields == 0 ){
00486     besRETURNVALUE = NULL;
00487     return COMMAND_ERROR_SUCCESS;
00488     }
00489 
00490   *Lval = besNEWARRAY(0,numfields-1);
00491   if( *Lval == NULL )return COMMAND_ERROR_MEMORY_LOW;
00492 
00493   /* get the actual data of the row */
00494   row = mysql_fetch_row(q->result);
00495   if( row == NULL ){
00496     besRETURNVALUE = NULL;
00497     return COMMAND_ERROR_SUCCESS;
00498     }
00499 
00500   lengths = mysql_fetch_lengths(q->result);
00501   if( lengths == NULL ){
00502     besRETURNVALUE = NULL;
00503     return COMMAND_ERROR_SUCCESS;
00504     }
00505 
00506   for( i= 0 ; ((unsigned)i) < numfields ; i++ ){
00507     ARRAYVALUE(*Lval,i) = besNEWSTRING(lengths[i]);
00508     if( ARRAYVALUE(*Lval,i) == NULL )return COMMAND_ERROR_MEMORY_LOW;
00509     memcpy(STRINGVALUE(ARRAYVALUE(*Lval,i)),row[i],lengths[i]);
00510     }
00511   besALLOC_RETURN_LONG;
00512   LONGVALUE(besRETURNVALUE) = -1;
00513 besEND
00514 
00515 besFUNCTION(mys_fetchhash)
00516   VARIABLE Argument;
00517   LEFTVALUE Lval;
00518   pmymysqlHANDLE q;
00519   int i;
00520   unsigned long *lengths;
00521   MYSQL_ROW row;
00522   MYSQL_FIELD *field;
00523   unsigned int numfields;
00524   unsigned long __refcount_;
00525   pmyOBJECT p;
00526 
00527   GET_DB_HANDLE
00528 
00529   besRETURNVALUE = NULL;
00530   /* db , array */
00531   if( besARGNR < 2 )return EX_ERROR_TOO_FEW_ARGUMENTS;
00532 
00533   if( q->result == NULL )return MYSQL_ERROR_NORS;
00534 
00535   Argument = besARGUMENT(2);
00536 
00537   besLEFTVALUE(Argument,Lval);
00538   if( ! Lval )return MYSQL_ERROR_LVAL;
00539 
00540   besRELEASE(*Lval);
00541   *Lval = NULL; /* to be safe it is undef until we fill in some new value */
00542 
00543   /* get the number of fields in the result */
00544   numfields = mysql_num_fields(q->result);
00545   if( numfields == 0 ){
00546     besRETURNVALUE = NULL;
00547     return COMMAND_ERROR_SUCCESS;
00548     }
00549 
00550   *Lval = besNEWARRAY(0,2*numfields-1);
00551   if( *Lval == NULL )return COMMAND_ERROR_MEMORY_LOW;
00552 
00553   field = mysql_fetch_fields(q->result);
00554   if( field == NULL ){
00555     besRETURNVALUE = NULL;
00556     return COMMAND_ERROR_SUCCESS;
00557     }
00558 
00559   /* get the actual data of the row */
00560   row = mysql_fetch_row(q->result);
00561   if( row == NULL ){
00562     besRETURNVALUE = NULL;
00563     return COMMAND_ERROR_SUCCESS;
00564     }
00565 
00566   lengths = mysql_fetch_lengths(q->result);
00567   if( lengths == NULL ){
00568     besRETURNVALUE = NULL;
00569     return COMMAND_ERROR_SUCCESS;
00570     }
00571 
00572   for( i= 0 ; ((unsigned)i) < numfields ; i++ ){
00573     ARRAYVALUE(*Lval,2*i) = besNEWSTRING(strlen(field[i].name));
00574     if( ARRAYVALUE(*Lval,2*i) == NULL )return COMMAND_ERROR_MEMORY_LOW;
00575     memcpy(STRINGVALUE(ARRAYVALUE(*Lval,2*i)),field[i].name,
00576                                          STRLEN(ARRAYVALUE(*Lval,2*i)));
00577 
00578     ARRAYVALUE(*Lval,2*i+1) = besNEWSTRING(lengths[i]);
00579     if( ARRAYVALUE(*Lval,2*i+1) == NULL )return COMMAND_ERROR_MEMORY_LOW;
00580     memcpy(STRINGVALUE(ARRAYVALUE(*Lval,2*i+1)),row[i],lengths[i]);
00581     }
00582   besALLOC_RETURN_LONG;
00583   LONGVALUE(besRETURNVALUE) = -1;
00584 besEND
00585 
00586 besFUNCTION(mys_query)
00587   VARIABLE Argument;
00588   pmymysqlHANDLE q;
00589   int iMyError;
00590   pmyOBJECT p;
00591 
00592   GET_DB_HANDLE
00593 
00594   /* we need the db connection and the query */
00595   if( besARGNR < 2 )return EX_ERROR_TOO_FEW_ARGUMENTS;
00596 
00597   /* if there is any result from any previous query then release it */
00598   if( q->result ){
00599     mysql_free_result(q->result);
00600     q->result = NULL;
00601     }
00602 
00603   /* get the query */
00604   Argument = besARGUMENT(2);
00605   besDEREFERENCE(Argument);
00606   if( ! Argument )return EX_ERROR_TOO_FEW_ARGUMENTS;
00607 
00608   Argument = besCONVERT2STRING(Argument);
00609 
00610   iMyError = mysql_real_query(q->hSQL,STRINGVALUE(Argument),STRLEN(Argument));
00611   if( iMyError != 0 )return convert_error(iMyError);
00612     q->result = mysql_store_result(q->hSQL);
00613     if( q->result == NULL ){
00614       iMyError = mysql_errno(q->hSQL);
00615       if( iMyError )return convert_error(iMyError);
00616       return COMMAND_ERROR_SUCCESS;
00617       }
00618     q->num_fields = mysql_num_fields(q->result);
00619   besRETURNVALUE = NULL;
00620 besEND
00621 
00622 besFUNCTION(mys_close)
00623   VARIABLE Argument;
00624   pmymysqlHANDLE q;
00625   pmyOBJECT p;
00626 
00627   GET_DB_HANDLE
00628 
00629   besHandleFreeHandle(p->HandleArray,besGETLONGVALUE(Argument));
00630 
00631   /* unlink it from the list of DB handles */
00632   if( q->prev )
00633     q->prev->next = q->next;
00634   else
00635     p->first = q->next;
00636 
00637   if( q->next )
00638     q->next->prev = q->prev;
00639 
00640   /* this is not needed, but I want to be safe and clean */
00641   q->prev = q->next = NULL;
00642 
00643 
00644 
00645 
00646 
00647 
00648   mysql_close(q->hSQL);
00649 
00650 
00651   besFREE(q);
00652   besRETURNVALUE = NULL;
00653   return COMMAND_ERROR_SUCCESS;
00654 besEND
00655 
00656 besFUNCTION(mys_real_connect)
00657   MYSQL *q;
00658   int iMyError;
00659   pmymysqlHANDLE pH;
00660   VARIABLE Argument;
00661   char *pszHost,*pszUser,*pszPassword,*pszDB,*pszUnixSocket;
00662   int lPort,lClientFlag;
00663   pmyOBJECT p;
00664 
00665   p = (pmyOBJECT)besMODULEPOINTER;
00666 
00667   pH = besALLOC(sizeof(mymysqlHANDLE));
00668   if( pH == NULL )return COMMAND_ERROR_MEMORY_LOW;
00669 
00670   pH->hSQL = mysql_init(NULL);
00671   pH->result = NULL;
00672 
00673   if( besARGNR < 4 )return EX_ERROR_TOO_FEW_ARGUMENTS;
00674 
00675   /* set the default values for the optional arguments */
00676   lPort = MYSQL_PORT;
00677   pszUnixSocket = NULL;
00678   lClientFlag = 0;
00679 
00680   /* Get the host */
00681   Argument = besARGUMENT(1);
00682   besDEREFERENCE(Argument);
00683   if( Argument ){
00684     besCONVERT2ZCHAR(Argument,pszHost);
00685   }else pszHost = NULL;
00686 
00687   /* Get the user */
00688   Argument = besARGUMENT(2);
00689   besDEREFERENCE(Argument);
00690   if( Argument ){
00691     besCONVERT2ZCHAR(Argument,pszUser);
00692   }else pszUser = NULL;
00693 
00694 
00695   /* Get the password */
00696   Argument = besARGUMENT(3);
00697   besDEREFERENCE(Argument);
00698   if( Argument ){
00699     besCONVERT2ZCHAR(Argument,pszPassword);
00700   }else pszPassword = NULL;
00701 
00702   /* Get the dbname */
00703   Argument = besARGUMENT(4);
00704   besDEREFERENCE(Argument);
00705   if( Argument ){
00706     besCONVERT2ZCHAR(Argument,pszDB);
00707   }else pszDB = NULL;
00708 
00709   if( besARGNR > 4 ){
00710     /* Get the port */
00711     Argument = besARGUMENT(5);
00712     besDEREFERENCE(Argument);
00713     if( Argument )
00714       lPort = (int)LONGVALUE(Argument);
00715     else lPort = MYSQL_PORT;
00716     }
00717 
00718   if( besARGNR > 5 ){
00719     /* Get the unix socket name */
00720     Argument = besARGUMENT(6);
00721     besDEREFERENCE(Argument);
00722     if( Argument ){
00723       besCONVERT2ZCHAR(Argument,pszUnixSocket);
00724     }else pszUnixSocket = NULL;
00725     }
00726 
00727   if( besARGNR > 6 ){
00728     /* Get the client flag */
00729     Argument = besARGUMENT(7);
00730     besDEREFERENCE(Argument);
00731     if( Argument ){
00732       lClientFlag = (int)LONGVALUE(Argument);
00733     }else lClientFlag = MYSQL_PORT;
00734     }
00735 
00736   q = mysql_real_connect(pH->hSQL,pszHost,pszUser,pszPassword,pszDB,lPort,pszUnixSocket,lClientFlag);
00737 
00738   if( pszHost       )besFREE(pszHost);
00739   if( pszUser       )besFREE(pszUser);
00740   if( pszPassword   )besFREE(pszPassword);
00741   if( pszDB         )besFREE(pszDB);
00742   if( pszUnixSocket )besFREE(pszUnixSocket);
00743 
00744   if( q ){
00745     besALLOC_RETURN_LONG;
00746     if( p->first )p->first->prev = pH;
00747     pH->next = p->first;
00748     p->first = pH;
00749     pH->prev = NULL;
00750     LONGVALUE(besRETURNVALUE) = besHandleGetHandle(p->HandleArray,pH);
00751     return COMMAND_ERROR_SUCCESS;
00752     }else{
00753     iMyError = mysql_errno(pH->hSQL);
00754     besFREE(pH);
00755     besRETURNVALUE = NULL;
00756     return convert_error(iMyError);
00757     }
00758 besEND
00759 
00760 besFUNCTION(mys_config_connect)
00761   MYSQL *q;
00762   int iMyError;
00763   pmymysqlHANDLE pH;
00764   VARIABLE Argument;
00765   char *pszHost,*pszUser,*pszPassword,*pszDB,*pszUnixSocket;
00766   long lPort,lClientFlag;
00767   pmyOBJECT p;
00768 #define CONFLEN 100
00769 #define CONFROOT "mysql.connections."
00770 #define MAXKL 20 /* the maximum key length used in connection config */
00771   char szConfigPath[CONFLEN],*pszConf;
00772   char *pszCname;
00773 
00774   p = (pmyOBJECT)besMODULEPOINTER;
00775 
00776   pH = besALLOC(sizeof(mymysqlHANDLE));
00777   if( pH == NULL )return COMMAND_ERROR_MEMORY_LOW;
00778 
00779   pH->hSQL = mysql_init(NULL);
00780   pH->result = NULL;
00781 
00782   /* set the default values for the optional arguments */
00783   lPort = MYSQL_PORT;
00784   pszUnixSocket = NULL;
00785   lClientFlag = 0;
00786 
00787   /* Get connection name  */
00788   Argument = besARGUMENT(1);
00789   besDEREFERENCE(Argument);
00790   if( Argument ){
00791     besCONVERT2ZCHAR(Argument,pszCname);
00792   }else return MYSQL_ERROR_NOCN;
00793 
00794   strcpy(szConfigPath,CONFROOT);
00795   if( STRLEN(Argument) > CONFLEN - strlen(CONFROOT) - MAXKL )
00796     return MYSQL_ERROR_BDCN;
00797   pszConf = szConfigPath + strlen(szConfigPath);
00798 
00799   memcpy(pszConf,STRINGVALUE(Argument),STRLEN(Argument));
00800   pszConf += STRLEN(Argument);
00801   *pszConf++ = '.';
00802 
00803   /* Get the host */
00804   strcpy(pszConf,"host");
00805   pszHost = besCONFIG(szConfigPath);
00806 
00807   /* Get the user */
00808   strcpy(pszConf,"user");
00809   pszUser = besCONFIG(szConfigPath);
00810 
00811   /* Get the password */
00812   strcpy(pszConf,"password");
00813   pszPassword = besCONFIG(szConfigPath);
00814 
00815   /* Get the dbname */
00816   strcpy(pszConf,"db");
00817   pszDB = besCONFIG(szConfigPath);
00818 
00819   /* Get the port */
00820   strcpy(pszConf,"port");
00821   besCONFIGEX(pSt->pEo->pConfig,szConfigPath,NULL,NULL,&lPort,NULL,NULL);
00822 
00823   /* Get the unix socket name */
00824   strcpy(pszConf,"socket");
00825   pszUnixSocket = besCONFIG(szConfigPath);
00826   if( pszUnixSocket && *pszUnixSocket == (char)0 )pszUnixSocket = NULL;
00827 
00828   /* Get the client flag */
00829   strcpy(pszConf,"flag");
00830   besCONFIGEX(pSt->pEo->pConfig,szConfigPath,NULL,NULL,&lClientFlag,NULL,NULL);
00831 
00832   q = mysql_real_connect(pH->hSQL,pszHost,pszUser,pszPassword,pszDB,(int)lPort,pszUnixSocket,lClientFlag);
00833 
00834   if( q ){
00835     besALLOC_RETURN_LONG;
00836     if( p->first )p->first->prev = pH;
00837     pH->next = p->first;
00838     p->first = pH;
00839     pH->prev = NULL;
00840     LONGVALUE(besRETURNVALUE) = besHandleGetHandle(p->HandleArray,pH);
00841     return COMMAND_ERROR_SUCCESS;
00842     }else{
00843     iMyError = mysql_errno(pH->hSQL);
00844     besFREE(pH);
00845     besRETURNVALUE = NULL;
00846     return convert_error(iMyError);
00847     }
00848 besEND

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