Location: PHPKode > scripts > Persistent Objects for PHP > persistent-objects-for-php/drivers/mysql.php
<?php

	/**
	 * Defines a class to be used for MySQL databases
	 * 
	 * @author Pablo Santiago Sánchez <hide@address.com>
	 * @copyright Copyright (c) 2008, Pablo Santiago Sánchez
	 * @license http://opensource.org/licenses/bsd-license.php BSD License
	 * @package pop
	 * @subpackage drivers
	 */

	/**
	 * Defines a class to be used for MySQL databases
	 * 
	 * @package pop
	 * @subpackage drivers
	 */
	class PMySQLDBDriver extends PGenericDBDriver
	{
		public $schema = "";
		public $separator = "_";
		
		/**
		 * Class constructor
		 * @param $pdodb
		 * @return void
		 */
		public function __construct($pdodb)
		{		
			POPDB::getConnection($pdodb)->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
		}
		
		/**
		 * Returns the last inserted itme id
		 * @param $pdodb
		 * @param $table
		 * @param $idname
		 * @return integer
		 */
		public function getLastId($pdodb, $table, $idname)
		{
			return POPDB::getConnection($pdodb)->lastInsertId();
		}

		/**
		 * Returns the required string for the createTable method
		 * @return string
		 */
		public function identityField()
		{
			return " integer not null auto_increment ";
		}
		
		/**
		 * Creates the required schema to avoid errors on creating tables
		 * @param $schema
		 * @return void
		 */
		public function createSchema($pdodb, $schema)
		{
			//mysql doesn't work with schemas in a way that it's necessary
			//to create the schema, so, this method does nothing
		}

		/**
		 * Returns the required SQL for field creation
		 * @param $type
		 * @param $defaultval
		 * @param $size
		 * @return void
		 */
		public function fieldCreation($type, $defaultval, $size=null)
		{
			$sql_field = $type." ";

			if ($type=="varchar")
				$sql_field .= "(".$size.") ";

			if (is_null($defaultval))
				$sql_field .= "default null";
			else
			{
				if ($defaultval && $type!="datetime" && $type!="date" && $type!="time" && $type!="varchar" && $type!="text")
					$sql_field .= "default ".$defaultval;
				else if ($type=="varchar")
					$sql_field .= "default '".$defaultval."'";
			}
			
			return $sql_field;
		}

		/**
		 * Alter table
		 * @return void
		 */
		public function alterTable()
		{
		}

		/**
		 * Drops a table
		 * @param $pdodb
		 * @param $schema
		 * @param $table
		 * @return void
		 */
		public function dropTable($pdodb, $schema, $table)
		{
			$sql = "show tables;";
			$rs = POPDB::getConnection($pdodb)->query($sql);
			$result = $rs->fetchAll();
			$rs->closeCursor();
			if (POPEnvironment::$debug)
				echo "<pre><font color=#009900>".$table.": ".$sql."</font></pre><br>";

			foreach ($result as $row)
				if($row[0]==$table)
				{
					$sql = "drop table ".$table.";\n";
					POPDB::getConnection($pdodb)->exec($sql);
					if (POPEnvironment::$debug)
						echo "<pre><font color=#009900>".$sql."</font></pre><br>";
				}
		}
	}

	/** 
	 * Register the class for the driver
	 */
	POPDBDriverRegistry::addDriver("mysql", "PMySQLDBDriver");
?>
Return current item: Persistent Objects for PHP