1
0
mirror of synced 2025-01-18 22:41:43 +03:00
doctrine2/classes/DataDict.php

102 lines
2.9 KiB
PHP
Raw Normal View History

2006-04-13 20:37:28 +00:00
<?php
class Doctrine_DataDict {
2006-04-14 22:25:02 +00:00
private $dbh;
public function __construct(PDO $dbh) {
$manager = Doctrine_Manager::getInstance();
2006-04-13 20:37:28 +00:00
require_once($manager->getRoot()."/adodb-hack/adodb.inc.php");
2006-04-14 22:25:02 +00:00
$this->dbh = $dbh;
2006-04-13 20:37:28 +00:00
$this->dict = NewDataDictionary($dbh);
}
2006-04-14 22:25:02 +00:00
public function metaColumns(Doctrine_Table $table) {
return $this->dict->metaColumns($table->getTableName());
2006-04-13 20:37:28 +00:00
}
2006-04-14 22:25:02 +00:00
public function createTable($tablename, $columns) {
foreach($columns as $name => $args) {
2006-04-23 08:12:01 +00:00
$r[] = $name." ".$this->getADOType($args[0],$args[1])." ".str_replace("|"," ",$args[2]);
2006-04-13 20:37:28 +00:00
}
2006-04-14 22:25:02 +00:00
2006-04-13 20:37:28 +00:00
$r = implode(", ",$r);
2006-04-14 22:25:02 +00:00
$a = $this->dict->createTableSQL($tablename,$r);
2006-04-13 20:37:28 +00:00
$return = true;
foreach($a as $sql) {
try {
2006-04-14 22:25:02 +00:00
$this->dbh->query($sql);
2006-04-13 20:37:28 +00:00
} catch(PDOException $e) {
2006-04-14 22:25:02 +00:00
if($this->dbh->getAttribute(PDO::ATTR_DRIVER_NAME) == "sqlite")
throw $e;
2006-04-13 20:37:28 +00:00
$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):
case "string":
case "s":
if($length < 255)
return "C($length)";
elseif($length < 4000)
return "X";
2006-04-16 08:41:45 +00:00
else
return "X2";
2006-04-13 20:37:28 +00:00
break;
case "mbstring":
if($length < 255)
return "C2($length)";
return "X2";
case "clob":
return "XL";
break;
2006-05-25 10:01:59 +00:00
case "d":
case "date":
return "D";
break;
2006-04-13 20:37:28 +00:00
case "float":
case "f":
case "double":
return "F";
break;
case "timestamp":
case "t":
return "T";
break;
case "boolean":
case "bool":
return "L";
break;
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;
}
}
?>