[pLog-svn] r2174 - in plog/trunk/class/database: . pdb pdb/drivers

oscar at devel.plogworld.net oscar at devel.plogworld.net
Mon Jun 6 21:21:33 GMT 2005


Author: oscar
Date: 2005-06-06 21:21:33 +0000 (Mon, 06 Jun 2005)
New Revision: 2174

Added:
   plog/trunk/class/database/pdb/
   plog/trunk/class/database/pdb/drivers/
   plog/trunk/class/database/pdb/drivers/pdbdriverbase.class.php
   plog/trunk/class/database/pdb/drivers/pdbmysqldriver.class.php
   plog/trunk/class/database/pdb/drivers/pdbmysqlrecordset.class.php
   plog/trunk/class/database/pdb/drivers/pdbrecordset.class.php
   plog/trunk/class/database/pdb/pdb.class.php
Log:
added PDb, which is plog's own database abstraction layer. This new module aims to be a drop-in
replacement for ADOdb, implementing only those methods that we really need while being
minimalistically small (5kb for an adodb replacement doesn't sound too bad!) Only a mysql
driver at the moment but more can be added anytime.
The only thing that has not been implemented yet is data dictionaries, which is a very nifty
feature from ADOdb that some plugins rely on... Therefore, this library is not completely usable
yet if you depend on certain plugins, even though it will work with everything else.


Added: plog/trunk/class/database/pdb/drivers/pdbdriverbase.class.php
===================================================================
--- plog/trunk/class/database/pdb/drivers/pdbdriverbase.class.php	2005-06-06 20:27:14 UTC (rev 2173)
+++ plog/trunk/class/database/pdb/drivers/pdbdriverbase.class.php	2005-06-06 21:21:33 UTC (rev 2174)
@@ -0,0 +1,80 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
+
+	class PDbDriverBase extends Object
+	{
+	
+		var $_debug;
+	
+		function PDbDriverBase()
+		{
+			$this->Object();
+			
+			$this->_debug = false;
+		}
+		
+		/** 
+		 * Allows drivers to use custom options
+		 *
+		 * @param key
+		 * @param value
+		 */
+		function setDriverOpt( $key, $value )
+		{
+			// to be implemented by child classes	
+		}
+		
+		function Execute( $query )
+		{
+			// to be implemented by child classes
+		}
+		
+		function Connect( $host, $username, $password, $dbname )
+		{
+			// to be implemented by child classes
+		}
+		
+		function PConnect( $host, $username, $password, $dbname )
+		{
+			// to be implemented by child classes
+		}
+
+		function Close()
+		{
+		    // to be implemented by child classes
+		}		
+		
+		function ErrorMsg()
+		{
+			// to be implemented by child classes
+		}
+		
+		function Insert_ID()
+		{
+			// to be implemented by child classes
+		}
+		
+		function Affected_Rows()
+		{
+			// to be implemented by child classes
+		}
+		
+		/**
+		 * @private
+		 */
+		function _debugQuery( $query )
+		{
+		    if( $this->_debug ) {
+		       print("<hr/>$query<hr/>");
+		    }
+		    
+		    return( true );
+		}
+		
+		function setDebug( $debug )
+		{
+		    $this->_debug = $debug;
+		}		
+	}
+?>
\ No newline at end of file

Added: plog/trunk/class/database/pdb/drivers/pdbmysqldriver.class.php
===================================================================
--- plog/trunk/class/database/pdb/drivers/pdbmysqldriver.class.php	2005-06-06 20:27:14 UTC (rev 2173)
+++ plog/trunk/class/database/pdb/drivers/pdbmysqldriver.class.php	2005-06-06 21:21:33 UTC (rev 2174)
@@ -0,0 +1,75 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/database/pdb/drivers/pdbdriverbase.class.php" );
+	include_once( PLOG_CLASS_PATH."class/database/pdb/drivers/pdbmysqlrecordset.class.php" );
+
+	class PDbMySQLDriver extends PDbDriverBase
+	{
+		
+		var $_res;
+	
+		function PDbMySQLDriver()
+		{
+			$this->PDbDriverBase();
+		}
+		
+		function Execute( $query )
+		{
+			// execute the query and see whether it was incorrect
+			$this->_debugQuery( $query );
+			$result = mysql_query( $query, $this->_res );
+			if( !$result ) {
+			    if( $this->_debug ) {
+			       print("<hr/>ERROR: $query<hr/>"); 
+			    } 
+				return false;
+            }
+				
+			// if not, create a RecordSet based on it
+			$rs = new PdbMySQLRecordSet( $result );
+			return( $rs );
+		}
+		
+		function Connect( $host, $username, $password, $dbname )
+		{
+			// try to connect and quit if unsuccessful
+			$this->_res = mysql_connect( $host, $username, $password );			
+			if( !$this->_res )
+				return false;
+				
+			// continue otherwise and try to select our db
+			return( mysql_select_db( $dbname, $this->_res ));
+		}
+		
+		function PConnect( $host, $username, $password, $dbname )
+		{
+			// try to connect and quit if unsuccessful
+			$this->_res = mysql_pconnect( $host, $username, $password );			
+			if( !$this->_res )
+				return false;
+				
+			// continue otherwise and try to select our db
+			return( mysql_select_db( $dbname, $this->_res ));
+		}
+		
+		function Close()
+		{
+		    return( mysql_close( $this->_res ));
+		}
+		
+		function ErrorMsg()
+		{
+			return( mysql_error( $this->_res ));	
+		}
+		
+		function Insert_ID()
+		{
+			return( mysql_insert_id( $this->_res ));
+		}
+		
+		function Affected_Rows()
+		{
+		    return( mysql_affected_rows( $this->_res ));
+		}
+	}
+?>
\ No newline at end of file

Added: plog/trunk/class/database/pdb/drivers/pdbmysqlrecordset.class.php
===================================================================
--- plog/trunk/class/database/pdb/drivers/pdbmysqlrecordset.class.php	2005-06-06 20:27:14 UTC (rev 2173)
+++ plog/trunk/class/database/pdb/drivers/pdbmysqlrecordset.class.php	2005-06-06 21:21:33 UTC (rev 2174)
@@ -0,0 +1,28 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/database/pdb/drivers/pdbrecordset.class.php" );
+
+	class PdbMySQLRecordSet extends PdbRecordSet
+	{
+	
+		function PdbMySQLRecordSet( $dbRes = null )
+		{
+			$this->PdbRecordSet( $dbRes );
+		}
+		
+		function FetchRow()
+		{
+			return( mysql_fetch_assoc( $this->_dbRes ));
+		}
+		
+		function RecordCount()
+		{
+			return( mysql_num_rows( $this->_dbRes ));
+		}
+		
+		function Close()
+		{
+		    return( mysql_free_result( $this->_dbRes ));
+		}
+	}
+?>
\ No newline at end of file

Added: plog/trunk/class/database/pdb/drivers/pdbrecordset.class.php
===================================================================
--- plog/trunk/class/database/pdb/drivers/pdbrecordset.class.php	2005-06-06 20:27:14 UTC (rev 2173)
+++ plog/trunk/class/database/pdb/drivers/pdbrecordset.class.php	2005-06-06 21:21:33 UTC (rev 2174)
@@ -0,0 +1,44 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
+
+	class PdbRecordSet extends Object
+	{
+		
+		var $_dbRes;
+	
+		function PdbRecordSet( $dbRes = null )
+		{
+			$this->_dbRes = $dbRes;
+		}
+		
+		/**
+		 * Returns a row from the resultset
+		 *
+		 * @return an associative array
+		 */
+		function FetchRow()
+		{
+			// to be implemented by chid classes
+		}
+		
+		/**
+		 * Returns the number of rows fetched in the last SELECT operation
+		 *
+		 * @return an integer
+		 */
+		function RecordCount()
+		{
+			
+		}
+		
+		/**
+		 * Alias for RecordCount()
+		 * @see RecordCount
+		 */
+		function RowCount()
+		{
+		    return( $this->RecordCount());
+		}		
+	}
+?>
\ No newline at end of file

Added: plog/trunk/class/database/pdb/pdb.class.php
===================================================================
--- plog/trunk/class/database/pdb/pdb.class.php	2005-06-06 20:27:14 UTC (rev 2173)
+++ plog/trunk/class/database/pdb/pdb.class.php	2005-06-06 21:21:33 UTC (rev 2174)
@@ -0,0 +1,40 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
+	
+	define( "PDB_DRIVER_FOLDER", PLOG_CLASS_PATH."class/database/pdb/drivers/" );
+
+	/**
+	 * PDb
+	 *
+	 * Plog's own lightweight database abstraction layer
+	 */
+	class PDb extends Object
+	{
+		var $_drivers;
+	
+		function PDb()
+		{
+			$this->Object();
+		}
+		
+		/**
+		 * return the right driver type
+		 */
+		function getDriver( $driver )
+		{
+			$drivers = Array( 
+			   "mysql" => "PDbMySQLDriver",
+			   "dbx" => "PDbDbxDriver" );			
+			
+			// load the driver class
+			$driverPath = PDB_DRIVER_FOLDER.strtolower( $drivers[$driver] ).".class.php";
+			include_once( $driverPath );
+			
+			// create an instance of it
+			$driverClass = new $drivers[$driver]();
+			
+			return( $driverClass );
+		}
+	}
+?>
\ No newline at end of file




More information about the pLog-svn mailing list