Thread: [Cs-webapplibs-commits] SF.net SVN: cs-webapplibs:[176] trunk/0.3/abstract/cs_singleTableHandler. a
Status: Beta
Brought to you by:
crazedsanity
From: <cra...@us...> - 2010-06-24 21:15:31
|
Revision: 176 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=176&view=rev Author: crazedsanity Date: 2010-06-24 21:15:25 +0000 (Thu, 24 Jun 2010) Log Message: ----------- Class that simplifies other classes which simply do database modifications on a single table. Added Paths: ----------- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php Copied: trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php (from rev 175, trunk/0.3/abstract/cs_webapplibs.abstract.class.php) =================================================================== --- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php (rev 0) +++ trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-06-24 21:15:25 UTC (rev 176) @@ -0,0 +1,202 @@ +<?php +/* + * Created on June 24, 2010 + * + * SVN INFORMATION::: + * ------------------- + * Last Author::::::::: $Author$ + * Current Revision:::: $Revision$ + * Repository Location: $HeadURL$ + * Last Updated:::::::: $Date$ + */ + +abstract class cs_singleTableHandlerAbstract extends cs_webapplibsAbstract { + + protected $gfObj; + abstract protected $tableName; + abstract protected $seqName; + abstract protected $pkeyField; + abstract protected $cleanStringArr; + abstract protected $dbParams; + + //------------------------------------------------------------------------- + /** + * Generic way of using a class to define how to update a single database table. + * + * @dbObj (object) Connected instance of cs_phpDB{}. + * @tableName (str) Name of table inserting/updating. + * @seqName (str) Name of sequence, used with PostgreSQL for retrieving the last inserted ID. + * @pkeyField (str) Name of the primary key field, for performing updates & retrieving specific records. + * @cleanStringArr (array) Array of {fieldName}=>{dataType} for allowing updates & creating records. + */ + function __construct(cs_phpDB $dbObj, $tableName, $seqName, $pkeyField, array $cleanStringArr) { + $this->set_version_file_location(dirname(__FILE__) . '/../VERSION'); + parent::__construct(true); + + if($dbObj->is_connnected()) { + $this->dbObj = $dbObj; + } + else { + throw new exception(__METHOD__ .":: database object not connected"); + } + + if(is_string($tableName) && strlen($tableName)) { + $this->tableName = $tableName; + } + else { + throw new exception(__METHOD__ .":: invalid table name (". $tableName .")"); + } + + if(is_string($seqName) && strlen($seqName)) { + $this->seqName = $seqName; + } + else { + throw new exception(__METHOD__ .":: invalid sequence name (". $seqName .")"); + } + + if(is_string($pkeyField) && strlen($pkeyField)) { + $this->pkeyField = $pkeyField; + } + else { + throw new exception(__METHOD__ .":: invalid primary key field name (". $pkeyField .")"); + } + + if(is_array($cleanStringArr) && count($cleanStringArr)) { + $this->cleanStringArr = $cleanStringArr; + } + else { + throw new exception(__METHOD__ .":: invalid clean string array"); + } + }//end __construct() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + /** + * Insert a new record into the table. + * + * @$data (array) field=>value pairs of data to be inserted. + * + * @RETURN (int) SUCCESS: the (int) is the last inserted ID. + * @EXCEPTION FAIL: exception indicates the error. + */ + protected function create_record(array $data) { + $sql = 'INSERT INTO '. $this->tableName .' ' + . $this->gfObj->string_from_array($data, 'insert', null, $this->cleanStringArr, true); + try { + $newId = $this->dbObj->run_insert($sql, $this->seqName); + } + catch(Exception $e) { + throw new exception(__METHOD__ .":: failed to create record, DETAILS::: ". $e->getMessage()); + } + return($newId); + }//end create_record() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + /** + * Retrieve a record based on a given ID, such as was returned from create_record(). + * + * @$recId (int) ID to retrieve. + * + * @RETURN (array) SUCCESS: list of field=>value of data from database. + * @EXCEPTION FAIL: exception indicates the error. + */ + protected function get_record_by_id($recId) { + if(is_numeric($recId)) { + try { + $data = $this->get_records(array($this->pkeyField => $recId)); + } + catch(Exception $e) { + throw new exception(__METHOD__ .":: error while retrieving record (". $recId ."), DETAILS::: ". $e->getMessage()); + } + } + else { + throw new exception(__METHOD__ .":: failed to retrieve record (". $recId ."), DETAILS::: ". $e->getMessage()); + } + return($data); + }//end get_record_by_id() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + /** + * 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". + * @limit (int) How many max records to display. + * @offset (int) Offset by this number of records. + * + * @RETURN (array) SUCCESS: Primary index is the record ID, sub-array is same as returned by get_record_by_id(). + * @EXCEPTION FAIL: exception indicates error. + */ + protected function get_records(array $filter=null, $orderBy=null, $limit=null, $offset=null) { + $limitOffsetStr = ''; + if(is_numeric($limit) && $limit > 0) { + $limitOffsetStr = ' LIMIT '. $limit; + + //While it appears to be acceptable to provide an offset without a limit, it seems ridiculous to me. + if(is_numeric($offset) && $offset > 0) { + $limitOffsetStr .= ' OFFSET '. $offset; + } + } + + $orderByStr = ''; + if(is_string($orderBy) && strlen($orderBy)) { + $orderByStr = ' ORDER BY '. $orderBy; + } + + $filterStr = ''; + if(is_array($filter) && count($filter) > 0) { + $filterStr = ' WHERE '. $this->gfObj->string_from_array($filter, 'select', null, $this->cleanStringArr, true); + } + + $sql = 'SELECT * FROM '. $this->tableName . $filterStr . $orderByStr . $limitOffsetStr; + try { + $data = $this->dbObj->run_query($sql, $this->pkeyField); + } + catch(Exception $e) { + throw new exception(__METHOD__ .":: failed to retrieve records, DETAILS::: ". $e->getMessage()); + } + return($data); + }//end get_records() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + /** + * Update a single record with the given changes. + * + * @recId (int) ID to update. + * @updates (array) field=>value list of changes. + * + * @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) { + 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); + } + 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()); + } + return($retval); + }//end update_record() + //------------------------------------------------------------------------- +} + +?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2010-07-08 16:08:52
|
Revision: 178 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=178&view=rev Author: crazedsanity Date: 2010-07-08 16:08:46 +0000 (Thu, 08 Jul 2010) Log Message: ----------- Method for retrieving a single record, updating header comments. Modified Paths: -------------- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php Modified: trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php =================================================================== --- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-07-07 14:14:46 UTC (rev 177) +++ trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-07-08 16:08:46 UTC (rev 178) @@ -23,11 +23,11 @@ /** * Generic way of using a class to define how to update a single database table. * - * @dbObj (object) Connected instance of cs_phpDB{}. - * @tableName (str) Name of table inserting/updating. - * @seqName (str) Name of sequence, used with PostgreSQL for retrieving the last inserted ID. - * @pkeyField (str) Name of the primary key field, for performing updates & retrieving specific records. - * @cleanStringArr (array) Array of {fieldName}=>{dataType} for allowing updates & creating records. + * @param $dbObj (object) Connected instance of cs_phpDB{}. + * @param $tableName (str) Name of table inserting/updating. + * @param $seqName (str) Name of sequence, used with PostgreSQL for retrieving the last inserted ID. + * @param $pkeyField (str) Name of the primary key field, for performing updates & retrieving specific records. + * @param $cleanStringArr (array) Array of {fieldName}=>{dataType} for allowing updates & creating records. */ function __construct(cs_phpDB $dbObj, $tableName, $seqName, $pkeyField, array $cleanStringArr) { $this->set_version_file_location(dirname(__FILE__) . '/../VERSION'); @@ -76,7 +76,7 @@ /** * Insert a new record into the table. * - * @$data (array) field=>value pairs of data to be inserted. + * @param $data (array) field=>value pairs of data to be inserted. * * @RETURN (int) SUCCESS: the (int) is the last inserted ID. * @EXCEPTION FAIL: exception indicates the error. @@ -100,10 +100,10 @@ /** * Retrieve a record based on a given ID, such as was returned from create_record(). * - * @$recId (int) ID to retrieve. + * @param $recId (int) ID to retrieve. * - * @RETURN (array) SUCCESS: list of field=>value of data from database. - * @EXCEPTION FAIL: exception indicates the error. + * @RETURN (array) SUCCESS: list of field=>value of data from database. + * @EXCEPTION FAIL: exception indicates the error. */ protected function get_record_by_id($recId) { if(is_numeric($recId)) { @@ -125,12 +125,37 @@ //------------------------------------------------------------------------- /** + * Just a simple wrapper to get_records(), no initial (id-based) record used. + * + * @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 + * + * @RETURN (array) SUCCESS: returns single record with all fields. + * @EXCEPTION FAIL: exception indicates error + */ + public function get_single_record($fieldname, $value, $orderBy=null, $limit=null, $offset=null) { + $data = $this->get_records(array($fieldname=>$value), $orderBy, $limit, $offset); + + $keys = array_keys($data); + $retval = $data[$keys[0]]; + + return($retval); + }//end get_single_record() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + /** * 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". - * @limit (int) How many max records to display. - * @offset (int) Offset by this number of records. + * @$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. * * @RETURN (array) SUCCESS: Primary index is the record ID, sub-array is same as returned by get_record_by_id(). * @EXCEPTION FAIL: exception indicates error. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2010-07-09 16:43:36
|
Revision: 179 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=179&view=rev Author: crazedsanity Date: 2010-07-09 16:43:30 +0000 (Fri, 09 Jul 2010) Log Message: ----------- Declare __construct() as public, change get_single_record() to protected. Modified Paths: -------------- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php Modified: trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php =================================================================== --- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-07-08 16:08:46 UTC (rev 178) +++ trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-07-09 16:43:30 UTC (rev 179) @@ -29,7 +29,7 @@ * @param $pkeyField (str) Name of the primary key field, for performing updates & retrieving specific records. * @param $cleanStringArr (array) Array of {fieldName}=>{dataType} for allowing updates & creating records. */ - function __construct(cs_phpDB $dbObj, $tableName, $seqName, $pkeyField, array $cleanStringArr) { + public function __construct(cs_phpDB $dbObj, $tableName, $seqName, $pkeyField, array $cleanStringArr) { $this->set_version_file_location(dirname(__FILE__) . '/../VERSION'); parent::__construct(true); @@ -136,7 +136,7 @@ * @RETURN (array) SUCCESS: returns single record with all fields. * @EXCEPTION FAIL: exception indicates error */ - public function get_single_record($fieldname, $value, $orderBy=null, $limit=null, $offset=null) { + protected function get_single_record($fieldname, $value, $orderBy=null, $limit=null, $offset=null) { $data = $this->get_records(array($fieldname=>$value), $orderBy, $limit, $offset); $keys = array_keys($data); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2010-09-01 14:13:03
|
Revision: 184 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=184&view=rev Author: crazedsanity Date: 2010-09-01 14:12:57 +0000 (Wed, 01 Sep 2010) Log Message: ----------- Throw exceptions when invalid data is encountered. /abstract/cs_singleTableHandler.abstract.class.php: * create_record(): -- throw an exception if there is no data. * get_single_record(): -- throw an exception if there's no data in the filter. * get_records(): -- throw an exception if there's a filter but no actual SQL appears to have been created (if the text is less than 3 characters). Modified Paths: -------------- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php Modified: trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php =================================================================== --- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-09-01 14:09:53 UTC (rev 183) +++ trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-09-01 14:12:57 UTC (rev 184) @@ -82,13 +82,18 @@ * @EXCEPTION FAIL: exception indicates the error. */ protected function create_record(array $data) { - $sql = 'INSERT INTO '. $this->tableName .' ' - . $this->gfObj->string_from_array($data, 'insert', null, $this->cleanStringArr, true); - try { - $newId = $this->dbObj->run_insert($sql, $this->seqName); + if(is_array($data) && count($data)) { + $sql = 'INSERT INTO '. $this->tableName .' ' + . $this->gfObj->string_from_array($data, 'insert', null, $this->cleanStringArr, true); + try { + $newId = $this->dbObj->run_insert($sql, $this->seqName); + } + catch(Exception $e) { + throw new exception(__METHOD__ .":: failed to create record, DETAILS::: ". $e->getMessage()); + } } - catch(Exception $e) { - throw new exception(__METHOD__ .":: failed to create record, DETAILS::: ". $e->getMessage()); + else { + throw new exception(__METHOD__ .":: no data passed"); } return($newId); }//end create_record() @@ -133,14 +138,19 @@ * @EXCEPTION FAIL: exception indicates error */ protected function get_single_record(array $filter) { - try { - $data = $this->get_records($filter, null, 1); - - $keys = array_keys($data); - $retval = $data[$keys[0]]; + if(is_array($filter) && count($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()); + } } - catch(Exception $e) { - throw new exception(__METHOD__ .":: failed to retrieve record, DETAILS::: ". $e->getMessage()); + else { + throw new exception(__METHOD__ .":: no filter passed"); } return($retval); @@ -179,7 +189,13 @@ $filterStr = ''; if(is_array($filter) && count($filter) > 0) { - $filterStr = ' WHERE '. $this->gfObj->string_from_array($filter, 'select', null, $this->cleanStringArr, true); + $filterSql = $this->gfObj->string_from_array($filter, 'select', null, $this->cleanStringArr, true); + if(strlen($filterSql) > 2) { + $filterStr = ' WHERE '. $filterSql; + } + else { + throw new exception(__METHOD__ .":: no filter created (". $this->gfObj->debug_print($filter,0) .")"); + } } $sql = 'SELECT * FROM '. $this->tableName . $filterStr . $orderByStr . $limitOffsetStr; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2010-09-02 14:35:36
|
Revision: 185 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=185&view=rev Author: crazedsanity Date: 2010-09-02 14:35:30 +0000 (Thu, 02 Sep 2010) Log Message: ----------- Change protected functions into public ones for simplicity's sake. Modified Paths: -------------- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php Modified: trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php =================================================================== --- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-09-01 14:12:57 UTC (rev 184) +++ trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-09-02 14:35:30 UTC (rev 185) @@ -81,7 +81,7 @@ * @RETURN (int) SUCCESS: the (int) is the last inserted ID. * @EXCEPTION FAIL: exception indicates the error. */ - protected function create_record(array $data) { + public function create_record(array $data) { if(is_array($data) && count($data)) { $sql = 'INSERT INTO '. $this->tableName .' ' . $this->gfObj->string_from_array($data, 'insert', null, $this->cleanStringArr, true); @@ -110,7 +110,7 @@ * @RETURN (array) SUCCESS: list of field=>value of data from database. * @EXCEPTION FAIL: exception indicates the error. */ - protected function get_record_by_id($recId) { + public function get_record_by_id($recId) { if(is_numeric($recId)) { try { $data = $this->get_records(array($this->pkeyField => $recId)); @@ -137,7 +137,7 @@ * @RETURN (array) SUCCESS: returns single record with all fields. * @EXCEPTION FAIL: exception indicates error */ - protected function get_single_record(array $filter) { + public function get_single_record(array $filter) { if(is_array($filter) && count($filter)) { try { $data = $this->get_records($filter, null, 1); @@ -171,7 +171,7 @@ * @RETURN (array) SUCCESS: Primary index is the record ID, sub-array is same as returned by get_record_by_id(). * @EXCEPTION FAIL: exception indicates error. */ - protected function get_records(array $filter=null, $orderBy=null, $limit=null, $offset=null) { + public function get_records(array $filter=null, $orderBy=null, $limit=null, $offset=null) { $limitOffsetStr = ''; if(is_numeric($limit) && $limit > 0) { $limitOffsetStr = ' LIMIT '. $limit; @@ -221,7 +221,7 @@ * @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, $removeEmptyVals=true) { + public function update_record($recId, array $updates, $removeEmptyVals=true) { if(is_numeric($recId) && $recId >= 0 && is_array($updates) && count($updates) > 0) { $updateString = $this->gfObj->string_from_array($updates, 'update', null, $this->cleanStringArr, $removeEmptyVals); if(is_null($updateString) || !strlen($updateString) || strlen($updateString) < 3) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2010-09-09 02:42:00
|
Revision: 186 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=186&view=rev Author: crazedsanity Date: 2010-09-09 02:41:54 +0000 (Thu, 09 Sep 2010) Log Message: ----------- Allow deleting of records. NOTE::: there is no logic for only allowing a single record to get deleted, and no transaction support (see TODO). /abstract/cs_singleTableHandler.abstract.class.php: * delete_record() [NEW]: -- allow deleting of record(s). -- see important TODO; this will hopefully be added in the near future. Modified Paths: -------------- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php Modified: trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php =================================================================== --- trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-09-02 14:35:30 UTC (rev 185) +++ trunk/0.3/abstract/cs_singleTableHandler.abstract.class.php 2010-09-09 02:41:54 UTC (rev 186) @@ -246,6 +246,29 @@ return($retval); }//end update_record() //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + /** + * TODO: only allow 1 record to be deleted, or a specific number of records (add transaction logic)? + */ + public function delete_record($recId) { + if(is_numeric($recId)) { + $sql = "DELETE FROM ". $this->tableName ." WHERE ". $this->pkeyField ."=". $recId; + try { + $result = $this->dbObj->run_update($sql); + } + catch(Exception $e) { + throw new exception(__METHOD__ .": failed to delete record, DETAILS::: ". $e->getMessage()); + } + } + else { + throw new exception(__METHOD__ .":: failed to delete record, invalid data (". $recId .")"); + } + return($result); + }//end delete_record() + //------------------------------------------------------------------------- } ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |