1
0
mirror of synced 2024-12-13 14:56:01 +03:00
doctrine2/Doctrine/DataDict.php

142 lines
4.4 KiB
PHP
Raw Normal View History

2006-05-30 12:42:10 +04:00
<?php
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";
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) {
$r[] = $name." ".$this->getADOType($args[0],$args[1])." ".str_replace("|"," ",$args[2]);
}
$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);
} catch(Exception $e) {
$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":
if($length <= 255)
2006-05-30 12:42:10 +04:00
return "C($length)";
elseif($length <= 4000)
2006-05-30 12:42:10 +04:00
return "X";
else
return "X2";
break;
case "mbstring":
if($length <= 255)
2006-05-30 12:42:10 +04:00
return "C2($length)";
2006-05-30 12:42:10 +04:00
return "X2";
case "clob":
return "XL";
break;
case "date":
return "D";
break;
case "float":
case "double":
return "F";
break;
case "timestamp":
return "T";
break;
case "boolean":
return "L";
break;
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";
elseif($length <= 20)
return "I8";
else
throw new Doctrine_Exception("Too long integer (max length is 20).");
break;
default:
throw new Doctrine_Exception("Unknown column type $type");
2006-05-30 12:42:10 +04:00
endswitch;
}
/**
* 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*/
}
/**
* 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).");
return true;
}
2006-05-30 12:42:10 +04:00
}