DataDict.php 4.52 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
    public function __construct(PDO $dbh) {
doctrine's avatar
doctrine committed
7
        $file = Doctrine::getPath().DIRECTORY_SEPARATOR."Doctrine".DIRECTORY_SEPARATOR."adodb-hack".DIRECTORY_SEPARATOR."adodb.inc.php";
8

9
        if( ! file_exists($file))
doctrine's avatar
doctrine committed
10 11 12
            throw new Doctrine_Exception("Couldn't include datadict. File $file does not exist");

        require_once($file);
doctrine's avatar
doctrine committed
13 14 15 16

        $this->dbh  = $dbh;
        $this->dict = NewDataDictionary($dbh);
    }
17 18 19 20 21 22
    /**
     * metaColumns
     *
     * @param Doctrine_Table $table
     * @return array
     */
doctrine's avatar
doctrine committed
23 24 25
    public function metaColumns(Doctrine_Table $table) {
        return $this->dict->metaColumns($table->getTableName());
    }
26 27 28 29 30 31 32 33
    /**
     * createTable
     *
     * @param string $tablename
     * @param array $columns
     * @return boolean
     */
    public function createTable($tablename, array $columns) {
doctrine's avatar
doctrine committed
34
        foreach($columns as $name => $args) {
35 36 37 38
            if( ! is_array($args[2]))
                $args[2] = array();
                
            $r[] = $name." ".$this->getADOType($args[0],$args[1])." ".implode(' ',$args[2]);
doctrine's avatar
doctrine committed
39 40 41 42 43 44 45 46
        }


        $r = implode(", ",$r);
        $a = $this->dict->createTableSQL($tablename,$r);

        $return = true;
        foreach($a as $sql) {
doctrine's avatar
doctrine committed
47
            try {
doctrine's avatar
doctrine committed
48
                $this->dbh->query($sql);
49
            } catch(Exception $e) {
50
                $return = $e;
doctrine's avatar
doctrine committed
51 52 53 54 55 56 57 58 59 60 61 62 63
            }
        }

        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):
64 65
            case "array":
            case "object":
doctrine's avatar
doctrine committed
66
            case "string":
67
                if($length <= 255)
doctrine's avatar
doctrine committed
68
                    return "C($length)";
69
                elseif($length <= 4000)
doctrine's avatar
doctrine committed
70 71 72 73 74
                    return "X";
                else
                    return "X2";
            break;
            case "mbstring":
75
                if($length <= 255)
doctrine's avatar
doctrine committed
76
                    return "C2($length)";
77

doctrine's avatar
doctrine committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
                return "X2";
            case "clob":
                return "XL";
            break;
            case "date":
                return "D";
            break;
            case "float":
            case "double":
                return "F";
            break;
            case "timestamp":
                return "T";
            break;
            case "boolean":
                return "L";
            break;
95
            case "enum":
doctrine's avatar
doctrine committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
            case "integer":
                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;
111 112
            default:
                throw new Doctrine_Exception("Unknown column type $type");
doctrine's avatar
doctrine committed
113 114
        endswitch;
    }
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    
    /**
     * Converts native database column type to doctrine data type     
     * 
     * @param string $column            column type
     * @param integer $length           column length
     * @param string $dbType            Database driver name as returned by PDO::getAttribute(PDO::ATTR_DRIVER_NAME)
     * @param string $dbVersion         Database server version as return by PDO::getAttribute(PDO::ATTR_SERVER_VERSION)
     * @return array of doctrine column type and column length. In future may also return a validator name.
     * @throws Doctrine_Exception on unknown column type
     * @author Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
     */
    public static function getDoctrineType($colType,$colLength, $dbType = null, $dbVersion = null) 
    {
    	return array($colType, $colLength); /* @todo FIXME i am incomplete*/
    }    
    
132 133 134 135 136 137 138 139
    /**
     * checks for valid class name (uses camel case and underscores)
     *
     * @param string $classname
     * @return boolean
     */
    public static function isValidClassname($classname) {
        if(preg_match('~(^[a-z])|(_[a-z])|([\W])|(_{2})~', $classname))
zYne's avatar
zYne committed
140
            throw new Doctrine_Exception("Class name is not valid. Use camel case and underscores (i.e My_PerfectClass).");
141 142
        return true;
    }
doctrine's avatar
doctrine committed
143
}
144