CVS update: php3/functions

From: Date: Sun, 16 Aug 1998 14:50:45 +0000
Subject: CVS update: php3/functions
Groups: php.dev 
Request: Send a blank email to php-dev+get-274@lists.php.net to get a copy of this message
Date: Sunday August 16, 1998 @ 10:50 Author: danny Update of /repository/php3/functions In directory asf:/tmp/cvs-serv4955/functions Modified Files: ifx.ec php3_ifx.h Log Message: 1. ifx_affected_rows($resultid) now returns : - the estimated number of rows for select statements - the real number of rows for insert/update/delete statements 2. some minor corrections 3. added ifx_prepare : prepares a query, sets affected_rows 4. added ifx_do : executes (or opens cursor for) a previously prepared query, sets affected rows for execute-immediate queries Index: php3/functions/ifx.ec diff -c php3/functions/ifx.ec:1.8 php3/functions/ifx.ec:1.9 *** php3/functions/ifx.ec:1.8 Sat Aug 15 13:47:17 1998 --- php3/functions/ifx.ec Sun Aug 16 10:50:43 1998 *************** *** 107,112 **** --- 107,114 ---- {"ifx_pconnect", php3_ifx_pconnect, NULL}, {"ifx_close", php3_ifx_close, NULL}, {"ifx_query", php3_ifx_query, NULL}, + {"ifx_prepare", php3_ifx_prepare, NULL}, + {"ifx_do", php3_ifx_do, NULL}, {"ifx_error", php3_ifx_error, NULL}, {"ifx_errormsg", php3_ifx_errormsg, NULL}, {"ifx_affected_rows", php3_ifx_affected_rows, NULL}, *************** *** 752,760 **** php3_list_delete(id); RETURN_TRUE; } - - - /* ---------------------------------------------------------------------- ** int ifx_query(string $query, int $connid, [string $hold, [string $scroll]]) --- 754,759 ---- *************** *** 765,770 **** --- 764,770 ---- ** select queries accept 2 optional params : "SCROLL" and/or "HOLD" ** ** returns a "result id" on success or FALSE on error + ** also sets "affected_rows for retrieval by ifx_affected_rows ** ---------------------------------------------------------------------- */ *************** *** 787,794 **** loc_t *locator; EXEC SQL END DECLARE SECTION; ! int locind; char *ctype1, *ctype2; Informix_TLS_VARS; --- 787,795 ---- loc_t *locator; EXEC SQL END DECLARE SECTION; ! int locind; char *ctype1, *ctype2; + int affected_rows; Informix_TLS_VARS; *************** *** 852,857 **** --- 853,859 ---- RETURN_FALSE; } + affected_rows = -1; /* invalid */ convert_to_string(query); *************** *** 874,879 **** --- 876,882 ---- ifx_error(ifx)); RETURN_FALSE; } + affected_rows = sqlca.sqlerrd[0]; /* save estimated affected rows */ EXEC SQL ALLOCATE DESCRIPTOR :descrpid WITH MAX 256; if (ifx_check() < 0) { php3_error(E_WARNING,"Allocate desciptor <%s> fails (%s)", *************** *** 906,914 **** strcpy(Ifx_Result->descrpid, descrpid); for (i = 0; i < MAXBLOBS; ++i) Ifx_Result->blob_data[i] = (loc_t *)NULL; if (sqlca.sqlcode != 0) { /* not a select, execute immediate */ ! strcpy(Ifx_Result->cursorid, ""); strcpy(Ifx_Result->descrpid, descrpid); strcpy(Ifx_Result->statemid, statemid); EXEC SQL EXECUTE :statemid; --- 909,918 ---- strcpy(Ifx_Result->descrpid, descrpid); for (i = 0; i < MAXBLOBS; ++i) Ifx_Result->blob_data[i] = (loc_t *)NULL; + Ifx_Result->iscursory = -1; /* prevent ifx_do */ if (sqlca.sqlcode != 0) { /* not a select, execute immediate */ ! strcpy(Ifx_Result->cursorid, ""); strcpy(Ifx_Result->descrpid, descrpid); strcpy(Ifx_Result->statemid, statemid); EXEC SQL EXECUTE :statemid; *************** *** 921,927 **** --- 925,933 ---- EXEC SQL free :statemid; RETURN_FALSE; } + Ifx_Result->affected_rows = sqlca.sqlerrd[2]; /* really affected */ } else { + Ifx_Result->affected_rows = affected_rows; /* saved estimated */ EXEC SQL GET DESCRIPTOR :descrpid :fieldcount = COUNT; if (ifx_check() < 0) { php3_error(E_WARNING,"Can not get descriptor %s (%s)", *************** *** 929,934 **** --- 935,941 ---- ifx_error(ifx)); EXEC SQL DEALLOCATE DESCRIPTOR :descrpid; EXEC SQL free :statemid; + efree(Ifx_Result); RETURN_FALSE; } Ifx_Result->numcols = fieldcount; *************** *** 1007,1013 **** --- 1014,1346 ---- return_value->type = IS_LONG; } + + /* ---------------------------------------------------------------------- + ** int ifx_prepare(string $query, int $connid, [string $hold, [string $scroll]]) + ** + ** prepares query $query on connection $connid + ** select queries accept 2 optional params : "SCROLL" and/or "HOLD" + ** + ** returns a "result id" on success or FALSE on error + ** also sets "affected_rows for retrieval by ifx_affected_rows + ** ---------------------------------------------------------------------- + */ + + void php3_ifx_prepare(INTERNAL_FUNCTION_PARAMETERS) + { + pval *query,*ifx_link, *cursortype1, *cursortype2; + int id,type; + + IFX_RES *Ifx_Result; + + EXEC SQL BEGIN DECLARE SECTION; + char *ifx; /* connection ID */ + char cursorid[32]; /* query cursor id */ + char statemid[32]; /* statement id */ + char descrpid[32]; /* descriptor id */ + char *statement; /* query text */ + int fieldcount; /* field count */ + int i; /* field index */ + short fieldtype; + loc_t *locator; + EXEC SQL END DECLARE SECTION; + + int locind; + char *ctype1, *ctype2; + int affected_rows; + + Informix_TLS_VARS; + + + switch(ARG_COUNT(ht)) { + case 1: + if (getParameters(ht, 1, &query)==FAILURE) { + RETURN_FALSE; + } + id = php3_ifx_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU); + ctype1 = ctype2 = NULL; + break; + case 2: + if (getParameters(ht, 2, &query, &ifx_link)==FAILURE) { + RETURN_FALSE; + } + convert_to_long(ifx_link); + id = ifx_link->value.lval; + ctype1 = ctype2 = NULL; + break; + case 3: + if (getParameters(ht, 3, + &query, + &ifx_link, + &cursortype1)==FAILURE) { + RETURN_FALSE; + } + convert_to_long(ifx_link); + id = ifx_link->value.lval; + ctype2 = NULL; + convert_to_string(cursortype1); + ctype1 = cursortype1->value.str.val; + break; + case 4: + if (getParameters(ht, 4, + &query, + &ifx_link, + &cursortype1, + &cursortype2)==FAILURE) { + RETURN_FALSE; + } + convert_to_long(ifx_link); + id = ifx_link->value.lval; + convert_to_string(cursortype1); + ctype1 = cursortype1->value.str.val; + convert_to_string(cursortype2); + ctype2 = cursortype2->value.str.val; + break; + default: + WRONG_PARAM_COUNT; + break; + } + + ifx = (char *) php3_list_find(id,&type); + if (type != Informix_GLOBAL(php3_ifx_module).le_link && + type!=Informix_GLOBAL(php3_ifx_module).le_plink) { + php3_error(E_WARNING, + "ifx_prepare : %d (type %d) is not a Informix link index", + id, + type); + RETURN_FALSE; + } + + affected_rows = -1; /* invalid */ + + convert_to_string(query); + + statement = query->value.str.val; + Informix_GLOBAL(php3_ifx_module).cursorid++; + sprintf(statemid, "statem%x", Informix_GLOBAL(php3_ifx_module).cursorid); + sprintf(cursorid, "cursor%x", Informix_GLOBAL(php3_ifx_module).cursorid); + sprintf(descrpid, "descrp%x", Informix_GLOBAL(php3_ifx_module).cursorid); + + EXEC SQL set connection :ifx; + if (ifx_check() < 0) { + php3_error(E_WARNING,"Set connection %s fails (%s)", + ifx, + ifx_error(ifx)); + RETURN_FALSE; + } + EXEC SQL PREPARE :statemid FROM :statement; + if (ifx_check() < 0) { + php3_error(E_WARNING,"Prepare fails (%s)", + ifx_error(ifx)); + RETURN_FALSE; + } + + affected_rows = sqlca.sqlerrd[0]; /* save estimated affected rows */ + + EXEC SQL ALLOCATE DESCRIPTOR :descrpid WITH MAX 256; + if (ifx_check() < 0) { + php3_error(E_WARNING,"Allocate desciptor <%s> fails (%s)", + descrpid, + ifx_error(ifx)); + EXEC SQL free :statemid; + RETURN_FALSE; + } + EXEC SQL DESCRIBE :statemid USING SQL DESCRIPTOR :descrpid; + if (ifx_check() < 0) { + php3_error(E_WARNING,"Describe fails (%s)", + ifx_error(ifx)); + EXEC SQL DEALLOCATE DESCRIPTOR :descrpid; + EXEC SQL free :statemid; + RETURN_FALSE; + } + + Ifx_Result = (IFX_RES *)emalloc(sizeof(IFX_RES)); + if (Ifx_Result == NULL) { + php3_error(E_WARNING,"Out of memory allocating IFX_RES"); + EXEC SQL DEALLOCATE DESCRIPTOR :descrpid; + EXEC SQL free :statemid; + RETURN_FALSE; + } + + /* initialize result data structure */ + + Ifx_Result->rowid = 0; + strcpy(Ifx_Result->connecid, ifx); + strcpy(Ifx_Result->descrpid, descrpid); + for (i = 0; i < MAXBLOBS; ++i) + Ifx_Result->blob_data[i] = (loc_t *)NULL; + strcpy(Ifx_Result->statemid, statemid); + + if (sqlca.sqlcode != 0) { /* not a select */ + strcpy(Ifx_Result->cursorid, ""); + Ifx_Result->iscursory = 0; + } else { /* cursory, select */ + strcpy(Ifx_Result->cursorid, cursorid); + Ifx_Result->iscursory = 1; + EXEC SQL GET DESCRIPTOR :descrpid :fieldcount = COUNT; + if (ifx_check() < 0) { + php3_error(E_WARNING,"Can not get descriptor %s (%s)", + descrpid, + ifx_error(ifx)); + EXEC SQL DEALLOCATE DESCRIPTOR :descrpid; + EXEC SQL free :statemid; + efree(Ifx_Result); + RETURN_FALSE; + } + Ifx_Result->numcols = fieldcount; + + Ifx_Result->isscroll = Ifx_Result->ishold = 0; + if (ctype1 != NULL) { + if (strcasecmp(ctype1,"SCROLL") == 0) + Ifx_Result->isscroll = 1; + else + if (strcasecmp(ctype1,"HOLD") == 0) + Ifx_Result->ishold = 1; + } + if (ctype2 != NULL) { + if (strcasecmp(ctype2,"SCROLL") == 0) + Ifx_Result->isscroll = 1; + else + if (strcasecmp(ctype2,"HOLD") == 0) + Ifx_Result->ishold = 1; + } + + } /* end cursory select */ + + Ifx_Result->affected_rows = affected_rows; /* saved estimated */ + + return_value->value.lval = + php3_list_insert(Ifx_Result, + Informix_GLOBAL(php3_ifx_module).le_result); + return_value->type = IS_LONG; + + } + /* ---------------------------------------------------------------------- + ** int ifx_do(int $resultid) + ** + ** executes a previously prepared query or opens a cursor for it + ** + ** returns TRUE on success, false on error + ** does NOT free $resultid on error !!! + ** + ** also sets (real) affected_rows for non-select statements + ** for retrieval by ifx_affected_rows + ** ---------------------------------------------------------------------- + */ + + void php3_ifx_do(INTERNAL_FUNCTION_PARAMETERS) + { + pval *result; + int type; + IFX_RES *Ifx_Result; + + EXEC SQL BEGIN DECLARE SECTION; + char *ifx; /* connection ID */ + char *cursorid; /* query cursor id */ + char *statemid; /* statement id */ + char *descrpid; /* descriptor id */ + int fieldcount; /* field count */ + int i; /* field index */ + short fieldtype; + loc_t *locator; + EXEC SQL END DECLARE SECTION; + + int locind; + + Informix_TLS_VARS; + + + switch(ARG_COUNT(ht)) { + case 0: + WRONG_PARAM_COUNT; + break; + case 1: + if (getParameters(ht, 1, &result)==FAILURE) { + RETURN_FALSE; + } + convert_to_long(result); + Ifx_Result = (IFX_RES *) php3_list_find(result->value.lval,&type); + break; + default: + WRONG_PARAM_COUNT; + break; + } + + if (type!=Informix_GLOBAL(php3_ifx_module).le_result) { + php3_error(E_WARNING,"%d is not an Informix result index", + result->value.lval); + RETURN_FALSE; + } + + ifx = Ifx_Result->connecid; + cursorid = Ifx_Result->cursorid; + statemid = Ifx_Result->statemid; + descrpid = Ifx_Result->descrpid; + fieldcount = Ifx_Result->numcols; + + + if (Ifx_Result->iscursory < 0) { + php3_error(E_WARNING, "Resultindex %d is not a prepared query", + result->value.lval); + RETURN_FALSE; + } + if (! Ifx_Result->iscursory) { /* execute immediate */ + EXEC SQL EXECUTE :statemid; + if (ifx_check() < 0) { + php3_error(E_WARNING,"Execute immediate fails : %s (%s)", + statemid, + ifx_error(ifx)); + RETURN_FALSE; + } + Ifx_Result->affected_rows = sqlca.sqlerrd[2]; /* really affected */ + } else { /* open cursor */ + if (Ifx_Result->isscroll) + if (Ifx_Result->ishold) + EXEC SQL DECLARE :cursorid SCROLL CURSOR WITH HOLD FOR :statemid; + else + EXEC SQL DECLARE :cursorid SCROLL CURSOR FOR :statemid; + else + if (Ifx_Result->ishold) + EXEC SQL DECLARE :cursorid CURSOR WITH HOLD FOR :statemid; + else + EXEC SQL DECLARE :cursorid CURSOR FOR :statemid; + + if (ifx_check() < 0) { + php3_error(E_WARNING,"Declare cursor fails (%s)", ifx_error(ifx)); + RETURN_FALSE; + } + EXEC SQL OPEN :cursorid; + if (ifx_check() < 0) { + php3_error(E_WARNING,"Open cursor fails (%s)", ifx_error(ifx)); + RETURN_FALSE; + } + + /* check for text/blob columns */ + + locind = 0; + for (i = 1; i <= fieldcount; ++i) { + EXEC SQL GET DESCRIPTOR :descrpid VALUE :i :fieldtype = TYPE; + if ((fieldtype == SQLTEXT) || (fieldtype == SQLBYTES)) { + locator = (loc_t *)malloc(sizeof(loc_t)); + Ifx_Result->blob_data[locind] = locator; + ++locind; + locator->loc_loctype = LOCMEMORY; + locator->loc_bufsize = INITIAL_BLOBSIZE - 4; + locator->loc_buffer = (char *)malloc(INITIAL_BLOBSIZE); + locator->loc_mflags = 0; + locator->loc_oflags = 0; + EXEC SQL SET DESCRIPTOR :descrpid VALUE :i DATA = :*locator; + } + } + + } /* end open cursor */ + + RETURN_TRUE; + + } + + /* ---------------------------------------------------------------------- ** string ifx_error(int connection_id); ** ** returns the Informix error codes (SQLSTATE & SQLCODE) *************** *** 1109,1152 **** RETURN_STRING(returnmsg,0); /* do not dup, emalloced ! */ } - - void php3_ifx_affected_rows(INTERNAL_FUNCTION_PARAMETERS) { ! pval *ifx_link; ! int id,type; ! IFX *ifx; Informix_TLS_VARS; switch(ARG_COUNT(ht)) { case 0: ! id = Informix_GLOBAL(php3_ifx_module).default_link; break; case 1: ! if (getParameters(ht, 1, &ifx_link)==FAILURE) { RETURN_FALSE; } ! convert_to_long(ifx_link); ! id = ifx_link->value.lval; break; default: WRONG_PARAM_COUNT; break; } ! ifx = (IFX *) php3_list_find(id,&type); ! if (type!=Informix_GLOBAL(php3_ifx_module).le_link && type!=Informix_GLOBAL(php3_ifx_module).le_plink) { ! php3_error(E_WARNING, ! "ifx_affected_rows : %d (type %d) is not a Informix link index", ! id, ! type); RETURN_FALSE; } ! return_value->value.lval = -1; /* not supported */ return_value->type = IS_LONG; } --- 1442,1490 ---- RETURN_STRING(returnmsg,0); /* do not dup, emalloced ! */ } + /* -------------------------------------------------------------- + ** int ifx_affected_rows(int $resultid) + ** + ** returns the number of rows affected by query $resultid + ** + ** for selects : estimated number of rows (sqlerrd[0]) + ** for insert/update/delete : real number (sqlerrd[2]) + ** --------------------------------------------------------------- + */ void php3_ifx_affected_rows(INTERNAL_FUNCTION_PARAMETERS) { ! pval *result; ! int type; ! IFX_RES *Ifx_Result; ! Informix_TLS_VARS; switch(ARG_COUNT(ht)) { case 0: ! WRONG_PARAM_COUNT; break; case 1: ! if (getParameters(ht, 1, &result)==FAILURE) { RETURN_FALSE; } ! convert_to_long(result); ! Ifx_Result = (IFX_RES *) php3_list_find(result->value.lval,&type); break; default: WRONG_PARAM_COUNT; break; } ! if (type!=Informix_GLOBAL(php3_ifx_module).le_result) { ! php3_error(E_WARNING,"%d is not an Informix result index", ! result->value.lval); RETURN_FALSE; } ! return_value->value.lval = Ifx_Result->affected_rows; return_value->type = IS_LONG; } *************** *** 2221,2227 **** ** returns an associative array with fieldnames as key ** and SQL fieldproperties as data for query $resultid ** ! ** properties are encoded as : "SQLTYPE;length;precision;scale" ** ** returns FALSE on error ** ---------------------------------------------------------------------- --- 2559,2567 ---- ** returns an associative array with fieldnames as key ** and SQL fieldproperties as data for query $resultid ** ! ** properties are encoded as : "SQLTYPE;length;precision;scale;ISNULLABLE" ! ** where SQLTYPE = the Informix type like "SQLVCHAR" etc... ! ** ISNULLABLE = "Y" or "N" ** ** returns FALSE on error ** ---------------------------------------------------------------------- *************** *** 2260,2265 **** --- 2600,2606 ---- int size; int precision; int scale; + int isnullable; EXEC SQL END DECLARE SECTION; int type; *************** *** 2325,2331 **** :fieldtype = TYPE, :size = LENGTH, :precision = PRECISION, ! :scale = SCALE; if (ifx_check() < 0) { php3_error(E_WARNING,"Get descriptor (field # %d) fails (%s)", i, --- 2666,2673 ---- :fieldtype = TYPE, :size = LENGTH, :precision = PRECISION, ! :scale = SCALE, ! :isnullable = NULLABLE; if (ifx_check() < 0) { php3_error(E_WARNING,"Get descriptor (field # %d) fails (%s)", i, *************** *** 2384,2390 **** break; } /* switch (fieldtype) */ ! sprintf(string_data,"%s;%d;%d;%d",char_data, size, precision, scale); add_assoc_string(return_value, fieldname, string_data, DUP); } /* for() */ --- 2726,2737 ---- break; } /* switch (fieldtype) */ ! sprintf(string_data,"%s;%d;%d;%d;%c", ! char_data, ! size, ! precision, ! scale, ! (isnullable?'Y':'N')); add_assoc_string(return_value, fieldname, string_data, DUP); } /* for() */ *************** *** 2515,2522 **** descrpid = Ifx_Result->descrpid; EXEC SQL free :statemid; ! if (strlen(cursorid) != 0) ! EXEC SQL free :cursorid; for (i = 0; i < MAXBLOBS; ++i) { if (Ifx_Result->blob_data[i] != NULL) { --- 2862,2871 ---- descrpid = Ifx_Result->descrpid; EXEC SQL free :statemid; ! if (strlen(cursorid) != 0) { ! EXEC SQL CLOSE :cursorid; ! EXEC SQL FREE :cursorid; ! } for (i = 0; i < MAXBLOBS; ++i) { if (Ifx_Result->blob_data[i] != NULL) { Index: php3/functions/php3_ifx.h diff -c php3/functions/php3_ifx.h:1.4 php3/functions/php3_ifx.h:1.5 *** php3/functions/php3_ifx.h:1.4 Sat Aug 15 13:47:17 1998 --- php3/functions/php3_ifx.h Sun Aug 16 10:50:44 1998 *************** *** 56,61 **** --- 56,63 ---- extern void php3_ifx_pconnect(INTERNAL_FUNCTION_PARAMETERS); extern void php3_ifx_close(INTERNAL_FUNCTION_PARAMETERS); extern void php3_ifx_query(INTERNAL_FUNCTION_PARAMETERS); + extern void php3_ifx_prepare(INTERNAL_FUNCTION_PARAMETERS); + extern void php3_ifx_do(INTERNAL_FUNCTION_PARAMETERS); extern void php3_ifx_error(INTERNAL_FUNCTION_PARAMETERS); extern void php3_ifx_errormsg(INTERNAL_FUNCTION_PARAMETERS); extern void php3_ifx_affected_rows(INTERNAL_FUNCTION_PARAMETERS); *************** *** 91,98 **** --- 93,102 ---- char statemid[16]; int isscroll; int ishold; + int iscursory; int numcols; int rowid; + int affected_rows; loc_t *blob_data[MAXBLOBS]; } IFX_RES; -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#274) next »