2006-05-30 12:42:10 +04:00
|
|
|
<?php
|
2006-09-24 13:56:23 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doctrine_DataDict
|
|
|
|
*
|
|
|
|
* @package Doctrine ORM
|
|
|
|
* @url www.phpdoctrine.com
|
|
|
|
* @license LGPL
|
|
|
|
*/
|
2006-05-30 12:42:10 +04:00
|
|
|
class Doctrine_DataDict {
|
2006-06-25 22:34:53 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
private $dbh;
|
2006-06-25 22:34:53 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
public function __construct(PDO $dbh) {
|
2006-08-08 02:40:06 +04:00
|
|
|
$file = Doctrine::getPath().DIRECTORY_SEPARATOR."Doctrine".DIRECTORY_SEPARATOR."adodb-hack".DIRECTORY_SEPARATOR."adodb.inc.php";
|
2006-08-09 00:31:45 +04:00
|
|
|
|
2006-08-21 19:30:18 +04:00
|
|
|
if( ! file_exists($file))
|
2006-08-08 02:40:06 +04:00
|
|
|
throw new Doctrine_Exception("Couldn't include datadict. File $file does not exist");
|
|
|
|
|
|
|
|
require_once($file);
|
2006-05-30 12:42:10 +04:00
|
|
|
|
|
|
|
$this->dbh = $dbh;
|
|
|
|
$this->dict = NewDataDictionary($dbh);
|
|
|
|
}
|
2006-06-25 22:34:53 +04:00
|
|
|
/**
|
|
|
|
* metaColumns
|
|
|
|
*
|
|
|
|
* @param Doctrine_Table $table
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-05-30 12:42:10 +04:00
|
|
|
public function metaColumns(Doctrine_Table $table) {
|
|
|
|
return $this->dict->metaColumns($table->getTableName());
|
|
|
|
}
|
2006-06-25 22:34:53 +04:00
|
|
|
/**
|
|
|
|
* createTable
|
|
|
|
*
|
|
|
|
* @param string $tablename
|
|
|
|
* @param array $columns
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function createTable($tablename, array $columns) {
|
2006-05-30 12:42:10 +04:00
|
|
|
foreach($columns as $name => $args) {
|
2006-08-31 14:00:09 +04:00
|
|
|
if( ! is_array($args[2]))
|
|
|
|
$args[2] = array();
|
2006-09-04 10:55:16 +04:00
|
|
|
|
2006-09-16 00:56:25 +04:00
|
|
|
unset($args[2]['default']);
|
|
|
|
|
|
|
|
$constraints = array_keys($args[2]);
|
2006-09-04 10:55:16 +04:00
|
|
|
|
|
|
|
$r[] = $name." ".$this->getADOType($args[0],$args[1])." ".implode(' ', $constraints);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$r = implode(", ",$r);
|
|
|
|
$a = $this->dict->createTableSQL($tablename,$r);
|
|
|
|
|
|
|
|
$return = true;
|
|
|
|
foreach($a as $sql) {
|
2006-08-08 02:40:06 +04:00
|
|
|
try {
|
2006-05-30 12:42:10 +04:00
|
|
|
$this->dbh->query($sql);
|
2006-07-10 13:18:09 +04:00
|
|
|
} catch(Exception $e) {
|
2006-07-05 22:59:46 +04:00
|
|
|
$return = $e;
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* converts doctrine type to adodb type
|
|
|
|
*
|
|
|
|
* @param string $type column type
|
|
|
|
* @param integer $length column length
|
|
|
|
*/
|
|
|
|
public function getADOType($type,$length) {
|
|
|
|
switch($type):
|
2006-06-01 15:58:05 +04:00
|
|
|
case "array":
|
|
|
|
case "object":
|
2006-05-30 12:42:10 +04:00
|
|
|
case "string":
|
2006-09-20 19:46:25 +04:00
|
|
|
case "gzip":
|
2006-07-06 11:25:28 +04:00
|
|
|
if($length <= 255)
|
2006-05-30 12:42:10 +04:00
|
|
|
return "C($length)";
|
2006-07-06 11:25:28 +04:00
|
|
|
elseif($length <= 4000)
|
2006-05-30 12:42:10 +04:00
|
|
|
return "X";
|
|
|
|
else
|
|
|
|
return "X2";
|
|
|
|
break;
|
|
|
|
case "mbstring":
|
2006-07-06 11:25:28 +04:00
|
|
|
if($length <= 255)
|
2006-05-30 12:42:10 +04:00
|
|
|
return "C2($length)";
|
2006-07-10 13:18:09 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
return "X2";
|
|
|
|
case "clob":
|
|
|
|
return "XL";
|
|
|
|
break;
|
2006-09-24 13:42:37 +04:00
|
|
|
case "blob":
|
|
|
|
return "B";
|
|
|
|
break;
|
2006-05-30 12:42:10 +04:00
|
|
|
case "date":
|
|
|
|
return "D";
|
|
|
|
break;
|
|
|
|
case "float":
|
|
|
|
case "double":
|
|
|
|
return "F";
|
|
|
|
break;
|
|
|
|
case "timestamp":
|
|
|
|
return "T";
|
|
|
|
break;
|
|
|
|
case "boolean":
|
|
|
|
return "L";
|
|
|
|
break;
|
2006-08-21 19:30:18 +04:00
|
|
|
case "enum":
|
2006-05-30 12:42:10 +04:00
|
|
|
case "integer":
|
|
|
|
if(empty($length))
|
|
|
|
return "I8";
|
|
|
|
elseif($length < 4)
|
|
|
|
return "I1";
|
|
|
|
elseif($length < 6)
|
|
|
|
return "I2";
|
|
|
|
elseif($length < 10)
|
|
|
|
return "I4";
|
|
|
|
else
|
2006-09-24 13:56:23 +04:00
|
|
|
return "I8";
|
2006-05-30 12:42:10 +04:00
|
|
|
break;
|
2006-08-09 00:31:45 +04:00
|
|
|
default:
|
|
|
|
throw new Doctrine_Exception("Unknown column type $type");
|
2006-05-30 12:42:10 +04:00
|
|
|
endswitch;
|
|
|
|
}
|
2006-08-27 06:21:20 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts native database column type to doctrine data type
|
|
|
|
*
|
|
|
|
* @param string $column column type
|
|
|
|
* @param integer $length column length
|
|
|
|
* @param string $dbType Database driver name as returned by PDO::getAttribute(PDO::ATTR_DRIVER_NAME)
|
|
|
|
* @param string $dbVersion Database server version as return by PDO::getAttribute(PDO::ATTR_SERVER_VERSION)
|
|
|
|
* @return array of doctrine column type and column length. In future may also return a validator name.
|
|
|
|
* @throws Doctrine_Exception on unknown column type
|
|
|
|
* @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
|
|
|
|
*/
|
|
|
|
public static function getDoctrineType($colType,$colLength, $dbType = null, $dbVersion = null)
|
|
|
|
{
|
|
|
|
return array($colType, $colLength); /* @todo FIXME i am incomplete*/
|
|
|
|
}
|
|
|
|
|
2006-08-21 19:30:18 +04:00
|
|
|
/**
|
|
|
|
* checks for valid class name (uses camel case and underscores)
|
|
|
|
*
|
|
|
|
* @param string $classname
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function isValidClassname($classname) {
|
|
|
|
if(preg_match('~(^[a-z])|(_[a-z])|([\W])|(_{2})~', $classname))
|
2006-08-29 23:34:03 +04:00
|
|
|
throw new Doctrine_Exception("Class name is not valid. Use camel case and underscores (i.e My_PerfectClass).");
|
2006-08-21 19:30:18 +04:00
|
|
|
return true;
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2006-08-27 06:21:20 +04:00
|
|
|
|