Skip to content

Commit

Permalink
Fixing DboSource not quoting table/field names with - in them. Tests …
Browse files Browse the repository at this point in the history
…expanded . Fixes #323
  • Loading branch information
markstory committed Feb 20, 2010
1 parent 416abed commit 7075aa5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
11 changes: 4 additions & 7 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -472,9 +472,6 @@ function name($data) {
if (is_object($data) && isset($data->type)) {
return $data->value;
}
if ($data == '*') {
return '*';
}
if ($data === '*') {
return '*';
}
Expand All @@ -492,20 +489,20 @@ function name($data) {
return $this->methodCache[__FUNCTION__][$cacheKey];
}
$data = trim($data);
if (preg_match('/^\w+(\.\w+)*$/', $data)) { // string, string.string
if (preg_match('/^[\w-]+(\.[\w-]+)*$/', $data)) { // string, string.string
if (strpos($data, '.') === false) { // string
return $this->startQuote . $data . $this->endQuote;
}
$items = explode('.', $data);
return $this->startQuote . implode($this->endQuote . '.' . $this->startQuote, $items) . $this->endQuote;
}
if (preg_match('/^\w+\.\*$/', $data)) { // string.*
if (preg_match('/^[\w-]+\.\*$/', $data)) { // string.*
return $this->startQuote . str_replace('.*', $this->endQuote . '.*', $data);
}
if (preg_match('/^(\w+)\((.*)\)$/', $data, $matches)) { // Functions
if (preg_match('/^([\w-]+)\((.*)\)$/', $data, $matches)) { // Functions
return $matches[1] . '(' . $this->name($matches[2]) . ')';
}
if (preg_match('/^(\w+(\.\w+|\(.*\))*)\s+' . preg_quote($this->alias) . '\s*(\w+)$/', $data, $matches)) {
if (preg_match('/^([\w-]+(\.[\w-]+|\(.*\))*)\s+' . preg_quote($this->alias) . '\s*([\w-]+)$/', $data, $matches)) {
return preg_replace('/\s{2,}/', ' ', $this->name($matches[1]) . ' ' . $this->alias . ' ' . $this->name($matches[3]));
}
return $this->methodCache[__FUNCTION__][$cacheKey] = $data;
Expand Down
4 changes: 4 additions & 0 deletions cake/tests/cases/libs/model/datasources/dbo_source.test.php
Expand Up @@ -4037,6 +4037,10 @@ function testName() {
$result = $this->testDb->name('name-with-minus');
$expected = '`name-with-minus`';
$this->assertEqual($result, $expected);

$result = $this->testDb->name(array('my-name', 'Foo-Model.*'));
$expected = array('`my-name`', '`Foo-Model`.*');
$this->assertEqual($result, $expected);
}

/**
Expand Down

0 comments on commit 7075aa5

Please sign in to comment.