Refactoring Router, fixed coding standards. Fixed quoting issue in DboSource::name(), affecting quoting of fields starting and ending with ()

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7485 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-08-22 04:06:29 +00:00
parent 4db90553ec
commit dbbbccdf18
3 changed files with 48 additions and 67 deletions

View file

@ -385,7 +385,6 @@ class DboSource extends DataSource {
return '*'; return '*';
} }
$array = is_array($data); $array = is_array($data);
$data = (array)$data; $data = (array)$data;
$count = count($data); $count = count($data);
@ -395,6 +394,7 @@ class DboSource extends DataSource {
} }
if (strpos($data[$i], '(') !== false && preg_match_all('/([^(]*)\((.*)\)(.*)/', $data[$i], $fields)) { if (strpos($data[$i], '(') !== false && preg_match_all('/([^(]*)\((.*)\)(.*)/', $data[$i], $fields)) {
$fields = Set::extract($fields, '{n}.0'); $fields = Set::extract($fields, '{n}.0');
if (!empty($fields[1])) { if (!empty($fields[1])) {
if (!empty($fields[2])) { if (!empty($fields[2])) {
$data[$i] = $fields[1] . '(' . $this->name($fields[2]) . ')' . $fields[3]; $data[$i] = $fields[1] . '(' . $this->name($fields[2]) . ')' . $fields[3];
@ -403,12 +403,15 @@ class DboSource extends DataSource {
} }
} }
} }
$data[$i] = $this->startQuote . str_replace('.', $this->endQuote . '.' . $this->startQuote, $data[$i]) . $this->endQuote; $data[$i] = str_replace('.', $this->endQuote . '.' . $this->startQuote, $data[$i]);
$data[$i] = $this->startQuote . $data[$i] . $this->endQuote;
$data[$i] = str_replace($this->startQuote . $this->startQuote, $this->startQuote, $data[$i]); $data[$i] = str_replace($this->startQuote . $this->startQuote, $this->startQuote, $data[$i]);
$data[$i] = str_replace($this->startQuote . '(', '(', $data[$i]);
$data[$i] = str_replace(')' . $this->startQuote, ')', $data[$i]);
if (strpos($data[$i], ' AS ')) { if (strpos($data[$i], ' AS ')) {
$data[$i] = str_replace(' AS ', $this->endQuote . ' AS ' . $this->startQuote, $data[$i]); $data[$i] = str_replace(' AS ', $this->endQuote . ' AS ' . $this->startQuote, $data[$i]);
} }
if (!empty($this->endQuote) && $this->endQuote == $this->startQuote) { if (!empty($this->endQuote) && $this->endQuote == $this->startQuote) {
if (substr_count($data[$i], $this->endQuote) % 2 == 1) { if (substr_count($data[$i], $this->endQuote) % 2 == 1) {
$data[$i] = trim($data[$i], $this->endQuote); $data[$i] = trim($data[$i], $this->endQuote);
@ -419,10 +422,7 @@ class DboSource extends DataSource {
} }
$data[$i] = str_replace($this->endQuote . $this->endQuote, $this->endQuote, $data[$i]); $data[$i] = str_replace($this->endQuote . $this->endQuote, $this->endQuote, $data[$i]);
} }
if (!$array) { return (!$array) ? $data[0] : $data;
return $data[0];
}
return $data;
} }
/** /**
* Checks if it's connected to the database * Checks if it's connected to the database
@ -1992,50 +1992,34 @@ class DboSource extends DataSource {
if (empty($keys) || (is_array($keys) && count($keys) && isset($keys[0]) && empty($keys[0]))) { if (empty($keys) || (is_array($keys) && count($keys) && isset($keys[0]) && empty($keys[0]))) {
return ''; return '';
} }
$flag = (isset($keys[0]) && $keys[0] == '(Model.field > 100) DESC');
if (is_array($keys)) { if (is_array($keys)) {
if (Set::countDim($keys) > 1) { $keys = (Set::countDim($keys) > 1) ? array_map(array(&$this, 'order'), $keys) : $keys;
$new = array();
foreach ($keys as $val) {
$val = $this->order($val);
$new[] = $val;
}
$keys = $new;
}
foreach ($keys as $key => $value) { foreach ($keys as $key => $value) {
if (is_numeric($key)) { if (is_numeric($key)) {
$value = ltrim(str_replace('ORDER BY ', '', $this->order($value))); $key = $value = ltrim(str_replace('ORDER BY ', '', $this->order($value)));
$key = $value; $value = (!preg_match('/\\x20ASC|\\x20DESC/i', $key) ? ' ' . $direction : '');
if (!preg_match('/\\x20ASC|\\x20DESC/i', $key)) {
$value = ' ' . $direction;
} else {
$value = '';
}
} else { } else {
$value = ' ' . $value; $value = ' ' . $value;
} }
if (!preg_match('/^.+\\(.*\\)/', $key) && !strpos($key, ',')) { if (!preg_match('/^.+\\(.*\\)/', $key) && !strpos($key, ',')) {
$dir = ''; if (preg_match('/\\x20ASC|\\x20DESC/i', $key, $dir)) {
$hasDir = preg_match('/\\x20ASC|\\x20DESC/i', $key, $dir);
if ($hasDir) {
$dir = $dir[0]; $dir = $dir[0];
$key = preg_replace('/\\x20ASC|\\x20DESC/i', '', $key); $key = preg_replace('/\\x20ASC|\\x20DESC/i', '', $key);
} else { } else {
$dir = ''; $dir = '';
} }
Configure::write('flag', $flag);
$key = trim($this->name(trim($key)) . ' ' . trim($dir)); $key = trim($this->name(trim($key)) . ' ' . trim($dir));
Configure::write('flag', false);
} }
$order[] = $this->order($key . $value); $order[] = $this->order($key . $value);
} }
return ' ORDER BY ' . trim(str_replace('ORDER BY', '', join(',', $order))); return ' ORDER BY ' . trim(str_replace('ORDER BY', '', join(',', $order)));
} else { }
$keys = preg_replace('/ORDER\\x20BY/i', '', $keys); $keys = preg_replace('/ORDER\\x20BY/i', '', $keys);
if (strpos($keys, '.')) { if (strpos($keys, '.')) {
@ -2045,21 +2029,14 @@ class DboSource extends DataSource {
for ($i = 0; $i < $pregCount; $i++) { for ($i = 0; $i < $pregCount; $i++) {
$keys = preg_replace('/' . $result['0'][$i] . '/', $this->name($result['0'][$i]), $keys); $keys = preg_replace('/' . $result['0'][$i] . '/', $this->name($result['0'][$i]), $keys);
} }
$result = ' ORDER BY ' . $keys;
return $result . (!preg_match('/\\x20ASC|\\x20DESC/i', $keys) ? ' ' . $direction : '');
if (preg_match('/\\x20ASC|\\x20DESC/i', $keys)) {
return ' ORDER BY ' . $keys;
} else {
return ' ORDER BY ' . $keys . ' ' . $direction;
}
} elseif (preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match)) { } elseif (preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match)) {
$direction = $match['1']; $direction = $match['1'];
$keys = preg_replace('/' . $match['1'] . '/', '', $keys); return ' ORDER BY ' . preg_replace('/' . $match['1'] . '/', '', $keys) . $direction;
return ' ORDER BY ' . $keys . $direction;
} else {
$direction = ' ' . $direction;
}
return ' ORDER BY ' . $keys . $direction;
} }
return ' ORDER BY ' . $keys . ' ' . $direction;
} }
/** /**
* Create a GROUP BY SQL clause * Create a GROUP BY SQL clause

View file

@ -203,12 +203,13 @@ class Router extends Object {
function connect($route, $default = array(), $params = array()) { function connect($route, $default = array(), $params = array()) {
$_this =& Router::getInstance(); $_this =& Router::getInstance();
$admin = Configure::read('Routing.admin'); $admin = Configure::read('Routing.admin');
$default = array_merge(array('action' => 'index'), $default);
if (!isset($default['action'])) {
$default['action'] = 'index';
}
if (isset($default[$admin])) { if (isset($default[$admin])) {
$default['prefix'] = $admin; $default['prefix'] = $admin;
} }
if (isset($default['prefix'])) { if (isset($default['prefix'])) {
$_this->__prefixes[] = $default['prefix']; $_this->__prefixes[] = $default['prefix'];
$_this->__prefixes = array_keys(array_flip($_this->__prefixes)); $_this->__prefixes = array_keys(array_flip($_this->__prefixes));
@ -298,10 +299,8 @@ class Router extends Object {
foreach ($_this->__resourceMap as $params) { foreach ($_this->__resourceMap as $params) {
extract($params); extract($params);
$url = $prefix . $urlName; $url = $prefix . $urlName . (($id) ? '/:id' : '');
if ($id) {
$url .= '/:id';
}
Router::connect($url, Router::connect($url,
array('controller' => $urlName, 'action' => $action, '[method]' => $params['method']), array('controller' => $urlName, 'action' => $action, '[method]' => $params['method']),
array('id' => $options['id'], 'pass' => array('id')) array('id' => $options['id'], 'pass' => array('id'))
@ -421,6 +420,7 @@ class Router extends Object {
$url = substr($url, 0, strpos($url, '?')); $url = substr($url, 0, strpos($url, '?'));
} }
extract($_this->__parseExtension($url)); extract($_this->__parseExtension($url));
foreach ($_this->routes as $route) { foreach ($_this->routes as $route) {
if (($r = $_this->matchRoute($route, $url)) !== false) { if (($r = $_this->matchRoute($route, $url)) !== false) {
$_this->__currentRoute[] = $route; $_this->__currentRoute[] = $route;

View file

@ -3608,11 +3608,15 @@ class DboSourceTest extends CakeTestCase {
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->testDb->name('MTD()'); $result = $this->testDb->name('MTD()');
$expected = '`MTD()`'; $expected = 'MTD()';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->testDb->name('(sm)'); $result = $this->testDb->name('(sm)');
$expected = '`(sm)`'; $expected = '(sm)';
$this->assertEqual($result, $expected);
$result = $this->testDb->name('Function(Something.foo)');
$expected = 'Function(`Something`.`foo`)';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
/** /**
@ -3676,6 +3680,6 @@ class DboSourceTest extends CakeTestCase {
$this->testDb->error = $oldError; $this->testDb->error = $oldError;
Configure::write('debug', $oldDebug); Configure::write('debug', $oldDebug);
} }
} }
?> ?>