Changeset 4768

Show
Ignore:
Timestamp:
08/12/08 02:19:49 (11 months ago)
Author:
jwage
Message:

BC BREAK - Completely removing emulated enum conversions. Version 1.0 will store complete enum value in varchar(255) column by default.

Location:
branches/1.0
Files:
16 modified

Legend:

Unmodified
Added
Removed
  • branches/1.0/lib/Doctrine/DataDict/Firebird.php

    r4252 r4768  
    6161        } 
    6262        switch ($field['type']) { 
     63            case 'enum': 
     64                $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 
    6365            case 'varchar': 
    6466            case 'string': 
     
    7981                return 'BLOB SUB_TYPE 0'; 
    8082            case 'integer': 
    81             case 'enum': 
    8283            case 'int': 
    8384                return 'INT'; 
  • branches/1.0/lib/Doctrine/DataDict/Informix.php

    r4252 r4768  
    6262        switch ($field['type']) { 
    6363            case 'char': 
     64            case 'enum': 
     65                $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 
    6466            case 'varchar': 
    6567            case 'array': 
  • branches/1.0/lib/Doctrine/DataDict/Mssql.php

    r4252 r4768  
    6363        } 
    6464        switch ($field['type']) { 
     65            case 'enum': 
     66                $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 
    6567            case 'array': 
    6668            case 'object': 
     
    9496                return 'IMAGE'; 
    9597            case 'integer': 
    96             case 'enum': 
    9798            case 'int': 
    9899                return 'INT'; 
  • branches/1.0/lib/Doctrine/DataDict/Mysql.php

    r4757 r4768  
    143143 
    144144                return $length ? 'CHAR('.$length.')' : 'CHAR(255)'; 
     145            case 'enum': 
     146                if ($this->conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) { 
     147                    $values = array(); 
     148                    foreach ($field['values'] as $value) { 
     149                      $values[] = $this->conn->quote($value, 'varchar'); 
     150                    } 
     151                    return 'ENUM('.implode(', ', $values).')'; 
     152                } else { 
     153                    $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 
     154                } 
    145155            case 'varchar': 
    146156            case 'array': 
     
    185195                } 
    186196                return 'LONGBLOB'; 
    187             case 'enum': 
    188                 if ($this->conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) { 
    189                     $values = array(); 
    190                     foreach ($field['values'] as $value) { 
    191                       $values[] = $this->conn->quote($value, 'varchar'); 
    192                     } 
    193                     return 'ENUM('.implode(', ', $values).')'; 
    194                 } 
    195                 // fall back to integer 
    196197            case 'integer': 
    197198            case 'int': 
  • branches/1.0/lib/Doctrine/DataDict/Oracle.php

    r4252 r4768  
    5959        } 
    6060        switch ($field['type']) { 
     61            case 'enum': 
     62                $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 
    6163            case 'string': 
    6264            case 'array': 
     
    7678                return 'BLOB'; 
    7779            case 'integer': 
    78             case 'enum': 
    7980            case 'int': 
    8081                if ( ! empty($field['length'])) { 
  • branches/1.0/lib/Doctrine/DataDict/Pgsql.php

    r4353 r4768  
    364364        } 
    365365        switch ($field['type']) { 
     366            case 'enum': 
     367                $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 
    366368            case 'char': 
    367369            case 'string': 
     
    382384            case 'blob': 
    383385                return 'BYTEA'; 
    384             case 'enum': 
    385386            case 'integer': 
    386387            case 'int': 
  • branches/1.0/lib/Doctrine/DataDict/Sqlite.php

    r4252 r4768  
    6161        } 
    6262        switch ($field['type']) { 
     63            case 'enum': 
     64                $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 
    6365            case 'text': 
    6466            case 'object': 
     
    98100                } 
    99101                return 'LONGBLOB'; 
    100             case 'enum': 
    101102            case 'integer': 
    102103            case 'boolean': 
  • branches/1.0/lib/Doctrine/Query.php

    r4735 r4768  
    237237 
    238238    /** 
    239      * addEnumParam 
    240      * sets input parameter as an enumerated parameter 
    241      * 
    242      * @param string $key   the key of the input parameter 
    243      * @return Doctrine_Query 
    244      */ 
    245     public function addEnumParam($key, $table = null, $column = null) 
    246     { 
    247         $array = (isset($table) || isset($column)) ? array($table, $column) : array(); 
    248  
    249         if ($key === '?') { 
    250             $this->_enumParams[] = $array; 
    251         } else { 
    252             $this->_enumParams[$key] = $array; 
    253         } 
    254     } 
    255  
    256     /** 
    257      * getEnumParams 
    258      * get all enumerated parameters 
    259      * 
    260      * @return array    all enumerated parameters 
    261      */ 
    262     public function getEnumParams() 
    263     { 
    264         return $this->_enumParams; 
    265     } 
    266  
    267  
    268     /** 
    269239     * fetchArray 
    270240     * Convenience method to execute using array fetching as hydration mode. 
     
    10841054        } 
    10851055        $this->_state = self::STATE_CLEAN; 
    1086  
    1087         $params = $this->convertEnums($params); 
    10881056 
    10891057        // Proceed with the generated SQL 
     
    18811849        $params = array_merge($this->_params['join'], $this->_params['where'], $this->_params['having'], $params); 
    18821850 
    1883         $params = $this->convertEnums($params); 
    1884  
    18851851        $results = $this->getConnection()->fetchAll($q, $params); 
    18861852 
     
    19551921        $this->_parsers = array(); 
    19561922        $this->_dqlParts = array(); 
    1957         $this->_enumParams = array(); 
    19581923    } 
    19591924 
  • branches/1.0/lib/Doctrine/Query/Abstract.php

    r4735 r4768  
    227227 
    228228    /** 
    229      * @var array $_enumParams              an array containing the keys of the parameters that should be enumerated 
    230      */ 
    231     protected $_enumParams = array(); 
    232  
    233     /** 
    234229     * @var boolean 
    235230     */ 
    236231    protected $_isLimitSubqueryUsed = false; 
    237  
    238     protected $_pendingSetParams = array(); 
    239232 
    240233    /** 
     
    579572    { 
    580573        return $this->_isLimitSubqueryUsed; 
    581     } 
    582  
    583     /** 
    584      * convertEnums 
    585      * convert enum parameters to their integer equivalents 
    586      * 
    587      * @return array    converted parameter array 
    588      */ 
    589     public function convertEnums($params) 
    590     { 
    591         $table = $this->getRoot(); 
    592  
    593         // $position tracks the position of the parameter, to ensure we're converting 
    594         // the right parameter value when simple ? placeholders are used. 
    595         // This only works because SET is only allowed in update statements and it's 
    596         // the first place where parameters can occur.. see issue #935 
    597         $position = 0; 
    598         foreach ($this->_pendingSetParams as $fieldName => $value) { 
    599             $e = explode('.', $fieldName); 
    600             $fieldName = isset($e[1]) ? $e[1]:$e[0]; 
    601             if ($table->getTypeOf($fieldName) == 'enum') { 
    602                 $value = $value === '?' ? $position : $value; 
    603                 $this->addEnumParam($value, $table, $fieldName); 
    604             } 
    605             ++$position; 
    606         } 
    607         $this->_pendingSetParams = array(); 
    608  
    609         foreach ($this->_enumParams as $key => $values) { 
    610             if (isset($params[$key])) { 
    611                 if ( ! empty($values)) { 
    612                     $params[$key] = $values[0]->enumIndex($values[1], $params[$key]); 
    613                 } 
    614             } 
    615         } 
    616  
    617         return $params; 
    618574    } 
    619575 
     
    958914                $query = $this->getSqlQuery($params); 
    959915            } 
    960             $params = $this->convertEnums($params); 
    961916        } else { 
    962917            $query = $this->_view->getSelectSql(); 
     
    12571212 
    12581213        // if there's no params, return (else we'll get a WHERE IN (), invalid SQL) 
    1259         if (!count($params)) 
    1260           return $this; 
     1214        if ( ! count($params)) { 
     1215            return $this; 
     1216        } 
    12611217 
    12621218        $a = array(); 
     
    14181374                } 
    14191375            } 
    1420  
    1421             $this->_pendingSetParams[$key] = $value; 
    14221376 
    14231377            return $this->_addDqlQueryPart('set', $key . ' = ' . $value, true); 
  • branches/1.0/lib/Doctrine/Query/JoinCondition.php

    r4715 r4768  
    7272            $map       = $this->query->getAliasDeclaration($reference); 
    7373            $table     = $map['table']; 
    74             // check if value is enumerated value 
    75             $enumIndex = $table->enumIndex($field, trim($value, "'")); 
    76  
    77             if (false !== $enumIndex && $conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) { 
    78                 $enumIndex = $conn->quote($enumIndex, 'text'); 
    79             } 
    8074 
    8175            // FIX: Issues with "(" XXX ")" 
     
    10599                    $value = array(); 
    106100                    foreach ($e as $part) { 
    107                         $index = $table->enumIndex($field, trim($part, "'")); 
    108  
    109                         if (false !== $index && $conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) { 
    110                             $index = $conn->quote($index, 'text'); 
    111                         } 
    112  
    113                         if ($index !== false) { 
    114                             $value[] = $index; 
    115                         } else { 
    116                             $value[] = $this->parseLiteralValue($part); 
    117                         } 
     101                        $value[] = $this->parseLiteralValue($part); 
    118102                    } 
    119103 
     
    123107                } 
    124108            } else { 
    125                 if ($enumIndex !== false) { 
    126                     $value = $enumIndex; 
    127                 } else { 
    128                     $value = $this->parseLiteralValue($value); 
    129                 } 
     109                $value = $this->parseLiteralValue($value); 
    130110            } 
    131111 
     
    135115                case '=': 
    136116                case '!=': 
    137                     if ($enumIndex !== false) { 
    138                         $value  = $enumIndex; 
    139                     } 
    140117                default: 
    141118                    $leftExpr = (($hasLeftAggExpression) ? $leftMatches[1] . '(' : '')  
  • branches/1.0/lib/Doctrine/Query/Where.php

    r4252 r4768  
    9191        $conn = $this->query->getConnection(); 
    9292 
     93        // If value is contained in paranthesis 
    9394        if (substr($value, 0, 1) == '(') { 
    9495            // trim brackets 
    9596            $trimmed = $this->_tokenizer->bracketTrim($value); 
    9697 
     98            // If subquery found which begins with FROM and SELECT 
     99            // FROM User u WHERE u.id IN(SELECT u.id FROM User u WHERE u.id = 1) 
    97100            if (substr($trimmed, 0, 4) == 'FROM' || 
    98101                substr($trimmed, 0, 6) == 'SELECT') { 
     
    102105                $value = '(' . $this->query->createSubquery()->parseQuery($trimmed, false)->getQuery() . ')'; 
    103106 
     107            // If custom sql for custom subquery 
     108            // You can specify SQL: followed by any valid sql expression 
     109            // FROM User u WHERE u.id = SQL:(select id from user where id = 1) 
    104110            } elseif (substr($trimmed, 0, 4) == 'SQL:') { 
    105111                $value = '(' . substr($trimmed, 4) . ')'; 
     112            // simple in expression found 
    106113            } else { 
    107                 // simple in expression found 
    108114                $e = $this->_tokenizer->sqlExplode($trimmed, ','); 
    109115 
     
    113119 
    114120                foreach ($e as $part) { 
    115                     if (isset($table) && isset($field)) { 
    116                         $index = $table->enumIndex($field, trim($part, "'")); 
    117  
    118                         if (false !== $index && $conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) { 
    119                             $index = $conn->quote($index, 'text'); 
    120                         } 
    121                     } 
    122  
    123                     if ($index !== false) { 
    124                         $value[] = $index; 
    125                     } else { 
    126                         $value[] = $this->parseLiteralValue($part); 
    127                     } 
     121                    $value[] = $this->parseLiteralValue($part); 
    128122                } 
    129123 
    130124                $value = '(' . implode(', ', $value) . ')'; 
    131125            } 
    132         } else if (substr($value, 0, 1) == ':' || $value === '?') { 
    133             // placeholder found 
    134             if (isset($table) && isset($field) && $table->getTypeOf($field) == 'enum') { 
    135                 $this->query->addEnumParam($value, $table, $field); 
    136             } else { 
    137                 $this->query->addEnumParam($value, null, null); 
    138             } 
    139126        } else { 
    140             $enumIndex = false; 
    141             if (isset($table) && isset($field)) { 
    142                 // check if value is enumerated value 
    143                 $enumIndex = $table->enumIndex($field, trim($value, "'")); 
    144  
    145                 if (false !== $enumIndex && $conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) { 
    146                     $enumIndex = $conn->quote($enumIndex, 'text'); 
    147                 } 
    148             } 
    149  
    150             if ($enumIndex !== false) { 
    151                 $value = $enumIndex; 
    152             } else { 
    153                 $value = $this->parseLiteralValue($value); 
    154             } 
     127            $value = $this->parseLiteralValue($value); 
    155128        } 
    156129        return $value; 
  • branches/1.0/lib/Doctrine/RawSql.php

    r4714 r4768  
    321321        $params = array_merge($this->_params['join'], $this->_params['where'], $this->_params['having'], $params); 
    322322 
    323         $params = $this->convertEnums($params); 
    324  
    325323        $results = $this->getConnection()->fetchAll($q, $params); 
    326324 
  • branches/1.0/lib/Doctrine/Record.php

    r4722 r4768  
    593593                        $vars['_data'][$k] = gzcompress($vars['_data'][$k]); 
    594594                        break; 
    595                     case 'enum': 
    596                         $vars['_data'][$k] = $this->_table->enumIndex($k, $vars['_data'][$k]); 
    597                         break; 
    598595                } 
    599596            } 
     
    12411238                    $a[$field] = $this->getTable()->getConnection()->convertBooleans($this->_data[$field]); 
    12421239                break; 
    1243                 case 'enum': 
    1244                     $a[$field] = $this->_table->enumIndex($field, $this->_data[$field]); 
    1245                     break; 
    12461240                default: 
    12471241                    if ($this->_data[$field] instanceof Doctrine_Record) { 
  • branches/1.0/lib/Doctrine/Table.php

    r4726 r4768  
    609609 
    610610            switch ($definition['type']) { 
    611                 case 'enum': 
    612                     if (isset($definition['default'])) { 
    613                         $definition['default'] = $this->enumIndex($name, $definition['default']); 
    614                     } 
    615                     break; 
    616611                case 'boolean': 
    617612                    if (isset($definition['default'])) { 
  • branches/1.0/tests/DataType/EnumTestCase.php

    r4177 r4768  
    106106            $this->assertEqual($ret, 1); 
    107107        } catch (Exception $e) { 
    108             $this->fail(); 
     108            $this->fail($e->getMessage()); 
    109109        } 
    110110    } 
     
    239239          ->execute(); 
    240240 
    241         $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.status AS e__status, e.text AS e__text, e2.text AS e2__text FROM enum_test e LEFT JOIN enum_test3 e2 ON e.text = e2.text WHERE e.status = 1"); 
     241        $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.status AS e__status, e.text AS e__text, e2.text AS e2__text FROM enum_test e LEFT JOIN enum_test3 e2 ON e.text = e2.text WHERE e.status = 'verified'"); 
     242    } 
     243 
     244    public function testInvalidValueErrors() 
     245    { 
     246        $orig = Doctrine_Manager::getInstance()->getAttribute(Doctrine::ATTR_VALIDATE); 
     247        Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL); 
     248        try { 
     249            $test = new EnumTest(); 
     250            $test->status = 'opeerertn'; 
     251            $test->save(); 
     252            $this->fail(); 
     253        } catch (Exception $e) { 
     254            $this->pass(); 
     255        } 
     256        Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_VALIDATE, $orig); 
    242257    } 
    243258} 
  • branches/1.0/tests/Query/WhereTestCase.php

    r4572 r4768  
    263263        $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id FROM entity e LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id AND e2.type = 1 WHERE e2.name = 'some group' AND (e.type = 0)"); 
    264264    } 
    265     public function testEnumValuesWorkInPlaceholders() 
    266     { 
    267         $e = new EnumTest; 
    268         $e->status = 'verified'; 
    269         $e->save(); 
    270  
    271         $q = new Doctrine_Query(); 
    272          
    273         $q->select('e.*')->from('EnumTest e')->where('e.status = ?'); 
    274  
    275         $q->getQuery(); 
    276  
    277         $this->assertEqual(count($q->getEnumParams()), 1); 
    278  
    279         $q->execute(array('verified')); 
    280     } 
    281  
    282     public function testEnumValuesWorkWithMultiplePlaceholders() 
    283     { 
    284         $q = new Doctrine_Query(); 
    285  
    286         $q->select('e.*')->from('EnumTest e')->where('e.id = ? AND e.status = ?'); 
    287          
    288         $q->getQuery(); 
    289  
    290         $p = $q->getEnumParams(); 
    291         $this->assertEqual(array_keys($p), array(0, 1)); 
    292         $this->assertTrue(empty($p[0])); 
    293         $q->execute(array(1, 'verified')); 
    294     } 
    295  
    296     public function testEnumValuesWorkWithMultipleNamedPlaceholders() 
    297     { 
    298         $q = new Doctrine_Query(); 
    299  
    300         $q->select('e.*')->from('EnumTest e')->where('e.id = :id AND e.status = :status'); 
    301          
    302         $q->getQuery(); 
    303  
    304         $p = $q->getEnumParams(); 
    305         $this->assertEqual(array_keys($p), array(':id', ':status')); 
    306         $this->assertTrue(empty($p[':id'])); 
    307         $q->execute(array(1, 'verified')); 
    308     } 
    309265 
    310266    public function testLiteralValueAsInOperatorOperandIsSupported()