DataDict.php 4.76 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
            if( ! is_array($args[2]))
                $args[2] = array();
37 38 39 40 41 42 43 44 45 46 47
            
            $constraints = array();

            foreach($args[2] as $k => $v) {
                if(is_string($k))
                    $constraints[] = $k;
                else
                    $constraints[] = $v;
            }

            $r[] = $name." ".$this->getADOType($args[0],$args[1])." ".implode(' ', $constraints);
doctrine's avatar
doctrine committed
48 49 50 51 52 53 54 55
        }


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

        $return = true;
        foreach($a as $sql) {
doctrine's avatar
doctrine committed
56
            try {
doctrine's avatar
doctrine committed
57
                $this->dbh->query($sql);
58
            } catch(Exception $e) {
59
                $return = $e;
doctrine's avatar
doctrine committed
60 61 62 63 64 65 66 67 68 69 70 71 72
            }
        }

        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):
73 74
            case "array":
            case "object":
doctrine's avatar
doctrine committed
75
            case "string":
76
                if($length <= 255)
doctrine's avatar
doctrine committed
77
                    return "C($length)";
78
                elseif($length <= 4000)
doctrine's avatar
doctrine committed
79 80 81 82 83
                    return "X";
                else
                    return "X2";
            break;
            case "mbstring":
84
                if($length <= 255)
doctrine's avatar
doctrine committed
85
                    return "C2($length)";
86

doctrine's avatar
doctrine committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
                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;
104
            case "enum":
doctrine's avatar
doctrine committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
            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;
120 121
            default:
                throw new Doctrine_Exception("Unknown column type $type");
doctrine's avatar
doctrine committed
122 123
        endswitch;
    }
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    
    /**
     * 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*/
    }    
    
141 142 143 144 145 146 147 148
    /**
     * 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
149
            throw new Doctrine_Exception("Class name is not valid. Use camel case and underscores (i.e My_PerfectClass).");
150 151
        return true;
    }
doctrine's avatar
doctrine committed
152
}
153