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";
|
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();
|
|
|
|
|
|
|
|
$r[] = $name." ".$this->getADOType($args[0],$args[1])." ".implode(' ',$args[2]);
|
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-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;
|
|
|
|
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";
|
|
|
|
elseif($length <= 20)
|
|
|
|
return "I8";
|
|
|
|
else
|
|
|
|
throw new Doctrine_Exception("Too long integer (max length is 20).");
|
|
|
|
|
|
|
|
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
|
|
|
|