[Cs-content-commits] SF.net SVN: cs-content:[329] trunk/1.0
PHP Templating & Includes System
Brought to you by:
crazedsanity
From: <cra...@us...> - 2009-01-29 19:39:59
|
Revision: 329 http://cs-content.svn.sourceforge.net/cs-content/?rev=329&view=rev Author: crazedsanity Date: 2009-01-29 19:39:55 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Add an abstract parent class for all database types. NOTE::: this was tested on the pgsql version (in a roundabout way), but not for mysql or sqlite. /cs_phpDB.php: * MAIN::: -- requires the new abstract parent class. /abstract/cs_phpDB.abstract.class.php [NEW]: * holds a list of methods required to be a proper database layer, along with a few classes and such to help minimize code replication. /db_types/cs_phpDB__mysql.class.php: * MAIN::: -- now extends cs_phpDBAbstract{}. * __construct(): -- calls parent::__construct(). -- removed unnecessary code (now in cs_phpDBAbstract{}) * sanity_check() [DELETED]: -- moved into cs_phpDBAbstract{} * disconnect() [DELETED]: -- moved into cs_phpDBAbstract{} * currRow() [DELETED]: -- moved into cs_phpDBAbstract{} * querySafe() [DELETED]: -- moved into cs_phpDBAbstract{} * sqlSafe() [DELETED]: -- moved into cs_phpDBAbstract{} * get_transaction_status(): -- arbitrarily returns false (previously was using PostgreSQL functions, so any return it might have given would have been invalid at best). * start_copy() [DELETED]: -- removed; it was a PostgreSQL-specific implementation. * put_line() [DELETED]: -- removed; it was a PostgreSQL-specific implementation. * end_copy() [DELETED]: -- removed; it was a PostgreSQL-specific implementation. * get_transaction_level() [DELETED]: -- removed; it was a PostgreSQL-specific implementation. * is_in_transaction(): -- arbitrarily returns 0, as there are no transaction implementations. /db_types/cs_phpDB__pgsql.class.php: * MAIN::: -- slightly modified header comments so Eclipse can shrink the whole thing. -- extends cs_phpDBAbstract{} * __construct(): -- calls parent::__construct() -- removed unnecessary code (now in cs_phpDBAbstract{}) * sanity_check() [DELETED]: -- moved into cs_phpDBAbstract{} * disconnect() [DELETED]: -- moved into cs_phpDBAbstract{} * currRow() [DELETED]: -- moved into cs_phpDBAbstract{} * querySafe() [DELETED]: -- moved into cs_phpDBAbstract{} * sqlSafe() [DELETED]: -- moved into cs_phpDBAbstract{} /db_types/cs_phpDB__sqlite.class.php: * MAIN::: -- now extends cs_phpDBAbstract{}. * __construct(): -- calls parent::__construct(). -- removed unnecessary code (now in cs_phpDBAbstract{}) * sanity_check() [DELETED]: -- moved into cs_phpDBAbstract{} * disconnect() [DELETED]: -- moved into cs_phpDBAbstract{} * querySafe() [DELETED]: -- moved into cs_phpDBAbstract{} * sqlSafe() [DELETED]: -- moved into cs_phpDBAbstract{} Modified Paths: -------------- trunk/1.0/cs_phpDB.php trunk/1.0/db_types/cs_phpDB__mysql.class.php trunk/1.0/db_types/cs_phpDB__pgsql.class.php trunk/1.0/db_types/cs_phpDB__sqlite.class.php Added Paths: ----------- trunk/1.0/abstract/ trunk/1.0/abstract/cs_phpDB.abstract.class.php Added: trunk/1.0/abstract/cs_phpDB.abstract.class.php =================================================================== --- trunk/1.0/abstract/cs_phpDB.abstract.class.php (rev 0) +++ trunk/1.0/abstract/cs_phpDB.abstract.class.php 2009-01-29 19:39:55 UTC (rev 329) @@ -0,0 +1,166 @@ +<?php +/* + * Created on Jan 29, 2009 + * + * FILE INFORMATION: + * + * $HeadURL$ + * $Id$ + * $LastChangedDate$ + * $LastChangedBy$ + * $LastChangedRevision$ + */ + +abstract class cs_phpDBAbstract { + + /** Internal result set pointer. */ + protected $result = NULL; + + /** Internal error code. */ + protected $errorCode = 0; + + /** Status of the current transaction. */ + protected $transStatus = NULL; + + /** Whether there is a transaction in progress or not. */ + protected $inTrans = FALSE; + + /** Holds the last query performed. */ + protected $lastQuery = NULL; + + /** List of queries that have been run */ + protected $queryList=array(); + + /** How many seconds to wait for a query before cancelling it. */ + protected $timeOutSeconds = NULL; + + /** Internal check to determine if a connection has been established. */ + protected $isConnected=FALSE; + + /** Internal check to determine if the parameters have been set. */ + protected $paramsAreSet=FALSE; + + /** Resource handle. */ + protected $connectionID = -1; + + /** Hostname or IP to connect to */ + protected $host; + + /** Port to connect to (default for Postgres is 5432) */ + protected $port; + + /** Name of the database */ + protected $dbname; + + /** Username to connect to the database */ + protected $user; + + /** password to connect to the database */ + protected $password; + + /** Row counter for looping through records */ + protected $row = -1; + + /** cs_globalFunctions object, for string stuff. */ + protected $gfObj; + + /** Internal check to ensure the object has been properly created. */ + protected $isInitialized=FALSE; + + /** List of prepared statements, indexed off the name, with the sub-array being fieldname=>dataType. */ + protected $preparedStatements = array(); + + /** Set to TRUE to save all queries into an array. */ + protected $useQueryList=FALSE; + + /** array that essentially remembers how many times beginTrans() was called. */ + protected $transactionTree = NULL; + + + + //Define some abstract methods so they MUST be provided in order for things to work. + abstract public function set_db_info(array $params); + abstract public function close(); + abstract public function connect(array $dbParams=NULL, $forceNewConnection=FALSE); + abstract public function exec($query); + abstract public function errorMsg($setMessage=null, $logError=null); + abstract public function fobject(); + abstract public function farray(); + abstract public function farray_fieldnames($index=null, $numbered=null,$unsetIndex=1); + abstract public function farray_nvp($name, $value); + abstract public function farray_numbered(); + abstract public function numAffected(); + abstract public function numRows(); + abstract public function is_connected(); + + + //========================================================================= + public function __construct() { + $this->gfObj = new cs_globalFunctions; + $this->isInitialized = true; + }//end __construct() + //========================================================================= + + + + //========================================================================= + /** + * Make sure the object is sane. + */ + final protected function sanity_check() { + if($this->isInitialized !== TRUE) { + throw new exception(__METHOD__ .": not properly initialized"); + } + }//end sanity_check() + //========================================================================= + + + + //========================================================================= + /** + * Disconnect from the database (calls internal "close()" method). + */ + public function disconnect() { + return($this->close()); + }//end disconnect() + //========================================================================= + + + + //========================================================================= + public function affectedRows() { + return($this->numAffected()); + }//end affectedRows() + //========================================================================= + + + + //========================================================================= + public function currRow() { + return($this->row); + }//end currRow() + //========================================================================= + + + + //========================================================================= + public function querySafe($string) { + return($this->gfObj->cleanString($string,"query")); + }//end querySafe() + //========================================================================= + + + + //========================================================================= + /** + * Make it SQL safe. + */ + public function sqlSafe($string) { + return($this->gfObj->cleanString($string,"sql")); + }//end sqlSafe() + //========================================================================= + + + +} +?> \ No newline at end of file Modified: trunk/1.0/cs_phpDB.php =================================================================== --- trunk/1.0/cs_phpDB.php 2009-01-27 16:25:08 UTC (rev 328) +++ trunk/1.0/cs_phpDB.php 2009-01-29 19:39:55 UTC (rev 329) @@ -25,6 +25,7 @@ /////////////////////// require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); +require_once(dirname(__FILE__) ."/abstract/cs_phpDB.abstract.class.php"); class cs_phpDB extends cs_versionAbstract { Modified: trunk/1.0/db_types/cs_phpDB__mysql.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-01-27 16:25:08 UTC (rev 328) +++ trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-01-29 19:39:55 UTC (rev 329) @@ -11,9 +11,8 @@ */ +class cs_phpDB__mysql extends cs_phpDBAbstract { -class cs_phpDB__mysql { - /** Internal result set pointer. */ protected $result = NULL; @@ -84,13 +83,7 @@ //========================================================================= public function __construct() { - $this->gfObj = new cs_globalFunctions; - - if(defined('DEBUGPRINTOPT')) { - $this->gfObj->debugPrintOpt = DEBUGPRINTOPT; - } - - $this->isInitialized = TRUE; + parent::__construct(); }//end __construct() //========================================================================= @@ -98,19 +91,6 @@ //========================================================================= /** - * Make sure the object is sane. - */ - final protected function sanity_check() { - if($this->isInitialized !== TRUE) { - throw new exception(__METHOD__ .": not properly initialized"); - } - }//end sanity_check() - //========================================================================= - - - - //========================================================================= - /** * Set appropriate parameters for database connection */ public function set_db_info(array $params){ @@ -143,18 +123,6 @@ //========================================================================= /** - * Wrapper for close() - */ - function disconnect() { - //Disconnect from $database - return($this->close()); - }//end disconnect() - //========================================================================= - - - - //========================================================================= - /** * Standard method to close connection. */ function close() { @@ -669,23 +637,11 @@ function affectedRows(){ return($this->numAffected()); }//end affectedRows() - //========================================================================= //========================================================================= /** - * Returns the current row number. - */ - function currRow(){ - return($this->row); - }//end currRow() - //========================================================================= - - - - //========================================================================= - /** * Get the number of fields in a result. */ // get the number of fields in a result @@ -750,28 +706,6 @@ //========================================================================= /** - * Gets rid of evil characters that might lead ot SQL injection attacks. - */ - function querySafe($string) { - return($this->gfObj->cleanString($string,"query")); - }//end querySafe() - //========================================================================= - - - - //========================================================================= - /** - * Make it SQL safe. - */ - function sqlSafe($string) { - return($this->gfObj->cleanString($string,"sql")); - }//end sqlSafe() - //========================================================================= - - - - //========================================================================= - /** * Gives textual explanation of the current status of our database * connection. * @@ -785,61 +719,7 @@ */ function get_transaction_status($goodOrBad=TRUE) { //TODO: implement MySQL version.. - $myStatus = pg_transaction_status($this->connectionID); - $text = 'unknown'; - switch($myStatus) { - case PGSQL_TRANSACTION_IDLE: { - //No query in progress: it's idle. - $goodOrBadValue = 1; - $text = 'idle'; - $this->inTrans = FALSE; - } - break; - - - case PGSQL_TRANSACTION_ACTIVE: { - //there's a command in progress. - $goodOrBadValue = 2; - $text = 'processing'; - } - break; - - - case PGSQL_TRANSACTION_INTRANS: { - //connection idle within a valid transaction block. - $goodOrBadValue = 1; - $text = 'valid transaction'; - $this->inTrans = TRUE; - } - break; - - - case PGSQL_TRANSACTION_INERROR: { - //connection idle within a broken transaction. - $goodOrBadValue = 0; - $text = 'failed transaction'; - $this->inTrans = TRUE; - } - break; - - - case PGSQL_TRANSACTION_UNKNOWN: - default: { - //the connection is bad. - $goodOrBadValue = -1; - $text = 'bad connection'; - } - break; - } - - //do they want text or the good/bad number? - $retval = $text; - $this->transactionStatus = $goodOrBadValue; - if($goodOrBad) { - //they want the number. - $retval = $goodOrBadValue; - } - + $retval = false; return($retval); }//end get_transaction_status() //========================================================================= @@ -861,105 +741,11 @@ //========================================================================= /** - * Starts a copy command. - * - * TODO: implement safeguards so they can only put a line until the copy is ended. - */ - public function start_copy($tableName, array $fields) { - //TODO: implement MySQL version.. - $retval = FALSE; - $copyStmt = "COPY ". $tableName ." (". $this->gfObj->string_from_array($fields, NULL, ", ") . ") FROM stdin;"; - $this->exec($copyStmt); - if(!strlen($this->errorMsg())) { - //TODO: set something here so that NOTHING ELSE can be done except put_line() and end_copy(). - $this->copyInProgress = TRUE; - $retval = TRUE; - } - else { - $this->end_copy(); - $retval = FALSE; - } - - return($retval); - }//end start_copy() - //========================================================================= - - - - //========================================================================= - /** - * Used to send a line to the COPY in progress (only if it was initiated by - * the internal start_copy() method). - * - * NOTE: the "end-of-copy" line, '\.', should NEVER be sent here. - */ - public function put_line($line) { - //TODO: implement MySQL version.. - $retval = FALSE; - if($this->copyInProgress === TRUE) { - $myLine = trim($line); - $myLine .= "\n"; - - $retval = pg_put_line($this->connectionID, $myLine); - } - else { - throw new exception(__METHOD__ .": cannot send line if no copy is in progress"); - } - - return($retval); - }//end put_line() - //========================================================================= - - - - //========================================================================= - public function end_copy() { - if($this->copyInProgress === TRUE) { - //send the end-of-copy line... - $this->put_line("\\.\n"); - } - - //TODO: implement MySQL version.. - $retval = pg_end_copy($this->connectionID); - - return($retval); - }//end end_copy() - //========================================================================= - - - - //========================================================================= - /** - * Determines how many times a transaction has been started. Starting - * multiple transactions does NOT protect the outer transaction from - * problems that occur in the inner transaction. In fact, it does the - * opposite: it protects the code from committing too early (which might - * destroy something that depending on the transaction). - */ - public function get_transaction_level() { - if(is_array($this->transactionTree)) { - $retval = count($this->transactionTree); - } - else { - $retval = 0; - } - - return($retval); - }//end get_transaction_level() - //========================================================================= - - - - //========================================================================= - /** * Simple way to determine if the current connection is inside a * transaction or not. */ public function is_in_transaction() { - $retval = 0; - if($this->inTrans || $this->get_transaction_level() != 0) { - $retval = TRUE; - } + $retval=0; return($retval); }//end is_in_transaction() //========================================================================= Modified: trunk/1.0/db_types/cs_phpDB__pgsql.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__pgsql.class.php 2009-01-27 16:25:08 UTC (rev 328) +++ trunk/1.0/db_types/cs_phpDB__pgsql.class.php 2009-01-29 19:39:55 UTC (rev 329) @@ -8,29 +8,29 @@ * Last Committted Date: $Date$ * Last Committed Path:: $HeadURL$ * + * ////////////////////// + * ORIGINATION INFO: + * Author: Trevin Chow (with contributions from Lee Pang, wle...@ho...) + * Email: t1...@ma... + * Date: February 21, 2000 + * Last Updated: August 14, 2001 + * + * Description: + * Abstracts both the php function calls and the server information to POSTGRES + * databases. Utilizes class variables to maintain connection information such + * as number of rows, result id of last operation, etc. + * + * ///////////////////// + * + * TODO: option to not use layered transactions + * TODO: rollbackTrans() in layered transaction causes abort when final layer is committed/aborted + * TODO: stop sending queries to backend when transction is bad/aborted. + * TODO: commit/abort specific layer requests (i.e. if there's 8 layers & the first is named "x", calling commitTrans("x") will cause the whole transaction to commit & all layers to be destroyed. */ -/////////////////////// -// ORIGINATION INFO: -// Author: Trevin Chow (with contributions from Lee Pang, wle...@ho...) -// Email: t1...@ma... -// Date: February 21, 2000 -// Last Updated: August 14, 2001 -// -// Description: -// Abstracts both the php function calls and the server information to POSTGRES -// databases. Utilizes class variables to maintain connection information such -// as number of rows, result id of last operation, etc. -// -/////////////////////// -//TODO: option to not use layered transactions -//TODO: rollbackTrans() in layered transaction causes abort when final layer is committed/aborted -//TODO: stop sending queries to backend when transction is bad/aborted. -//TODO: commit/abort specific layer requests (i.e. if there's 8 layers & the first is named "x", calling commitTrans("x") will cause the whole transaction to commit & all layers to be destroyed. +class cs_phpDB__pgsql extends cs_phpDBAbstract { -class cs_phpDB__pgsql { - /** Internal result set pointer. */ protected $result = NULL; @@ -101,13 +101,7 @@ //========================================================================= public function __construct() { - $this->gfObj = new cs_globalFunctions; - - if(defined('DEBUGPRINTOPT')) { - $this->gfObj->debugPrintOpt = DEBUGPRINTOPT; - } - - $this->isInitialized = TRUE; + parent::__construct(); }//end __construct() //========================================================================= @@ -115,19 +109,6 @@ //========================================================================= /** - * Make sure the object is sane. - */ - final protected function sanity_check() { - if($this->isInitialized !== TRUE) { - throw new exception(__METHOD__ .": not properly initialized"); - } - }//end sanity_check() - //========================================================================= - - - - //========================================================================= - /** * Set appropriate parameters for database connection */ public function set_db_info(array $params){ @@ -160,18 +141,6 @@ //========================================================================= /** - * Wrapper for close() - */ - function disconnect() { - //Disconnect from $database - return($this->close()); - }//end disconnect() - //========================================================================= - - - - //========================================================================= - /** * Standard method to close connection. */ function close() { @@ -702,17 +671,6 @@ //========================================================================= /** - * Returns the current row number. - */ - function currRow(){ - return($this->row); - }//end currRow() - //========================================================================= - - - - //========================================================================= - /** * Get the number of fields in a result. */ // get the number of fields in a result @@ -882,28 +840,6 @@ //========================================================================= /** - * Gets rid of evil characters that might lead ot SQL injection attacks. - */ - function querySafe($string) { - return($this->gfObj->cleanString($string,"query")); - }//end querySafe() - //========================================================================= - - - - //========================================================================= - /** - * Make it SQL safe. - */ - function sqlSafe($string) { - return($this->gfObj->cleanString($string,"sql")); - }//end sqlSafe() - //========================================================================= - - - - //========================================================================= - /** * Gives textual explanation of the current status of our database * connection. * Modified: trunk/1.0/db_types/cs_phpDB__sqlite.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__sqlite.class.php 2009-01-27 16:25:08 UTC (rev 328) +++ trunk/1.0/db_types/cs_phpDB__sqlite.class.php 2009-01-29 19:39:55 UTC (rev 329) @@ -11,7 +11,7 @@ */ -class cs_phpDB__sqlite { +class cs_phpDB__sqlite extends cs_phpDBAbstract { /** Internal result set pointer. */ protected $result = NULL; @@ -71,13 +71,7 @@ //========================================================================= public function __construct() { - $this->gfObj = new cs_globalFunctions; - - if(defined('DEBUGPRINTOPT')) { - $this->gfObj->debugPrintOpt = DEBUGPRINTOPT; - } - - $this->isInitialized = TRUE; + parent::__construct(); }//end __construct() //========================================================================= @@ -85,19 +79,6 @@ //========================================================================= /** - * Make sure the object is sane. - */ - final protected function sanity_check() { - if($this->isInitialized !== TRUE) { - throw new exception(__METHOD__ .": not properly initialized"); - } - }//end sanity_check() - //========================================================================= - - - - //========================================================================= - /** * Set appropriate parameters for database connection */ public function set_db_info(array $params){ @@ -130,18 +111,6 @@ //========================================================================= /** - * Wrapper for close() - */ - function disconnect() { - //Disconnect from $database - return($this->close()); - }//end disconnect() - //========================================================================= - - - - //========================================================================= - /** * Standard method to close connection. */ function close() { @@ -599,28 +568,6 @@ //========================================================================= - /** - * Gets rid of evil characters that might lead ot SQL injection attacks. - */ - function querySafe($string) { - return($this->gfObj->cleanString($string,"query")); - }//end querySafe() - //========================================================================= - - - - //========================================================================= - /** - * Make it SQL safe. - */ - function sqlSafe($string) { - return($this->gfObj->cleanString($string,"sql")); - }//end sqlSafe() - //========================================================================= - - - - //========================================================================= public function is_connected() { $retval = FALSE; if(is_resource($this->connectionID) && $this->isConnected === TRUE) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |