Minor optimizations in DboMysql.

This commit is contained in:
Juan Basso 2011-02-25 17:22:15 -03:00
parent 73b3b65a53
commit fea2b5798c
2 changed files with 20 additions and 22 deletions

View file

@ -209,21 +209,19 @@ class DboMysql extends DboSource {
$this->map = array(); $this->map = array();
$numFields = $results->columnCount(); $numFields = $results->columnCount();
$index = 0; $index = 0;
$j = 0;
while ($j < $numFields) { while ($numFields-- > 0) {
$column = $results->getColumnMeta($j); $column = $results->getColumnMeta($index);
if (!empty($column['native_type'])) { if (empty($column['native_type'])) {
$type = $column['native_type'];
} else {
$type = ($column['len'] == 1) ? 'boolean' : 'string'; $type = ($column['len'] == 1) ? 'boolean' : 'string';
} else {
$type = $column['native_type'];
} }
if (!empty($column['table']) && strpos($column['name'], $this->virtualFieldSeparator) === false) { if (!empty($column['table']) && strpos($column['name'], $this->virtualFieldSeparator) === false) {
$this->map[$index++] = array($column['table'], $column['name'], $type); $this->map[$index++] = array($column['table'], $column['name'], $type);
} else { } else {
$this->map[$index++] = array(0, $column['name'], $type); $this->map[$index++] = array(0, $column['name'], $type);
} }
$j++;
} }
} }
@ -238,7 +236,7 @@ class DboMysql extends DboSource {
foreach ($this->map as $col => $meta) { foreach ($this->map as $col => $meta) {
list($table, $column, $type) = $meta; list($table, $column, $type) = $meta;
$resultRow[$table][$column] = $row[$col]; $resultRow[$table][$column] = $row[$col];
if ($type == 'boolean' && !is_null($row[$col])) { if ($type === 'boolean' && !is_null($row[$col])) {
$resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]); $resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]);
} }
} }
@ -304,7 +302,7 @@ class DboMysql extends DboSource {
foreach ($cols as $column) { foreach ($cols as $column) {
$fields[$column->Field] = array( $fields[$column->Field] = array(
'type' => $this->column($column->Type), 'type' => $this->column($column->Type),
'null' => ($column->Null == 'YES' ? true : false), 'null' => ($column->Null === 'YES' ? true : false),
'default' => $column->Default, 'default' => $column->Default,
'length' => $this->length($column->Type), 'length' => $this->length($column->Type),
); );
@ -365,7 +363,7 @@ class DboMysql extends DboSource {
return false; return false;
} }
if (!$this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions')))) { if ($this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions'))) === false) {
$model->onError(); $model->onError();
return false; return false;
} }
@ -409,7 +407,7 @@ class DboMysql extends DboSource {
* @param string $enc Database encoding * @param string $enc Database encoding
*/ */
function setEncoding($enc) { function setEncoding($enc) {
return $this->_execute('SET NAMES ' . $enc) != false; return $this->_execute('SET NAMES ' . $enc) !== false;
} }
/** /**
@ -499,7 +497,7 @@ class DboMysql extends DboSource {
} }
$colList = array_merge($colList, $this->_alterIndexes($curTable, $indexes)); $colList = array_merge($colList, $this->_alterIndexes($curTable, $indexes));
$colList = array_merge($colList, $this->_alterTableParameters($curTable, $tableParameters)); $colList = array_merge($colList, $this->_alterTableParameters($curTable, $tableParameters));
$out .= "\t" . join(",\n\t", $colList) . ";\n\n"; $out .= "\t" . implode(",\n\t", $colList) . ";\n\n";
} }
} }
return $out; return $out;
@ -516,7 +514,7 @@ class DboMysql extends DboSource {
function dropSchema(CakeSchema $schema, $table = null) { function dropSchema(CakeSchema $schema, $table = null) {
$out = ''; $out = '';
foreach ($schema->tables as $curTable => $columns) { foreach ($schema->tables as $curTable => $columns) {
if (!$table || $table == $curTable) { if (!$table || $table === $curTable) {
$out .= 'DROP TABLE IF EXISTS ' . $this->fullTableName($curTable) . ";\n"; $out .= 'DROP TABLE IF EXISTS ' . $this->fullTableName($curTable) . ";\n";
} }
} }
@ -628,7 +626,7 @@ class DboMysql extends DboSource {
if (is_array($real)) { if (is_array($real)) {
$col = $real['name']; $col = $real['name'];
if (isset($real['limit'])) { if (isset($real['limit'])) {
$col .= '('.$real['limit'].')'; $col .= '(' . $real['limit'] . ')';
} }
return $col; return $col;
} }
@ -642,19 +640,19 @@ class DboMysql extends DboSource {
if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) { if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
return $col; return $col;
} }
if (($col == 'tinyint' && $limit == 1) || $col == 'boolean') { if (($col === 'tinyint' && $limit == 1) || $col === 'boolean') {
return 'boolean'; return 'boolean';
} }
if (strpos($col, 'int') !== false) { if (strpos($col, 'int') !== false) {
return 'integer'; return 'integer';
} }
if (strpos($col, 'char') !== false || $col == 'tinytext') { if (strpos($col, 'char') !== false || $col === 'tinytext') {
return 'string'; return 'string';
} }
if (strpos($col, 'text') !== false) { if (strpos($col, 'text') !== false) {
return 'text'; return 'text';
} }
if (strpos($col, 'blob') !== false || $col == 'binary') { if (strpos($col, 'blob') !== false || $col === 'binary') {
return 'binary'; return 'binary';
} }
if (strpos($col, 'float') !== false || strpos($col, 'double') !== false || strpos($col, 'decimal') !== false) { if (strpos($col, 'float') !== false || strpos($col, 'double') !== false || strpos($col, 'decimal') !== false) {

View file

@ -2744,7 +2744,7 @@ class DboSource extends DataSource {
if (strpos($col, '(') !== false) { if (strpos($col, '(') !== false) {
list($col, $limit) = explode('(', $col); list($col, $limit) = explode('(', $col);
} }
if ($limit != null) { if ($limit !== null) {
return intval($limit); return intval($limit);
} }
return null; return null;
@ -2762,10 +2762,10 @@ class DboSource extends DataSource {
$isFloat = in_array($type, array('dec', 'decimal', 'float', 'numeric', 'double')); $isFloat = in_array($type, array('dec', 'decimal', 'float', 'numeric', 'double'));
if ($isFloat && $offset) { if ($isFloat && $offset) {
return $length.','.$offset; return $length . ',' . $offset;
} }
if (($real[0] == $type) && (count($real) == 1)) { if (($real[0] == $type) && (count($real) === 1)) {
return null; return null;
} }
@ -2777,7 +2777,7 @@ class DboSource extends DataSource {
} elseif (in_array($type, array('enum', 'set'))) { } elseif (in_array($type, array('enum', 'set'))) {
$length = 0; $length = 0;
foreach ($typeArr as $key => $enumValue) { foreach ($typeArr as $key => $enumValue) {
if ($key == 0) { if ($key === 0) {
continue; continue;
} }
$tmpLength = strlen($enumValue); $tmpLength = strlen($enumValue);