1
0
mirror of synced 2024-12-14 07:06:04 +03:00
doctrine2/Doctrine/DataDict.php

118 lines
3.1 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) {
$manager = Doctrine_Manager::getInstance();
require_once($manager->getRoot()."/adodb-hack/adodb.inc.php");
$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) {
try {
$this->dbh->query($sql);
} catch(PDOException $e) {
$return = false;
}
}
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":
if($length < 255)
return "C($length)";
elseif($length < 4000)
return "X";
else
return "X2";
break;
case "mbstring":
if($length < 255)
return "C2($length)";
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;
}
}
?>