DataDict.php 2.85 KB
Newer Older
doctrine's avatar
doctrine committed
1 2
<?php
class Doctrine_DataDict {
doctrine's avatar
doctrine committed
3 4 5
    private $dbh;
    public function __construct(PDO $dbh) {
        $manager = Doctrine_Manager::getInstance();
doctrine's avatar
doctrine committed
6 7
        require_once($manager->getRoot()."/adodb-hack/adodb.inc.php");

doctrine's avatar
doctrine committed
8
        $this->dbh  = $dbh;
doctrine's avatar
doctrine committed
9 10 11
        $this->dict = NewDataDictionary($dbh);
    }

doctrine's avatar
doctrine committed
12 13
    public function metaColumns(Doctrine_Table $table) {
        return $this->dict->metaColumns($table->getTableName());
doctrine's avatar
doctrine committed
14 15 16
    }


doctrine's avatar
doctrine committed
17 18
    public function createTable($tablename, $columns) {
        foreach($columns as $name => $args) {
19
            $r[] = $name." ".$this->getADOType($args[0],$args[1])." ".str_replace("|"," ",$args[2]);
doctrine's avatar
doctrine committed
20
        }
doctrine's avatar
doctrine committed
21

doctrine's avatar
doctrine committed
22 23

        $r = implode(", ",$r);
doctrine's avatar
doctrine committed
24
        $a = $this->dict->createTableSQL($tablename,$r);
doctrine's avatar
doctrine committed
25 26 27 28

        $return = true;
        foreach($a as $sql) {
            try {
doctrine's avatar
doctrine committed
29
                $this->dbh->query($sql);
doctrine's avatar
doctrine committed
30
            } catch(PDOException $e) {
doctrine's avatar
doctrine committed
31 32
                if($this->dbh->getAttribute(PDO::ATTR_DRIVER_NAME) == "sqlite")
                    throw $e;
doctrine's avatar
doctrine committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
                $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";
doctrine's avatar
doctrine committed
53 54
                else
                    return "X2";
doctrine's avatar
doctrine committed
55 56 57 58 59 60 61 62 63
            break;
            case "mbstring":
                if($length < 255) 
                    return "C2($length)";
                
                return "X2";
            case "clob":
                return "XL";
            break;
doctrine's avatar
doctrine committed
64 65 66 67
            case "d":
            case "date":
                return "D";
            break;
doctrine's avatar
doctrine committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            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;
    }
}
?>