DataDict.php 5.68 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* 
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

/**
 * Doctrine_DataDict
 *
 * @package     Doctrine ORM
 * @url         www.phpdoctrine.com
 * @license     LGPL
 */
doctrine's avatar
doctrine committed
29
class Doctrine_DataDict {
30

zYne's avatar
zYne committed
31
    protected $dbh;
32

doctrine's avatar
doctrine committed
33
    public function __construct(PDO $dbh) {
doctrine's avatar
doctrine committed
34
        $file = Doctrine::getPath().DIRECTORY_SEPARATOR."Doctrine".DIRECTORY_SEPARATOR."adodb-hack".DIRECTORY_SEPARATOR."adodb.inc.php";
35

36
        if( ! file_exists($file))
doctrine's avatar
doctrine committed
37 38 39
            throw new Doctrine_Exception("Couldn't include datadict. File $file does not exist");

        require_once($file);
doctrine's avatar
doctrine committed
40 41 42 43

        $this->dbh  = $dbh;
        $this->dict = NewDataDictionary($dbh);
    }
44 45 46 47 48 49
    /**
     * metaColumns
     *
     * @param Doctrine_Table $table
     * @return array
     */
doctrine's avatar
doctrine committed
50 51 52
    public function metaColumns(Doctrine_Table $table) {
        return $this->dict->metaColumns($table->getTableName());
    }
53 54 55 56 57 58 59 60
    /**
     * createTable
     *
     * @param string $tablename
     * @param array $columns
     * @return boolean
     */
    public function createTable($tablename, array $columns) {
doctrine's avatar
doctrine committed
61
        foreach($columns as $name => $args) {
62 63
            if( ! is_array($args[2]))
                $args[2] = array();
64

65 66 67
            unset($args[2]['default']);

            $constraints = array_keys($args[2]);
68 69

            $r[] = $name." ".$this->getADOType($args[0],$args[1])." ".implode(' ', $constraints);
doctrine's avatar
doctrine committed
70 71 72 73 74 75 76 77
        }


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

        $return = true;
        foreach($a as $sql) {
doctrine's avatar
doctrine committed
78
            try {
doctrine's avatar
doctrine committed
79
                $this->dbh->query($sql);
80
            } catch(Exception $e) {
81
                $return = $e;
doctrine's avatar
doctrine committed
82 83 84 85 86 87 88 89 90 91 92 93 94
            }
        }

        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):
95 96
            case "array":
            case "object":
doctrine's avatar
doctrine committed
97
            case "string":
zYne's avatar
zYne committed
98
            case "gzip":
99
                if($length <= 255)
doctrine's avatar
doctrine committed
100
                    return "C($length)";
101
                elseif($length <= 4000)
doctrine's avatar
doctrine committed
102 103 104 105 106
                    return "X";
                else
                    return "X2";
            break;
            case "mbstring":
107
                if($length <= 255)
doctrine's avatar
doctrine committed
108
                    return "C2($length)";
109

doctrine's avatar
doctrine committed
110 111 112 113
                return "X2";
            case "clob":
                return "XL";
            break;
zYne's avatar
zYne committed
114 115 116
            case "blob":
                return "B";
            break;
doctrine's avatar
doctrine committed
117 118 119 120 121 122 123 124 125 126 127 128 129
            case "date":
                return "D";
            break;
            case "float":
            case "double":
                return "F";
            break;
            case "timestamp":
                return "T";
            break;
            case "boolean":
                return "L";
            break;
130
            case "enum":
doctrine's avatar
doctrine committed
131 132 133 134 135 136 137 138 139 140
            case "integer":
                if(empty($length))
                    return "I8";
                elseif($length < 4)
                    return "I1";
                elseif($length < 6)
                    return "I2";
                elseif($length < 10)
                    return "I4";
                else
zYne's avatar
zYne committed
141
                    return "I8";
doctrine's avatar
doctrine committed
142
            break;
143 144
            default:
                throw new Doctrine_Exception("Unknown column type $type");
doctrine's avatar
doctrine committed
145 146
        endswitch;
    }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    
    /**
     * 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*/
    }    
    
164 165 166 167 168 169 170 171
    /**
     * 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
172
            throw new Doctrine_Exception("Class name is not valid. Use camel case and underscores (i.e My_PerfectClass).");
173 174
        return true;
    }
doctrine's avatar
doctrine committed
175
}
176