Thread: [Cs-webapplibs-commits] SF.net SVN: cs-webapplibs:[165] trunk/0.3/cs_phpDB.class.php
Status: Beta
Brought to you by:
crazedsanity
From: <cra...@us...> - 2010-06-15 02:11:01
|
Revision: 165 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=165&view=rev Author: crazedsanity Date: 2010-06-15 02:10:54 +0000 (Tue, 15 Jun 2010) Log Message: ----------- Minor logic changes & ability to execute an entire SQL file. /cs_phpDB.class.php: * run_query(): -- shorten minimum SQL length to 15 (technically, valid SQL could be shorter if it were calling a stored proc or "NOW()") * run_sql_file() [NEW]: -- execute the contents of an SQL file (for schema & such). Modified Paths: -------------- trunk/0.3/cs_phpDB.class.php Modified: trunk/0.3/cs_phpDB.class.php =================================================================== --- trunk/0.3/cs_phpDB.class.php 2010-06-15 02:07:39 UTC (rev 164) +++ trunk/0.3/cs_phpDB.class.php 2010-06-15 02:10:54 UTC (rev 165) @@ -93,8 +93,8 @@ $retval = array(); - //length must be 19 as that's about the shortest valid SQL: "select * from table" - if(strlen($sql) >= 19) { + //length must be 15 as that's about the shortest valid SQL: "select * from x" + if(strlen($sql) >= 15) { $this->exec($sql); $numRows = $this->numRows(); @@ -187,6 +187,36 @@ }//end reconnect() //========================================================================= + + //========================================================================= + /** + * Execute the entire contents of the given file (with absolute path) as SQL. + */ + public function run_sql_file($filename) { + if(!is_object($this->fsObj)) { + if(class_exists('cs_fileSystem')) { + $fsObj = new cs_fileSystem(dirname($filename)); + } + else { + throw new exception(__METHOD__ .": required library (cs_fileSystem) not found"); + } + } + + $this->lastSQLFile = $filename; + + $fileContents = $fsObj->read($filename); + try { + $this->db->run_update($fileContents, true); + $this->build_cache(); + $retval = TRUE; + } + catch(exception $e) { + $retval = FALSE; + } + + return($retval); + }//end run_sql_file() + //========================================================================= } // end class phpDB ?> 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:07:59
|
Revision: 182 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=182&view=rev Author: crazedsanity Date: 2010-09-01 14:07:52 +0000 (Wed, 01 Sep 2010) Log Message: ----------- Ability to write all SQL queries to a file. /cs_phpDB.class.php: * MAIN::: -- NEW (protected) ARG: $fsObj -- NEW (protected) ARG: $logFile -- NEW (protected) ARG: $writeCommandsToFile * __construct(): -- ARG CHANGE: NEW ARG: #2 ($writeCommandsToFile=null) -- updated header -- added option to write all queries to a log file, whose name can be specified (i.e. $writeCommandsToFile=myfile.txt) -- logic to create a file if one not present * __call(): -- write data to a file if method was 'exec' and internal var, writeCommandsToFile, has been set. Modified Paths: -------------- trunk/0.3/cs_phpDB.class.php Modified: trunk/0.3/cs_phpDB.class.php =================================================================== --- trunk/0.3/cs_phpDB.class.php 2010-07-31 17:50:13 UTC (rev 181) +++ trunk/0.3/cs_phpDB.class.php 2010-09-01 14:07:52 UTC (rev 182) @@ -31,9 +31,21 @@ private $dbType; public $connectParams = array(); protected $gfObj; + protected $fsObj; + protected $logFile; + protected $writeCommandsToFile; //========================================================================= - public function __construct($type='pgsql') { + /** + * + * @param string $type + * @param bool $writeCommandsToFile (change this to a string for a filename, + * or use boolean true and it write to + * a default filename (__CLASS__.log). + * @return unknown_type + */ + public function __construct($type='pgsql', $writeCommandsToFile=null) { + if(is_null($type) || !strlen($type)) { $type = 'pgsql'; } @@ -46,6 +58,21 @@ parent::__construct(true); $this->isInitialized = TRUE; + + $this->writeCommandsToFile = $writeCommandsToFile; + + if($this->writeCommandsToFile) { + $this->logFile = __CLASS__ .".log"; + if(is_string($this->writeCommandsToFile)) { + $this->logFile = $this->writeCommandsToFile; + } + $this->fsObj = new cs_fileSystem(constant('RWDIR')); + $lsData = $this->fsObj->ls(); + if(!isset($lsData[$this->logFile])) { + $this->fsObj->create_file($this->logFile); + } + $this->fsObj->openFile($this->logFile, 'a'); + } }//end __construct() //========================================================================= @@ -67,6 +94,11 @@ array_pop($this->queryList); } array_unshift($this->queryList, $args[0]); + + //log it to a file. + if($this->writeCommandsToFile) { + $this->fsObj->append_to_file(date('D, d M Y H:i:s') . ' ('. microtime(true) . ')::: '. $args[0]); + } } $retval = call_user_func_array(array($this->dbLayerObj, $methodName), $args); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |