Changeset 4768
- Timestamp:
- 08/12/08 02:19:49 (11 months ago)
- Location:
- branches/1.0
- Files:
-
- 16 modified
-
lib/Doctrine/DataDict/Firebird.php (modified) (2 diffs)
-
lib/Doctrine/DataDict/Informix.php (modified) (1 diff)
-
lib/Doctrine/DataDict/Mssql.php (modified) (2 diffs)
-
lib/Doctrine/DataDict/Mysql.php (modified) (2 diffs)
-
lib/Doctrine/DataDict/Oracle.php (modified) (2 diffs)
-
lib/Doctrine/DataDict/Pgsql.php (modified) (2 diffs)
-
lib/Doctrine/DataDict/Sqlite.php (modified) (2 diffs)
-
lib/Doctrine/Query.php (modified) (4 diffs)
-
lib/Doctrine/Query/Abstract.php (modified) (5 diffs)
-
lib/Doctrine/Query/JoinCondition.php (modified) (4 diffs)
-
lib/Doctrine/Query/Where.php (modified) (3 diffs)
-
lib/Doctrine/RawSql.php (modified) (1 diff)
-
lib/Doctrine/Record.php (modified) (2 diffs)
-
lib/Doctrine/Table.php (modified) (1 diff)
-
tests/DataType/EnumTestCase.php (modified) (2 diffs)
-
tests/Query/WhereTestCase.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/1.0/lib/Doctrine/DataDict/Firebird.php
r4252 r4768 61 61 } 62 62 switch ($field['type']) { 63 case 'enum': 64 $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 63 65 case 'varchar': 64 66 case 'string': … … 79 81 return 'BLOB SUB_TYPE 0'; 80 82 case 'integer': 81 case 'enum':82 83 case 'int': 83 84 return 'INT'; -
branches/1.0/lib/Doctrine/DataDict/Informix.php
r4252 r4768 62 62 switch ($field['type']) { 63 63 case 'char': 64 case 'enum': 65 $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 64 66 case 'varchar': 65 67 case 'array': -
branches/1.0/lib/Doctrine/DataDict/Mssql.php
r4252 r4768 63 63 } 64 64 switch ($field['type']) { 65 case 'enum': 66 $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 65 67 case 'array': 66 68 case 'object': … … 94 96 return 'IMAGE'; 95 97 case 'integer': 96 case 'enum':97 98 case 'int': 98 99 return 'INT'; -
branches/1.0/lib/Doctrine/DataDict/Mysql.php
r4757 r4768 143 143 144 144 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 } 145 155 case 'varchar': 146 156 case 'array': … … 185 195 } 186 196 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 integer196 197 case 'integer': 197 198 case 'int': -
branches/1.0/lib/Doctrine/DataDict/Oracle.php
r4252 r4768 59 59 } 60 60 switch ($field['type']) { 61 case 'enum': 62 $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 61 63 case 'string': 62 64 case 'array': … … 76 78 return 'BLOB'; 77 79 case 'integer': 78 case 'enum':79 80 case 'int': 80 81 if ( ! empty($field['length'])) { -
branches/1.0/lib/Doctrine/DataDict/Pgsql.php
r4353 r4768 364 364 } 365 365 switch ($field['type']) { 366 case 'enum': 367 $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 366 368 case 'char': 367 369 case 'string': … … 382 384 case 'blob': 383 385 return 'BYTEA'; 384 case 'enum':385 386 case 'integer': 386 387 case 'int': -
branches/1.0/lib/Doctrine/DataDict/Sqlite.php
r4252 r4768 61 61 } 62 62 switch ($field['type']) { 63 case 'enum': 64 $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255; 63 65 case 'text': 64 66 case 'object': … … 98 100 } 99 101 return 'LONGBLOB'; 100 case 'enum':101 102 case 'integer': 102 103 case 'boolean': -
branches/1.0/lib/Doctrine/Query.php
r4735 r4768 237 237 238 238 /** 239 * addEnumParam240 * sets input parameter as an enumerated parameter241 *242 * @param string $key the key of the input parameter243 * @return Doctrine_Query244 */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 * getEnumParams258 * get all enumerated parameters259 *260 * @return array all enumerated parameters261 */262 public function getEnumParams()263 {264 return $this->_enumParams;265 }266 267 268 /**269 239 * fetchArray 270 240 * Convenience method to execute using array fetching as hydration mode. … … 1084 1054 } 1085 1055 $this->_state = self::STATE_CLEAN; 1086 1087 $params = $this->convertEnums($params);1088 1056 1089 1057 // Proceed with the generated SQL … … 1881 1849 $params = array_merge($this->_params['join'], $this->_params['where'], $this->_params['having'], $params); 1882 1850 1883 $params = $this->convertEnums($params);1884 1885 1851 $results = $this->getConnection()->fetchAll($q, $params); 1886 1852 … … 1955 1921 $this->_parsers = array(); 1956 1922 $this->_dqlParts = array(); 1957 $this->_enumParams = array();1958 1923 } 1959 1924 -
branches/1.0/lib/Doctrine/Query/Abstract.php
r4735 r4768 227 227 228 228 /** 229 * @var array $_enumParams an array containing the keys of the parameters that should be enumerated230 */231 protected $_enumParams = array();232 233 /**234 229 * @var boolean 235 230 */ 236 231 protected $_isLimitSubqueryUsed = false; 237 238 protected $_pendingSetParams = array();239 232 240 233 /** … … 579 572 { 580 573 return $this->_isLimitSubqueryUsed; 581 }582 583 /**584 * convertEnums585 * convert enum parameters to their integer equivalents586 *587 * @return array converted parameter array588 */589 public function convertEnums($params)590 {591 $table = $this->getRoot();592 593 // $position tracks the position of the parameter, to ensure we're converting594 // the right parameter value when simple ? placeholders are used.595 // This only works because SET is only allowed in update statements and it's596 // the first place where parameters can occur.. see issue #935597 $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;618 574 } 619 575 … … 958 914 $query = $this->getSqlQuery($params); 959 915 } 960 $params = $this->convertEnums($params);961 916 } else { 962 917 $query = $this->_view->getSelectSql(); … … 1257 1212 1258 1213 // 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 } 1261 1217 1262 1218 $a = array(); … … 1418 1374 } 1419 1375 } 1420 1421 $this->_pendingSetParams[$key] = $value;1422 1376 1423 1377 return $this->_addDqlQueryPart('set', $key . ' = ' . $value, true); -
branches/1.0/lib/Doctrine/Query/JoinCondition.php
r4715 r4768 72 72 $map = $this->query->getAliasDeclaration($reference); 73 73 $table = $map['table']; 74 // check if value is enumerated value75 $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 }80 74 81 75 // FIX: Issues with "(" XXX ")" … … 105 99 $value = array(); 106 100 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); 118 102 } 119 103 … … 123 107 } 124 108 } else { 125 if ($enumIndex !== false) { 126 $value = $enumIndex; 127 } else { 128 $value = $this->parseLiteralValue($value); 129 } 109 $value = $this->parseLiteralValue($value); 130 110 } 131 111 … … 135 115 case '=': 136 116 case '!=': 137 if ($enumIndex !== false) {138 $value = $enumIndex;139 }140 117 default: 141 118 $leftExpr = (($hasLeftAggExpression) ? $leftMatches[1] . '(' : '') -
branches/1.0/lib/Doctrine/Query/Where.php
r4252 r4768 91 91 $conn = $this->query->getConnection(); 92 92 93 // If value is contained in paranthesis 93 94 if (substr($value, 0, 1) == '(') { 94 95 // trim brackets 95 96 $trimmed = $this->_tokenizer->bracketTrim($value); 96 97 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) 97 100 if (substr($trimmed, 0, 4) == 'FROM' || 98 101 substr($trimmed, 0, 6) == 'SELECT') { … … 102 105 $value = '(' . $this->query->createSubquery()->parseQuery($trimmed, false)->getQuery() . ')'; 103 106 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) 104 110 } elseif (substr($trimmed, 0, 4) == 'SQL:') { 105 111 $value = '(' . substr($trimmed, 4) . ')'; 112 // simple in expression found 106 113 } else { 107 // simple in expression found108 114 $e = $this->_tokenizer->sqlExplode($trimmed, ','); 109 115 … … 113 119 114 120 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); 128 122 } 129 123 130 124 $value = '(' . implode(', ', $value) . ')'; 131 125 } 132 } else if (substr($value, 0, 1) == ':' || $value === '?') {133 // placeholder found134 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 }139 126 } 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); 155 128 } 156 129 return $value; -
branches/1.0/lib/Doctrine/RawSql.php
r4714 r4768 321 321 $params = array_merge($this->_params['join'], $this->_params['where'], $this->_params['having'], $params); 322 322 323 $params = $this->convertEnums($params);324 325 323 $results = $this->getConnection()->fetchAll($q, $params); 326 324 -
branches/1.0/lib/Doctrine/Record.php
r4722 r4768 593 593 $vars['_data'][$k] = gzcompress($vars['_data'][$k]); 594 594 break; 595 case 'enum':596 $vars['_data'][$k] = $this->_table->enumIndex($k, $vars['_data'][$k]);597 break;598 595 } 599 596 } … … 1241 1238 $a[$field] = $this->getTable()->getConnection()->convertBooleans($this->_data[$field]); 1242 1239 break; 1243 case 'enum':1244 $a[$field] = $this->_table->enumIndex($field, $this->_data[$field]);1245 break;1246 1240 default: 1247 1241 if ($this->_data[$field] instanceof Doctrine_Record) { -
branches/1.0/lib/Doctrine/Table.php
r4726 r4768 609 609 610 610 switch ($definition['type']) { 611 case 'enum':612 if (isset($definition['default'])) {613 $definition['default'] = $this->enumIndex($name, $definition['default']);614 }615 break;616 611 case 'boolean': 617 612 if (isset($definition['default'])) { -
branches/1.0/tests/DataType/EnumTestCase.php
r4177 r4768 106 106 $this->assertEqual($ret, 1); 107 107 } catch (Exception $e) { 108 $this->fail( );108 $this->fail($e->getMessage()); 109 109 } 110 110 } … … 239 239 ->execute(); 240 240 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); 242 257 } 243 258 } -
branches/1.0/tests/Query/WhereTestCase.php
r4572 r4768 263 263 $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)"); 264 264 } 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 }309 265 310 266 public function testLiteralValueAsInOperatorOperandIsSupported()