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))
|
|
|
|
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);
|
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 "a":
|
|
|
|
case "object":
|
|
|
|
case "o":
|
2006-05-30 12:42:10 +04:00
|
|
|
case "string":
|
|
|
|
case "s":
|
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 "d":
|
|
|
|
case "date":
|
|
|
|
return "D";
|
|
|
|
break;
|
|
|
|
case "float":
|
|
|
|
case "f":
|
|
|
|
case "double":
|
|
|
|
return "F";
|
|
|
|
break;
|
|
|
|
case "timestamp":
|
|
|
|
case "t":
|
|
|
|
return "T";
|
|
|
|
break;
|
|
|
|
case "boolean":
|
|
|
|
case "bool":
|
|
|
|
return "L";
|
|
|
|
break;
|
2006-06-18 02:46:03 +04:00
|
|
|
case "enum":
|
|
|
|
case "e":
|
2006-05-30 12:42:10 +04:00
|
|
|
case "integer":
|
|
|
|
case "int":
|
|
|
|
case "i":
|
|
|
|
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;
|
|
|
|
endswitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|