DataDict.php 3.13 KB
Newer Older
doctrine's avatar
doctrine committed
1 2
<?php
class Doctrine_DataDict {
3

doctrine's avatar
doctrine committed
4
    private $dbh;
5

doctrine's avatar
doctrine committed
6 7 8 9 10 11 12
    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);
    }
13 14 15 16 17 18
    /**
     * metaColumns
     *
     * @param Doctrine_Table $table
     * @return array
     */
doctrine's avatar
doctrine committed
19 20 21
    public function metaColumns(Doctrine_Table $table) {
        return $this->dict->metaColumns($table->getTableName());
    }
22 23 24 25 26 27 28 29
    /**
     * createTable
     *
     * @param string $tablename
     * @param array $columns
     * @return boolean
     */
    public function createTable($tablename, array $columns) {
doctrine's avatar
doctrine committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        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):
57 58 59 60
            case "array":
            case "a":
            case "object":
            case "o":
doctrine's avatar
doctrine committed
61 62 63 64 65 66 67 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
            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;
doctrine's avatar
doctrine committed
95 96
            case "enum":
            case "e":
doctrine's avatar
doctrine committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            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;
    }
}
?>