[Cs-webapplibs-commits] SF.net SVN: cs-webapplibs:[180] trunk/0.3
Status: Beta
Brought to you by:
crazedsanity
From: <cra...@us...> - 2010-07-23 14:17:31
|
Revision: 180 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=180&view=rev Author: crazedsanity Date: 2010-07-23 14:17:25 +0000 (Fri, 23 Jul 2010) Log Message: ----------- Updates to single-table handling & minor bug fixes. /cs_phpDB.class.php: * run_insert(): -- fix call to numRows() (was mistyped to instead appear to access a property instead of a method. /abstract/cs_singleTableHandler.abstract.class.php: * get_single_record(): -- header updated with new functionality -- accepts an array which is passed on to get_records() to allow for complex queries. -- forcibly limit result to 1 -- NOTE::: it may be desirable to allow an ordering too, and possibly a limit + offset, and NOT force limit to 1: if more than a single record is returned, it can throw an exception (the current way will always return a single record, but might actually be lulling them into a false sense of security due to the forced limit). * get_records(): -- minor update for header * update_record(): -- extra check to ensure cs_globalFunctions::string_from_array() actually created a string (handles the situation where all items in the array have empty values and are thereby removed) Modified Paths: -------------- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php trunk/0.3/cs_phpDB.class.php Modified: trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php =================================================================== --- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-07-09 16:43:30 UTC (rev 179) +++ trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-07-23 14:17:25 UTC (rev 180) @@ -125,23 +125,24 @@ //------------------------------------------------------------------------- /** - * Just a simple wrapper to get_records(), no initial (id-based) record used. + * Just a simple wrapper to get_records(), guaranteed to return a single record. * - * @param $fieldname (str) field to search (for $value) - * @param $value (str) value to find (in $field) - * @param $orderBy (str) field to order by; can contain "DESC" or "ASC" - * @param $limit (int) how many records to display - * @param $offset (int) offset by this many records + * @param $filter (array) fieldname=>value list of filters. * * @RETURN (array) SUCCESS: returns single record with all fields. * @EXCEPTION FAIL: exception indicates error */ - protected function get_single_record($fieldname, $value, $orderBy=null, $limit=null, $offset=null) { - $data = $this->get_records(array($fieldname=>$value), $orderBy, $limit, $offset); + protected function get_single_record(array $filter) { + try { + $data = $this->get_records($filter, null, 1); + + $keys = array_keys($data); + $retval = $data[$keys[0]]; + } + catch(Exception $e) { + throw new exception(__METHOD__ .":: failed to retrieve record, DETAILS::: ". $e->getMessage()); + } - $keys = array_keys($data); - $retval = $data[$keys[0]]; - return($retval); }//end get_single_record() //------------------------------------------------------------------------- @@ -153,7 +154,7 @@ * Retrieves a number of records based on arguments. * * @$filter (array) Field=>value list of filters (i.e. 'my_id'=>1) - * @$orderBy (str) Field to order by; can contain "DESC" or "ASC". + * @$orderBy (str) Field to order by; can contain "DESC" or "ASC". * @$limit (int) How many max records to display. * @$offset (int) Offset by this number of records. * @@ -204,20 +205,27 @@ * @RETURN (int) SUCCESS: (int) is the number of records updated (should always be 1) * @EXCEPTION FAIL: exception indicates the error. */ - protected function update_record($recId, array $updates) { + protected function update_record($recId, array $updates, $removeEmptyVals=true) { if(is_numeric($recId) && $recId >= 0 && is_array($updates) && count($updates) > 0) { - $sql = 'UPDATE '. $this->tableName .' SET ' - . $this->gfObj->string_from_array($updates, 'update', null, $this->cleanStringArr, true) - .' WHERE '. $this->pkeyField .'='. $recId; - try { - $retval = $this->dbObj->run_update($sql, true); + $updateString = $this->gfObj->string_from_array($updates, 'update', null, $this->cleanStringArr, $removeEmptyVals); + if(is_null($updateString) || !strlen($updateString) || strlen($updateString) < 3) { + throw new exception(__METHOD__ .":: no update string created (". $updateSTring ."), contents::: ". $this->gfObj->debug_var_dump($updates,0)); } - catch(Exception $e) { - throw new exception(__METHOD__ .":: failed to update record (". $recId ."), DETAILS::: ". $e->getMessage()); + else { + $sql = 'UPDATE '. $this->tableName .' SET ' + . $updateString + .' WHERE '. $this->pkeyField .'='. $recId; + try { + $retval = $this->dbObj->run_update($sql, true); +#$this->gfObj->debug_print(__METHOD__ .":: retval=(". $retval ."), SQL::: ". $sql ,1); + } + catch(Exception $e) { + throw new exception(__METHOD__ .":: failed to update record (". $recId ."), DETAILS::: ". $e->getMessage()); + } } } else { - throw new exception(__METHOD__ .":: failed to update record (". $recId ."), DETAILS::: ". $e->getMessage()); + throw new exception(__METHOD__ .":: failed to update record (". $recId ."), invalid recordId (". $recId ."), or no data in array::: ". $this->gfObj->debug_var_dump($updates,0)); } return($retval); }//end update_record() Modified: trunk/0.3/cs_phpDB.class.php =================================================================== --- trunk/0.3/cs_phpDB.class.php 2010-07-09 16:43:30 UTC (rev 179) +++ trunk/0.3/cs_phpDB.class.php 2010-07-23 14:17:25 UTC (rev 180) @@ -151,7 +151,7 @@ } else { //something broke... - throw new exception(__METHOD__ .": failed to insert, rows=(". $this->numRows .")... " + throw new exception(__METHOD__ .": failed to insert, rows=(". $this->numRows() .")... " ."ERROR::: ". $this->errorMsg() ."\n -- SQL:::: ". $sql); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |