diff --git a/cake/console/shells/schema.php b/cake/console/shells/schema.php index dc863f6e5..157377482 100644 --- a/cake/console/shells/schema.php +++ b/cake/console/shells/schema.php @@ -299,7 +299,7 @@ class SchemaShell extends Shell { * * @access private */ - function __create(&$Schema, $table = null) { + function __create($Schema, $table = null) { $db = ConnectionManager::getDataSource($this->Schema->connection); $drop = $create = array(); diff --git a/cake/console/shells/tasks/db_config.php b/cake/console/shells/tasks/db_config.php index d7e5a20c0..a98b6b053 100644 --- a/cake/console/shells/tasks/db_config.php +++ b/cake/console/shells/tasks/db_config.php @@ -106,7 +106,7 @@ class DbConfigTask extends Shell { } } - $driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'mysqli', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql'); + $driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql'); $persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n'); if (strtolower($persistent) == 'n') { diff --git a/cake/console/templates/skel/config/database.php.default b/cake/console/templates/skel/config/database.php.default index 2a605e498..8c7e39661 100644 --- a/cake/console/templates/skel/config/database.php.default +++ b/cake/console/templates/skel/config/database.php.default @@ -31,7 +31,6 @@ * * driver => The name of a supported driver; valid options are as follows: * mysql - MySQL 4 & 5, - * mysqli - MySQL 4 & 5 Improved Interface (PHP5 only), * sqlite - SQLite (PHP5 only), * postgres - PostgreSQL 7 and higher, * mssql - Microsoft SQL Server 2000 and higher, @@ -67,7 +66,7 @@ * 'public', DB2 defaults to empty. * * encoding => - * For MySQL, MySQLi, Postgres and DB2, specifies the character encoding to use when connecting to the + * For MySQL, Postgres and DB2, specifies the character encoding to use when connecting to the * database. Uses database default. * */ diff --git a/cake/libs/model/behaviors/translate.php b/cake/libs/model/behaviors/translate.php index d7ea5ecf0..563b9ac15 100644 --- a/cake/libs/model/behaviors/translate.php +++ b/cake/libs/model/behaviors/translate.php @@ -89,6 +89,7 @@ class TranslateBehavior extends ModelBehavior { * @return array Modified query */ public function beforeFind(&$model, $query) { + $this->runtime[$model->alias]['virtualFields'] = $model->virtualFields; $locale = $this->_getLocale($model); if (empty($locale)) { return $query; @@ -115,48 +116,28 @@ class TranslateBehavior extends ModelBehavior { ); return $query; } - $autoFields = false; - if (empty($query['fields'])) { - $query['fields'] = array($model->alias.'.*'); - - $recursive = $model->recursive; - if (isset($query['recursive'])) { - $recursive = $query['recursive']; - } - - if ($recursive >= 0) { - foreach (array('hasOne', 'belongsTo') as $type) { - foreach ($model->{$type} as $key => $value) { - - if (empty($value['fields'])) { - $query['fields'][] = $key.'.*'; - } else { - foreach ($value['fields'] as $field) { - $query['fields'][] = $key.'.'.$field; - } - } - } - } - } - $autoFields = true; - } $fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']); $addFields = array(); - if (is_array($query['fields'])) { + if (empty($query['fields'])) { + $addFields = $fields; + } else if (is_array($query['fields'])) { foreach ($fields as $key => $value) { $field = (is_numeric($key)) ? $value : $key; - if (in_array($model->alias.'.*', $query['fields']) || $autoFields || in_array($model->alias.'.'.$field, $query['fields']) || in_array($field, $query['fields'])) { + if (in_array($model->alias.'.*', $query['fields']) || in_array($model->alias.'.'.$field, $query['fields']) || in_array($field, $query['fields'])) { $addFields[] = $field; } } } + $this->runtime[$model->alias]['virtualFields'] = $model->virtualFields; if ($addFields) { - foreach ($addFields as $field) { - foreach (array($field, $model->alias.'.'.$field) as $_field) { - $key = array_search($_field, $query['fields']); + foreach ($addFields as $_f => $field) { + $aliasField = is_numeric($_f) ? $field : $_f; + + foreach (array($aliasField, $model->alias.'.'.$aliasField) as $_field) { + $key = array_search($_field, (array)$query['fields']); if ($key !== false) { unset($query['fields'][$key]); @@ -165,7 +146,10 @@ class TranslateBehavior extends ModelBehavior { if (is_array($locale)) { foreach ($locale as $_locale) { - $query['fields'][] = 'I18n__'.$field.'__'.$_locale.'.content'; + $model->virtualFields['i18n_'.$field.'_'.$_locale] = 'I18n__'.$field.'__'.$_locale.'.content'; + if (!empty($query['fields'])) { + $query['fields'][] = 'i18n_'.$field.'_'.$_locale; + } $query['joins'][] = array( 'type' => 'LEFT', 'alias' => 'I18n__'.$field.'__'.$_locale, @@ -173,13 +157,16 @@ class TranslateBehavior extends ModelBehavior { 'conditions' => array( $model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}__{$_locale}.foreign_key"), 'I18n__'.$field.'__'.$_locale.'.model' => $model->name, - 'I18n__'.$field.'__'.$_locale.'.'.$RuntimeModel->displayField => $field, + 'I18n__'.$field.'__'.$_locale.'.'.$RuntimeModel->displayField => $aliasField, 'I18n__'.$field.'__'.$_locale.'.locale' => $_locale ) ); } } else { - $query['fields'][] = 'I18n__'.$field.'.content'; + $model->virtualFields['i18n_'.$field] = 'I18n__'.$field.'.content'; + if (!empty($query['fields'])) { + $query['fields'][] = 'i18n_'.$field; + } $query['joins'][] = array( 'type' => 'LEFT', 'alias' => 'I18n__'.$field, @@ -187,7 +174,7 @@ class TranslateBehavior extends ModelBehavior { 'conditions' => array( $model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}.foreign_key"), 'I18n__'.$field.'.model' => $model->name, - 'I18n__'.$field.'.'.$RuntimeModel->displayField => $field + 'I18n__'.$field.'.'.$RuntimeModel->displayField => $aliasField ) ); @@ -199,9 +186,6 @@ class TranslateBehavior extends ModelBehavior { } } } - if (is_array($query['fields'])) { - $query['fields'] = array_merge($query['fields']); - } $this->runtime[$model->alias]['beforeFind'] = $addFields; return $query; } @@ -215,7 +199,8 @@ class TranslateBehavior extends ModelBehavior { * @return array Modified results */ public function afterFind(&$model, $results, $primary) { - $this->runtime[$model->alias]['fields'] = array(); + $model->virtualFields = $this->runtime[$model->alias]['virtualFields']; + $this->runtime[$model->alias]['virtualFields'] = $this->runtime[$model->alias]['fields'] = array(); $locale = $this->_getLocale($model); if (empty($locale) || empty($results) || empty($this->runtime[$model->alias]['beforeFind'])) { @@ -223,28 +208,30 @@ class TranslateBehavior extends ModelBehavior { } $beforeFind = $this->runtime[$model->alias]['beforeFind']; - foreach ($results as $key => $row) { - $results[$key][$model->alias]['locale'] = (is_array($locale)) ? @$locale[0] : $locale; + foreach ($results as $key => &$row) { + $results[$key][$model->alias]['locale'] = (is_array($locale)) ? current($locale) : $locale; + foreach ($beforeFind as $_f => $field) { + $aliasField = is_numeric($_f) ? $field : $_f; - foreach ($beforeFind as $field) { if (is_array($locale)) { foreach ($locale as $_locale) { - if (!isset($results[$key][$model->alias][$field]) && !empty($results[$key]['I18n__'.$field.'__'.$_locale]['content'])) { - $results[$key][$model->alias][$field] = $results[$key]['I18n__'.$field.'__'.$_locale]['content']; + if (!isset($row[$model->alias][$aliasField]) && !empty($row[$model->alias]['i18n_'.$field.'_'.$_locale])) { + $row[$model->alias][$aliasField] = $row[$model->alias]['i18n_'.$field.'_'.$_locale]; + $row[$model->alias]['locale'] = $_locale; } - unset($results[$key]['I18n__'.$field.'__'.$_locale]); + unset($row[$model->alias]['i18n_'.$field.'_'.$_locale]); } - if (!isset($results[$key][$model->alias][$field])) { - $results[$key][$model->alias][$field] = ''; + if (!isset($row[$model->alias][$aliasField])) { + $row[$model->alias][$aliasField] = ''; } } else { $value = ''; - if (!empty($results[$key]['I18n__'.$field]['content'])) { - $value = $results[$key]['I18n__'.$field]['content']; + if (!empty($row[$model->alias]['i18n_' . $field])) { + $value = $row[$model->alias]['i18n_' . $field]; } - $results[$key][$model->alias][$field] = $value; - unset($results[$key]['I18n__'.$field]); + $row[$model->alias][$aliasField] = $value; + unset($row[$model->alias]['i18n_' . $field]); } } } @@ -380,8 +367,7 @@ class TranslateBehavior extends ModelBehavior { } elseif (empty($model->translateTable) && empty($model->translateModel)) { $this->runtime[$model->alias]['model']->setSource('i18n'); } - $model =& $this->runtime[$model->alias]['model']; - return $model; + return $this->runtime[$model->alias]['model']; } /** diff --git a/cake/libs/model/behaviors/tree.php b/cake/libs/model/behaviors/tree.php index 51510538d..991d4fd1a 100644 --- a/cake/libs/model/behaviors/tree.php +++ b/cake/libs/model/behaviors/tree.php @@ -65,7 +65,7 @@ class TreeBehavior extends ModelBehavior { if (in_array($settings['scope'], $Model->getAssociated('belongsTo'))) { $data = $Model->getAssociated($settings['scope']); - $parent =& $Model->{$settings['scope']}; + $parent = $Model->{$settings['scope']}; $settings['scope'] = $Model->alias . '.' . $data['foreignKey'] . ' = ' . $parent->alias . '.' . $parent->primaryKey; $settings['recursive'] = 0; } @@ -599,7 +599,7 @@ class TreeBehavior extends ModelBehavior { $this->_setParent($Model, $array[$Model->alias][$parent]); } } else { - $db =& ConnectionManager::getDataSource($Model->useDbConfig); + $db = ConnectionManager::getDataSource($Model->useDbConfig); foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) { $path = $this->getPath($Model, $array[$Model->alias][$Model->primaryKey]); if ($path == null || count($path) < 2) { @@ -702,7 +702,7 @@ class TreeBehavior extends ModelBehavior { $parentNode[$right] = $node[$right] + 1; } - $db =& ConnectionManager::getDataSource($Model->useDbConfig); + $db = ConnectionManager::getDataSource($Model->useDbConfig); $Model->updateAll( array($parent => $db->value($node[$parent], $parent)), array($Model->escapeField($parent) => $node[$Model->primaryKey]) @@ -888,7 +888,7 @@ class TreeBehavior extends ModelBehavior { * @access private */ function __getMax($Model, $scope, $right, $recursive = -1, $created = false) { - $db =& ConnectionManager::getDataSource($Model->useDbConfig); + $db = ConnectionManager::getDataSource($Model->useDbConfig); if ($created) { if (is_string($scope)) { $scope .= " AND {$Model->alias}.{$Model->primaryKey} <> "; @@ -916,7 +916,7 @@ class TreeBehavior extends ModelBehavior { * @access private */ function __getMin($Model, $scope, $left, $recursive = -1) { - $db =& ConnectionManager::getDataSource($Model->useDbConfig); + $db = ConnectionManager::getDataSource($Model->useDbConfig); $name = $Model->alias . '.' . $left; list($edge) = array_values($Model->find('first', array( 'conditions' => $scope, diff --git a/cake/libs/model/cake_schema.php b/cake/libs/model/cake_schema.php index 360de5717..1ebf8ddaa 100644 --- a/cake/libs/model/cake_schema.php +++ b/cake/libs/model/cake_schema.php @@ -95,11 +95,7 @@ class CakeSchema extends Object { } if (empty($options['path'])) { - if (is_dir(CONFIGS . 'schema')) { - $this->path = CONFIGS . 'schema'; - } else { - $this->path = CONFIGS . 'sql'; - } + $this->path = CONFIGS . 'schema'; } $options = array_merge(get_object_vars($this), $options); @@ -161,7 +157,7 @@ class CakeSchema extends Object { * @param array $options schema object properties * @return array Set of name and tables */ - public function &load($options = array()) { + public function load($options = array()) { if (is_string($options)) { $options = array('path' => $options); } @@ -183,8 +179,8 @@ class CakeSchema extends Object { $Schema = new $class($options); return $Schema; } - $false = false; - return $false; + + return false; } /** @@ -299,7 +295,9 @@ class CakeSchema extends Object { $systemTables = array( 'aros', 'acos', 'aros_acos', Configure::read('Session.table'), 'i18n' ); - + if (get_class($Object) === 'AppModel') { + continue; + } if (in_array($table, $systemTables)) { $tables[$Object->table] = $this->__columns($Object); $tables[$Object->table]['indexes'] = $db->index($Object); diff --git a/cake/libs/model/datasources/dbo/dbo_mysql.php b/cake/libs/model/datasources/dbo/dbo_mysql.php index cff28d572..069cf089a 100644 --- a/cake/libs/model/datasources/dbo/dbo_mysql.php +++ b/cake/libs/model/datasources/dbo/dbo_mysql.php @@ -19,19 +19,42 @@ */ /** - * Provides common base for MySQL & MySQLi connections + * MySQL DBO driver object + * + * Provides connection and SQL generation for MySQL RDMS * * @package cake * @subpackage cake.cake.libs.model.datasources.dbo */ -class DboMysqlBase extends DboSource { +class DboMysql extends DboSource { /** - * Description property. + * Datasource description * * @var string */ - public $description = "MySQL DBO Base Driver"; + public $description = "MySQL DBO Driver"; + +/** + * Base configuration settings for MySQL driver + * + * @var array + */ + protected $_baseConfig = array( + 'persistent' => true, + 'host' => 'localhost', + 'login' => 'root', + 'password' => '', + 'database' => 'cake', + 'port' => '3306' + ); + +/** + * Reference to the PDO object connection + * + * @var PDO $_connection + */ + protected $_connection = null; /** * Start quote @@ -110,49 +133,190 @@ class DboMysqlBase extends DboSource { 'boolean' => array('name' => 'tinyint', 'limit' => '1') ); +/** + * Connects to the database using options in the given configuration array. + * + * @return boolean True if the database could be connected, else false + */ + function connect() { + $config = $this->config; + $this->connected = false; + try { + $flags = array( + PDO::ATTR_PERSISTENT => $config['persistent'], + PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true + ); + if (!empty($config['encoding'])) { + $flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $config['encoding']; + } + $this->_connection = new PDO( + "mysql:{$config['host']};port={$config['port']};dbname={$config['database']}", + $config['login'], + $config['password'], + $flags + ); + $this->connected = true; + } catch (PDOException $e) { + $this->errors[] = $e->getMessage(); + } + + $this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">="); + + return $this->connected; + } + +/** + * Check whether the MySQL extension is installed/loaded + * + * @return boolean + */ + function enabled() { + return in_array('mysql', PDO::getAvailableDrivers()); + } + +/** + * Returns an array of sources (tables) in the database. + * + * @return array Array of tablenames in the database + */ + function listSources($data = null) { + $cache = parent::listSources(); + if ($cache != null) { + return $cache; + } + $result = $this->_execute('SHOW TABLES FROM ' . $this->config['database']); + + if (!$result) { + $result->closeCursor(); + return array(); + } else { + $tables = array(); + + while ($line = $result->fetch()) { + $tables[] = $line[0]; + } + + $result->closeCursor(); + parent::listSources($tables); + return $tables; + } + } + +/** + * Builds a map of the columns contained in a result + * + * @param PDOStatement $results + */ + function resultSet($results) { + $this->map = array(); + $numFields = $results->columnCount(); + $index = 0; + $j = 0; + + while ($j < $numFields) { + $column = $results->getColumnMeta($j); + if (!empty($column['table']) && strpos($column['name'], $this->virtualFieldSeparator) === false) { + $this->map[$index++] = array($column['table'], $column['name']); + } else { + $this->map[$index++] = array(0, $column['name']); + } + $j++; + } + } + +/** + * Fetches the next row from the current result set + * + * @return mixed array with results fetched and mapped to column names or false if there is no results left to fetch + */ + function fetchResult() { + if ($row = $this->_result->fetch()) { + $resultRow = array(); + foreach ($this->map as $col => $meta) { + list($table, $column) = $meta; + $resultRow[$table][$column] = $row[$col]; + } + return $resultRow; + } else { + $this->_result->closeCursor(); + return false; + } + } + +/** + * Gets the database encoding + * + * @return string The database encoding + */ + public function getEncoding() { + return $this->_execute('SHOW VARIABLES LIKE ?', array('character_set_client'))->fetchObject()->Value; + } + +/** + * Gets the version string of the database server + * + * @return string The database encoding + */ + public function getVersion() { + return $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION); + } + +/** + * Query charset by collation + * + * @param string $name Collation name + * @return string Character set name + */ + public function getCharsetName($name) { + if ((bool)version_compare($this->getVersion(), "5", ">=")) { + $r = $this->_execute('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?', array($name)); + $cols = $r->fetch(); + + if (isset($cols['CHARACTER_SET_NAME'])) { + return $cols['CHARACTER_SET_NAME']; + } + } + return false; + } + /** * Returns an array of the fields in given table name. * - * @param string $tableName Name of database table to inspect + * @param mixed $tableName Name of database table to inspect or model instance * @return array Fields in table. Keys are name and type */ - function describe(&$model) { + function describe($model) { $cache = parent::describe($model); if ($cache != null) { return $cache; } $fields = false; - $cols = $this->query('SHOW FULL COLUMNS FROM ' . $this->fullTableName($model)); + $cols = $this->_execute('SHOW FULL COLUMNS FROM ' . $this->fullTableName($model)); foreach ($cols as $column) { - $colKey = array_keys($column); - if (isset($column[$colKey[0]]) && !isset($column[0])) { - $column[0] = $column[$colKey[0]]; + $fields[$column->Field] = array( + 'type' => $this->column($column->Type), + 'null' => ($column->Null == 'YES' ? true : false), + 'default' => $column->Default, + 'length' => $this->length($column->Type), + ); + if (!empty($column->Key) && isset($this->index[$column->Key])) { + $fields[$column->Field]['key'] = $this->index[$column->Key]; } - if (isset($column[0])) { - $fields[$column[0]['Field']] = array( - 'type' => $this->column($column[0]['Type']), - 'null' => ($column[0]['Null'] == 'YES' ? true : false), - 'default' => $column[0]['Default'], - 'length' => $this->length($column[0]['Type']), - ); - if (!empty($column[0]['Key']) && isset($this->index[$column[0]['Key']])) { - $fields[$column[0]['Field']]['key'] = $this->index[$column[0]['Key']]; + foreach ($this->fieldParameters as $name => $value) { + if (!empty($column->{$value['column']})) { + $fields[$column->Field][$name] = $column->{$value['column']}; } - foreach ($this->fieldParameters as $name => $value) { - if (!empty($column[0][$value['column']])) { - $fields[$column[0]['Field']][$name] = $column[0][$value['column']]; - } - } - if (isset($fields[$column[0]['Field']]['collate'])) { - $charset = $this->getCharsetName($fields[$column[0]['Field']]['collate']); - if ($charset) { - $fields[$column[0]['Field']]['charset'] = $charset; - } + } + if (isset($fields[$column->Field]['collate'])) { + $charset = $this->getCharsetName($fields[$column->Field]['collate']); + if ($charset) { + $fields[$column->Field]['charset'] = $charset; } } } $this->__cacheDescription($this->fullTableName($model, false), $fields); + $cols->closeCursor(); return $fields; } @@ -249,26 +413,26 @@ class DboMysqlBase extends DboSource { function index($model) { $index = array(); $table = $this->fullTableName($model); + $old = version_compare($this->getVersion(), '4.1', '<='); if ($table) { - $indexes = $this->query('SHOW INDEX FROM ' . $table); - if (isset($indexes[0]['STATISTICS'])) { - $keys = Set::extract($indexes, '{n}.STATISTICS'); - } else { - $keys = Set::extract($indexes, '{n}.0'); - } - foreach ($keys as $i => $key) { - if (!isset($index[$key['Key_name']])) { + $indices = $this->_execute('SHOW INDEX FROM ' . $table); + while ($idx = $indices->fetch()) { + if ($old) { + $idx = (object) current((array)$idx); + } + if (!isset($index[$idx->Key_name]['column'])) { $col = array(); - $index[$key['Key_name']]['column'] = $key['Column_name']; - $index[$key['Key_name']]['unique'] = intval($key['Non_unique'] == 0); + $index[$idx->Key_name]['column'] = $idx->Column_name; + $index[$idx->Key_name]['unique'] = intval($idx->Non_unique == 0); } else { - if (!is_array($index[$key['Key_name']]['column'])) { - $col[] = $index[$key['Key_name']]['column']; + if (!empty($index[$idx->Key_name]['column']) && !is_array($index[$idx->Key_name]['column'])) { + $col[] = $index[$idx->Key_name]['column']; } - $col[] = $key['Column_name']; - $index[$key['Key_name']]['column'] = $col; + $col[] = $idx->Column_name; + $index[$idx->Key_name]['column'] = $col; } } + $indices->closeCursor(); } return $index; } @@ -412,21 +576,6 @@ class DboMysqlBase extends DboSource { return $alter; } -/** - * Inserts multiple values into a table - * - * @param string $table - * @param string $fields - * @param array $values - */ - function insertMulti($table, $fields, $values) { - $table = $this->fullTableName($table); - if (is_array($fields)) { - $fields = implode(', ', array_map(array(&$this, 'name'), $fields)); - } - $values = implode(', ', $values); - $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}"); - } /** * Returns an detailed array of sources (tables) in the database. * @@ -435,23 +584,29 @@ class DboMysqlBase extends DboSource { */ function listDetailedSources($name = null) { $condition = ''; + $params = array(); if (is_string($name)) { - $condition = ' LIKE ' . $this->value($name); + $condition = ' WHERE name = ?' ; + $params = array($name); } - $result = $this->query('SHOW TABLE STATUS FROM ' . $this->name($this->config['database']) . $condition . ';'); + $result = $this->_execute('SHOW TABLE STATUS ' . $condition, $params); + if (!$result) { + $result->closeCursor(); return array(); } else { $tables = array(); - foreach ($result as $row) { - $tables[$row['TABLES']['Name']] = $row['TABLES']; - if (!empty($row['TABLES']['Collation'])) { - $charset = $this->getCharsetName($row['TABLES']['Collation']); + while ($row = $result->fetch()) { + $tables[$row->Name] = (array) $row; + unset($tables[$row->Name]['queryString']); + if (!empty($row->Collation)) { + $charset = $this->getCharsetName($row->Collation); if ($charset) { - $tables[$row['TABLES']['Name']]['charset'] = $charset; + $tables[$row->Name]['charset'] = $charset; } } } + $result->closeCursor(); if (is_string($name)) { return $tables[$name]; } @@ -507,292 +662,3 @@ class DboMysqlBase extends DboSource { return 'text'; } } - -/** - * MySQL DBO driver object - * - * Provides connection and SQL generation for MySQL RDMS - * - * @package cake - * @subpackage cake.cake.libs.model.datasources.dbo - */ -class DboMysql extends DboMysqlBase { - -/** - * Datasource description - * - * @var string - */ - public $description = "MySQL DBO Driver"; - -/** - * Base configuration settings for MySQL driver - * - * @var array - */ - protected $_baseConfig = array( - 'persistent' => true, - 'host' => 'localhost', - 'login' => 'root', - 'password' => '', - 'database' => 'cake', - 'port' => '3306' - ); - -/** - * Connects to the database using options in the given configuration array. - * - * @return boolean True if the database could be connected, else false - */ - function connect() { - $config = $this->config; - $this->connected = false; - - if (!$config['persistent']) { - $this->connection = mysql_connect($config['host'] . ':' . $config['port'], $config['login'], $config['password'], true); - $config['connect'] = 'mysql_connect'; - } else { - $this->connection = mysql_pconnect($config['host'] . ':' . $config['port'], $config['login'], $config['password']); - } - - if (mysql_select_db($config['database'], $this->connection)) { - $this->connected = true; - } - - if (!empty($config['encoding'])) { - $this->setEncoding($config['encoding']); - } - - $this->_useAlias = (bool)version_compare(mysql_get_server_info($this->connection), "4.1", ">="); - - return $this->connected; - } - -/** - * Check whether the MySQL extension is installed/loaded - * - * @return boolean - */ - function enabled() { - return extension_loaded('mysql'); - } -/** - * Disconnects from database. - * - * @return boolean True if the database could be disconnected, else false - */ - function disconnect() { - if (isset($this->results) && is_resource($this->results)) { - mysql_free_result($this->results); - } - $this->connected = !@mysql_close($this->connection); - return !$this->connected; - } - -/** - * Executes given SQL statement. - * - * @param string $sql SQL statement - * @return resource Result resource identifier - */ - protected function _execute($sql) { - return mysql_query($sql, $this->connection); - } - -/** - * Returns an array of sources (tables) in the database. - * - * @return array Array of tablenames in the database - */ - function listSources() { - $cache = parent::listSources(); - if ($cache != null) { - return $cache; - } - $result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';'); - - if (!$result) { - return array(); - } else { - $tables = array(); - - while ($line = mysql_fetch_row($result)) { - $tables[] = $line[0]; - } - parent::listSources($tables); - return $tables; - } - } - -/** - * Returns a quoted and escaped string of $data for use in an SQL statement. - * - * @param string $data String to be prepared for use in an SQL statement - * @param string $column The column into which this data will be inserted - * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided - * @return string Quoted and escaped data - */ - function value($data, $column = null, $safe = false) { - $parent = parent::value($data, $column, $safe); - - if ($parent != null) { - return $parent; - } - if ($data === null || (is_array($data) && empty($data))) { - return 'NULL'; - } - if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') { - return "''"; - } - if (empty($column)) { - $column = $this->introspectType($data); - } - - switch ($column) { - case 'boolean': - return $this->boolean((bool)$data); - break; - case 'integer': - case 'float': - if ($data === '') { - return 'NULL'; - } - if (is_float($data)) { - return sprintf('%F', $data); - } - if ((is_int($data) || $data === '0') || ( - is_numeric($data) && strpos($data, ',') === false && - $data[0] != '0' && strpos($data, 'e') === false) - ) { - return $data; - } - default: - return "'" . mysql_real_escape_string($data, $this->connection) . "'"; - break; - } - } - -/** - * Returns a formatted error message from previous database operation. - * - * @return string Error message with error number - */ - function lastError() { - if (mysql_errno($this->connection)) { - return mysql_errno($this->connection).': '.mysql_error($this->connection); - } - return null; - } - -/** - * Returns number of affected rows in previous database operation. If no previous operation exists, - * this returns false. - * - * @return integer Number of affected rows - */ - function lastAffected() { - if ($this->_result) { - return mysql_affected_rows($this->connection); - } - return null; - } - -/** - * Returns number of rows in previous resultset. If no previous resultset exists, - * this returns false. - * - * @return integer Number of rows in resultset - */ - function lastNumRows() { - if ($this->hasResult()) { - return mysql_num_rows($this->_result); - } - return null; - } - -/** - * Returns the ID generated from the previous INSERT operation. - * - * @param unknown_type $source - * @return in - */ - function lastInsertId($source = null) { - $id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false); - if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) { - return $id[0]['insertID']; - } - - return null; - } - -/** - * Enter description here... - * - * @param unknown_type $results - */ - function resultSet(&$results) { - if (isset($this->results) && is_resource($this->results) && $this->results != $results) { - mysql_free_result($this->results); - } - $this->results =& $results; - $this->map = array(); - $numFields = mysql_num_fields($results); - $index = 0; - $j = 0; - - while ($j < $numFields) { - $column = mysql_fetch_field($results, $j); - if (!empty($column->table) && strpos($column->name, $this->virtualFieldSeparator) === false) { - $this->map[$index++] = array($column->table, $column->name); - } else { - $this->map[$index++] = array(0, $column->name); - } - $j++; - } - } - -/** - * Fetches the next row from the current result set - * - * @return unknown - */ - function fetchResult() { - if ($row = mysql_fetch_row($this->results)) { - $resultRow = array(); - $i = 0; - foreach ($row as $index => $field) { - list($table, $column) = $this->map[$index]; - $resultRow[$table][$column] = $row[$index]; - $i++; - } - return $resultRow; - } else { - return false; - } - } - -/** - * Gets the database encoding - * - * @return string The database encoding - */ - function getEncoding() { - return mysql_client_encoding($this->connection); - } - -/** - * Query charset by collation - * - * @param string $name Collation name - * @return string Character set name - */ - function getCharsetName($name) { - if ((bool)version_compare(mysql_get_server_info($this->connection), "5", ">=")) { - $cols = $this->query('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME= ' . $this->value($name) . ';'); - if (isset($cols[0]['COLLATIONS']['CHARACTER_SET_NAME'])) { - return $cols[0]['COLLATIONS']['CHARACTER_SET_NAME']; - } - } - return false; - } -} diff --git a/cake/libs/model/datasources/dbo/dbo_mysqli.php b/cake/libs/model/datasources/dbo/dbo_mysqli.php deleted file mode 100644 index 7c6cdfd5d..000000000 --- a/cake/libs/model/datasources/dbo/dbo_mysqli.php +++ /dev/null @@ -1,336 +0,0 @@ - true, - 'host' => 'localhost', - 'login' => 'root', - 'password' => '', - 'database' => 'cake', - 'port' => '3306' - ); - -/** - * Connects to the database using options in the given configuration array. - * - * @return boolean True if the database could be connected, else false - */ - function connect() { - $config = $this->config; - $this->connected = false; - - if (is_numeric($config['port'])) { - $config['socket'] = null; - } else { - $config['socket'] = $config['port']; - $config['port'] = null; - } - - $this->connection = mysqli_connect($config['host'], $config['login'], $config['password'], $config['database'], $config['port'], $config['socket']); - - if ($this->connection !== false) { - $this->connected = true; - } - - $this->_useAlias = (bool)version_compare(mysqli_get_server_info($this->connection), "4.1", ">="); - - if (!empty($config['encoding'])) { - $this->setEncoding($config['encoding']); - } - return $this->connected; - } - -/** - * Check that MySQLi is installed/enabled - * - * @return boolean - */ - function enabled() { - return extension_loaded('mysqli'); - } -/** - * Disconnects from database. - * - * @return boolean True if the database could be disconnected, else false - */ - function disconnect() { - if (isset($this->results) && is_resource($this->results)) { - mysqli_free_result($this->results); - } - $this->connected = !@mysqli_close($this->connection); - return !$this->connected; - } - -/** - * Executes given SQL statement. - * - * @param string $sql SQL statement - * @return resource Result resource identifier - */ - protected function _execute($sql) { - if (preg_match('/^\s*call/i', $sql)) { - return $this->_executeProcedure($sql); - } - return mysqli_query($this->connection, $sql); - } - -/** - * Executes given SQL statement (procedure call). - * - * @param string $sql SQL statement (procedure call) - * @return resource Result resource identifier for first recordset - */ - protected function _executeProcedure($sql) { - $answer = mysqli_multi_query($this->connection, $sql); - - $firstResult = mysqli_store_result($this->connection); - - if (mysqli_more_results($this->connection)) { - while ($lastResult = mysqli_next_result($this->connection)); - } - return $firstResult; - } - -/** - * Returns an array of sources (tables) in the database. - * - * @return array Array of tablenames in the database - */ - function listSources() { - $cache = parent::listSources(); - if ($cache !== null) { - return $cache; - } - $result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';'); - - if (!$result) { - return array(); - } - - $tables = array(); - - while ($line = mysqli_fetch_row($result)) { - $tables[] = $line[0]; - } - parent::listSources($tables); - return $tables; - } - -/** - * Returns a quoted and escaped string of $data for use in an SQL statement. - * - * @param string $data String to be prepared for use in an SQL statement - * @param string $column The column into which this data will be inserted - * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided - * @return string Quoted and escaped data - */ - function value($data, $column = null, $safe = false) { - $parent = parent::value($data, $column, $safe); - - if ($parent != null) { - return $parent; - } - if ($data === null || (is_array($data) && empty($data))) { - return 'NULL'; - } - if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') { - return "''"; - } - if (empty($column)) { - $column = $this->introspectType($data); - } - - switch ($column) { - case 'boolean': - return $this->boolean((bool)$data); - break; - case 'integer' : - case 'float' : - case null : - if ($data === '') { - return 'NULL'; - } - if ((is_int($data) || is_float($data) || $data === '0') || ( - is_numeric($data) && strpos($data, ',') === false && - $data[0] != '0' && strpos($data, 'e') === false)) { - return $data; - } - default: - $data = "'" . mysqli_real_escape_string($this->connection, $data) . "'"; - break; - } - - return $data; - } - -/** - * Returns a formatted error message from previous database operation. - * - * @return string Error message with error number - */ - function lastError() { - if (mysqli_errno($this->connection)) { - return mysqli_errno($this->connection).': '.mysqli_error($this->connection); - } - return null; - } - -/** - * Returns number of affected rows in previous database operation. If no previous operation exists, - * this returns false. - * - * @return integer Number of affected rows - */ - function lastAffected() { - if ($this->_result) { - return mysqli_affected_rows($this->connection); - } - return null; - } - -/** - * Returns number of rows in previous resultset. If no previous resultset exists, - * this returns false. - * - * @return integer Number of rows in resultset - */ - function lastNumRows() { - if ($this->hasResult()) { - return mysqli_num_rows($this->_result); - } - return null; - } - -/** - * Returns the ID generated from the previous INSERT operation. - * - * @param unknown_type $source - * @return in - */ - function lastInsertId($source = null) { - $id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false); - if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) { - return $id[0]['insertID']; - } - return null; - } - -/** - * Enter description here... - * - * @param unknown_type $results - */ - function resultSet(&$results) { - if (isset($this->results) && is_resource($this->results) && $this->results != $results) { - mysqli_free_result($this->results); - } - $this->results =& $results; - $this->map = array(); - $numFields = mysqli_num_fields($results); - $index = 0; - $j = 0; - while ($j < $numFields) { - $column = mysqli_fetch_field_direct($results, $j); - if (!empty($column->table)) { - $this->map[$index++] = array($column->table, $column->name); - } else { - $this->map[$index++] = array(0, $column->name); - } - $j++; - } - } - -/** - * Fetches the next row from the current result set - * - * @return unknown - */ - function fetchResult() { - if ($row = mysqli_fetch_row($this->results)) { - $resultRow = array(); - foreach ($row as $index => $field) { - $table = $column = null; - if (count($this->map[$index]) === 2) { - list($table, $column) = $this->map[$index]; - } - $resultRow[$table][$column] = $row[$index]; - } - return $resultRow; - } - return false; - } - -/** - * Gets the database encoding - * - * @return string The database encoding - */ - function getEncoding() { - return mysqli_client_encoding($this->connection); - } - -/** - * Query charset by collation - * - * @param string $name Collation name - * @return string Character set name - */ - function getCharsetName($name) { - if ((bool)version_compare(mysqli_get_server_info($this->connection), "5", ">=")) { - $cols = $this->query('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME= ' . $this->value($name) . ';'); - if (isset($cols[0]['COLLATIONS']['CHARACTER_SET_NAME'])) { - return $cols[0]['COLLATIONS']['CHARACTER_SET_NAME']; - } - } - return false; - } - -/** - * Checks if the result is valid - * - * @return boolean True if the result is valid, else false - */ - function hasResult() { - return is_object($this->_result); - } -} diff --git a/cake/libs/model/datasources/dbo/dbo_postgres.php b/cake/libs/model/datasources/dbo/dbo_postgres.php index 7b37f5412..bebbb5447 100644 --- a/cake/libs/model/datasources/dbo/dbo_postgres.php +++ b/cake/libs/model/datasources/dbo/dbo_postgres.php @@ -112,23 +112,29 @@ class DboPostgres extends DboSource { */ function connect() { $config = $this->config; - $conn = "host='{$config['host']}' port='{$config['port']}' dbname='{$config['database']}' "; - $conn .= "user='{$config['login']}' password='{$config['password']}'"; - - if (!$config['persistent']) { - $this->connection = pg_connect($conn, PGSQL_CONNECT_FORCE_NEW); - } else { - $this->connection = pg_pconnect($conn); - } $this->connected = false; + try { + $flags = array( + PDO::ATTR_PERSISTENT => $config['persistent'] + ); + $this->_connection = new PDO( + "pgsql:host={$config['host']};port={$config['port']};dbname={$config['database']}", + $config['login'], + $config['password'], + $flags + ); - if ($this->connection) { $this->connected = true; - $this->_execute("SET search_path TO " . $config['schema']); - } - if (!empty($config['encoding'])) { - $this->setEncoding($config['encoding']); + if (!empty($config['encoding'])) { + $this->setEncoding($config['encoding']); + } + if (!empty($config['schema'])) { + $this->_execute('SET search_path TO ' . $config['schema']); + } + } catch (PDOException $e) { + $this->errors[] = $e->getMessage(); } + return $this->connected; } @@ -138,33 +144,7 @@ class DboPostgres extends DboSource { * @return boolean */ function enabled() { - return extension_loaded('pgsql'); - } -/** - * Disconnects from database. - * - * @return boolean True if the database could be disconnected, else false - */ - function disconnect() { - if ($this->hasResult()) { - pg_free_result($this->_result); - } - if (is_resource($this->connection)) { - $this->connected = !pg_close($this->connection); - } else { - $this->connected = false; - } - return !$this->connected; - } - -/** - * Executes given SQL statement. - * - * @param string $sql SQL statement - * @return resource Result resource identifier - */ - function _execute($sql) { - return pg_query($this->connection, $sql); + return in_array('pgsql', PDO::getAvailableDrivers()); } /** @@ -180,18 +160,20 @@ class DboPostgres extends DboSource { } $schema = $this->config['schema']; - $sql = "SELECT table_name as name FROM INFORMATION_SCHEMA.tables WHERE table_schema = '{$schema}';"; - $result = $this->fetchAll($sql, false); + $sql = "SELECT table_name as name FROM INFORMATION_SCHEMA.tables WHERE table_schema = ?"; + $result = $this->_execute($sql, array($schema)); if (!$result) { + $result->closeCursor(); return array(); } else { $tables = array(); foreach ($result as $item) { - $tables[] = $item[0]['name']; + $tables[] = $item->name; } + $result->closeCursor(); parent::listSources($tables); return $tables; } @@ -209,61 +191,56 @@ class DboPostgres extends DboSource { $this->_sequenceMap[$table] = array(); if ($fields === null) { - $cols = $this->fetchAll( + $cols = $this->_execute( "SELECT DISTINCT column_name AS name, data_type AS type, is_nullable AS null, column_default AS default, ordinal_position AS position, character_maximum_length AS char_length, character_octet_length AS oct_length FROM information_schema.columns - WHERE table_name = " . $this->value($table) . " AND table_schema = " . - $this->value($this->config['schema'])." ORDER BY position", - false + WHERE table_name = ? AND table_schema = ? ORDER BY position", + array($table, $this->config['schema']) ); - foreach ($cols as $column) { - $colKey = array_keys($column); - - if (isset($column[$colKey[0]]) && !isset($column[0])) { - $column[0] = $column[$colKey[0]]; - } - - if (isset($column[0])) { - $c = $column[0]; - - if (!empty($c['char_length'])) { - $length = intval($c['char_length']); - } elseif (!empty($c['oct_length'])) { - if ($c['type'] == 'character varying') { - $length = null; - $c['type'] = 'text'; - } else { - $length = intval($c['oct_length']); - } + foreach ($cols as $c) { + $type = $c->type; + if (!empty($c->oct_length) && $c->char_length === null) { + if ($c->type == 'character varying') { + $length = null; + $type = 'text'; } else { - $length = $this->length($c['type']); + $length = intval($c->oct_length); } - $fields[$c['name']] = array( - 'type' => $this->column($c['type']), - 'null' => ($c['null'] == 'NO' ? false : true), - 'default' => preg_replace( - "/^'(.*)'$/", - "$1", - preg_replace('/::.*/', '', $c['default']) - ), - 'length' => $length - ); - if ($c['name'] == $model->primaryKey) { - $fields[$c['name']]['key'] = 'primary'; - if ($fields[$c['name']]['type'] !== 'string') { - $fields[$c['name']]['length'] = 11; + } elseif (!empty($c->char_length)) { + $length = intval($c->char_length); + } else { + $length = $this->length($c->type); + } + if (empty($length)) { + $length = null; + } + $fields[$c->name] = array( + 'type' => $this->column($type), + 'null' => ($c->null == 'NO' ? false : true), + 'default' => preg_replace( + "/^'(.*)'$/", + "$1", + preg_replace('/::.*/', '', $c->default) + ), + 'length' => $length + ); + if ($model instanceof Model) { + if ($c->name == $model->primaryKey) { + $fields[$c->name]['key'] = 'primary'; + if ($fields[$c->name]['type'] !== 'string') { + $fields[$c->name]['length'] = 11; } } - if ( - $fields[$c['name']]['default'] == 'NULL' || - preg_match('/nextval\([\'"]?([\w.]+)/', $c['default'], $seq) - ) { - $fields[$c['name']]['default'] = null; - if (!empty($seq) && isset($seq[1])) { - $this->_sequenceMap[$table][$c['name']] = $seq[1]; - } + } + if ( + $fields[$c->name]['default'] == 'NULL' || + preg_match('/nextval\([\'"]?([\w.]+)/', $c->default, $seq) + ) { + $fields[$c->name]['default'] = null; + if (!empty($seq) && isset($seq[1])) { + $this->_sequenceMap[$table][$c->default] = $seq[1]; } } } @@ -272,93 +249,11 @@ class DboPostgres extends DboSource { if (isset($model->sequence)) { $this->_sequenceMap[$table][$model->primaryKey] = $model->sequence; } + + $cols->closeCursor(); return $fields; } -/** - * Returns a quoted and escaped string of $data for use in an SQL statement. - * - * @param string $data String to be prepared for use in an SQL statement - * @param string $column The column into which this data will be inserted - * @param boolean $read Value to be used in READ or WRITE context - * @return string Quoted and escaped - * @todo Add logic that formats/escapes data based on column type - */ - function value($data, $column = null, $read = true) { - - $parent = parent::value($data, $column); - if ($parent != null) { - return $parent; - } - - if ($data === null || (is_array($data) && empty($data))) { - return 'NULL'; - } - if (empty($column)) { - $column = $this->introspectType($data); - } - - switch($column) { - case 'binary': - $data = pg_escape_bytea($data); - break; - case 'boolean': - if ($data === true || $data === 't' || $data === 'true') { - return 'TRUE'; - } elseif ($data === false || $data === 'f' || $data === 'false') { - return 'FALSE'; - } - return (!empty($data) ? 'TRUE' : 'FALSE'); - break; - case 'float': - if (is_float($data)) { - $data = sprintf('%F', $data); - } - case 'inet': - case 'integer': - case 'date': - case 'datetime': - case 'timestamp': - case 'time': - if ($data === '') { - return $read ? 'NULL' : 'DEFAULT'; - } - default: - $data = pg_escape_string($data); - break; - } - return "'" . $data . "'"; - } - -/** - * Returns a formatted error message from previous database operation. - * - * @return string Error message - */ - function lastError() { - $error = pg_last_error($this->connection); - return ($error) ? $error : null; - } - -/** - * Returns number of affected rows in previous database operation. If no previous operation exists, this returns false. - * - * @return integer Number of affected rows - */ - function lastAffected() { - return ($this->_result) ? pg_affected_rows($this->_result) : false; - } - -/** - * Returns number of rows in previous resultset. If no previous resultset exists, - * this returns false. - * - * @return integer Number of rows in resultset - */ - function lastNumRows() { - return ($this->_result) ? pg_num_rows($this->_result) : false; - } - /** * Returns the ID generated from the previous INSERT operation. * @@ -368,8 +263,7 @@ class DboPostgres extends DboSource { */ function lastInsertId($source, $field = 'id') { $seq = $this->getSequence($source, $field); - $data = $this->fetchRow("SELECT currval('{$seq}') as max"); - return $data[0]['max']; + return $this->_connection->lastInsertId($seq); } /** @@ -394,20 +288,22 @@ class DboPostgres extends DboSource { * Deletes all the records in a table and drops all associated auto-increment sequences * * @param mixed $table A string or model class representing the table to be truncated - * @param integer $reset If -1, sequences are dropped, if 0 (default), sequences are reset, + * @param boolean $reset true for resseting the sequence, false to leave it as is. * and if 1, sequences are not modified * @return boolean SQL TRUNCATE TABLE statement, false if not applicable. */ - public function truncate($table, $reset = 0) { + public function truncate($table, $reset = true) { + $table = $this->fullTableName($table, false); + if (!isset($this->_sequenceMap[$table])) { + $cache = $this->cacheSources; + $this->cacheSources = false; + $this->describe($table); + $this->cacheSources = $cache; + } if (parent::truncate($table)) { - $table = $this->fullTableName($table, false); - if (isset($this->_sequenceMap[$table]) && $reset !== 1) { + if (isset($this->_sequenceMap[$table]) && $reset) { foreach ($this->_sequenceMap[$table] as $field => $sequence) { - if ($reset === 0) { - $this->execute("ALTER SEQUENCE \"{$sequence}\" RESTART WITH 1"); - } elseif ($reset === -1) { - $this->execute("DROP SEQUENCE IF EXISTS \"{$sequence}\""); - } + $this->_execute("ALTER SEQUENCE \"{$sequence}\" RESTART WITH 1"); } } return true; @@ -447,7 +343,7 @@ class DboPostgres extends DboSource { } $count = count($fields); - if ($count >= 1 && strpos($fields[0], 'COUNT(*)') === false) { + if ($count >= 1 && !preg_match('/^\s*COUNT\(\*/', $fields[0])) { $result = array(); for ($i = 0; $i < $count; $i++) { if (!preg_match('/^.+\\(.*\\)/', $fields[$i]) && !preg_match('/\s+AS\s+/', $fields[$i])) { @@ -781,20 +677,18 @@ class DboPostgres extends DboSource { * @param unknown_type $results */ function resultSet(&$results) { - $this->results =& $results; $this->map = array(); - $num_fields = pg_num_fields($results); + $numFields = $results->columnCount(); $index = 0; $j = 0; - while ($j < $num_fields) { - $columnName = pg_field_name($results, $j); - - if (strpos($columnName, '__')) { - $parts = explode('__', $columnName); - $this->map[$index++] = array($parts[0], $parts[1]); + while ($j < $numFields) { + $column = $results->getColumnMeta($j); + if (strpos($column['name'], '__')) { + list($table, $name) = explode('__', $column['name']); + $this->map[$index++] = array($table, $name, $column['native_type']); } else { - $this->map[$index++] = array(0, $columnName); + $this->map[$index++] = array(0, $column['name'], $column['native_type']); } $j++; } @@ -806,12 +700,11 @@ class DboPostgres extends DboSource { * @return unknown */ function fetchResult() { - if ($row = pg_fetch_row($this->results)) { + if ($row = $this->_result->fetch()) { $resultRow = array(); - foreach ($row as $index => $field) { - list($table, $column) = $this->map[$index]; - $type = pg_field_type($this->results, $index); + foreach ($this->map as $index => $meta) { + list($table, $column, $type) = $meta; switch ($type) { case 'bool': @@ -819,7 +712,7 @@ class DboPostgres extends DboSource { break; case 'binary': case 'bytea': - $resultRow[$table][$column] = pg_unescape_bytea($row[$index]); + $resultRow[$table][$column] = stream_get_contents($row[$index]); break; default: $resultRow[$table][$column] = $row[$index]; @@ -828,6 +721,7 @@ class DboPostgres extends DboSource { } return $resultRow; } else { + $this->_result->closeCursor(); return false; } } @@ -835,24 +729,33 @@ class DboPostgres extends DboSource { /** * Translates between PHP boolean values and PostgreSQL boolean values * - * @param mixed $data Value to be translated - * @param boolean $quote True to quote value, false otherwise - * @return mixed Converted boolean value + * @param mixed $data Value to be translated + * @param boolean $quote true to quote a boolean to be used in a query, flase to return the boolean value + * @return boolean Converted boolean value */ function boolean($data, $quote = true) { switch (true) { case ($data === true || $data === false): - return $data; + $result = $data; + break; case ($data === 't' || $data === 'f'): - return ($data === 't'); + $result = ($data === 't'); + break; case ($data === 'true' || $data === 'false'): - return ($data === 'true'); + $result = ($data === 'true'); + break; case ($data === 'TRUE' || $data === 'FALSE'): - return ($data === 'TRUE'); + $result = ($data === 'TRUE'); + break; default: - return (bool)$data; + $result = (bool) $data; break; } + + if ($quote) { + return ($result) ? 'TRUE' : 'FALSE'; + } + return (int) $result; } /** @@ -862,7 +765,10 @@ class DboPostgres extends DboSource { * @return boolean True on success, false on failure */ function setEncoding($enc) { - return pg_set_client_encoding($this->connection, $enc) == 0; + if ($this->_execute('SET NAMES ?', array($enc))) { + return true; + } + return false; } /** @@ -871,7 +777,7 @@ class DboPostgres extends DboSource { * @return string The database encoding */ function getEncoding() { - return pg_client_encoding($this->connection); + $cosa = $this->_execute('SHOW client_encoding')->fetch(); } /** diff --git a/cake/libs/model/datasources/dbo/dbo_sqlite.php b/cake/libs/model/datasources/dbo/dbo_sqlite.php index 819f3f02c..8c5722762 100644 --- a/cake/libs/model/datasources/dbo/dbo_sqlite.php +++ b/cake/libs/model/datasources/dbo/dbo_sqlite.php @@ -19,11 +19,11 @@ */ /** - * DBO implementation for the SQLite DBMS. + * DBO implementation for the SQLite3 DBMS. * - * Long description for class + * A DboSource adapter for SQLite 3 using PDO * - * @package cake + * @package datasources * @subpackage cake.cake.libs.model.datasources.dbo */ class DboSqlite extends DboSource { @@ -32,63 +32,48 @@ class DboSqlite extends DboSource { * Datasource Description * * @var string + * @access public */ - public $description = "SQLite DBO Driver"; + var $description = "SQLite DBO Driver"; /** - * Opening quote for quoted identifiers + * Quote Start * * @var string + * @access public */ - public $startQuote = '"'; + var $startQuote = '"'; /** - * Closing quote for quoted identifiers + * Quote End * * @var string + * @access public */ - public $endQuote = '"'; + var $endQuote = '"'; /** - * Keeps the transaction statistics of CREATE/UPDATE/DELETE queries + * Base configuration settings for SQLite3 driver * * @var array - * @access protected + * @access public */ - protected $_queryStats = array(); - -/** - * Base configuration settings for SQLite driver - * - * @var array - */ - protected $_baseConfig = array( - 'persistent' => true, + var $_baseConfig = array( + 'persistent' => false, 'database' => null ); /** - * Index of basic SQL commands + * SQLite3 column definition * * @var array - * @access protected + * @access public */ - protected $_commands = array( - 'begin' => 'BEGIN TRANSACTION', - 'commit' => 'COMMIT TRANSACTION', - 'rollback' => 'ROLLBACK TRANSACTION' - ); - -/** - * SQLite column definition - * - * @var array - */ - public $columns = array( - 'primary_key' => array('name' => 'integer primary key'), + var $columns = array( + 'primary_key' => array('name' => 'integer primary key autoincrement'), 'string' => array('name' => 'varchar', 'limit' => '255'), 'text' => array('name' => 'text'), - 'integer' => array('name' => 'integer', 'limit' => 11, 'formatter' => 'intval'), + 'integer' => array('name' => 'integer', 'limit' => null, 'formatter' => 'intval'), 'float' => array('name' => 'float', 'formatter' => 'floatval'), 'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'), 'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'), @@ -104,12 +89,12 @@ class DboSqlite extends DboSource { * @var array * @access public */ - public $fieldParameters = array( + var $fieldParameters = array( 'collate' => array( 'value' => 'COLLATE', 'quote' => false, - 'join' => ' ', - 'column' => 'Collate', + 'join' => ' ', + 'column' => 'Collate', 'position' => 'afterDefault', 'options' => array( 'BINARY', 'NOCASE', 'RTRIM' @@ -122,83 +107,46 @@ class DboSqlite extends DboSource { * * @param array $config Configuration array for connecting * @return mixed + * @access public */ function connect() { $config = $this->config; - if (!$config['persistent']) { - $this->connection = sqlite_open($config['database']); - } else { - $this->connection = sqlite_popen($config['database']); + $flags = array(PDO::ATTR_PERSISTENT => $config['persistent']); + try { + $this->_connection = new PDO('sqlite:' . $config['database'], null, null, $flags); + $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->connected = true; } - $this->connected = is_resource($this->connection); - - if ($this->connected) { - $this->_execute('PRAGMA count_changes = 1;'); + catch(PDOException $e) { + $this->errors[] = $e->getMessage(); } return $this->connected; } /** - * Check that SQLite is enabled/installed + * Check whether the MySQL extension is installed/loaded * * @return boolean */ function enabled() { - return extension_loaded('sqlite'); - } -/** - * Disconnects from database. - * - * @return boolean True if the database could be disconnected, else false - */ - function disconnect() { - @sqlite_close($this->connection); - $this->connected = false; - return $this->connected; - } - -/** - * Executes given SQL statement. - * - * @param string $sql SQL statement - * @return resource Result resource identifier - */ - function _execute($sql) { - $result = sqlite_query($this->connection, $sql); - - if (preg_match('/^(INSERT|UPDATE|DELETE)/', $sql)) { - $this->resultSet($result); - list($this->_queryStats) = $this->fetchResult(); - } - return $result; - } - -/** - * Overrides DboSource::execute() to correctly handle query statistics - * - * @param string $sql - * @return unknown - */ - function execute($sql) { - $result = parent::execute($sql); - $this->_queryStats = array(); - return $result; + return in_array('sqlite', PDO::getAvailableDrivers()); } /** * Returns an array of tables in the database. If there are no tables, an error is raised and the application exits. * * @return array Array of tablenames in the database + * @access public */ function listSources() { $cache = parent::listSources(); - if ($cache != null) { return $cache; } + $result = $this->fetchAll("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;", false); - if (empty($result)) { + if (!$result || empty($result)) { return array(); } else { $tables = array(); @@ -216,6 +164,7 @@ class DboSqlite extends DboSource { * * @param string $tableName Name of database table to inspect * @return array Fields in table. Keys are name and type + * @access public */ function describe(&$model) { $cache = parent::describe($model); @@ -223,65 +172,32 @@ class DboSqlite extends DboSource { return $cache; } $fields = array(); - $result = $this->fetchAll('PRAGMA table_info(' . $this->fullTableName($model) . ')'); + $result = $this->_execute('PRAGMA table_info(' . $model->tablePrefix . $model->table . ')'); foreach ($result as $column) { - $fields[$column[0]['name']] = array( - 'type' => $this->column($column[0]['type']), - 'null' => !$column[0]['notnull'], - 'default' => $column[0]['dflt_value'], - 'length' => $this->length($column[0]['type']) + $column = (array) $column; + $default = ($column['dflt_value'] === 'NULL') ? null : trim($column['dflt_value'], "'"); + + $fields[$column['name']] = array( + 'type' => $this->column($column['type']), + 'null' => !$column['notnull'], + 'default' => $default, + 'length' => $this->length($column['type']) ); - if ($column[0]['pk'] == 1) { - $colLength = $this->length($column[0]['type']); - $fields[$column[0]['name']] = array( - 'type' => $fields[$column[0]['name']]['type'], - 'null' => false, - 'default' => $column[0]['dflt_value'], - 'key' => $this->index['PRI'], - 'length'=> ($colLength != null) ? $colLength : 11 - ); + if ($column['pk'] == 1) { + $fields[$column['name']]['key'] = $this->index['PRI']; + $fields[$column['name']]['null'] = false; + if (empty($fields[$column['name']]['length'])) { + $fields[$column['name']]['length'] = 11; + } } } + $result->closeCursor(); $this->__cacheDescription($model->tablePrefix . $model->table, $fields); return $fields; } -/** - * Returns a quoted and escaped string of $data for use in an SQL statement. - * - * @param string $data String to be prepared for use in an SQL statement - * @return string Quoted and escaped - */ - function value($data, $column = null, $safe = false) { - $parent = parent::value($data, $column, $safe); - - if ($parent != null) { - return $parent; - } - if ($data === null) { - return 'NULL'; - } - if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') { - return "''"; - } - switch ($column) { - case 'boolean': - $data = $this->boolean((bool)$data); - break; - case 'integer': - case 'float': - if ($data === '') { - return 'NULL'; - } - default: - $data = sqlite_escape_string($data); - break; - } - return "'" . $data . "'"; - } - /** * Generates and executes an SQL UPDATE statement for given model, fields, and values. * @@ -290,6 +206,7 @@ class DboSqlite extends DboSource { * @param array $values * @param mixed $conditions * @return array + * @access public */ function update(&$model, $fields = array(), $values = null, $conditions = null) { if (empty($values) && !empty($fields)) { @@ -302,8 +219,7 @@ class DboSqlite extends DboSource { } } } - $result = parent::update($model, $fields, $values, $conditions); - return $result; + return parent::update($model, $fields, $values, $conditions); } /** @@ -312,60 +228,11 @@ class DboSqlite extends DboSource { * * @param mixed $table A string or model class representing the table to be truncated * @return boolean SQL TRUNCATE TABLE statement, false if not applicable. + * @access public */ - public function truncate($table) { - return $this->execute('DELETE From ' . $this->fullTableName($table)); - } - -/** - * Returns a formatted error message from previous database operation. - * - * @return string Error message - */ - function lastError() { - $error = sqlite_last_error($this->connection); - if ($error) { - return $error.': '.sqlite_error_string($error); - } - return null; - } - -/** - * Returns number of affected rows in previous database operation. If no previous operation exists, this returns false. - * - * @return integer Number of affected rows - */ - function lastAffected() { - if (!empty($this->_queryStats)) { - foreach (array('rows inserted', 'rows updated', 'rows deleted') as $key) { - if (array_key_exists($key, $this->_queryStats)) { - return $this->_queryStats[$key]; - } - } - } - return false; - } - -/** - * Returns number of rows in previous resultset. If no previous resultset exists, - * this returns false. - * - * @return integer Number of rows in resultset - */ - function lastNumRows() { - if ($this->hasResult()) { - sqlite_num_rows($this->_result); - } - return false; - } - -/** - * Returns the ID generated from the previous INSERT operation. - * - * @return int - */ - function lastInsertId() { - return sqlite_last_insert_rowid($this->connection); + function truncate($table) { + $this->_execute('DELETE FROM sqlite_sequence where name=' . $this->fullTableName($table)); + return $this->execute('DELETE FROM ' . $this->fullTableName($table)); } /** @@ -373,6 +240,7 @@ class DboSqlite extends DboSource { * * @param string $real Real database-layer column type (i.e. "varchar(255)") * @return string Abstract column type (i.e. "string") + * @access public */ function column($real) { if (is_array($real)) { @@ -385,9 +253,7 @@ class DboSqlite extends DboSource { $col = strtolower(str_replace(')', '', $real)); $limit = null; - if (strpos($col, '(') !== false) { - list($col, $limit) = explode('(', $col); - } + @list($col, $limit) = explode('(', $col); if (in_array($col, array('text', 'integer', 'float', 'boolean', 'timestamp', 'date', 'datetime', 'time'))) { return $col; @@ -398,29 +264,55 @@ class DboSqlite extends DboSource { if (in_array($col, array('blob', 'clob'))) { return 'binary'; } - if (strpos($col, 'numeric') !== false) { + if (strpos($col, 'numeric') !== false || strpos($col, 'decimal') !== false) { return 'float'; } return 'text'; } /** - * Enter description here... + * Generate ResultSet * - * @param unknown_type $results + * @param mixed $results + * @access public */ - function resultSet(&$results) { - $this->results =& $results; + function resultSet($results) { + $this->results = $results; $this->map = array(); - $fieldCount = sqlite_num_fields($results); - $index = $j = 0; + $num_fields = $results->columnCount(); + $index = 0; + $j = 0; - while ($j < $fieldCount) { - $columnName = str_replace('"', '', sqlite_field_name($results, $j)); + //PDO::getColumnMeta is experimental and does not work with sqlite3, + // so try to figure it out based on the querystring + $querystring = $results->queryString; + if (stripos($querystring, 'SELECT') === 0) { + $last = stripos($querystring, 'FROM'); + if ($last !== false) { + $selectpart = substr($querystring, 7, $last - 8); + $selects = explode(',', $selectpart); + } + } elseif (strpos($querystring, 'PRAGMA table_info') === 0) { + $selects = array('cid', 'name', 'type', 'notnull', 'dflt_value', 'pk'); + } elseif(strpos($querystring, 'PRAGMA index_list') === 0) { + $selects = array('seq', 'name', 'unique'); + } elseif(strpos($querystring, 'PRAGMA index_info') === 0) { + $selects = array('seqno', 'cid', 'name'); + } + while ($j < $num_fields) { + if (preg_match('/\bAS\s+(.*)/i', $selects[$j], $matches)) { + $columnName = trim($matches[1],'"'); + } else { + $columnName = trim(str_replace('"', '', $selects[$j])); + } + + if (strpos($selects[$j], 'DISTINCT') === 0) { + $columnName = str_ireplace('DISTINCT', '', $columnName); + } if (strpos($columnName, '.')) { $parts = explode('.', $columnName); - $this->map[$index++] = array($parts[0], $parts[1]); + $this->map[$index++] = array(trim($parts[0]), trim($parts[1])); } else { $this->map[$index++] = array(0, $columnName); } @@ -431,34 +323,30 @@ class DboSqlite extends DboSource { /** * Fetches the next row from the current result set * - * @return unknown + * @return mixed array with results fetched and mapped to column names or false if there is no results left to fetch */ function fetchResult() { - if ($row = sqlite_fetch_array($this->results, SQLITE_ASSOC)) { + if ($row = $this->_result->fetch()) { $resultRow = array(); - $i = 0; - - foreach ($row as $index => $field) { - if (strpos($index, '.')) { - list($table, $column) = explode('.', str_replace('"', '', $index)); - $resultRow[$table][$column] = $row[$index]; - } else { - $resultRow[0][str_replace('"', '', $index)] = $row[$index]; - } - $i++; + foreach ($this->map as $col => $meta) { + list($table, $column) = $meta; + $resultRow[$table][$column] = $row[$col]; } return $resultRow; } else { + $this->_result->closeCursor(); return false; } } + /** * Returns a limit statement in the correct format for the particular database. * * @param integer $limit Limit of results returned * @param integer $offset Offset from which to start results * @return string SQL limit/offset statement + * @access public */ function limit($limit, $offset = null) { if ($limit) { @@ -479,8 +367,9 @@ class DboSqlite extends DboSource { * Generate a database-native column schema string * * @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]), - * where options can be 'default', 'length', or 'key'. + * where options can be 'default', 'length', or 'key'. * @return string + * @access public */ function buildColumn($column) { $name = $type = null; @@ -488,12 +377,12 @@ class DboSqlite extends DboSource { extract($column); if (empty($name) || empty($type)) { - trigger_error(__('Column name or type not defined in schema'), E_USER_WARNING); + trigger_error('Column name or type not defined in schema', E_USER_WARNING); return null; } if (!isset($this->columns[$type])) { - trigger_error(sprintf(__('Column type %s does not exist'), $type), E_USER_WARNING); + trigger_error("Column type {$type} does not exist", E_USER_WARNING); return null; } @@ -509,6 +398,7 @@ class DboSqlite extends DboSource { * Sets the database encoding * * @param string $enc Database encoding + * @access public */ function setEncoding($enc) { if (!in_array($enc, array("UTF-8", "UTF-16", "UTF-16le", "UTF-16be"))) { @@ -521,6 +411,7 @@ class DboSqlite extends DboSource { * Gets the database encoding * * @return string The database encoding + * @access public */ function getEncoding() { return $this->fetchRow('PRAGMA encoding'); @@ -532,6 +423,7 @@ class DboSqlite extends DboSource { * @param array $indexes * @param string $table * @return string + * @access public */ function buildIndex($indexes, $table = null) { $join = array(); @@ -547,11 +439,12 @@ class DboSqlite extends DboSource { $out .= 'UNIQUE '; } if (is_array($value['column'])) { - $value['column'] = implode(', ', array_map(array(&$this, 'name'), $value['column'])); + $value['column'] = join(', ', array_map(array(&$this, 'name'), $value['column'])); } else { $value['column'] = $this->name($value['column']); } - $out .= "INDEX {$name} ON {$table}({$value['column']});"; + $t = trim($table, '"'); + $out .= "INDEX {$t}_{$name} ON {$table}({$value['column']});"; $join[] = $out; } return $join; @@ -563,13 +456,17 @@ class DboSqlite extends DboSource { * * @param string $model Name of model to inspect * @return array Fields in table. Keys are column and unique + * @access public */ function index(&$model) { $index = array(); $table = $this->fullTableName($model); if ($table) { $indexes = $this->query('PRAGMA index_list(' . $table . ')'); - $tableInfo = $this->query('PRAGMA table_info(' . $table . ')'); + + if (is_bool($indexes)) { + return array(); + } foreach ($indexes as $i => $info) { $key = array_pop($info); $keyInfo = $this->query('PRAGMA index_info("' . $key['name'] . '")'); @@ -600,16 +497,17 @@ class DboSqlite extends DboSource { * @param string $type * @param array $data * @return string + * @access public */ function renderStatement($type, $data) { switch (strtolower($type)) { case 'schema': extract($data); - - foreach (array('columns', 'indexes') as $var) { - if (is_array(${$var})) { - ${$var} = "\t" . implode(",\n\t", array_filter(${$var})); - } + if (is_array($columns)) { + $columns = "\t" . join(",\n\t", array_filter($columns)); + } + if (is_array($indexes)) { + $indexes = "\t" . join("\n\t", array_filter($indexes)); } return "CREATE TABLE {$table} (\n{$columns});\n{$indexes}"; break; @@ -618,4 +516,32 @@ class DboSqlite extends DboSource { break; } } -} + +/** + * PDO deals in objects, not resources, so overload accordingly. + * + * @return boolean + * @access public + */ + function hasResult() { + return is_object($this->_result); + } + +/** + * Generate a "drop table" statement for the given Schema object + * + * @param object $schema An instance of a subclass of CakeSchema + * @param string $table Optional. If specified only the table name given will be generated. + * Otherwise, all tables defined in the schema are generated. + * @return string + */ + public function dropSchema(CakeSchema $schema, $table = null) { + $out = ''; + foreach ($schema->tables as $curTable => $columns) { + if (!$table || $table == $curTable) { + $out .= 'DROP TABLE IF EXISTS ' . $this->fullTableName($curTable) . ";\n"; + } + } + return $out; + } +} \ No newline at end of file diff --git a/cake/tests/cases/console/shells/acl.test.php b/cake/tests/cases/console/shells/acl.test.php index 305ee1ebc..0bb6489b1 100644 --- a/cake/tests/cases/console/shells/acl.test.php +++ b/cake/tests/cases/console/shells/acl.test.php @@ -44,8 +44,6 @@ class AclShellTest extends CakeTestCase { * @return void */ public function setUp() { - parent::setUp(); - Configure::write('Acl.database', 'test'); Configure::write('Acl.classname', 'DbAcl'); @@ -59,7 +57,6 @@ class AclShellTest extends CakeTestCase { ); $collection = new ComponentCollection(); $this->Task->Acl = new AclComponent($collection); - $this->Task->params['datasource'] = 'test'; } @@ -206,8 +203,9 @@ class AclShellTest extends CakeTestCase { $this->Task->expects($this->at(0))->method('out') ->with($this->matchesRegularExpression('/granted/'), true); $this->Task->grant(); + $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2)); + $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']); - $node = $this->Task->Acl->Aro->read(null, 4); $this->assertFalse(empty($node['Aco'][0])); $this->assertEqual($node['Aco'][0]['Permission']['_create'], 1); } @@ -224,7 +222,8 @@ class AclShellTest extends CakeTestCase { $this->Task->deny(); - $node = $this->Task->Acl->Aro->read(null, 4); + $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2)); + $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']); $this->assertFalse(empty($node['Aco'][0])); $this->assertEqual($node['Aco'][0]['Permission']['_create'], -1); } @@ -274,7 +273,8 @@ class AclShellTest extends CakeTestCase { $this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'all'); $this->Task->inherit(); - $node = $this->Task->Acl->Aro->read(null, 4); + $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2)); + $node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']); $this->assertFalse(empty($node['Aco'][0])); $this->assertEqual($node['Aco'][0]['Permission']['_create'], 0); } @@ -286,9 +286,13 @@ class AclShellTest extends CakeTestCase { */ public function testGetPath() { $this->Task->args = array('aro', 'AuthUser.2'); - $this->Task->expects($this->at(2))->method('out')->with('[1] ROOT'); - $this->Task->expects($this->at(3))->method('out')->with(' [2] admins'); - $this->Task->expects($this->at(4))->method('out')->with(' [4] Elrond'); + $node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2)); + $first = $node[0]['Aro']['id']; + $second = $node[1]['Aro']['id']; + $last = $node[2]['Aro']['id']; + $this->Task->expects($this->at(2))->method('out')->with('['.$last.'] ROOT'); + $this->Task->expects($this->at(3))->method('out')->with(' ['.$second.'] admins'); + $this->Task->expects($this->at(4))->method('out')->with(' ['.$first.'] Elrond'); $this->Task->getPath(); } diff --git a/cake/tests/cases/console/shells/bake.test.php b/cake/tests/cases/console/shells/bake.test.php index fc5e79163..6f68e8bb3 100644 --- a/cake/tests/cases/console/shells/bake.test.php +++ b/cake/tests/cases/console/shells/bake.test.php @@ -96,7 +96,7 @@ class BakeShellTest extends CakeTestCase { $this->Shell->expects($this->at(1))->method('out')->with('Bake All'); $this->Shell->expects($this->at(3))->method('out')->with('User Model was baked.'); - $this->Shell->expects($this->at(5))->method('out')->with('User Controller was baked.'); + $this->Shell->expects($this->at(5))->method('out')->with('Bake All complete'); $this->Shell->expects($this->at(7))->method('out')->with('User Views were baked.'); $this->Shell->expects($this->at(8))->method('out')->with('Bake All complete'); diff --git a/cake/tests/cases/console/shells/schema.test.php b/cake/tests/cases/console/shells/schema.test.php index 611928e53..6a7a619a1 100644 --- a/cake/tests/cases/console/shells/schema.test.php +++ b/cake/tests/cases/console/shells/schema.test.php @@ -223,7 +223,7 @@ class SchemaShellTest extends CakeTestCase { $this->file = new File(TMP . 'tests' . DS . 'i18n.sql'); $contents = $this->file->read(); $this->assertPattern('/DROP TABLE/', $contents); - $this->assertPattern('/CREATE TABLE `i18n`/', $contents); + $this->assertPattern('/CREATE TABLE.*?i18n/', $contents); $this->assertPattern('/id/', $contents); $this->assertPattern('/model/', $contents); $this->assertPattern('/field/', $contents); @@ -253,7 +253,7 @@ class SchemaShellTest extends CakeTestCase { $this->file = new File(TMP . 'tests' . DS . 'dump_test.sql'); $contents = $this->file->read(); - $this->assertPattern('/CREATE TABLE `test_plugin_acos`/', $contents); + $this->assertPattern('/CREATE TABLE.*?test_plugin_acos/', $contents); $this->assertPattern('/id/', $contents); $this->assertPattern('/model/', $contents); @@ -339,6 +339,7 @@ class SchemaShellTest extends CakeTestCase { ), true); App::objects('plugin', null, false); + $this->db->cacheSources = false; $this->Shell->params = array( 'plugin' => 'TestPlugin', 'connection' => 'test' @@ -366,8 +367,7 @@ class SchemaShellTest extends CakeTestCase { */ public function testCreateNoArgs() { $this->Shell->params = array( - 'connection' => 'test', - 'path' => APP . 'config' . DS . 'sql' + 'connection' => 'test' ); $this->Shell->args = array('i18n'); $this->Shell->startup(); @@ -375,6 +375,8 @@ class SchemaShellTest extends CakeTestCase { $this->Shell->create(); $db = ConnectionManager::getDataSource('test'); + + $db->cacheSources = false; $sources = $db->listSources(); $this->assertTrue(in_array($db->config['prefix'] . 'i18n', $sources)); @@ -396,7 +398,7 @@ class SchemaShellTest extends CakeTestCase { $this->Shell->params = array( 'connection' => 'test', 'name' => 'DbAcl', - 'path' => APP . 'config' . DS . 'schema' + 'path' => CONFIGS . 'schema' ); $this->Shell->args = array('DbAcl', 'acos'); $this->Shell->startup(); @@ -404,6 +406,7 @@ class SchemaShellTest extends CakeTestCase { $this->Shell->create(); $db = ConnectionManager::getDataSource('test'); + $db->cacheSources = false; $sources = $db->listSources(); $this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources), 'acos should be present.'); $this->assertFalse(in_array($db->config['prefix'] . 'aros', $sources), 'aros should not be found.'); diff --git a/cake/tests/cases/libs/all_database.test.php b/cake/tests/cases/libs/all_database.test.php index 797541d05..8c4fc024a 100644 --- a/cake/tests/cases/libs/all_database.test.php +++ b/cake/tests/cases/libs/all_database.test.php @@ -37,8 +37,15 @@ class AllDatabaseTest extends PHPUnit_Framework_TestSuite { $suite = new PHPUnit_Framework_TestSuite('Datasources, Schema and DbAcl tests'); $path = CORE_TEST_CASES . DS . 'libs' . DS . 'model' . DS; - - $tasks = array('db_acl', 'cake_schema', 'connection_manager', 'datasources' . DS . 'dbo_source'); + $tasks = array( + 'db_acl', + 'cake_schema', + 'connection_manager', + 'datasources' . DS . 'dbo_source', + 'datasources' . DS . 'dbo' . DS . 'dbo_mysql', + 'datasources' . DS . 'dbo' . DS . 'dbo_postgres', + 'datasources' . DS . 'dbo' . DS . 'dbo_sqlite' + ); foreach ($tasks as $task) { $suite->addTestFile($path . $task . '.test.php'); } diff --git a/cake/tests/cases/libs/model/behavior_collection.test.php b/cake/tests/cases/libs/model/behavior_collection.test.php index 7528d3b0a..12ce1dd4f 100644 --- a/cake/tests/cases/libs/model/behavior_collection.test.php +++ b/cake/tests/cases/libs/model/behavior_collection.test.php @@ -546,6 +546,7 @@ class BehaviorCollectionTest extends CakeTestCase { */ function testBehaviorToggling() { $Apple = new Apple(); + $expected = $Apple->find('all'); $this->assertIdentical($Apple->Behaviors->enabled(), array()); $Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value'))); diff --git a/cake/tests/cases/libs/model/behaviors/translate.test.php b/cake/tests/cases/libs/model/behaviors/translate.test.php index aa833dc33..ee13fe3bb 100644 --- a/cake/tests/cases/libs/model/behaviors/translate.test.php +++ b/cake/tests/cases/libs/model/behaviors/translate.test.php @@ -185,6 +185,7 @@ class TranslateBehaviorTest extends CakeTestCase { $TestModel = new TranslatedItem(); $TestModel->locale = 'eng'; + $result = $TestModel->read(null, 1); $expected = array( 'TranslatedItem' => array( @@ -313,17 +314,17 @@ class TranslateBehaviorTest extends CakeTestCase { $result = $TestModel->find('all', array('fields' => array('TranslatedItem.title'))); $expected = array( array( - 'TranslatedItem' => array('id' => 1, 'locale' => 'eng', 'title' => 'Title #1'), + 'TranslatedItem' => array('id' => 1, 'locale' => 'eng', 'title' => 'Title #1', 'slug' => 'first_translated'), 'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')), 'Content' => array(array('foreign_key' => 1, 'content' => 'Content #1')) ), array( - 'TranslatedItem' => array('id' => 2, 'locale' => 'eng', 'title' => 'Title #2'), + 'TranslatedItem' => array('id' => 2, 'locale' => 'eng', 'title' => 'Title #2', 'slug' => 'second_translated'), 'Title' => array(array('foreign_key' => 2, 'content' => 'Title #2')), 'Content' => array(array('foreign_key' => 2, 'content' => 'Content #2')) ), array( - 'TranslatedItem' => array('id' => 3, 'locale' => 'eng', 'title' => 'Title #3'), + 'TranslatedItem' => array('id' => 3, 'locale' => 'eng', 'title' => 'Title #3','slug' => 'third_translated'), 'Title' => array(array('foreign_key' => 3, 'content' => 'Title #3')), 'Content' => array(array('foreign_key' => 3, 'content' => 'Content #3')) ) @@ -342,16 +343,7 @@ class TranslateBehaviorTest extends CakeTestCase { $TestModel = new TranslatedItem(); $TestModel->locale = array('deu', 'eng', 'cze'); - $delete = array( - array('locale' => 'deu'), - array('foreign_key' => 1, 'field' => 'title', 'locale' => 'eng'), - array('foreign_key' => 1, 'field' => 'content', 'locale' => 'cze'), - array('foreign_key' => 2, 'field' => 'title', 'locale' => 'cze'), - array('foreign_key' => 2, 'field' => 'content', 'locale' => 'eng'), - array('foreign_key' => 3, 'field' => 'title') - ); - $I18nModel = ClassRegistry::getObject('TranslateTestModel'); - $I18nModel->deleteAll(array('or' => $delete)); + $result = $TestModel->read(null, 1); $expected = array( @@ -359,8 +351,8 @@ class TranslateBehaviorTest extends CakeTestCase { 'id' => 1, 'slug' => 'first_translated', 'locale' => 'deu', - 'title' => 'Titulek #1', - 'content' => 'Content #1' + 'title' => 'Titel #1', + 'content' => 'Inhalt #1' ) ); $this->assertEqual($result, $expected); @@ -371,28 +363,29 @@ class TranslateBehaviorTest extends CakeTestCase { 'TranslatedItem' => array( 'slug' => 'first_translated', 'locale' => 'deu', - 'title' => 'Titulek #1', - 'content' => 'Content #1' + 'content' => 'Inhalt #1', + 'title' => 'Titel #1' ) ), array( 'TranslatedItem' => array( 'slug' => 'second_translated', 'locale' => 'deu', - 'title' => 'Title #2', - 'content' => 'Obsah #2' + 'title' => 'Titel #2', + 'content' => 'Inhalt #2' ) ), array( 'TranslatedItem' => array( 'slug' => 'third_translated', 'locale' => 'deu', - 'title' => '', - 'content' => 'Content #3' + 'title' => 'Titel #3', + 'content' => 'Inhalt #3' ) ) ); - $this->assertEqual($result, $expected); + + $this->assertEquals($result, $expected); } /** @@ -640,6 +633,8 @@ class TranslateBehaviorTest extends CakeTestCase { $translations = array('title' => 'Title', 'content' => 'Content'); $TestModel->bindTranslation($translations, false); $result = $TestModel->read(null, 1); + $result['Title'] = Set::sort($result['Title'], '{n}.id', 'asc'); + $result['Content'] = Set::sort($result['Content'], '{n}.id', 'asc'); $expected = array( 'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated', 'locale' => 'cze', 'title' => 'Titulek #1', 'content' => 'Upraveny obsah #1'), 'Title' => array( diff --git a/cake/tests/cases/libs/model/behaviors/tree.test.php b/cake/tests/cases/libs/model/behaviors/tree.test.php index 19d55e668..75375459f 100644 --- a/cake/tests/cases/libs/model/behaviors/tree.test.php +++ b/cake/tests/cases/libs/model/behaviors/tree.test.php @@ -1431,7 +1431,6 @@ class ScopedTreeTest extends CakeTestCase { function testTranslatingTree() { $this->Tree = new FlagTree(); $this->Tree->cacheQueries = false; - $this->Tree->translateModel = 'TranslateTreeTestModel'; $this->Tree->Behaviors->attach('Translate', array('name')); //Save diff --git a/cake/tests/cases/libs/model/cake_schema.test.php b/cake/tests/cases/libs/model/cake_schema.test.php index 1eb8e7228..ff5c8ffdd 100644 --- a/cake/tests/cases/libs/model/cake_schema.test.php +++ b/cake/tests/cases/libs/model/cake_schema.test.php @@ -210,7 +210,7 @@ class TestAppSchema extends CakeSchema { */ public $datatypes = array( 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'), - 'float_field' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => ''), + 'float_field' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => '', 'collate' => null, 'comment' => null), 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)), 'tableParameters' => array() ); @@ -523,6 +523,7 @@ class CakeSchemaTest extends CakeTestCase { */ function setUp() { parent::setUp(); + ConnectionManager::getDataSource('test')->cacheSources = false; $this->Schema = new TestAppSchema(); } @@ -577,18 +578,28 @@ class CakeSchemaTest extends CakeTestCase { } $this->assertEqual( - $read['tables']['datatypes']['float_field'], - $this->Schema->tables['datatypes']['float_field'] + $read['tables']['datatypes']['float_field']['length'], + $this->Schema->tables['datatypes']['float_field']['length'] ); - $db =& ConnectionManager::getDataSource('test'); + $this->assertEqual( + $read['tables']['datatypes']['float_field']['type'], + $this->Schema->tables['datatypes']['float_field']['type'] + ); + + $this->assertEqual( + $read['tables']['datatypes']['float_field']['null'], + $this->Schema->tables['datatypes']['float_field']['null'] + ); + + $db = ConnectionManager::getDataSource('test'); $config = $db->config; $config['prefix'] = 'schema_test_prefix_'; ConnectionManager::create('schema_prefix', $config); $read = $this->Schema->read(array('connection' => 'schema_prefix', 'models' => false)); $this->assertTrue(empty($read['tables'])); - $SchemaPost =& ClassRegistry::init('SchemaPost'); + $SchemaPost = ClassRegistry::init('SchemaPost'); $SchemaPost->table = 'sts'; $SchemaPost->tablePrefix = 'po'; $read = $this->Schema->read(array( @@ -612,7 +623,7 @@ class CakeSchemaTest extends CakeTestCase { * @return void */ function testSchemaReadWithTablePrefix() { - $model =& new SchemaPrefixAuthUser(); + $model = new SchemaPrefixAuthUser(); $Schema = new CakeSchema(); $read = $Schema->read(array( @@ -684,7 +695,7 @@ class CakeSchemaTest extends CakeTestCase { return; } - $db2 =& ConnectionManager::getDataSource('test2'); + $db2 = ConnectionManager::getDataSource('test2'); $fixture = new SchemaCrossDatabaseFixture(); $fixture->create($db2); $fixture->insert($db2); @@ -953,7 +964,7 @@ class CakeSchemaTest extends CakeTestCase { * @return void */ function testSchemaLoading() { - $Other =& $this->Schema->load(array('name' => 'MyOtherApp', 'path' => TMP . 'tests')); + $Other = $this->Schema->load(array('name' => 'MyOtherApp', 'path' => TMP . 'tests')); $this->assertEqual($Other->name, 'MyOtherApp'); $this->assertEqual($Other->tables, $this->Schema->tables); } @@ -967,7 +978,7 @@ class CakeSchemaTest extends CakeTestCase { App::build(array( 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS) )); - $Other =& $this->Schema->load(array('name' => 'TestPluginApp', 'plugin' => 'TestPlugin')); + $Other = $this->Schema->load(array('name' => 'TestPluginApp', 'plugin' => 'TestPlugin')); $this->assertEqual($Other->name, 'TestPluginApp'); $this->assertEqual(array_keys($Other->tables), array('test_plugin_acos')); @@ -981,7 +992,7 @@ class CakeSchemaTest extends CakeTestCase { * @return void */ function testSchemaCreateTable() { - $db =& ConnectionManager::getDataSource('test'); + $db = ConnectionManager::getDataSource('test'); $db->cacheSources = false; $Schema = new CakeSchema(array( diff --git a/cake/tests/cases/libs/model/connection_manager.test.php b/cake/tests/cases/libs/model/connection_manager.test.php index d8e8b848f..04212fa4a 100644 --- a/cake/tests/cases/libs/model/connection_manager.test.php +++ b/cake/tests/cases/libs/model/connection_manager.test.php @@ -226,7 +226,6 @@ class ConnectionManagerTest extends CakeTestCase { function testLoadDataSource() { $connections = array( array('classname' => 'DboMysql', 'filename' => 'dbo' . DS . 'dbo_mysql'), - array('classname' => 'DboMysqli', 'filename' => 'dbo' . DS . 'dbo_mysqli'), array('classname' => 'DboMssql', 'filename' => 'dbo' . DS . 'dbo_mssql'), array('classname' => 'DboOracle', 'filename' => 'dbo' . DS . 'dbo_oracle'), ); diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php index 5b81954c9..9df7be0fa 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php @@ -18,138 +18,8 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysql')); - -/** - * DboMysqlTestDb class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class DboMysqlTestDb extends DboMysql { - -/** - * simulated property - * - * @var array - * @access public - */ - public $simulated = array(); - -/** - * testing property - * - * @var bool true - * @access public - */ - public $testing = true; - -/** - * execute method - * - * @param mixed $sql - * @access protected - * @return void - */ - function _execute($sql) { - if ($this->testing) { - $this->simulated[] = $sql; - return null; - } - return parent::_execute($sql); - } - -/** - * getLastQuery method - * - * @access public - * @return void - */ - function getLastQuery() { - return $this->simulated[count($this->simulated) - 1]; - } -} - -/** - * MysqlTestModel class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class MysqlTestModel extends Model { - -/** - * name property - * - * @var string 'MysqlTestModel' - * @access public - */ - public $name = 'MysqlTestModel'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * find method - * - * @param mixed $conditions - * @param mixed $fields - * @param mixed $order - * @param mixed $recursive - * @access public - * @return void - */ - function find($conditions = null, $fields = null, $order = null, $recursive = null) { - return $conditions; - } - -/** - * findAll method - * - * @param mixed $conditions - * @param mixed $fields - * @param mixed $order - * @param mixed $recursive - * @access public - * @return void - */ - function findAll($conditions = null, $fields = null, $order = null, $recursive = null) { - return $conditions; - } - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - return array( - 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'), - 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'), - 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''), - 'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''), - 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), - 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) - ); - } -} +App::import('Model', 'App'); +require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php'; /** * DboMysqlTest class @@ -158,7 +28,26 @@ class MysqlTestModel extends Model { * @subpackage cake.tests.cases.libs.model.datasources.dbo */ class DboMysqlTest extends CakeTestCase { - public $fixtures = array('core.binary_test'); +/** + * autoFixtures property + * + * @var bool false + * @access public + */ + public $autoFixtures = false; + +/** + * fixtures property + * + * @var array + * @access public + */ + public $fixtures = array( + 'core.apple', 'core.article', 'core.articles_tag', 'core.attachment', 'core.comment', + 'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author', 'core.data_test', + 'core.binary_test' + ); + /** * The Dbo instance to be tested * @@ -173,6 +62,7 @@ class DboMysqlTest extends CakeTestCase { */ public function setUp() { $this->Dbo = ConnectionManager::getDataSource('test'); + $this->skipIf(!($this->Dbo instanceof DboMysql)); if ($this->Dbo->config['driver'] !== 'mysql') { $this->markTestSkipped('The MySQL extension is not available.'); } @@ -194,6 +84,7 @@ class DboMysqlTest extends CakeTestCase { /** * Test Dbo value method * + * @group quoting */ public function testQuoting() { $result = $this->Dbo->fields($this->model); @@ -236,7 +127,7 @@ class DboMysqlTest extends CakeTestCase { $result = $this->Dbo->value('', 'integer'); $this->assertEqual($expected, $result); - $expected = 0; + $expected = "'0'"; $result = $this->Dbo->value('', 'boolean'); $this->assertEqual($expected, $result); @@ -252,16 +143,17 @@ class DboMysqlTest extends CakeTestCase { /** * test that localized floats don't cause trouble. * + * @group quoting * @return void */ function testLocalizedFloats() { $restore = setlocale(LC_ALL, null); setlocale(LC_ALL, 'de_DE'); - $result = $this->db->value(3.141593, 'float'); + $result = $this->Dbo->value(3.141593, 'float'); $this->assertEqual((string)$result, '3.141593'); - - $result = $this->db->value(3.141593); + + $result = $this->Dbo->value(3.141593); $this->assertEqual((string)$result, '3.141593'); setlocale(LC_ALL, $restore); @@ -270,13 +162,13 @@ class DboMysqlTest extends CakeTestCase { /** * testTinyintCasting method * - * @access public + * * @return void */ function testTinyintCasting() { $this->Dbo->cacheSources = false; $tableName = 'tinyint_' . uniqid(); - $this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));'); + $this->Dbo->rawQuery('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));'); $this->model = new CakeTestModel(array( 'name' => 'Tinyint', 'table' => $tableName, 'ds' => 'test' @@ -304,47 +196,49 @@ class DboMysqlTest extends CakeTestCase { $this->assertIdentical($result['Tinyint']['small_int'], '0'); $this->model->deleteAll(true); - $this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($tableName)); + $this->Dbo->rawQuery('DROP TABLE ' . $this->Dbo->fullTableName($tableName)); } /** * testIndexDetection method * + * @group indices * @return void */ public function testIndexDetection() { $this->Dbo->cacheSources = false; $name = $this->Dbo->fullTableName('simple'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));'); + $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));'); $expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1)); $result = $this->Dbo->index('simple', false); + $this->Dbo->rawQuery('DROP TABLE ' . $name); $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); + $name = $this->Dbo->fullTableName('with_a_key'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));'); + $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));'); $expected = array( 'PRIMARY' => array('column' => 'id', 'unique' => 1), 'pointless_bool' => array('column' => 'bool', 'unique' => 0), ); $result = $this->Dbo->index('with_a_key', false); + $this->Dbo->rawQuery('DROP TABLE ' . $name); $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); $name = $this->Dbo->fullTableName('with_two_keys'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));'); + $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));'); $expected = array( 'PRIMARY' => array('column' => 'id', 'unique' => 1), 'pointless_bool' => array('column' => 'bool', 'unique' => 0), 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), ); $result = $this->Dbo->index('with_two_keys', false); + $this->Dbo->rawQuery('DROP TABLE ' . $name); $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); $name = $this->Dbo->fullTableName('with_compound_keys'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));'); + $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));'); $expected = array( 'PRIMARY' => array('column' => 'id', 'unique' => 1), 'pointless_bool' => array('column' => 'bool', 'unique' => 0), @@ -352,11 +246,11 @@ class DboMysqlTest extends CakeTestCase { 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0), ); $result = $this->Dbo->index('with_compound_keys', false); + $this->Dbo->rawQuery('DROP TABLE ' . $name); $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); $name = $this->Dbo->fullTableName('with_multiple_compound_keys'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));'); + $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));'); $expected = array( 'PRIMARY' => array('column' => 'id', 'unique' => 1), 'pointless_bool' => array('column' => 'bool', 'unique' => 0), @@ -365,14 +259,13 @@ class DboMysqlTest extends CakeTestCase { 'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0), ); $result = $this->Dbo->index('with_multiple_compound_keys', false); + $this->Dbo->rawQuery('DROP TABLE ' . $name); $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); } /** * testBuildColumn method * - * @access public * @return void */ function testBuildColumn() { @@ -409,12 +302,13 @@ class DboMysqlTest extends CakeTestCase { * MySQL 4.x returns index data in a different format, * Using a mock ensure that MySQL 4.x output is properly parsed. * + * @group indices * @return void */ function testIndexOnMySQL4Output() { $name = $this->Dbo->fullTableName('simple'); - $mockDbo = $this->getMock('DboMysql', array('query')); + $mockDbo = $this->getMock('DboMysql', array('connect', '_execute', 'getVersion')); $columnData = array( array('0' => array( 'Table' => 'with_compound_keys', @@ -487,10 +381,17 @@ class DboMysqlTest extends CakeTestCase { 'Comment' => '' )) ); + + $mockDbo->expects($this->once())->method('getVersion')->will($this->returnValue('4.1')); + $resultMock = $this->getMock('PDOStatement', array('fetch')); $mockDbo->expects($this->once()) - ->method('query') + ->method('_execute') ->with('SHOW INDEX FROM ' . $name) - ->will($this->returnValue($columnData)); + ->will($this->returnValue($resultMock)); + + foreach ($columnData as $i => $data) { + $resultMock->expects($this->at($i))->method('fetch')->will($this->returnValue((object) $data)); + } $result = $mockDbo->index($name, false); $expected = array( @@ -552,7 +453,7 @@ class DboMysqlTest extends CakeTestCase { /** * testAlterSchemaIndexes method * - * @access public + * @group indices * @return void */ function testAlterSchemaIndexes() { @@ -568,7 +469,15 @@ class DboMysqlTest extends CakeTestCase { 'group1' => array('type' => 'integer', 'null' => true), 'group2' => array('type' => 'integer', 'null' => true) ))); - $this->Dbo->query($this->Dbo->createSchema($schema1)); + $result = $this->Dbo->createSchema($schema1); + $this->assertContains('`id` int(11) DEFAULT 0 NOT NULL,', $result); + $this->assertContains('`name` varchar(50) NOT NULL,', $result); + $this->assertContains('`group1` int(11) DEFAULT NULL', $result); + $this->assertContains('`group2` int(11) DEFAULT NULL', $result); + + //Test that the string is syntactically correct + $query = $this->Dbo->getConnection()->prepare($result); + $this->assertEquals($result, $query->queryString); $schema2 = new CakeSchema(array( 'name' => 'AlterTest2', @@ -584,10 +493,17 @@ class DboMysqlTest extends CakeTestCase { 'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => 0), 'PRIMARY' => array('column' => 'id', 'unique' => 1)) ))); - $this->Dbo->query($this->Dbo->alterSchema($schema2->compare($schema1))); - $indexes = $this->Dbo->index('altertest'); - $this->assertEqual($schema2->tables['altertest']['indexes'], $indexes); + $result = $this->Dbo->alterSchema($schema2->compare($schema1)); + $this->assertContains('ALTER TABLE `altertest`', $result); + $this->assertContains('ADD KEY name_idx (`name`),', $result); + $this->assertContains('ADD KEY group_idx (`group1`),', $result); + $this->assertContains('ADD KEY compound_idx (`group1`, `group2`),', $result); + $this->assertContains('ADD PRIMARY KEY (`id`);', $result); + + //Test that the string is syntactically correct + $query = $this->Dbo->getConnection()->prepare($result); + $this->assertEquals($result, $query->queryString); // Change three indexes, delete one and add another one $schema3 = new CakeSchema(array( @@ -605,35 +521,50 @@ class DboMysqlTest extends CakeTestCase { 'id_name_idx' => array('column' => array('id', 'name'), 'unique' => 0)) ))); - $this->Dbo->query($this->Dbo->alterSchema($schema3->compare($schema2))); + $result = $this->Dbo->alterSchema($schema3->compare($schema2)); + $this->assertContains('ALTER TABLE `altertest`', $result); + $this->assertContains('DROP PRIMARY KEY,', $result); + $this->assertContains('DROP KEY name_idx,', $result); + $this->assertContains('DROP KEY group_idx,', $result); + $this->assertContains('DROP KEY compound_idx,', $result); + $this->assertContains('ADD KEY id_name_idx (`id`, `name`),', $result); + $this->assertContains('ADD UNIQUE KEY name_idx (`name`),', $result); + $this->assertContains('ADD KEY group_idx (`group2`),', $result); + $this->assertContains('ADD KEY compound_idx (`group2`, `group1`);', $result); - $indexes = $this->Dbo->index('altertest'); - $this->assertEqual($schema3->tables['altertest']['indexes'], $indexes); + $query = $this->Dbo->getConnection()->prepare($result); + $this->assertEquals($result, $query->queryString); // Compare us to ourself. $this->assertEqual($schema3->compare($schema3), array()); // Drop the indexes - $this->Dbo->query($this->Dbo->alterSchema($schema1->compare($schema3))); + $result = $this->Dbo->alterSchema($schema1->compare($schema3)); - $indexes = $this->Dbo->index('altertest'); - $this->assertEqual(array(), $indexes); + $this->assertContains('ALTER TABLE `altertest`', $result); + $this->assertContains('DROP KEY name_idx,', $result); + $this->assertContains('DROP KEY group_idx,', $result); + $this->assertContains('DROP KEY compound_idx,', $result); + $this->assertContains('DROP KEY id_name_idx;', $result); - $this->Dbo->query($this->Dbo->dropSchema($schema1)); + $query = $this->Dbo->getConnection()->prepare($result); + $this->assertEquals($result, $query->queryString); } + /** * test saving and retrieval of blobs * * @return void */ function testBlobSaving() { + $this->loadFixtures('BinaryTest'); $this->Dbo->cacheSources = false; $data = "GIF87ab Ò4A¿¿¿ˇˇˇ,b ¢îè©ÀÌ#¥⁄ã≥fi:¯Ü‚Héá¶jV∂ÓúÎL≥çÀóËıÎ…>ï≈ vFE%ÒâLFI<†µw˝±≈£7˘ç^H“≤« >Éâ*∑ÇnÖA•Ù|flêèj£:=ÿ6óUàµ5'∂®àA¬ñ∆ˆGE(gt’≈àÚyÁó«7 ‚VìöÇ√˙Ç™ k”:;kÀAõ{*¡€Î˚˚[;;"; - $model = new AppModel(array('name' => 'BinaryTest', 'ds' => 'test')); + $model = new CakeTestModel(array('name' => 'BinaryTest', 'ds' => 'test')); $model->save(compact('data')); $result = $model->find('first'); @@ -662,7 +593,7 @@ class DboMysqlTest extends CakeTestCase { ) ) )); - $this->Dbo->query($this->Dbo->createSchema($schema1)); + $this->Dbo->rawQuery($this->Dbo->createSchema($schema1)); $schema2 = new CakeSchema(array( 'name' => 'AlterTest1', 'connection' => 'test', @@ -677,17 +608,17 @@ class DboMysqlTest extends CakeTestCase { ) )); $result = $this->Dbo->alterSchema($schema2->compare($schema1)); - $this->assertPattern('/DEFAULT CHARSET=utf8/', $result); - $this->assertPattern('/ENGINE=InnoDB/', $result); - $this->assertPattern('/COLLATE=utf8_general_ci/', $result); + $this->assertContains('DEFAULT CHARSET=utf8', $result); + $this->assertContains('ENGINE=InnoDB', $result); + $this->assertContains('COLLATE=utf8_general_ci', $result); - $this->Dbo->query($result); + $this->Dbo->rawQuery($result); $result = $this->Dbo->listDetailedSources('altertest'); $this->assertEqual($result['Collation'], 'utf8_general_ci'); $this->assertEqual($result['Engine'], 'InnoDB'); $this->assertEqual($result['charset'], 'utf8'); - $this->Dbo->query($this->Dbo->dropSchema($schema1)); + $this->Dbo->rawQuery($this->Dbo->dropSchema($schema1)); } /** @@ -720,7 +651,7 @@ class DboMysqlTest extends CakeTestCase { 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50), ) )); - $result = $this->db->alterSchema($schema2->compare($schema1)); + $result = $this->Dbo->alterSchema($schema2->compare($schema1)); $this->assertEqual(2, substr_count($result, 'field_two'), 'Too many fields'); } @@ -733,23 +664,23 @@ class DboMysqlTest extends CakeTestCase { function testReadTableParameters() { $this->Dbo->cacheSources = $this->Dbo->testing = false; $tableName = 'tinyint_' . uniqid(); - $this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'); + $this->Dbo->rawQuery('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'); $result = $this->Dbo->readTableParameters($tableName); + $this->Dbo->rawQuery('DROP TABLE ' . $this->Dbo->fullTableName($tableName)); $expected = array( 'charset' => 'utf8', 'collate' => 'utf8_unicode_ci', 'engine' => 'InnoDB'); $this->assertEqual($result, $expected); - $this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($tableName)); - $this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=MyISAM DEFAULT CHARSET=cp1250 COLLATE=cp1250_general_ci;'); + $this->Dbo->rawQuery('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=MyISAM DEFAULT CHARSET=cp1250 COLLATE=cp1250_general_ci;'); $result = $this->Dbo->readTableParameters($tableName); + $this->Dbo->rawQuery('DROP TABLE ' . $this->Dbo->fullTableName($tableName)); $expected = array( 'charset' => 'cp1250', 'collate' => 'cp1250_general_ci', 'engine' => 'MyISAM'); $this->assertEqual($result, $expected); - $this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($tableName)); } /** @@ -792,13 +723,15 @@ class DboMysqlTest extends CakeTestCase { * @return void */ function testVirtualFieldSeparators() { + $this->loadFixtures('BinaryTest'); $model = new CakeTestModel(array('table' => 'binary_tests', 'ds' => 'test', 'name' => 'BinaryTest')); $model->virtualFields = array( 'other__field' => 'SUM(id)' ); - - $this->db->virtualFieldSeparator = '_$_'; - $result = $this->db->fields($model, null, array('data', 'other__field')); + + $this->Dbo->virtualFieldSeparator = '_$_'; + $result = $this->Dbo->fields($model, null, array('data', 'other__field')); + $expected = array('`BinaryTest`.`data`', '(SUM(id)) AS `BinaryTest_$_other__field`'); $this->assertEqual($result, $expected); } @@ -827,15 +760,2612 @@ class DboMysqlTest extends CakeTestCase { ) ) )); - $this->db->execute($this->db->createSchema($schema)); + + $this->Dbo->execute($this->Dbo->createSchema($schema)); $model = new CakeTestModel(array('table' => 'testdescribes', 'name' => 'Testdescribes')); - $result = $this->db->describe($model); + $result = $this->Dbo->describe($model); + $this->Dbo->execute($this->Dbo->dropSchema($schema)); + $this->assertEqual($result['stringy']['collate'], 'cp1250_general_ci'); $this->assertEqual($result['stringy']['charset'], 'cp1250'); $this->assertEqual($result['other_col']['comment'], 'Test Comment'); - - $this->db->execute($this->db->dropSchema($schema)); } +/** + * Tests that listSources method sends the correct query and parses the result accordingly + * @return void + */ + public function testListSources() { + $db = $this->getMock('DboMysql', array('connect', '_execute')); + $queryResult = $this->getMock('PDOStatement'); + $db->expects($this->once()) + ->method('_execute') + ->with('SHOW TABLES FROM cake') + ->will($this->returnValue($queryResult)); + $queryResult->expects($this->at(0)) + ->method('fetch') + ->will($this->returnValue(array('cake_table'))); + $queryResult->expects($this->at(1)) + ->method('fetch') + ->will($this->returnValue(array('another_table'))); + $queryResult->expects($this->at(2)) + ->method('fetch') + ->will($this->returnValue(null)); + + $tables = $db->listSources(); + $this->assertEqual($tables, array('cake_table', 'another_table')); + } + +/** + * Tests that getVersion method sends the correct query for getting the mysql version + * @return void + */ + public function testGetVersion() { + $version = $this->Dbo->getVersion(); + $this->assertTrue(is_string($version)); + } + +/** + * Tests that getVersion method sends the correct query for getting the client encoding + * @return void + */ + public function testGetEncoding() { + $db = $this->getMock('DboMysql', array('connect', '_execute')); + $queryResult = $this->getMock('PDOStatement'); + + $db->expects($this->once()) + ->method('_execute') + ->with('SHOW VARIABLES LIKE ?', array('character_set_client')) + ->will($this->returnValue($queryResult)); + $result = new StdClass; + $result->Value = 'utf-8'; + $queryResult->expects($this->once()) + ->method('fetchObject') + ->will($this->returnValue($result)); + + $encoding = $db->getEncoding(); + $this->assertEqual('utf-8', $encoding); + } + +/** + * testFieldDoubleEscaping method + * + * @access public + * @return void + */ + function testFieldDoubleEscaping() { + $test = $this->getMock('DboMysql', array('connect', '_execute', 'execute')); + $this->Model = $this->getMock('Article2', array('getDataSource')); + $this->Model->alias = 'Article'; + $this->Model->expects($this->any()) + ->method('getDataSource') + ->will($this->returnValue($test)); + + $this->assertEqual($this->Model->escapeField(), '`Article`.`id`'); + $result = $test->fields($this->Model, null, $this->Model->escapeField()); + $this->assertEqual($result, array('`Article`.`id`')); + + $test->expects($this->at(0))->method('execute') + ->with('SELECT `Article`.`id` FROM `articles` AS `Article` WHERE 1 = 1'); + + $result = $test->read($this->Model, array( + 'fields' => $this->Model->escapeField(), + 'conditions' => null, + 'recursive' => -1 + )); + + $test->startQuote = '['; + $test->endQuote = ']'; + $this->assertEqual($this->Model->escapeField(), '[Article].[id]'); + + $result = $test->fields($this->Model, null, $this->Model->escapeField()); + $this->assertEqual($result, array('[Article].[id]')); + + $test->expects($this->at(0))->method('execute') + ->with('SELECT [Article].[id] FROM [' . $test->fullTableName('articles', false) . '] AS [Article] WHERE 1 = 1'); + $result = $test->read($this->Model, array( + 'fields' => $this->Model->escapeField(), + 'conditions' => null, + 'recursive' => -1 + )); + } + +/** + * testGenerateAssociationQuerySelfJoin method + * + * @return void + */ + function testGenerateAssociationQuerySelfJoin() { + $this->Dbo = $this->getMock('DboMysql', array('connect', '_execute', 'execute')); + $this->startTime = microtime(true); + $this->Model = new Article2(); + $this->_buildRelatedModels($this->Model); + $this->_buildRelatedModels($this->Model->Category2); + $this->Model->Category2->ChildCat = new Category2(); + $this->Model->Category2->ParentCat = new Category2(); + + $queryData = array(); + + foreach ($this->Model->Category2->associations() as $type) { + foreach ($this->Model->Category2->{$type} as $assoc => $assocData) { + $linkModel = $this->Model->Category2->{$assoc}; + $external = isset($assocData['external']); + + if ($this->Model->Category2->alias == $linkModel->alias && $type != 'hasAndBelongsToMany' && $type != 'hasMany') { + $result = $this->Dbo->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null); + $this->assertFalse(empty($result)); + } else { + if ($this->Model->Category2->useDbConfig == $linkModel->useDbConfig) { + $result = $this->Dbo->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null); + $this->assertFalse(empty($result)); + } + } + } + } + + $query = $this->Dbo->generateAssociationQuery($this->Model->Category2, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+(.+)FROM(.+)`Category2`\.`group_id`\s+=\s+`Group`\.`id`\)\s+LEFT JOIN(.+)WHERE\s+1 = 1\s*$/', $query); + + $this->Model = new TestModel4(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $_queryData = $queryData; + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertTrue($result); + + $expected = array( + 'conditions' => array(), + 'fields' => array( + '`TestModel4`.`id`', + '`TestModel4`.`name`', + '`TestModel4`.`created`', + '`TestModel4`.`updated`', + '`TestModel4Parent`.`id`', + '`TestModel4Parent`.`name`', + '`TestModel4Parent`.`created`', + '`TestModel4Parent`.`updated`' + ), + 'joins' => array( + array( + 'table' => '`test_model4`', + 'alias' => 'TestModel4Parent', + 'type' => 'LEFT', + 'conditions' => '`TestModel4`.`parent_id` = `TestModel4Parent`.`id`' + ) + ), + 'order' => array(), + 'limit' => array(), + 'offset' => array(), + 'group' => array() + ); + $this->assertEqual($queryData, $expected); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/', $result); + $this->assertPattern('/FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+`test_model4` AS `TestModel4Parent`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/', $result); + $this->assertPattern('/\s+WHERE\s+1 = 1\s+$/', $result); + + $params['assocData']['type'] = 'INNER'; + $this->Model->belongsTo['TestModel4Parent']['type'] = 'INNER'; + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $_queryData, $params['external'], $resultSet); + $this->assertTrue($result); + $this->assertEqual($_queryData['joins'][0]['type'], 'INNER'); + } + +/** + * buildRelatedModels method + * + * @param mixed $model + * @access protected + * @return void + */ + function _buildRelatedModels(&$model) { + foreach ($model->associations() as $type) { + foreach ($model->{$type} as $assoc => $assocData) { + if (is_string($assocData)) { + $className = $assocData; + } elseif (isset($assocData['className'])) { + $className = $assocData['className']; + } + $model->$className = new $className(); + $model->$className->schema(); + } + } + } + +/** + * &_prepareAssociationQuery method + * + * @param mixed $model + * @param mixed $queryData + * @param mixed $binding + * @access public + * @return void + */ + function &_prepareAssociationQuery(&$model, &$queryData, $binding) { + $type = $binding['type']; + $assoc = $binding['model']; + $assocData = $model->{$type}[$assoc]; + $className = $assocData['className']; + + $linkModel = $model->{$className}; + $external = isset($assocData['external']); + $queryData = $this->Dbo->__scrubQueryData($queryData); + + $result = array_merge(array('linkModel' => &$linkModel), compact('type', 'assoc', 'assocData', 'external')); + return $result; + } + +/** + * testGenerateInnerJoinAssociationQuery method + * + * @access public + * @return void + */ + function testGenerateInnerJoinAssociationQuery() { + $test = $this->getMock('DboMysql', array('connect', '_execute', 'execute')); + $this->Model = $this->getMock('TestModel9', array('getDataSource')); + $this->Model->expects($this->any()) + ->method('getDataSource') + ->will($this->returnValue($test)); + + $this->Model->TestModel8 = $this->getMock('TestModel8', array('getDataSource')); + $this->Model->TestModel8->expects($this->any()) + ->method('getDataSource') + ->will($this->returnValue($test)); + + $test->expects($this->at(0))->method('execute') + ->with(new PHPUnit_Framework_Constraint_PCREMatch('/`TestModel9` LEFT JOIN `test_model8`/')); + + $test->expects($this->at(1))->method('execute') + ->with(new PHPUnit_Framework_Constraint_PCREMatch('/`TestModel9` INNER JOIN `test_model8`/')); + + $test->read($this->Model, array('recursive' => 1)); + $this->Model->belongsTo['TestModel8']['type'] = 'INNER'; + $test->read($this->Model, array('recursive' => 1)); + } + +/** + * testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding method + * + * @access public + * @return void + */ + function testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding() { + $this->Model = new TestModel8(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'hasOne', 'model' => 'TestModel9'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + $_queryData = $queryData; + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertTrue($result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`, `TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`\s+/', $result); + $this->assertPattern('/FROM\s+`test_model8` AS `TestModel8`\s+LEFT JOIN\s+`test_model9` AS `TestModel9`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel9`\.`name` != \'mariano\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + } + +/** + * testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding method + * + * @access public + * @return void + */ + function testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding() { + $this->Model = new TestModel9(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'belongsTo', 'model' => 'TestModel8'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertTrue($result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`, `TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`\s+/', $result); + $this->assertPattern('/FROM\s+`test_model9` AS `TestModel9`\s+LEFT JOIN\s+`test_model8` AS `TestModel8`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel8`\.`name` != \'larry\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + } + +/** + * testGenerateAssociationQuerySelfJoinWithConditions method + * + * @access public + * @return void + */ + function testGenerateAssociationQuerySelfJoinWithConditions() { + $this->Model = new TestModel4(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent'); + $queryData = array('conditions' => array('TestModel4Parent.name !=' => 'mariano')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertTrue($result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/', $result); + $this->assertPattern('/FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+`test_model4` AS `TestModel4Parent`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?`TestModel4Parent`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result); + + $this->Featured2 = new Featured2(); + $this->Featured2->schema(); + + $this->Featured2->bindModel(array( + 'belongsTo' => array( + 'ArticleFeatured2' => array( + 'conditions' => 'ArticleFeatured2.published = \'Y\'', + 'fields' => 'id, title, user_id, published' + ) + ) + )); + + $this->_buildRelatedModels($this->Featured2); + + $binding = array('type' => 'belongsTo', 'model' => 'ArticleFeatured2'); + $queryData = array('conditions' => array()); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Featured2, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Featured2, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertTrue($result); + $result = $this->Dbo->generateAssociationQuery($this->Featured2, $null, null, null, null, $queryData, false, $null); + + $this->assertPattern( + '/^SELECT\s+`Featured2`\.`id`, `Featured2`\.`article_id`, `Featured2`\.`category_id`, `Featured2`\.`name`,\s+'. + '`ArticleFeatured2`\.`id`, `ArticleFeatured2`\.`title`, `ArticleFeatured2`\.`user_id`, `ArticleFeatured2`\.`published`\s+' . + 'FROM\s+`featured2` AS `Featured2`\s+LEFT JOIN\s+`article_featured` AS `ArticleFeatured2`' . + '\s+ON\s+\(`ArticleFeatured2`.`published` = \'Y\'\s+AND\s+`Featured2`\.`article_featured2_id` = `ArticleFeatured2`\.`id`\)' . + '\s+WHERE\s+1\s+=\s+1\s*$/', + $result + ); + } + +/** + * testGenerateAssociationQueryHasOne method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasOne() { + $this->Model = new TestModel4(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'hasOne', 'model' => 'TestModel5'); + + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertTrue($result); + + $result = $this->Dbo->buildJoinStatement($queryData['joins'][0]); + $expected = ' LEFT JOIN `test_model5` AS `TestModel5` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)'; + $this->assertEqual(trim($result), trim($expected)); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+/', $result); + $this->assertPattern('/`test_model5` AS `TestModel5`\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result); + } + +/** + * testGenerateAssociationQueryHasOneWithConditions method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasOneWithConditions() { + $this->Model = new TestModel4(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'hasOne', 'model' => 'TestModel5'); + + $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertTrue($result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + + $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+`test_model5` AS `TestModel5`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel5`.`test_model4_id`\s+=\s+`TestModel4`.`id`\)\s+WHERE/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?\s*`TestModel5`.`name`\s+!=\s+\'mariano\'\s*(?:\))?\s*$/', $result); + } + +/** + * testGenerateAssociationQueryBelongsTo method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryBelongsTo() { + $this->Model = new TestModel5(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type'=>'belongsTo', 'model'=>'TestModel4'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertTrue($result); + + $result = $this->Dbo->buildJoinStatement($queryData['joins'][0]); + $expected = ' LEFT JOIN `test_model4` AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)'; + $this->assertEqual(trim($result), trim($expected)); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+`test_model4` AS `TestModel4`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result); + } + +/** + * testGenerateAssociationQueryBelongsToWithConditions method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryBelongsToWithConditions() { + $this->Model = new TestModel5(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'belongsTo', 'model' => 'TestModel4'); + $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertTrue($result); + + $result = $this->Dbo->buildJoinStatement($queryData['joins'][0]); + $expected = ' LEFT JOIN `test_model4` AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)'; + $this->assertEqual(trim($result), trim($expected)); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+`test_model4` AS `TestModel4`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+`TestModel5`.`name` != \'mariano\'\s*$/', $result); + } + +/** + * testGenerateAssociationQueryHasMany method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasMany() { + $this->Model = new TestModel5(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + + $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE/', $result); + $this->assertPattern('/\s+WHERE\s+`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result); + } + +/** + * testGenerateAssociationQueryHasManyWithLimit method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasManyWithLimit() { + $this->Model = new TestModel5(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $this->Model->hasMany['TestModel6']['limit'] = 2; + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern( + '/^SELECT\s+' . + '`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+'. + 'FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+' . + '`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)\s*'. + 'LIMIT \d*'. + '\s*$/', $result + ); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern( + '/^SELECT\s+'. + '`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+'. + 'FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+'. + '(?:\()?\s*1 = 1\s*(?:\))?'. + '\s*$/', $result + ); + } + +/** + * testGenerateAssociationQueryHasManyWithConditions method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasManyWithConditions() { + $this->Model = new TestModel5(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); + $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?`TestModel5`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result); + } + +/** + * testGenerateAssociationQueryHasManyWithOffsetAndLimit method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasManyWithOffsetAndLimit() { + $this->Model = new TestModel5(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $__backup = $this->Model->hasMany['TestModel6']; + + $this->Model->hasMany['TestModel6']['offset'] = 2; + $this->Model->hasMany['TestModel6']['limit'] = 5; + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + + $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); + $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); + $this->assertPattern('/\s+LIMIT 2,\s*5\s*$/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + + $this->Model->hasMany['TestModel6'] = $__backup; + } + +/** + * testGenerateAssociationQueryHasManyWithPageAndLimit method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasManyWithPageAndLimit() { + $this->Model = new TestModel5(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $__backup = $this->Model->hasMany['TestModel6']; + + $this->Model->hasMany['TestModel6']['page'] = 2; + $this->Model->hasMany['TestModel6']['limit'] = 5; + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); + $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); + $this->assertPattern('/\s+LIMIT 5,\s*5\s*$/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + + $this->Model->hasMany['TestModel6'] = $__backup; + } + +/** + * testGenerateAssociationQueryHasManyWithFields method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasManyWithFields() { + $this->Model = new TestModel5(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array('fields' => array('`TestModel5`.`name`')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); + $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`id`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array('fields' => array('`TestModel5`.`id`, `TestModel5`.`name`')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); + $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array('fields' => array('`TestModel5`.`name`', '`TestModel5`.`created`')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); + $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`id`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + + $this->Model->hasMany['TestModel6']['fields'] = array('name'); + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); + $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + + unset($this->Model->hasMany['TestModel6']['fields']); + + $this->Model->hasMany['TestModel6']['fields'] = array('id', 'name'); + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); + $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + + unset($this->Model->hasMany['TestModel6']['fields']); + + $this->Model->hasMany['TestModel6']['fields'] = array('test_model5_id', 'name'); + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel6`\.`test_model5_id`, `TestModel6`\.`name`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); + $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + + unset($this->Model->hasMany['TestModel6']['fields']); + } + +/** + * test generateAssociationQuery with a hasMany and an aggregate function. + * + * @return void + */ + function testGenerateAssociationQueryHasManyAndAggregateFunction() { + $this->Model = new TestModel5(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); + $queryData = array('fields' => array('MIN(TestModel5.test_model4_id)')); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + $this->Model->recursive = 0; + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, $params['type'], $params['assoc'], $params['assocData'], $queryData, false, $resultSet); + $this->assertPattern('/^SELECT\s+MIN\(`TestModel5`\.`test_model4_id`\)\s+FROM/', $result); + } + +/** + * testGenerateAssociationQueryHasAndBelongsToMany method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasAndBelongsToMany() { + $this->Model = new TestModel4(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model7` AS `TestModel7`\s+JOIN\s+`' . $this->Dbo->fullTableName('test_model4_test_model7', false) . '`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+AND/', $result); + $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)/', $result); + $this->assertPattern('/WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE/', $result); + $this->assertPattern('/\s+WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result); + } + +/** + * testGenerateAssociationQueryHasAndBelongsToManyWithConditions method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasAndBelongsToManyWithConditions() { + $this->Model = new TestModel4(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7'); + $queryData = array('conditions' => array('TestModel4.name !=' => 'mariano')); + $resultSet = null; + $null = null; + + $params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/', $result); + $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?`TestModel4`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result); + } + +/** + * testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit() { + $this->Model = new TestModel4(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $__backup = $this->Model->hasAndBelongsToMany['TestModel7']; + + $this->Model->hasAndBelongsToMany['TestModel7']['offset'] = 2; + $this->Model->hasAndBelongsToMany['TestModel7']['limit'] = 5; + + $binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+/', $result); + $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result); + $this->assertPattern('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 2,\s*5\s*$/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + + $this->Model->hasAndBelongsToMany['TestModel7'] = $__backup; + } + +/** + * testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit method + * + * @access public + * @return void + */ + function testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit() { + $this->Model = new TestModel4(); + $this->Model->schema(); + $this->_buildRelatedModels($this->Model); + + $__backup = $this->Model->hasAndBelongsToMany['TestModel7']; + + $this->Model->hasAndBelongsToMany['TestModel7']['page'] = 2; + $this->Model->hasAndBelongsToMany['TestModel7']['limit'] = 5; + + $binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7'); + $queryData = array(); + $resultSet = null; + $null = null; + + $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); + $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result); + $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/', $result); + $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result); + $this->assertPattern('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 5,\s*5\s*$/', $result); + + $result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); + $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); + $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); + + $this->Model->hasAndBelongsToMany['TestModel7'] = $__backup; + } + +/** + * testSelectDistict method + * + * @access public + * @return void + */ + function testSelectDistict() { + $this->Model = new TestModel4(); + $result = $this->Dbo->fields($this->Model, 'Vendor', "DISTINCT Vendor.id, Vendor.name"); + $expected = array('DISTINCT `Vendor`.`id`', '`Vendor`.`name`'); + $this->assertEqual($result, $expected); + } + +/** + * testStringConditionsParsing method + * + * @access public + * @return void + */ + function testStringConditionsParsing() { + $result = $this->Dbo->conditions("ProjectBid.project_id = Project.id"); + $expected = " WHERE `ProjectBid`.`project_id` = `Project`.`id`"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("Candy.name LIKE 'a' AND HardCandy.name LIKE 'c'"); + $expected = " WHERE `Candy`.`name` LIKE 'a' AND `HardCandy`.`name` LIKE 'c'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("HardCandy.name LIKE 'a' AND Candy.name LIKE 'c'"); + $expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("Post.title = '1.1'"); + $expected = " WHERE `Post`.`title` = '1.1'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("User.id != 0 AND User.user LIKE '%arr%'"); + $expected = " WHERE `User`.`id` != 0 AND `User`.`user` LIKE '%arr%'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("SUM(Post.comments_count) > 500"); + $expected = " WHERE SUM(`Post`.`comments_count`) > 500"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)"); + $expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("score BETWEEN 90.1 AND 95.7"); + $expected = " WHERE score BETWEEN 90.1 AND 95.7"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('score' => array(2=>1, 2, 10))); + $expected = " WHERE score IN (1, 2, 10)"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("Aro.rght = Aro.lft + 1.1"); + $expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)"); + $expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2'); + $expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2'; + $this->assertPattern('/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/', $result); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"'); + $expected = ' WHERE `Sportstaette`.`sportstaettenart_id` = 2 AND `Sportstaette`.`sportstaette` LIKE "%ru%"'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('SUM(Post.comments_count) > 500 AND NOT Post.title IS NULL AND NOT Post.extended_title IS NULL'); + $expected = ' WHERE SUM(`Post`.`comments_count`) > 500 AND NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('NOT Post.title IS NULL AND NOT Post.extended_title IS NULL AND SUM(Post.comments_count) > 500'); + $expected = ' WHERE NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL AND SUM(`Post`.`comments_count`) > 500'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('NOT Post.extended_title IS NULL AND NOT Post.title IS NULL AND Post.title != "" AND SPOON(SUM(Post.comments_count) + 1.1) > 500'); + $expected = ' WHERE NOT `Post`.`extended_title` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title` != "" AND SPOON(SUM(`Post`.`comments_count`) + 1.1) > 500'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('NOT Post.title_extended IS NULL AND NOT Post.title IS NULL AND Post.title_extended != Post.title'); + $expected = ' WHERE NOT `Post`.`title_extended` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title_extended` != `Post`.`title`'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("Comment.id = 'a'"); + $expected = " WHERE `Comment`.`id` = 'a'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("lower(Article.title) LIKE 'a%'"); + $expected = " WHERE lower(`Article`.`title`) LIKE 'a%'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('((MATCH(Video.title) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(Video.description) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(Video.tags) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))'); + $expected = ' WHERE ((MATCH(`Video`.`title`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(`Video`.`description`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(`Video`.`tags`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('DATEDIFF(NOW(),Article.published) < 1 && Article.live=1'); + $expected = " WHERE DATEDIFF(NOW(),`Article`.`published`) < 1 && `Article`.`live`=1"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('file = "index.html"'); + $expected = ' WHERE file = "index.html"'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions("file = 'index.html'"); + $expected = " WHERE file = 'index.html'"; + $this->assertEqual($result, $expected); + + $letter = $letter = 'd.a'; + $conditions = array('Company.name like ' => $letter . '%'); + $result = $this->Dbo->conditions($conditions); + $expected = " WHERE `Company`.`name` like 'd.a%'"; + $this->assertEqual($result, $expected); + + $conditions = array('Artist.name' => 'JUDY and MARY'); + $result = $this->Dbo->conditions($conditions); + $expected = " WHERE `Artist`.`name` = 'JUDY and MARY'"; + $this->assertEqual($result, $expected); + + $conditions = array('Artist.name' => 'JUDY AND MARY'); + $result = $this->Dbo->conditions($conditions); + $expected = " WHERE `Artist`.`name` = 'JUDY AND MARY'"; + $this->assertEqual($result, $expected); + } + +/** + * testQuotesInStringConditions method + * + * @access public + * @return void + */ + function testQuotesInStringConditions() { + $result = $this->Dbo->conditions('Member.email = \'mariano@cricava.com\''); + $expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\''; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('Member.email = "mariano@cricava.com"'); + $expected = ' WHERE `Member`.`email` = "mariano@cricava.com"'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions('Member.email = \'mariano@cricava.com\' AND Member.user LIKE \'mariano.iglesias%\''); + $expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\' AND `Member`.`user` LIKE \'mariano.iglesias%\''; + $this->assertEqual($result, $expected); + + + $result = $this->Dbo->conditions('Member.email = "mariano@cricava.com" AND Member.user LIKE "mariano.iglesias%"'); + $expected = ' WHERE `Member`.`email` = "mariano@cricava.com" AND `Member`.`user` LIKE "mariano.iglesias%"'; + $this->assertEqual($result, $expected); + } + +/** + * testParenthesisInStringConditions method + * + * @access public + * @return void + */ + function testParenthesisInStringConditions() { + $result = $this->Dbo->conditions('Member.name = \'(lu\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \')lu\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'va(lu\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'va)lu\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'va(lu)\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'va(lu)e\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'(mariano)\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'(mariano)iglesias\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'(mariano) iglesias\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'(mariano word) iglesias\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'(mariano.iglesias)\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias)\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias) CakePHP\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result); + + $result = $this->Dbo->conditions('Member.name = \'(mariano.iglesias) CakePHP\''); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result); + } + +/** + * testParenthesisInArrayConditions method + * + * @access public + * @return void + */ + function testParenthesisInArrayConditions() { + $result = $this->Dbo->conditions(array('Member.name' => '(lu')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => ')lu')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => 'va(lu')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => 'va)lu')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => 'va(lu)')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => 'va(lu)e')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => '(mariano)')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => '(mariano)iglesias')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => '(mariano) iglesias')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => '(mariano word) iglesias')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => '(mariano.iglesias)')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias)')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias) CakePHP')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result); + + $result = $this->Dbo->conditions(array('Member.name' => '(mariano.iglesias) CakePHP')); + $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result); + } + +/** + * testArrayConditionsParsing method + * + * @access public + * @return void + */ + function testArrayConditionsParsing() { + $this->loadFixtures('Post', 'Author'); + $result = $this->Dbo->conditions(array('Stereo.type' => 'in dash speakers')); + $this->assertPattern("/^\s+WHERE\s+`Stereo`.`type`\s+=\s+'in dash speakers'/", $result); + + $result = $this->Dbo->conditions(array('Candy.name LIKE' => 'a', 'HardCandy.name LIKE' => 'c')); + $this->assertPattern("/^\s+WHERE\s+`Candy`.`name` LIKE\s+'a'\s+AND\s+`HardCandy`.`name`\s+LIKE\s+'c'/", $result); + + $result = $this->Dbo->conditions(array('HardCandy.name LIKE' => 'a', 'Candy.name LIKE' => 'c')); + $expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('HardCandy.name LIKE' => 'a%', 'Candy.name LIKE' => '%c%')); + $expected = " WHERE `HardCandy`.`name` LIKE 'a%' AND `Candy`.`name` LIKE '%c%'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('HardCandy.name LIKE' => 'to be or%', 'Candy.name LIKE' => '%not to be%')); + $expected = " WHERE `HardCandy`.`name` LIKE 'to be or%' AND `Candy`.`name` LIKE '%not to be%'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('score BETWEEN ? AND ?' => array(90.1, 95.7))); + $expected = " WHERE `score` BETWEEN 90.100000 AND 95.700000"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Post.title' => 1.1)); + $expected = " WHERE `Post`.`title` = 1.100000"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Post.title' => 1.1), true, true, new Post()); + $expected = " WHERE `Post`.`title` = '1.1'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('SUM(Post.comments_count) >' => '500')); + $expected = " WHERE SUM(`Post`.`comments_count`) > '500'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('MAX(Post.rating) >' => '50')); + $expected = " WHERE MAX(`Post`.`rating`) > '50'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('title LIKE' => '%hello')); + $expected = " WHERE `title` LIKE '%hello'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Post.name' => 'mad(g)ik')); + $expected = " WHERE `Post`.`name` = 'mad(g)ik'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('score' => array(1, 2, 10))); + $expected = " WHERE score IN (1, 2, 10)"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('score' => array())); + $expected = " WHERE `score` IS NULL"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('score !=' => array())); + $expected = " WHERE `score` IS NOT NULL"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('score !=' => '20')); + $expected = " WHERE `score` != '20'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('score >' => '20')); + $expected = " WHERE `score` > '20'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('client_id >' => '20'), true, true, new TestModel()); + $expected = " WHERE `client_id` > 20"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('OR' => array( + array('User.user' => 'mariano'), + array('User.user' => 'nate') + ))); + + $expected = " WHERE ((`User`.`user` = 'mariano') OR (`User`.`user` = 'nate'))"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('or' => array( + 'score BETWEEN ? AND ?' => array('4', '5'), 'rating >' => '20' + ))); + $expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`rating` > '20'))"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('or' => array( + 'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20') + ))); + $expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`score` > '20'))"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('and' => array( + 'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20') + ))); + $expected = " WHERE ((`score` BETWEEN '4' AND '5') AND (`score` > '20'))"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array( + 'published' => 1, 'or' => array('score >' => '2', array('score >' => '20')) + )); + $expected = " WHERE `published` = 1 AND ((`score` > '2') OR (`score` > '20'))"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array(array('Project.removed' => false))); + $expected = " WHERE `Project`.`removed` = '0'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array(array('Project.removed' => true))); + $expected = " WHERE `Project`.`removed` = '1'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array(array('Project.removed' => null))); + $expected = " WHERE `Project`.`removed` IS NULL"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array(array('Project.removed !=' => null))); + $expected = " WHERE `Project`.`removed` IS NOT NULL"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('(Usergroup.permissions) & 4' => 4)); + $expected = " WHERE (`Usergroup`.`permissions`) & 4 = 4"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('((Usergroup.permissions) & 4)' => 4)); + $expected = " WHERE ((`Usergroup`.`permissions`) & 4) = 4"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Post.modified >=' => 'DATE_SUB(NOW(), INTERVAL 7 DAY)')); + $expected = " WHERE `Post`.`modified` >= 'DATE_SUB(NOW(), INTERVAL 7 DAY)'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Post.modified >= DATE_SUB(NOW(), INTERVAL 7 DAY)')); + $expected = " WHERE `Post`.`modified` >= DATE_SUB(NOW(), INTERVAL 7 DAY)"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array( + 'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)), + 'Enrollment.yearcompleted >' => '0') + ); + $this->assertPattern('/^\s*WHERE\s+\(NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(level_of_education_id IN \(912, 999\)\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result); + + $result = $this->Dbo->conditions(array('id <>' => '8')); + $this->assertPattern('/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/', $result); + + $result = $this->Dbo->conditions(array('TestModel.field =' => 'gribe$@()lu')); + $expected = " WHERE `TestModel`.`field` = 'gribe$@()lu'"; + $this->assertEqual($result, $expected); + + $conditions['NOT'] = array('Listing.expiration BETWEEN ? AND ?' => array("1", "100")); + $conditions[0]['OR'] = array( + "Listing.title LIKE" => "%term%", + "Listing.description LIKE" => "%term%" + ); + $conditions[1]['OR'] = array( + "Listing.title LIKE" => "%term_2%", + "Listing.description LIKE" => "%term_2%" + ); + $result = $this->Dbo->conditions($conditions); + $expected = " WHERE NOT (`Listing`.`expiration` BETWEEN '1' AND '100') AND" . + " ((`Listing`.`title` LIKE '%term%') OR (`Listing`.`description` LIKE '%term%')) AND" . + " ((`Listing`.`title` LIKE '%term_2%') OR (`Listing`.`description` LIKE '%term_2%'))"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('MD5(CONCAT(Reg.email,Reg.id))' => 'blah')); + $expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) = 'blah'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array( + 'MD5(CONCAT(Reg.email,Reg.id))' => array('blah', 'blahblah') + )); + $expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) IN ('blah', 'blahblah')"; + $this->assertEqual($result, $expected); + + $conditions = array('id' => array(2, 5, 6, 9, 12, 45, 78, 43, 76)); + $result = $this->Dbo->conditions($conditions); + $expected = " WHERE id IN (2, 5, 6, 9, 12, 45, 78, 43, 76)"; + $this->assertEqual($result, $expected); + + $conditions = array('title' => 'user(s)'); + $result = $this->Dbo->conditions($conditions); + $expected = " WHERE `title` = 'user(s)'"; + $this->assertEqual($result, $expected); + + $conditions = array('title' => 'user(s) data'); + $result = $this->Dbo->conditions($conditions); + $expected = " WHERE `title` = 'user(s) data'"; + $this->assertEqual($result, $expected); + + $conditions = array('title' => 'user(s,arg) data'); + $result = $this->Dbo->conditions($conditions); + $expected = " WHERE `title` = 'user(s,arg) data'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array("Book.book_name" => 'Java(TM)')); + $expected = " WHERE `Book`.`book_name` = 'Java(TM)'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array("Book.book_name" => 'Java(TM) ')); + $expected = " WHERE `Book`.`book_name` = 'Java(TM) '"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array("Book.id" => 0)); + $expected = " WHERE `Book`.`id` = 0"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array("Book.id" => NULL)); + $expected = " WHERE `Book`.`id` IS NULL"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Listing.beds >=' => 0)); + $expected = " WHERE `Listing`.`beds` >= 0"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array( + 'ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN ? AND ?' => array(65, 90) + )); + $expected = ' WHERE ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN 65 AND 90'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('or' => array( + '? BETWEEN Model.field1 AND Model.field2' => '2009-03-04' + ))); + $expected = " WHERE '2009-03-04' BETWEEN Model.field1 AND Model.field2"; + $this->assertEqual($result, $expected); + } + +/** + * testArrayConditionsParsingComplexKeys method + * + * @access public + * @return void + */ + function testArrayConditionsParsingComplexKeys() { + $result = $this->Dbo->conditions(array( + 'CAST(Book.created AS DATE)' => '2008-08-02' + )); + $expected = " WHERE CAST(`Book`.`created` AS DATE) = '2008-08-02'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array( + 'CAST(Book.created AS DATE) <=' => '2008-08-02' + )); + $expected = " WHERE CAST(`Book`.`created` AS DATE) <= '2008-08-02'"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array( + '(Stats.clicks * 100) / Stats.views >' => 50 + )); + $expected = " WHERE (`Stats`.`clicks` * 100) / `Stats`.`views` > 50"; + $this->assertEqual($result, $expected); + } + +/** + * testMixedConditionsParsing method + * + * @access public + * @return void + */ + function testMixedConditionsParsing() { + $conditions[] = 'User.first_name = \'Firstname\''; + $conditions[] = array('User.last_name' => 'Lastname'); + $result = $this->Dbo->conditions($conditions); + $expected = " WHERE `User`.`first_name` = 'Firstname' AND `User`.`last_name` = 'Lastname'"; + $this->assertEqual($result, $expected); + + $conditions = array( + 'Thread.project_id' => 5, + 'Thread.buyer_id' => 14, + '1=1 GROUP BY Thread.project_id' + ); + $result = $this->Dbo->conditions($conditions); + $this->assertPattern('/^\s*WHERE\s+`Thread`.`project_id`\s*=\s*5\s+AND\s+`Thread`.`buyer_id`\s*=\s*14\s+AND\s+1\s*=\s*1\s+GROUP BY `Thread`.`project_id`$/', $result); + } + +/** + * testConditionsOptionalArguments method + * + * @access public + * @return void + */ + function testConditionsOptionalArguments() { + $result = $this->Dbo->conditions( array('Member.name' => 'Mariano'), true, false); + $this->assertPattern('/^\s*`Member`.`name`\s*=\s*\'Mariano\'\s*$/', $result); + + $result = $this->Dbo->conditions( array(), true, false); + $this->assertPattern('/^\s*1\s*=\s*1\s*$/', $result); + } + +/** + * testConditionsWithModel + * + * @access public + * @return void + */ + function testConditionsWithModel() { + $this->Model = new Article2(); + + $result = $this->Dbo->conditions(array('Article2.viewed >=' => 0), true, true, $this->Model); + $expected = " WHERE `Article2`.`viewed` >= 0"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Article2.viewed >=' => '0'), true, true, $this->Model); + $expected = " WHERE `Article2`.`viewed` >= 0"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Article2.viewed >=' => '1'), true, true, $this->Model); + $expected = " WHERE `Article2`.`viewed` >= 1"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array(0, 10)), true, true, $this->Model); + $expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('0', '10')), true, true, $this->Model); + $expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('1', '10')), true, true, $this->Model); + $expected = " WHERE `Article2`.`rate_sum` BETWEEN 1 AND 10"; + $this->assertEqual($result, $expected); + } + +/** + * testFieldParsing method + * + * @access public + * @return void + */ + function testFieldParsing() { + $this->Model = new TestModel(); + $result = $this->Dbo->fields($this->Model, 'Vendor', "Vendor.id, COUNT(Model.vendor_id) AS `Vendor`.`count`"); + $expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, 'Vendor', "`Vendor`.`id`, COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`"); + $expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, 'Post', "CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name, Node.created"); + $expected = array("CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", "`Node`.`created`"); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, 'round( (3.55441 * fooField), 3 ) AS test'); + $this->assertEqual($result, array('round( (3.55441 * fooField), 3 ) AS test')); + + $result = $this->Dbo->fields($this->Model, null, 'ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating'); + $this->assertEqual($result, array('ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating')); + + $result = $this->Dbo->fields($this->Model, null, 'ROUND(Rating.rate_total / Rating.rate_count,2) AS rating'); + $this->assertEqual($result, array('ROUND(Rating.rate_total / Rating.rate_count,2) AS rating')); + + $result = $this->Dbo->fields($this->Model, 'Post', "Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name"); + $expected = array("`Node`.`created`", "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name"); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, 'Post', "2.2,COUNT(*), SUM(Something.else) as sum, Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name,Post.title,Post.1,1.1"); + $expected = array( + '2.2', 'COUNT(*)', 'SUM(`Something`.`else`) as sum', '`Node`.`created`', + "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", '`Post`.`title`', '`Post`.`1`', '1.1' + ); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, "(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`"); + $expected = array("(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`"); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, 'Post'); + $expected = array( + '`Post`.`id`', '`Post`.`client_id`', '`Post`.`name`', '`Post`.`login`', + '`Post`.`passwd`', '`Post`.`addr_1`', '`Post`.`addr_2`', '`Post`.`zip_code`', + '`Post`.`city`', '`Post`.`country`', '`Post`.`phone`', '`Post`.`fax`', + '`Post`.`url`', '`Post`.`email`', '`Post`.`comments`', '`Post`.`last_login`', + '`Post`.`created`', '`Post`.`updated`' + ); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, 'Other'); + $expected = array( + '`Other`.`id`', '`Other`.`client_id`', '`Other`.`name`', '`Other`.`login`', + '`Other`.`passwd`', '`Other`.`addr_1`', '`Other`.`addr_2`', '`Other`.`zip_code`', + '`Other`.`city`', '`Other`.`country`', '`Other`.`phone`', '`Other`.`fax`', + '`Other`.`url`', '`Other`.`email`', '`Other`.`comments`', '`Other`.`last_login`', + '`Other`.`created`', '`Other`.`updated`' + ); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, array(), false); + $expected = array('id', 'client_id', 'name', 'login', 'passwd', 'addr_1', 'addr_2', 'zip_code', 'city', 'country', 'phone', 'fax', 'url', 'email', 'comments', 'last_login', 'created', 'updated'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, 'COUNT(*)'); + $expected = array('COUNT(*)'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, 'SUM(Thread.unread_buyer) AS ' . $this->Dbo->name('sum_unread_buyer')); + $expected = array('SUM(`Thread`.`unread_buyer`) AS `sum_unread_buyer`'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, 'name, count(*)'); + $expected = array('`TestModel`.`name`', 'count(*)'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, 'count(*), name'); + $expected = array('count(*)', '`TestModel`.`name`'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields( + $this->Model, null, 'field1, field2, field3, count(*), name' + ); + $expected = array( + '`TestModel`.`field1`', '`TestModel`.`field2`', + '`TestModel`.`field3`', 'count(*)', '`TestModel`.`name`' + ); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, array('dayofyear(now())')); + $expected = array('dayofyear(now())'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, array('MAX(Model.field) As Max')); + $expected = array('MAX(`Model`.`field`) As Max'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, array('Model.field AS AnotherName')); + $expected = array('`Model`.`field` AS `AnotherName`'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, array('field AS AnotherName')); + $expected = array('`field` AS `AnotherName`'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, null, array( + 'TestModel.field AS AnotherName' + )); + $expected = array('`TestModel`.`field` AS `AnotherName`'); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fields($this->Model, 'Foo', array( + 'id', 'title', '(user_count + discussion_count + post_count) AS score' + )); + $expected = array( + '`Foo`.`id`', + '`Foo`.`title`', + '(user_count + discussion_count + post_count) AS score' + ); + $this->assertEqual($result, $expected); + } + +/** + * test that fields() will accept objects made from DboSource::expression + * + * @return void + */ + function testFieldsWithExpression() { + $this->Model = new TestModel; + $expression = $this->Dbo->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col"); + $result = $this->Dbo->fields($this->Model, null, array("id", $expression)); + $expected = array( + '`TestModel`.`id`', + "CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col" + ); + $this->assertEqual($result, $expected); + } + +/** + * testRenderStatement method + * + * @access public + * @return void + */ + function testRenderStatement() { + $result = $this->Dbo->renderStatement('select', array( + 'fields' => 'id', 'table' => 'table', 'conditions' => 'WHERE 1=1', + 'alias' => '', 'joins' => '', 'order' => '', 'limit' => '', 'group' => '' + )); + $this->assertPattern('/^\s*SELECT\s+id\s+FROM\s+table\s+WHERE\s+1=1\s*$/', $result); + + $result = $this->Dbo->renderStatement('update', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => '')); + $this->assertPattern('/^\s*UPDATE\s+table\s+SET\s+value=2\s+WHERE\s+1=1\s*$/', $result); + + $result = $this->Dbo->renderStatement('update', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => 'alias', 'joins' => '')); + $this->assertPattern('/^\s*UPDATE\s+table\s+AS\s+alias\s+SET\s+value=2\s+WHERE\s+1=1\s*$/', $result); + + $result = $this->Dbo->renderStatement('delete', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => '')); + $this->assertPattern('/^\s*DELETE\s+FROM\s+table\s+WHERE\s+1=1\s*$/', $result); + + $result = $this->Dbo->renderStatement('delete', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => 'alias', 'joins' => '')); + $this->assertPattern('/^\s*DELETE\s+alias\s+FROM\s+table\s+AS\s+alias\s+WHERE\s+1=1\s*$/', $result); + } + +/** + * testSchema method + * + * @access public + * @return void + */ + function testSchema() { + $Schema = new CakeSchema(); + $Schema->tables = array('table' => array(), 'anotherTable' => array()); + + $this->expectError(); + $result = $this->Dbo->dropSchema(null); + $this->assertTrue($result === null); + + $result = $this->Dbo->dropSchema($Schema, 'non_existing'); + $this->assertTrue(empty($result)); + + $result = $this->Dbo->dropSchema($Schema, 'table'); + $this->assertPattern('/^\s*DROP TABLE IF EXISTS\s+' . $this->Dbo->fullTableName('table') . ';\s*$/s', $result); + } + +/** + * testOrderParsing method + * + * @access public + * @return void + */ + function testOrderParsing() { + $result = $this->Dbo->order("ADDTIME(Event.time_begin, '-06:00:00') ASC"); + $expected = " ORDER BY ADDTIME(`Event`.`time_begin`, '-06:00:00') ASC"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->order("title, id"); + $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result); + + $result = $this->Dbo->order("title desc, id desc"); + $this->assertPattern('/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/', $result); + + $result = $this->Dbo->order(array("title desc, id desc")); + $this->assertPattern('/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/', $result); + + $result = $this->Dbo->order(array("title", "id")); + $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result); + + $result = $this->Dbo->order(array(array('title'), array('id'))); + $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result); + + $result = $this->Dbo->order(array("Post.title" => 'asc', "Post.id" => 'desc')); + $this->assertPattern('/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/', $result); + + $result = $this->Dbo->order(array(array("Post.title" => 'asc', "Post.id" => 'desc'))); + $this->assertPattern('/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/', $result); + + $result = $this->Dbo->order(array("title")); + $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result); + + $result = $this->Dbo->order(array(array("title"))); + $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result); + + $result = $this->Dbo->order("Dealer.id = 7 desc, Dealer.id = 3 desc, Dealer.title asc"); + $expected = " ORDER BY `Dealer`.`id` = 7 desc, `Dealer`.`id` = 3 desc, `Dealer`.`title` asc"; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->order(array("Page.name" => "='test' DESC")); + $this->assertPattern("/^\s*ORDER BY\s+`Page`\.`name`\s*='test'\s+DESC\s*$/", $result); + + $result = $this->Dbo->order("Page.name = 'view' DESC"); + $this->assertPattern("/^\s*ORDER BY\s+`Page`\.`name`\s*=\s*'view'\s+DESC\s*$/", $result); + + $result = $this->Dbo->order("(Post.views)"); + $this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\)\s+ASC\s*$/", $result); + + $result = $this->Dbo->order("(Post.views)*Post.views"); + $this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\)\*`Post`\.`views`\s+ASC\s*$/", $result); + + $result = $this->Dbo->order("(Post.views) * Post.views"); + $this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\) \* `Post`\.`views`\s+ASC\s*$/", $result); + + $result = $this->Dbo->order("(Model.field1 + Model.field2) * Model.field3"); + $this->assertPattern("/^\s*ORDER BY\s+\(`Model`\.`field1` \+ `Model`\.`field2`\) \* `Model`\.`field3`\s+ASC\s*$/", $result); + + $result = $this->Dbo->order("Model.name+0 ASC"); + $this->assertPattern("/^\s*ORDER BY\s+`Model`\.`name`\+0\s+ASC\s*$/", $result); + + $result = $this->Dbo->order("Anuncio.destaque & 2 DESC"); + $expected = ' ORDER BY `Anuncio`.`destaque` & 2 DESC'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->order("3963.191 * id"); + $expected = ' ORDER BY 3963.191 * id ASC'; + $this->assertEqual($result, $expected); + + $result = $this->Dbo->order(array('Property.sale_price IS NULL')); + $expected = ' ORDER BY `Property`.`sale_price` IS NULL ASC'; + $this->assertEqual($result, $expected); + } + +/** + * testComplexSortExpression method + * + * @return void + */ + public function testComplexSortExpression() { + $result = $this->Dbo->order(array('(Model.field > 100) DESC', 'Model.field ASC')); + $this->assertPattern("/^\s*ORDER BY\s+\(`Model`\.`field`\s+>\s+100\)\s+DESC,\s+`Model`\.`field`\s+ASC\s*$/", $result); + } + +/** + * testCalculations method + * + * @access public + * @return void + */ + function testCalculations() { + $result = $this->Dbo->calculate($this->Model, 'count'); + $this->assertEqual($result, 'COUNT(*) AS `count`'); + + $result = $this->Dbo->calculate($this->Model, 'count', array('id')); + $this->assertEqual($result, 'COUNT(`id`) AS `count`'); + + $result = $this->Dbo->calculate( + $this->Model, + 'count', + array($this->Dbo->expression('DISTINCT id')) + ); + $this->assertEqual($result, 'COUNT(DISTINCT id) AS `count`'); + + $result = $this->Dbo->calculate($this->Model, 'count', array('id', 'id_count')); + $this->assertEqual($result, 'COUNT(`id`) AS `id_count`'); + + $result = $this->Dbo->calculate($this->Model, 'count', array('Model.id', 'id_count')); + $this->assertEqual($result, 'COUNT(`Model`.`id`) AS `id_count`'); + + $result = $this->Dbo->calculate($this->Model, 'max', array('id')); + $this->assertEqual($result, 'MAX(`id`) AS `id`'); + + $result = $this->Dbo->calculate($this->Model, 'max', array('Model.id', 'id')); + $this->assertEqual($result, 'MAX(`Model`.`id`) AS `id`'); + + $result = $this->Dbo->calculate($this->Model, 'max', array('`Model`.`id`', 'id')); + $this->assertEqual($result, 'MAX(`Model`.`id`) AS `id`'); + + $result = $this->Dbo->calculate($this->Model, 'min', array('`Model`.`id`', 'id')); + $this->assertEqual($result, 'MIN(`Model`.`id`) AS `id`'); + + $result = $this->Dbo->calculate($this->Model, 'min', 'left'); + $this->assertEqual($result, 'MIN(`left`) AS `left`'); + } + +/** + * testLength method + * + * @access public + * @return void + */ + function testLength() { + $result = $this->Dbo->length('varchar(255)'); + $expected = 255; + $this->assertIdentical($result, $expected); + + $result = $this->Dbo->length('int(11)'); + $expected = 11; + $this->assertIdentical($result, $expected); + + $result = $this->Dbo->length('float(5,3)'); + $expected = '5,3'; + $this->assertIdentical($result, $expected); + + $result = $this->Dbo->length('decimal(5,2)'); + $expected = '5,2'; + $this->assertIdentical($result, $expected); + + $result = $this->Dbo->length("enum('test','me','now')"); + $expected = 4; + $this->assertIdentical($result, $expected); + + $result = $this->Dbo->length("set('a','b','cd')"); + $expected = 2; + $this->assertIdentical($result, $expected); + + $this->expectError(); + $result = $this->Dbo->length(false); + $this->assertTrue($result === null); + + $result = $this->Dbo->length('datetime'); + $expected = null; + $this->assertIdentical($result, $expected); + + $result = $this->Dbo->length('text'); + $expected = null; + $this->assertIdentical($result, $expected); + } + +/** + * testBuildIndex method + * + * @access public + * @return void + */ + function testBuildIndex() { + $data = array( + 'PRIMARY' => array('column' => 'id') + ); + $result = $this->Dbo->buildIndex($data); + $expected = array('PRIMARY KEY (`id`)'); + $this->assertIdentical($result, $expected); + + $data = array( + 'MyIndex' => array('column' => 'id', 'unique' => true) + ); + $result = $this->Dbo->buildIndex($data); + $expected = array('UNIQUE KEY `MyIndex` (`id`)'); + $this->assertEqual($result, $expected); + + $data = array( + 'MyIndex' => array('column' => array('id', 'name'), 'unique' => true) + ); + $result = $this->Dbo->buildIndex($data); + $expected = array('UNIQUE KEY `MyIndex` (`id`, `name`)'); + $this->assertEqual($result, $expected); + } + +/** + * testBuildColumn method + * + * @access public + * @return void + */ + function testBuildColumn2() { + $this->expectError(); + $data = array( + 'name' => 'testName', + 'type' => 'varchar(255)', + 'default', + 'null' => true, + 'key' + ); + $this->Dbo->buildColumn($data); + + $data = array( + 'name' => 'testName', + 'type' => 'string', + 'length' => 255, + 'default', + 'null' => true, + 'key' + ); + $result = $this->Dbo->buildColumn($data); + $expected = '`testName` varchar(255) DEFAULT NULL'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'int_field', + 'type' => 'integer', + 'default' => '', + 'null' => false, + ); + $restore = $this->Dbo->columns; + + $this->Dbo->columns = array('integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'), ); + $result = $this->Dbo->buildColumn($data); + $expected = '`int_field` int(11) NOT NULL'; + $this->assertEqual($result, $expected); + + $this->Dbo->fieldParameters['param'] = array( + 'value' => 'COLLATE', + 'quote' => false, + 'join' => ' ', + 'column' => 'Collate', + 'position' => 'beforeDefault', + 'options' => array('GOOD', 'OK') + ); + $data = array( + 'name' => 'int_field', + 'type' => 'integer', + 'default' => '', + 'null' => false, + 'param' => 'BAD' + ); + $result = $this->Dbo->buildColumn($data); + $expected = '`int_field` int(11) NOT NULL'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'int_field', + 'type' => 'integer', + 'default' => '', + 'null' => false, + 'param' => 'GOOD' + ); + $result = $this->Dbo->buildColumn($data); + $expected = '`int_field` int(11) COLLATE GOOD NOT NULL'; + $this->assertEqual($result, $expected); + + $this->Dbo->columns = $restore; + + $data = array( + 'name' => 'created', + 'type' => 'timestamp', + 'default' => 'current_timestamp', + 'null' => false, + ); + $result = $this->Dbo->buildColumn($data); + $expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'created', + 'type' => 'timestamp', + 'default' => 'CURRENT_TIMESTAMP', + 'null' => true, + ); + $result = $this->Dbo->buildColumn($data); + $expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'modified', + 'type' => 'timestamp', + 'null' => true, + ); + $result = $this->Dbo->buildColumn($data); + $expected = '`modified` timestamp NULL'; + $this->assertEqual($result, $expected); + + $data = array( + 'name' => 'modified', + 'type' => 'timestamp', + 'default' => null, + 'null' => true, + ); + $result = $this->Dbo->buildColumn($data); + $expected = '`modified` timestamp NULL'; + $this->assertEqual($result, $expected); + } + +/** + * test hasAny() + * + * @return void + */ + function testHasAny() { + $this->Dbo = $this->getMock('DboMysql', array('connect', '_execute', 'execute', 'value')); + $this->Model = $this->getMock('TestModel', array('getDataSource')); + $this->Model->expects($this->any()) + ->method('getDataSource') + ->will($this->returnValue($this->Dbo)); + + $this->Dbo->expects($this->at(0))->method('value') + ->with('harry') + ->will($this->returnValue("'harry'")); + + $this->Dbo->expects($this->at(1))->method('execute') + ->with('SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE `TestModel`.`name` = \'harry\''); + $this->Dbo->expects($this->at(2))->method('execute') + ->with('SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE 1 = 1'); + + $this->Dbo->hasAny($this->Model, array('TestModel.name' => 'harry')); + $this->Dbo->hasAny($this->Model, array()); + + } + +/** + * test fields generating usable virtual fields to use in query + * + * @return void + */ + function testVirtualFields() { + $this->loadFixtures('Article', 'Comment'); + $this->Dbo->virtualFieldSeparator = '__'; + $Article = ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'this_moment' => 'NOW()', + 'two' => '1 + 1', + 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->Dbo->fullTableName('comments') . + ' WHERE Article.id = ' . $this->Dbo->fullTableName('comments') . '.article_id' + ); + $result = $this->Dbo->fields($Article); + $expected = array( + '`Article`.`id`', + '`Article`.`user_id`', + '`Article`.`title`', + '`Article`.`body`', + '`Article`.`published`', + '`Article`.`created`', + '`Article`.`updated`', + '(NOW()) AS `Article__this_moment`', + '(1 + 1) AS `Article__two`', + '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`' + ); + $this->assertEqual($expected, $result); + + $result = $this->Dbo->fields($Article, null, array('this_moment', 'title')); + $expected = array( + '`Article`.`title`', + '(NOW()) AS `Article__this_moment`', + ); + $this->assertEqual($expected, $result); + + $result = $this->Dbo->fields($Article, null, array('Article.title', 'Article.this_moment')); + $expected = array( + '`Article`.`title`', + '(NOW()) AS `Article__this_moment`', + ); + $this->assertEqual($expected, $result); + + $result = $this->Dbo->fields($Article, null, array('Article.this_moment', 'Article.title')); + $expected = array( + '`Article`.`title`', + '(NOW()) AS `Article__this_moment`', + ); + $this->assertEqual($expected, $result); + + $result = $this->Dbo->fields($Article, null, array('Article.*')); + $expected = array( + '`Article`.*', + '(NOW()) AS `Article__this_moment`', + '(1 + 1) AS `Article__two`', + '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`' + ); + $this->assertEqual($expected, $result); + + $result = $this->Dbo->fields($Article, null, array('*')); + $expected = array( + '*', + '(NOW()) AS `Article__this_moment`', + '(1 + 1) AS `Article__two`', + '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`' + ); + $this->assertEqual($expected, $result); + } + +/** + * test conditions to generate query conditions for virtual fields + * + * @return void + */ + function testVirtualFieldsInConditions() { + $Article = ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'this_moment' => 'NOW()', + 'two' => '1 + 1', + 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->Dbo->fullTableName('comments') . + ' WHERE Article.id = ' . $this->Dbo->fullTableName('comments') . '.article_id' + ); + $conditions = array('two' => 2); + $result = $this->Dbo->conditions($conditions, true, false, $Article); + $expected = '(1 + 1) = 2'; + $this->assertEqual($expected, $result); + + $conditions = array('this_moment BETWEEN ? AND ?' => array(1,2)); + $expected = 'NOW() BETWEEN 1 AND 2'; + $result = $this->Dbo->conditions($conditions, true, false, $Article); + $this->assertEqual($expected, $result); + + $conditions = array('comment_count >' => 5); + $expected = '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) > 5'; + $result = $this->Dbo->conditions($conditions, true, false, $Article); + $this->assertEqual($expected, $result); + + $conditions = array('NOT' => array('two' => 2)); + $result = $this->Dbo->conditions($conditions, true, false, $Article); + $expected = 'NOT ((1 + 1) = 2)'; + $this->assertEqual($expected, $result); + } + +/** + * test that virtualFields with complex functions and aliases work. + * + * @return void + */ + function testConditionsWithComplexVirtualFields() { + $Article = ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'distance' => 'ACOS(SIN(20 * PI() / 180) + * SIN(Article.latitude * PI() / 180) + + COS(20 * PI() / 180) + * COS(Article.latitude * PI() / 180) + * COS((50 - Article.longitude) * PI() / 180) + ) * 180 / PI() * 60 * 1.1515 * 1.609344' + ); + $conditions = array('distance >=' => 20); + $result = $this->Dbo->conditions($conditions, true, true, $Article); + + $this->assertPattern('/\) >= 20/', $result); + $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result); + $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result); + } + +/** + * test calculate to generate claculate statements on virtual fields + * + * @return void + */ + function testVirtualFieldsInCalculate() { + $Article = ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'this_moment' => 'NOW()', + 'two' => '1 + 1', + 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->Dbo->fullTableName('comments') . + ' WHERE Article.id = ' . $this->Dbo->fullTableName('comments'). '.article_id' + ); + + $result = $this->Dbo->calculate($Article, 'count', array('this_moment')); + $expected = 'COUNT(NOW()) AS `count`'; + $this->assertEqual($expected, $result); + + $result = $this->Dbo->calculate($Article, 'max', array('comment_count')); + $expected = 'MAX(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `comment_count`'; + $this->assertEqual($expected, $result); + } + +/** + * test reading virtual fields containing newlines when recursive > 0 + * + * @return void + */ + function testReadVirtualFieldsWithNewLines() { + $Article = new Article(); + $Article->recursive = 1; + $Article->virtualFields = array( + 'test' => ' + User.id + User.id + ' + ); + $result = $this->Dbo->fields($Article, null, array()); + $result = $this->Dbo->fields($Article, $Article->alias, $result); + $this->assertPattern('/[`\"]User[`\"]\.[`\"]id[`\"] \+ [`\"]User[`\"]\.[`\"]id[`\"]/', $result[7]); + } + +/** + * test group to generate GROUP BY statements on virtual fields + * + * @return void + */ + function testVirtualFieldsInGroup() { + $Article = ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'this_year' => 'YEAR(Article.created)' + ); + + $result = $this->Dbo->group('this_year', $Article); + + $expected = " GROUP BY (YEAR(`Article`.`created`))"; + $this->assertEqual($expected, $result); + } + +/** + * test that virtualFields with complex functions and aliases work. + * + * @return void + */ + function testFieldsWithComplexVirtualFields() { + $Article = new Article(); + $Article->virtualFields = array( + 'distance' => 'ACOS(SIN(20 * PI() / 180) + * SIN(Article.latitude * PI() / 180) + + COS(20 * PI() / 180) + * COS(Article.latitude * PI() / 180) + * COS((50 - Article.longitude) * PI() / 180) + ) * 180 / PI() * 60 * 1.1515 * 1.609344' + ); + + $fields = array('id', 'distance'); + $result = $this->Dbo->fields($Article, null, $fields); + $qs = $this->Dbo->startQuote; + $qe = $this->Dbo->endQuote; + + $this->assertEqual($result[0], "{$qs}Article{$qe}.{$qs}id{$qe}"); + $this->assertPattern('/Article__distance/', $result[1]); + $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result[1]); + $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result[1]); + } + +/** + * test that execute runs queries. + * + * @return void + */ + function testExecute() { + $query = 'SELECT * FROM ' . $this->Dbo->fullTableName('articles') . ' WHERE 1 = 1'; + $this->Dbo->took = null; + $this->Dbo->affected = null; + $result = $this->Dbo->execute($query, array('log' => false)); + $this->assertNotNull($result, 'No query performed! %s'); + $this->assertNull($this->Dbo->took, 'Stats were set %s'); + $this->assertNull($this->Dbo->affected, 'Stats were set %s'); + + $result = $this->Dbo->execute($query); + $this->assertNotNull($result, 'No query performed! %s'); + $this->assertNotNull($this->Dbo->took, 'Stats were not set %s'); + $this->assertNotNull($this->Dbo->affected, 'Stats were not set %s'); + } + +/** + * test a full example of using virtual fields + * + * @return void + */ + function testVirtualFieldsFetch() { + $this->loadFixtures('Article', 'Comment'); + + $Article = ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->Dbo->fullTableName('comments') . + ' WHERE Article.id = ' . $this->Dbo->fullTableName('comments') . '.article_id' + ); + + $conditions = array('comment_count >' => 2); + $query = 'SELECT ' . join(',',$this->Dbo->fields($Article, null, array('id', 'comment_count'))) . + ' FROM ' . $this->Dbo->fullTableName($Article) . ' Article ' . $this->Dbo->conditions($conditions, true, true, $Article); + $result = $this->Dbo->fetchAll($query); + $expected = array(array( + 'Article' => array('id' => 1, 'comment_count' => 4) + )); + $this->assertEqual($expected, $result); + } + +/** + * test reading complex virtualFields with subqueries. + * + * @return void + */ + function testVirtualFieldsComplexRead() { + $this->loadFixtures('DataTest', 'Article', 'Comment', 'User', 'Tag'); + + $Article = ClassRegistry::init('Article'); + $commentTable = $this->Dbo->fullTableName('comments'); + $Article = ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'comment_count' => 'SELECT COUNT(*) FROM ' . $commentTable . + ' AS Comment WHERE Article.id = Comment.article_id' + ); + $result = $Article->find('all'); + $this->assertTrue(count($result) > 0); + $this->assertTrue($result[0]['Article']['comment_count'] > 0); + + $DataTest = ClassRegistry::init('DataTest'); + $DataTest->virtualFields = array( + 'complicated' => 'ACOS(SIN(20 * PI() / 180) + * SIN(DataTest.float * PI() / 180) + + COS(20 * PI() / 180) + * COS(DataTest.count * PI() / 180) + * COS((50 - DataTest.float) * PI() / 180) + ) * 180 / PI() * 60 * 1.1515 * 1.609344' + ); + $result = $DataTest->find('all'); + $this->assertTrue(count($result) > 0); + $this->assertTrue($result[0]['DataTest']['complicated'] > 0); + } + +/** + * testIntrospectType method + * + * @access public + * @return void + */ + function testIntrospectType() { + $this->assertEqual($this->Dbo->introspectType(0), 'integer'); + $this->assertEqual($this->Dbo->introspectType(2), 'integer'); + $this->assertEqual($this->Dbo->introspectType('2'), 'string'); + $this->assertEqual($this->Dbo->introspectType('2.2'), 'string'); + $this->assertEqual($this->Dbo->introspectType(2.2), 'float'); + $this->assertEqual($this->Dbo->introspectType('stringme'), 'string'); + $this->assertEqual($this->Dbo->introspectType('0stringme'), 'string'); + + $data = array(2.2); + $this->assertEqual($this->Dbo->introspectType($data), 'float'); + + $data = array('2.2'); + $this->assertEqual($this->Dbo->introspectType($data), 'float'); + + $data = array(2); + $this->assertEqual($this->Dbo->introspectType($data), 'integer'); + + $data = array('2'); + $this->assertEqual($this->Dbo->introspectType($data), 'integer'); + + $data = array('string'); + $this->assertEqual($this->Dbo->introspectType($data), 'string'); + + $data = array(2.2, '2.2'); + $this->assertEqual($this->Dbo->introspectType($data), 'float'); + + $data = array(2, '2'); + $this->assertEqual($this->Dbo->introspectType($data), 'integer'); + + $data = array('string one', 'string two'); + $this->assertEqual($this->Dbo->introspectType($data), 'string'); + + $data = array('2.2', 3); + $this->assertEqual($this->Dbo->introspectType($data), 'integer'); + + $data = array('2.2', '0stringme'); + $this->assertEqual($this->Dbo->introspectType($data), 'string'); + + $data = array(2.2, 3); + $this->assertEqual($this->Dbo->introspectType($data), 'integer'); + + $data = array(2.2, '0stringme'); + $this->assertEqual($this->Dbo->introspectType($data), 'string'); + + $data = array(2, 'stringme'); + $this->assertEqual($this->Dbo->introspectType($data), 'string'); + + $data = array(2, '2.2', 'stringgme'); + $this->assertEqual($this->Dbo->introspectType($data), 'string'); + + $data = array(2, '2.2'); + $this->assertEqual($this->Dbo->introspectType($data), 'integer'); + + $data = array(2, 2.2); + $this->assertEqual($this->Dbo->introspectType($data), 'integer'); + + + // NULL + $result = $this->Dbo->value(null, 'boolean'); + $this->assertEqual($result, 'NULL'); + + // EMPTY STRING + $result = $this->Dbo->value('', 'boolean'); + $this->assertEqual($result, "'0'"); + + + // BOOLEAN + $result = $this->Dbo->value('true', 'boolean'); + $this->assertEqual($result, "'1'"); + + $result = $this->Dbo->value('false', 'boolean'); + $this->assertEqual($result, "'1'"); + + $result = $this->Dbo->value(true, 'boolean'); + $this->assertEqual($result, "'1'"); + + $result = $this->Dbo->value(false, 'boolean'); + $this->assertEqual($result, "'0'"); + + $result = $this->Dbo->value(1, 'boolean'); + $this->assertEqual($result, "'1'"); + + $result = $this->Dbo->value(0, 'boolean'); + $this->assertEqual($result, "'0'"); + + $result = $this->Dbo->value('abc', 'boolean'); + $this->assertEqual($result, "'1'"); + + $result = $this->Dbo->value(1.234, 'boolean'); + $this->assertEqual($result, "'1'"); + + $result = $this->Dbo->value('1.234e05', 'boolean'); + $this->assertEqual($result, "'1'"); + + // NUMBERS + $result = $this->Dbo->value(123, 'integer'); + $this->assertEqual($result, 123); + + $result = $this->Dbo->value('123', 'integer'); + $this->assertEqual($result, '123'); + + $result = $this->Dbo->value('0123', 'integer'); + $this->assertEqual($result, "'0123'"); + + $result = $this->Dbo->value('0x123ABC', 'integer'); + $this->assertEqual($result, "'0x123ABC'"); + + $result = $this->Dbo->value('0x123', 'integer'); + $this->assertEqual($result, "'0x123'"); + + $result = $this->Dbo->value(1.234, 'float'); + $this->assertEqual($result, 1.234); + + $result = $this->Dbo->value('1.234', 'float'); + $this->assertEqual($result, '1.234'); + + $result = $this->Dbo->value(' 1.234 ', 'float'); + $this->assertEqual($result, "' 1.234 '"); + + $result = $this->Dbo->value('1.234e05', 'float'); + $this->assertEqual($result, "'1.234e05'"); + + $result = $this->Dbo->value('1.234e+5', 'float'); + $this->assertEqual($result, "'1.234e+5'"); + + $result = $this->Dbo->value('1,234', 'float'); + $this->assertEqual($result, "'1,234'"); + + $result = $this->Dbo->value('FFF', 'integer'); + $this->assertEqual($result, "'FFF'"); + + $result = $this->Dbo->value('abc', 'integer'); + $this->assertEqual($result, "'abc'"); + + // STRINGS + $result = $this->Dbo->value('123', 'string'); + $this->assertEqual($result, "'123'"); + + $result = $this->Dbo->value(123, 'string'); + $this->assertEqual($result, "'123'"); + + $result = $this->Dbo->value(1.234, 'string'); + $this->assertEqual($result, "'1.234'"); + + $result = $this->Dbo->value('abc', 'string'); + $this->assertEqual($result, "'abc'"); + + $result = $this->Dbo->value(' abc ', 'string'); + $this->assertEqual($result, "' abc '"); + + $result = $this->Dbo->value('a bc', 'string'); + $this->assertEqual($result, "'a bc'"); + } + +/** + * testRealQueries method + * + * @access public + * @return void + */ + function testRealQueries() { + $this->loadFixtures('Apple', 'Article', 'User', 'Comment', 'Tag', 'Sample'); + + $Apple = ClassRegistry::init('Apple'); + $Article = ClassRegistry::init('Article'); + + $result = $this->Dbo->rawQuery('SELECT color, name FROM ' . $this->Dbo->fullTableName('apples')); + $this->assertTrue(!empty($result)); + + $result = $this->Dbo->fetchRow($result); + $expected = array($this->Dbo->fullTableName('apples', false) => array( + 'color' => 'Red 1', + 'name' => 'Red Apple 1' + )); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->fetchAll('SELECT name FROM ' . $this->Dbo->fullTableName('apples') . ' ORDER BY id'); + $expected = array( + array($this->Dbo->fullTableName('apples', false) => array('name' => 'Red Apple 1')), + array($this->Dbo->fullTableName('apples', false) => array('name' => 'Bright Red Apple')), + array($this->Dbo->fullTableName('apples', false) => array('name' => 'green blue')), + array($this->Dbo->fullTableName('apples', false) => array('name' => 'Test Name')), + array($this->Dbo->fullTableName('apples', false) => array('name' => 'Blue Green')), + array($this->Dbo->fullTableName('apples', false) => array('name' => 'My new apple')), + array($this->Dbo->fullTableName('apples', false) => array('name' => 'Some odd color')) + ); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->field($this->Dbo->fullTableName('apples', false), 'SELECT color, name FROM ' . $this->Dbo->fullTableName('apples') . ' ORDER BY id'); + $expected = array( + 'color' => 'Red 1', + 'name' => 'Red Apple 1' + ); + $this->assertEqual($result, $expected); + + $Apple->unbindModel(array(), false); + $result = $this->Dbo->read($Apple, array( + 'fields' => array($Apple->escapeField('name')), + 'conditions' => null, + 'recursive' => -1 + )); + $expected = array( + array('Apple' => array('name' => 'Red Apple 1')), + array('Apple' => array('name' => 'Bright Red Apple')), + array('Apple' => array('name' => 'green blue')), + array('Apple' => array('name' => 'Test Name')), + array('Apple' => array('name' => 'Blue Green')), + array('Apple' => array('name' => 'My new apple')), + array('Apple' => array('name' => 'Some odd color')) + ); + $this->assertEqual($result, $expected); + + $result = $this->Dbo->read($Article, array( + 'fields' => array('id', 'user_id', 'title'), + 'conditions' => null, + 'recursive' => 1 + )); + + $this->assertTrue(Set::matches('/Article[id=1]', $result)); + $this->assertTrue(Set::matches('/Comment[id=1]', $result)); + $this->assertTrue(Set::matches('/Comment[id=2]', $result)); + $this->assertFalse(Set::matches('/Comment[id=10]', $result)); + } } diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php deleted file mode 100644 index 4ab881478..000000000 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php +++ /dev/null @@ -1,328 +0,0 @@ -testing) { - $this->simulated[] = $sql; - return null; - } - return parent::_execute($sql); - } - -/** - * getLastQuery method - * - * @access public - * @return void - */ - function getLastQuery() { - return $this->simulated[count($this->simulated) - 1]; - } -} - -/** - * MysqliTestModel class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class MysqliTestModel extends Model { - -/** - * name property - * - * @var string 'MysqlTestModel' - * @access public - */ - public $name = 'MysqliTestModel'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - return array( - 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'), - 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'), - 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''), - 'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''), - 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), - 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) - ); - } -} - -/** - * DboMysqliTest class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources.dbo - */ -class DboMysqliTest extends CakeTestCase { - public $fixtures = array('core.datatype'); -/** - * The Dbo instance to be tested - * - * @var DboSource - * @access public - */ - public $Dbo = null; - -/** - * Sets up a Dbo class instance for testing - * - */ - public function setUp() { - $this->Dbo = ConnectionManager::getDataSource('test'); - if ($this->Dbo->config['driver'] !== 'mysqli') { - $this->markTestSkipped('The MySQLi extension is not available.'); - } - $this->model = new MysqliTestModel(); - } - -/** - * Sets up a Dbo class instance for testing - * - */ - public function tearDown() { - unset($this->model); - ClassRegistry::flush(); - } - -/** - * testIndexDetection method - * - * @return void - */ - public function testIndexDetection() { - $this->Dbo->cacheSources = false; - - $name = $this->Dbo->fullTableName('simple'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));'); - $expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1)); - $result = $this->Dbo->index($name, false); - $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); - - $name = $this->Dbo->fullTableName('with_a_key'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));'); - $expected = array( - 'PRIMARY' => array('column' => 'id', 'unique' => 1), - 'pointless_bool' => array('column' => 'bool', 'unique' => 0), - ); - $result = $this->Dbo->index($name, false); - $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); - - $name = $this->Dbo->fullTableName('with_two_keys'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));'); - $expected = array( - 'PRIMARY' => array('column' => 'id', 'unique' => 1), - 'pointless_bool' => array('column' => 'bool', 'unique' => 0), - 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), - ); - $result = $this->Dbo->index($name, false); - $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); - - $name = $this->Dbo->fullTableName('with_compound_keys'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));'); - $expected = array( - 'PRIMARY' => array('column' => 'id', 'unique' => 1), - 'pointless_bool' => array('column' => 'bool', 'unique' => 0), - 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), - 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0), - ); - $result = $this->Dbo->index($name, false); - $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); - - $name = $this->Dbo->fullTableName('with_multiple_compound_keys'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));'); - $expected = array( - 'PRIMARY' => array('column' => 'id', 'unique' => 1), - 'pointless_bool' => array('column' => 'bool', 'unique' => 0), - 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), - 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0), - 'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0), - ); - $result = $this->Dbo->index($name, false); - $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); - } - -/** - * testColumn method - * - * @return void - */ - public function testColumn() { - $result = $this->Dbo->column('varchar(50)'); - $expected = 'string'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('text'); - $expected = 'text'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('int(11)'); - $expected = 'integer'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('int(11) unsigned'); - $expected = 'integer'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('tinyint(1)'); - $expected = 'boolean'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('boolean'); - $expected = 'boolean'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('float'); - $expected = 'float'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('float unsigned'); - $expected = 'float'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('double unsigned'); - $expected = 'float'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('decimal(14,7) unsigned'); - $expected = 'float'; - $this->assertEqual($result, $expected); - } - -/** - * test transaction commands. - * - * @return void - */ - public function testTransactions() { - $this->Dbo->testing = false; - $result = $this->Dbo->begin($this->model); - $this->assertTrue($result); - - $log = $this->Dbo->getLog(); - $beginSqlCalls = Set::extract('/.[query=START TRANSACTION]', $log['log']); - $this->assertEqual(1, count($beginSqlCalls)); - - $result = $this->Dbo->commit($this->model); - $this->assertTrue($result); - } -/** - * test that float values are correctly identified - * - * @return void - */ - function testFloatParsing() { - $model = new Model(array('ds' => 'test', 'table' => 'datatypes', 'name' => 'Datatype')); - $result = $this->Dbo->describe($model); - $this->assertEqual((string)$result['float_field']['length'], '5,2'); - } - -/** - * test that tableParameters like collation, charset and engine are functioning. - * - * @access public - * @return void - */ - function testReadTableParameters() { - $table = 'tinyint' . uniqid(); - $this->Dbo->cacheSources = $this->Dbo->testing = false; - $this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($table) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'); - $result = $this->Dbo->readTableParameters($table); - $expected = array( - 'charset' => 'utf8', - 'collate' => 'utf8_unicode_ci', - 'engine' => 'InnoDB'); - $this->assertEqual($result, $expected); - - $this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($table)); - $this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($table) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=MyISAM DEFAULT CHARSET=cp1250 COLLATE=cp1250_general_ci;'); - $result = $this->Dbo->readTableParameters($table); - $expected = array( - 'charset' => 'cp1250', - 'collate' => 'cp1250_general_ci', - 'engine' => 'MyISAM'); - $this->assertEqual($result, $expected); - $this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($table)); - } -} diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php index bf3ebd77c..1338ed791 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php @@ -244,6 +244,7 @@ class DboPostgresTest extends CakeTestCase { public function setUp() { Configure::write('Cache.disable', true); $this->Dbo = ConnectionManager::getDataSource('test'); + $this->skipIf(!($this->Dbo instanceof DboPostgres)); $this->Dbo2 = new DboPostgresTestDb($this->Dbo->config, false); $this->skipUnless($this->Dbo->config['driver'] == 'postgres', 'PostgreSQL connection not available'); $this->model = new PostgresTestModel(); @@ -330,30 +331,30 @@ class DboPostgresTest extends CakeTestCase { * @return void */ function testValueQuoting() { - $this->assertIdentical($this->db2->value(1.2, 'float'), "'1.200000'"); - $this->assertEqual($this->db2->value('1,2', 'float'), "'1,2'"); + $this->assertEqual($this->Dbo->value(1.2, 'float'), "1.200000"); + $this->assertEqual($this->Dbo->value('1,2', 'float'), "'1,2'"); - $this->assertEqual($this->Dbo2->value('0', 'integer'), "'0'"); - $this->assertEqual($this->Dbo2->value('', 'integer'), 'NULL'); - $this->assertEqual($this->Dbo2->value('', 'float'), 'NULL'); - $this->assertEqual($this->Dbo2->value('', 'integer', false), "DEFAULT"); - $this->assertEqual($this->Dbo2->value('', 'float', false), "DEFAULT"); - $this->assertEqual($this->Dbo2->value('0.0', 'float'), "'0.0'"); + $this->assertEqual($this->Dbo->value('0', 'integer'), "0"); + $this->assertEqual($this->Dbo->value('', 'integer'), 'NULL'); + $this->assertEqual($this->Dbo->value('', 'float'), 'NULL'); + $this->assertEqual($this->Dbo->value('', 'integer', false), "NULL"); + $this->assertEqual($this->Dbo->value('', 'float', false), "NULL"); + $this->assertEqual($this->Dbo->value('0.0', 'float'), "'0.0'"); - $this->assertEqual($this->Dbo2->value('t', 'boolean'), "TRUE"); - $this->assertEqual($this->Dbo2->value('f', 'boolean'), "FALSE"); - $this->assertEqual($this->Dbo2->value(true), "TRUE"); - $this->assertEqual($this->Dbo2->value(false), "FALSE"); - $this->assertEqual($this->Dbo2->value('t'), "'t'"); - $this->assertEqual($this->Dbo2->value('f'), "'f'"); - $this->assertEqual($this->Dbo2->value('true', 'boolean'), 'TRUE'); - $this->assertEqual($this->Dbo2->value('false', 'boolean'), 'FALSE'); - $this->assertEqual($this->Dbo2->value('', 'boolean'), 'FALSE'); - $this->assertEqual($this->Dbo2->value(0, 'boolean'), 'FALSE'); - $this->assertEqual($this->Dbo2->value(1, 'boolean'), 'TRUE'); - $this->assertEqual($this->Dbo2->value('1', 'boolean'), 'TRUE'); - $this->assertEqual($this->Dbo2->value(null, 'boolean'), "NULL"); - $this->assertEqual($this->Dbo2->value(array()), "NULL"); + $this->assertEqual($this->Dbo->value('t', 'boolean'), "'TRUE'"); + $this->assertEqual($this->Dbo->value('f', 'boolean'), "'FALSE'"); + $this->assertEqual($this->Dbo->value(true), "'TRUE'"); + $this->assertEqual($this->Dbo->value(false), "'FALSE'"); + $this->assertEqual($this->Dbo->value('t'), "'t'"); + $this->assertEqual($this->Dbo->value('f'), "'f'"); + $this->assertEqual($this->Dbo->value('true', 'boolean'), "'TRUE'"); + $this->assertEqual($this->Dbo->value('false', 'boolean'), "'FALSE'"); + $this->assertEqual($this->Dbo->value('', 'boolean'), "'FALSE'"); + $this->assertEqual($this->Dbo->value(0, 'boolean'), "'FALSE'"); + $this->assertEqual($this->Dbo->value(1, 'boolean'), "'TRUE'"); + $this->assertEqual($this->Dbo->value('1', 'boolean'), "'TRUE'"); + $this->assertEqual($this->Dbo->value(null, 'boolean'), "NULL"); + $this->assertEqual($this->Dbo->value(array()), "NULL"); } /** @@ -366,10 +367,10 @@ class DboPostgresTest extends CakeTestCase { setlocale(LC_ALL, 'de_DE'); $result = $this->db->value(3.141593, 'float'); - $this->assertEqual((string)$result, "'3.141593'"); + $this->assertEqual((string)$result, "3.141593"); $result = $this->db->value(3.14); - $this->assertEqual((string)$result, "'3.140000'"); + $this->assertEqual((string)$result, "3.140000"); setlocale(LC_ALL, $restore); } @@ -380,17 +381,17 @@ class DboPostgresTest extends CakeTestCase { * @return void */ function testDateAndTimeAsNull() { - $this->assertEqual($this->Dbo2->value(null, 'date'), 'NULL'); - $this->assertEqual($this->Dbo2->value('', 'date'), 'NULL'); + $this->assertEqual($this->Dbo->value(null, 'date'), 'NULL'); + $this->assertEqual($this->Dbo->value('', 'date'), 'NULL'); - $this->assertEqual($this->Dbo2->value('', 'datetime'), 'NULL'); - $this->assertEqual($this->Dbo2->value(null, 'datetime'), 'NULL'); + $this->assertEqual($this->Dbo->value('', 'datetime'), 'NULL'); + $this->assertEqual($this->Dbo->value(null, 'datetime'), 'NULL'); - $this->assertEqual($this->Dbo2->value('', 'timestamp'), 'NULL'); - $this->assertEqual($this->Dbo2->value(null, 'timestamp'), 'NULL'); + $this->assertEqual($this->Dbo->value('', 'timestamp'), 'NULL'); + $this->assertEqual($this->Dbo->value(null, 'timestamp'), 'NULL'); - $this->assertEqual($this->Dbo2->value('', 'time'), 'NULL'); - $this->assertEqual($this->Dbo2->value(null, 'time'), 'NULL'); + $this->assertEqual($this->Dbo->value('', 'time'), 'NULL'); + $this->assertEqual($this->Dbo->value(null, 'time'), 'NULL'); } /** @@ -400,19 +401,19 @@ class DboPostgresTest extends CakeTestCase { * @return void */ function testBooleanNormalization() { - $this->assertTrue($this->Dbo2->boolean('t')); - $this->assertTrue($this->Dbo2->boolean('true')); - $this->assertTrue($this->Dbo2->boolean('TRUE')); - $this->assertTrue($this->Dbo2->boolean(true)); - $this->assertTrue($this->Dbo2->boolean(1)); - $this->assertTrue($this->Dbo2->boolean(" ")); + $this->assertEquals(1, $this->Dbo2->boolean('t', false)); + $this->assertEquals(1, $this->Dbo2->boolean('true', false)); + $this->assertEquals(1, $this->Dbo2->boolean('TRUE', false)); + $this->assertEquals(1, $this->Dbo2->boolean(true, false)); + $this->assertEquals(1, $this->Dbo2->boolean(1, false)); + $this->assertEquals(1, $this->Dbo2->boolean(" ", false)); - $this->assertFalse($this->Dbo2->boolean('f')); - $this->assertFalse($this->Dbo2->boolean('false')); - $this->assertFalse($this->Dbo2->boolean('FALSE')); - $this->assertFalse($this->Dbo2->boolean(false)); - $this->assertFalse($this->Dbo2->boolean(0)); - $this->assertFalse($this->Dbo2->boolean('')); + $this->assertEquals(0, $this->Dbo2->boolean('f', false)); + $this->assertEquals(0, $this->Dbo2->boolean('false', false)); + $this->assertEquals(0, $this->Dbo2->boolean('FALSE', false)); + $this->assertEquals(0, $this->Dbo2->boolean(false, false)); + $this->assertEquals(0, $this->Dbo2->boolean(0, false)); + $this->assertEquals(0, $this->Dbo2->boolean('', false)); } /** @@ -427,7 +428,7 @@ class DboPostgresTest extends CakeTestCase { $db2 = clone $db1; $db2->connect(); - $this->assertNotEqual($db1->connection, $db2->connection); + $this->assertNotSame($db1->getConnection(), $db2->getConnection()); $table = $db1->fullTableName('users', false); $password = '5f4dcc3b5aa765d61d8327deb882cf99'; @@ -556,12 +557,12 @@ class DboPostgresTest extends CakeTestCase { $db1 = ConnectionManager::getDataSource('test'); $db1->cacheSources = false; $db1->reconnect(array('persistent' => false)); - $db1->query('CREATE TABLE ' . $db1->fullTableName('datatypes') . ' ( + $db1->rawQuery('CREATE TABLE ' . $db1->fullTableName('datatypes') . ' ( id serial NOT NULL, "varchar" character varying(40) NOT NULL, "full_length" character varying NOT NULL, "timestamp" timestamp without time zone, - date date, + "date" date, CONSTRAINT test_data_types_pkey PRIMARY KEY (id) )'); $model = new Model(array('name' => 'Datatype', 'ds' => 'test')); @@ -570,21 +571,21 @@ class DboPostgresTest extends CakeTestCase { 'connection' => 'test', 'models' => array('Datatype') )); - $schema->tables = array('datatypes' => $result['tables']['datatypes']); + + $schema->tables = array('datatypes' => $result['tables']['missing']['datatypes']); $result = $db1->createSchema($schema, 'datatypes'); + $db1->rawQuery('DROP TABLE ' . $db1->fullTableName('datatypes')); $this->assertNoPattern('/timestamp DEFAULT/', $result); $this->assertPattern('/\"full_length\"\s*text\s.*,/', $result); $this->assertPattern('/timestamp\s*,/', $result); - $db1->query('DROP TABLE ' . $db1->fullTableName('datatypes')); - $db1->query($result); $result2 = $schema->read(array( 'connection' => 'test', 'models' => array('Datatype') )); - $schema->tables = array('datatypes' => $result2['tables']['datatypes']); + $schema->tables = array('datatypes' => $result2['tables']['missing']['datatypes']); $result2 = $db1->createSchema($schema, 'datatypes'); $this->assertEqual($result, $result2); @@ -602,25 +603,25 @@ class DboPostgresTest extends CakeTestCase { $this->Dbo->query('CREATE INDEX pointless_bool ON ' . $name . '("bool")'); $this->Dbo->query('CREATE UNIQUE INDEX char_index ON ' . $name . '("small_char")'); $expected = array( - 'PRIMARY' => array('column' => 'id', 'unique' => true), - 'pointless_bool' => array('column' => 'bool', 'unique' => false), - 'char_index' => array('column' => 'small_char', 'unique' => true), + 'PRIMARY' => array('column' => 'id', 'unique' => 1), + 'pointless_bool' => array('column' => 'bool', 'unique' => 0), + 'char_index' => array('column' => 'small_char', 'unique' => 1), ); $result = $this->Dbo->index($name); + $this->Dbo->query('DROP TABLE ' . $name); $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); $name = $this->Dbo->fullTableName('index_test_2', false); $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" serial NOT NULL PRIMARY KEY, "bool" integer, "small_char" varchar(50), "description" varchar(40) )'); $this->Dbo->query('CREATE UNIQUE INDEX multi_col ON ' . $name . '("small_char", "bool")'); $expected = array( - 'PRIMARY' => array('column' => 'id', 'unique' => true), - 'multi_col' => array('column' => array('small_char', 'bool'), 'unique' => true), + 'PRIMARY' => array('column' => 'id', 'unique' => 1), + 'multi_col' => array('column' => array('small_char', 'bool'), 'unique' => 1), ); $result = $this->Dbo->index($name); - $this->assertEqual($expected, $result); $this->Dbo->query('DROP TABLE ' . $name); + $this->assertEqual($expected, $result); } /** @@ -691,21 +692,21 @@ class DboPostgresTest extends CakeTestCase { 'group2' => array('type' => 'integer', 'null' => true) ) )); - $this->Dbo->query($this->Dbo->createSchema($schema1)); + $this->Dbo->rawQuery($this->Dbo->createSchema($schema1)); $schema2 = new CakeSchema(array( 'name' => 'AlterTest2', 'connection' => 'test', 'altertest' => array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => 0), - 'name' => array('type' => 'string', 'null' => false, 'length' => 50), - 'group1' => array('type' => 'integer', 'null' => true), - 'group2' => array('type' => 'integer', 'null' => true), + 'id' => array('type' => 'integer', 'null' => 0, 'default' => 0), + 'name' => array('type' => 'string', 'null' => 0, 'length' => 50), + 'group1' => array('type' => 'integer', 'null' => 1), + 'group2' => array('type' => 'integer', 'null' => 1), 'indexes' => array( - 'name_idx' => array('column' => 'name', 'unique' => false), - 'group_idx' => array('column' => 'group1', 'unique' => false), - 'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => false), - 'PRIMARY' => array('column' => 'id', 'unique' => true) + 'name_idx' => array('column' => 'name', 'unique' => 0), + 'group_idx' => array('column' => 'group1', 'unique' => 0), + 'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => 0), + 'PRIMARY' => array('column' => 'id', 'unique' => 1) ) ) )); @@ -719,15 +720,15 @@ class DboPostgresTest extends CakeTestCase { 'name' => 'AlterTest3', 'connection' => 'test', 'altertest' => array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => 0), - 'name' => array('type' => 'string', 'null' => false, 'length' => 50), - 'group1' => array('type' => 'integer', 'null' => true), - 'group2' => array('type' => 'integer', 'null' => true), + 'id' => array('type' => 'integer', 'null' => 0, 'default' => 0), + 'name' => array('type' => 'string', 'null' => 0, 'length' => 50), + 'group1' => array('type' => 'integer', 'null' => 1), + 'group2' => array('type' => 'integer', 'null' => 1), 'indexes' => array( - 'name_idx' => array('column' => 'name', 'unique' => true), - 'group_idx' => array('column' => 'group2', 'unique' => false), - 'compound_idx' => array('column' => array('group2', 'group1'), 'unique' => false), - 'another_idx' => array('column' => array('group1', 'name'), 'unique' => false)) + 'name_idx' => array('column' => 'name', 'unique' => 1), + 'group_idx' => array('column' => 'group2', 'unique' => 0), + 'compound_idx' => array('column' => array('group2', 'group1'), 'unique' => 0), + 'another_idx' => array('column' => array('group1', 'name'), 'unique' => 0)) ))); $this->Dbo->query($this->Dbo->alterSchema($schema3->compare($schema2))); diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php index 4cffa8e4a..e253b1a26 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php @@ -90,14 +90,6 @@ class DboSqliteTest extends CakeTestCase { */ public $Dbo = null; -/** - * Simulated DB connection used in testing - * - * @var DboSource - * @access public - */ - public $Dbo2 = null; - /** * Sets up a Dbo class instance for testing * @@ -108,7 +100,6 @@ class DboSqliteTest extends CakeTestCase { if ($this->Dbo->config['driver'] !== 'sqlite') { $this->markTestSkipped('The Sqlite extension is not available.'); } - $this->Dbo2 = new DboSqliteTestDb($this->Dbo->config, false); } /** @@ -117,7 +108,6 @@ class DboSqliteTest extends CakeTestCase { */ public function tearDown() { Configure::write('Cache.disable', false); - unset($this->Dbo2); } /** @@ -127,10 +117,11 @@ class DboSqliteTest extends CakeTestCase { public function testTableListCacheDisabling() { $this->assertFalse(in_array('foo_test', $this->Dbo->listSources())); - $this->Dbo->query('CREATE TABLE foo_test (test VARCHAR(255));'); + $this->Dbo->query('CREATE TABLE foo_test (test VARCHAR(255))'); $this->assertTrue(in_array('foo_test', $this->Dbo->listSources())); - $this->Dbo->query('DROP TABLE foo_test;'); + $this->Dbo->cacheSources = false; + $this->Dbo->query('DROP TABLE foo_test'); $this->assertFalse(in_array('foo_test', $this->Dbo->listSources())); } @@ -207,7 +198,7 @@ class DboSqliteTest extends CakeTestCase { 'null' => false, ); $result = $this->Dbo->buildColumn($data); - $expected = '"int_field" integer(11) NOT NULL'; + $expected = '"int_field" integer NOT NULL'; $this->assertEqual($result, $expected); $data = array( @@ -251,7 +242,7 @@ class DboSqliteTest extends CakeTestCase { 'null' => false, ); $result = $this->Dbo->buildColumn($data); - $expected = '"testName" integer(10) DEFAULT \'10\' NOT NULL'; + $expected = '"testName" integer(10) DEFAULT 10 NOT NULL'; $this->assertEqual($result, $expected); $data = array( @@ -263,7 +254,7 @@ class DboSqliteTest extends CakeTestCase { 'collate' => 'BADVALUE' ); $result = $this->Dbo->buildColumn($data); - $expected = '"testName" integer(10) DEFAULT \'10\' NOT NULL'; + $expected = '"testName" integer(10) DEFAULT 10 NOT NULL'; $this->assertEqual($result, $expected); } diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index fc09e5d55..9ac868c3b 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -21,1230 +21,6 @@ App::import('Model', array('Model', 'DataSource', 'DboSource', 'DboMysql', 'App')); require_once dirname(dirname(__FILE__)) . DS . 'models.php'; -/** - * TestModel class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class TestModel extends CakeTestModel { - -/** - * name property - * - * @var string 'TestModel' - * @access public - */ - public $name = 'TestModel'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * schema property - * - * @var array - * @access protected - */ - protected $_schema = array( - 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '11'), - 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'), - 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => '155'), - 'last_login' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''), - 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), - 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) - ); - -/** - * find method - * - * @param mixed $conditions - * @param mixed $fields - * @param mixed $order - * @param mixed $recursive - * @access public - * @return void - */ - function find($conditions = null, $fields = null, $order = null, $recursive = null) { - return array($conditions, $fields); - } - -/** - * findAll method - * - * @param mixed $conditions - * @param mixed $fields - * @param mixed $order - * @param mixed $recursive - * @access public - * @return void - */ - function findAll($conditions = null, $fields = null, $order = null, $recursive = null) { - return $conditions; - } -} - -/** - * TestModel2 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class TestModel2 extends CakeTestModel { - -/** - * name property - * - * @var string 'TestModel2' - * @access public - */ - public $name = 'TestModel2'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; -} - -/** - * TestModel4 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class TestModel3 extends CakeTestModel { - -/** - * name property - * - * @var string 'TestModel3' - * @access public - */ - public $name = 'TestModel3'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; -} - -/** - * TestModel4 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class TestModel4 extends CakeTestModel { - -/** - * name property - * - * @var string 'TestModel4' - * @access public - */ - public $name = 'TestModel4'; - -/** - * table property - * - * @var string 'test_model4' - * @access public - */ - public $table = 'test_model4'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array( - 'TestModel4Parent' => array( - 'className' => 'TestModel4', - 'foreignKey' => 'parent_id' - ) - ); - -/** - * hasOne property - * - * @var array - * @access public - */ - public $hasOne = array( - 'TestModel5' => array( - 'className' => 'TestModel5', - 'foreignKey' => 'test_model4_id' - ) - ); - -/** - * hasAndBelongsToMany property - * - * @var array - * @access public - */ - public $hasAndBelongsToMany = array('TestModel7' => array( - 'className' => 'TestModel7', - 'joinTable' => 'test_model4_test_model7', - 'foreignKey' => 'test_model4_id', - 'associationForeignKey' => 'test_model7_id', - 'with' => 'TestModel4TestModel7' - )); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), - 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) - ); - } - return $this->_schema; - } -} - -/** - * TestModel4TestModel7 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class TestModel4TestModel7 extends CakeTestModel { - -/** - * name property - * - * @var string 'TestModel4TestModel7' - * @access public - */ - public $name = 'TestModel4TestModel7'; - -/** - * table property - * - * @var string 'test_model4_test_model7' - * @access public - */ - public $table = 'test_model4_test_model7'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'test_model4_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'test_model7_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8') - ); - } - return $this->_schema; - } -} - -/** - * TestModel5 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class TestModel5 extends CakeTestModel { - -/** - * name property - * - * @var string 'TestModel5' - * @access public - */ - public $name = 'TestModel5'; - -/** - * table property - * - * @var string 'test_model5' - * @access public - */ - public $table = 'test_model5'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array('TestModel4' => array( - 'className' => 'TestModel4', - 'foreignKey' => 'test_model4_id' - )); - -/** - * hasMany property - * - * @var array - * @access public - */ - public $hasMany = array('TestModel6' => array( - 'className' => 'TestModel6', - 'foreignKey' => 'test_model5_id' - )); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'test_model4_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), - 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) - ); - } - return $this->_schema; - } -} - -/** - * TestModel6 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class TestModel6 extends CakeTestModel { - -/** - * name property - * - * @var string 'TestModel6' - * @access public - */ - public $name = 'TestModel6'; - -/** - * table property - * - * @var string 'test_model6' - * @access public - */ - public $table = 'test_model6'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array('TestModel5' => array( - 'className' => 'TestModel5', - 'foreignKey' => 'test_model5_id' - )); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'test_model5_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), - 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) - ); - } - return $this->_schema; - } -} - -/** - * TestModel7 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class TestModel7 extends CakeTestModel { - -/** - * name property - * - * @var string 'TestModel7' - * @access public - */ - public $name = 'TestModel7'; - -/** - * table property - * - * @var string 'test_model7' - * @access public - */ - public $table = 'test_model7'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), - 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) - ); - } - return $this->_schema; - } -} - -/** - * TestModel8 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class TestModel8 extends CakeTestModel { - -/** - * name property - * - * @var string 'TestModel8' - * @access public - */ - public $name = 'TestModel8'; - -/** - * table property - * - * @var string 'test_model8' - * @access public - */ - public $table = 'test_model8'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * hasOne property - * - * @var array - * @access public - */ - public $hasOne = array( - 'TestModel9' => array( - 'className' => 'TestModel9', - 'foreignKey' => 'test_model8_id', - 'conditions' => 'TestModel9.name != \'mariano\'' - ) - ); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'test_model9_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), - 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) - ); - } - return $this->_schema; - } -} - -/** - * TestModel9 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class TestModel9 extends CakeTestModel { - -/** - * name property - * - * @var string 'TestModel9' - * @access public - */ - public $name = 'TestModel9'; - -/** - * table property - * - * @var string 'test_model9' - * @access public - */ - public $table = 'test_model9'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array('TestModel8' => array( - 'className' => 'TestModel8', - 'foreignKey' => 'test_model8_id', - 'conditions' => 'TestModel8.name != \'larry\'' - )); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'test_model8_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '11'), - 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), - 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) - ); - } - return $this->_schema; - } -} - -/** - * Level class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class Level extends CakeTestModel { - -/** - * name property - * - * @var string 'Level' - * @access public - */ - public $name = 'Level'; - -/** - * table property - * - * @var string 'level' - * @access public - */ - public $table = 'level'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * hasMany property - * - * @var array - * @access public - */ - public $hasMany = array( - 'Group'=> array( - 'className' => 'Group' - ), - 'User2' => array( - 'className' => 'User2' - ) - ); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), - 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'), - ); - } - return $this->_schema; - } -} - -/** - * Group class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class Group extends CakeTestModel { - -/** - * name property - * - * @var string 'Group' - * @access public - */ - public $name = 'Group'; - -/** - * table property - * - * @var string 'group' - * @access public - */ - public $table = 'group'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array('Level'); - -/** - * hasMany property - * - * @var array - * @access public - */ - public $hasMany = array('Category2', 'User2'); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), - 'level_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), - 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'), - ); - } - return $this->_schema; - } - -} - -/** - * User2 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class User2 extends CakeTestModel { - -/** - * name property - * - * @var string 'User2' - * @access public - */ - public $name = 'User2'; - -/** - * table property - * - * @var string 'user' - * @access public - */ - public $table = 'user'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array( - 'Group' => array( - 'className' => 'Group' - ), - 'Level' => array( - 'className' => 'Level' - ) - ); - -/** - * hasMany property - * - * @var array - * @access public - */ - public $hasMany = array( - 'Article2' => array( - 'className' => 'Article2' - ), - ); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), - 'group_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), - 'level_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), - 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'), - ); - } - return $this->_schema; - } -} - -/** - * Category2 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class Category2 extends CakeTestModel { - -/** - * name property - * - * @var string 'Category2' - * @access public - */ - public $name = 'Category2'; - -/** - * table property - * - * @var string 'category' - * @access public - */ - public $table = 'category'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array( - 'Group' => array( - 'className' => 'Group', - 'foreignKey' => 'group_id' - ), - 'ParentCat' => array( - 'className' => 'Category2', - 'foreignKey' => 'parent_id' - ) - ); - -/** - * hasMany property - * - * @var array - * @access public - */ - public $hasMany = array( - 'ChildCat' => array( - 'className' => 'Category2', - 'foreignKey' => 'parent_id' - ), - 'Article2' => array( - 'className' => 'Article2', - 'order'=>'Article2.published_date DESC', - 'foreignKey' => 'category_id', - 'limit'=>'3') - ); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), - 'group_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), - 'parent_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), - 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'), - 'icon' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'), - 'description' => array('type' => 'text', 'null' => false, 'default' => '', 'length' => null), - - ); - } - return $this->_schema; - } -} - -/** - * Article2 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class Article2 extends CakeTestModel { - -/** - * name property - * - * @var string 'Article2' - * @access public - */ - public $name = 'Article2'; - -/** - * table property - * - * @var string 'article' - * @access public - */ - public $table = 'article'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array( - 'Category2' => array('className' => 'Category2'), - 'User2' => array('className' => 'User2') - ); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), - 'category_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'user_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'rate_count' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'rate_sum' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'viewed' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'version' => array('type' => 'string', 'null' => true, 'default' => '', 'length' => '45'), - 'title' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '200'), - 'intro' => array('text' => 'string', 'null' => true, 'default' => '', 'length' => null), - 'comments' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '4'), - 'body' => array('text' => 'string', 'null' => true, 'default' => '', 'length' => null), - 'isdraft' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'), - 'allow_comments' => array('type' => 'boolean', 'null' => false, 'default' => '1', 'length' => '1'), - 'moderate_comments' => array('type' => 'boolean', 'null' => false, 'default' => '1', 'length' => '1'), - 'published' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'), - 'multipage' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'), - 'published_date' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null), - 'created' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null), - 'modified' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null) - ); - } - return $this->_schema; - } -} - -/** - * CategoryFeatured2 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class CategoryFeatured2 extends CakeTestModel { - -/** - * name property - * - * @var string 'CategoryFeatured2' - * @access public - */ - public $name = 'CategoryFeatured2'; - -/** - * table property - * - * @var string 'category_featured' - * @access public - */ - public $table = 'category_featured'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), - 'parent_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), - 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'), - 'icon' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'), - 'description' => array('text' => 'string', 'null' => false, 'default' => '', 'length' => null) - ); - } - return $this->_schema; - } -} - -/** - * Featured2 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class Featured2 extends CakeTestModel { - -/** - * name property - * - * @var string 'Featured2' - * @access public - */ - public $name = 'Featured2'; - -/** - * table property - * - * @var string 'featured2' - * @access public - */ - public $table = 'featured2'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array( - 'CategoryFeatured2' => array( - 'className' => 'CategoryFeatured2' - ) - ); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), - 'article_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'category_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20') - ); - } - return $this->_schema; - } -} - -/** - * Comment2 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class Comment2 extends CakeTestModel { - -/** - * name property - * - * @var string 'Comment2' - * @access public - */ - public $name = 'Comment2'; - -/** - * table property - * - * @var string 'comment' - * @access public - */ - public $table = 'comment'; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array('ArticleFeatured2', 'User2'); - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), - 'article_featured_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'user_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20') - ); - } - return $this->_schema; - } -} - -/** - * ArticleFeatured2 class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class ArticleFeatured2 extends CakeTestModel { - -/** - * name property - * - * @var string 'ArticleFeatured2' - * @access public - */ - public $name = 'ArticleFeatured2'; - -/** - * table property - * - * @var string 'article_featured' - * @access public - */ - public $table = 'article_featured'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * belongsTo property - * - * @var array - * @access public - */ - public $belongsTo = array( - 'CategoryFeatured2' => array('className' => 'CategoryFeatured2'), - 'User2' => array('className' => 'User2') - ); - -/** - * hasOne property - * - * @var array - * @access public - */ - public $hasOne = array( - 'Featured2' => array('className' => 'Featured2') - ); - -/** - * hasMany property - * - * @var array - * @access public - */ - public $hasMany = array( - 'Comment2' => array('className'=>'Comment2', 'dependent' => true) - ); - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - if (!isset($this->_schema)) { - $this->_schema = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), - 'category_featured_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'user_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), - 'title' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'), - 'body' => array('text' => 'string', 'null' => true, 'default' => '', 'length' => null), - 'published' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'), - 'published_date' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null), - 'created' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null), - 'modified' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null) - ); - } - return $this->_schema; - } -} - /** * DboSourceTest class * @@ -1339,918 +115,6 @@ class DboSourceTest extends CakeTestCase { unset($this->Model); } -/** - * testFieldDoubleEscaping method - * - * @access public - * @return void - */ - function testFieldDoubleEscaping() { - $config = array_merge($this->__config, array('driver' => 'test')); - $test = ConnectionManager::create('quoteTest', $config); - $test->simulated = array(); - - $this->Model = new Article2(array('alias' => 'Article', 'ds' => 'quoteTest')); - $this->Model->setDataSource('quoteTest'); - - $this->assertEqual($this->Model->escapeField(), '`Article`.`id`'); - $result = $test->fields($this->Model, null, $this->Model->escapeField()); - $this->assertEqual($result, array('`Article`.`id`')); - - $result = $test->read($this->Model, array( - 'fields' => $this->Model->escapeField(), - 'conditions' => null, - 'recursive' => -1 - )); - $this->assertEqual(trim($test->simulated[0]), 'SELECT `Article`.`id` FROM `' . $this->testDb->fullTableName('article', false) . '` AS `Article` WHERE 1 = 1'); - - $test->startQuote = '['; - $test->endQuote = ']'; - $this->assertEqual($this->Model->escapeField(), '[Article].[id]'); - - $result = $test->fields($this->Model, null, $this->Model->escapeField()); - $this->assertEqual($result, array('[Article].[id]')); - - $result = $test->read($this->Model, array( - 'fields' => $this->Model->escapeField(), - 'conditions' => null, - 'recursive' => -1 - )); - $this->assertEqual(trim($test->simulated[1]), 'SELECT [Article].[id] FROM [' . $this->testDb->fullTableName('article', false) . '] AS [Article] WHERE 1 = 1'); - - ClassRegistry::removeObject('Article'); - } - -/** - * testGenerateAssociationQuerySelfJoin method - * - * @access public - * @return void - */ - function testGenerateAssociationQuerySelfJoin() { - $this->startTime = microtime(true); - $this->Model = new Article2(); - $this->_buildRelatedModels($this->Model); - $this->_buildRelatedModels($this->Model->Category2); - $this->Model->Category2->ChildCat = new Category2(); - $this->Model->Category2->ParentCat = new Category2(); - - $queryData = array(); - - foreach ($this->Model->Category2->associations() as $type) { - foreach ($this->Model->Category2->{$type} as $assoc => $assocData) { - $linkModel = $this->Model->Category2->{$assoc}; - $external = isset($assocData['external']); - - if ($this->Model->Category2->alias == $linkModel->alias && $type != 'hasAndBelongsToMany' && $type != 'hasMany') { - $result = $this->testDb->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null); - $this->assertFalse(empty($result)); - } else { - if ($this->Model->Category2->useDbConfig == $linkModel->useDbConfig) { - $result = $this->testDb->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null); - $this->assertFalse(empty($result)); - } - } - } - } - - $query = $this->testDb->generateAssociationQuery($this->Model->Category2, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+(.+)FROM(.+)`Category2`\.`group_id`\s+=\s+`Group`\.`id`\)\s+LEFT JOIN(.+)WHERE\s+1 = 1\s*$/', $query); - - $this->Model = new TestModel4(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $_queryData = $queryData; - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertTrue($result); - - $expected = array( - 'conditions' => array(), - 'fields' => array( - '`TestModel4`.`id`', - '`TestModel4`.`name`', - '`TestModel4`.`created`', - '`TestModel4`.`updated`', - '`TestModel4Parent`.`id`', - '`TestModel4Parent`.`name`', - '`TestModel4Parent`.`created`', - '`TestModel4Parent`.`updated`' - ), - 'joins' => array( - array( - 'table' => '`test_model4`', - 'alias' => 'TestModel4Parent', - 'type' => 'LEFT', - 'conditions' => '`TestModel4`.`parent_id` = `TestModel4Parent`.`id`' - ) - ), - 'order' => array(), - 'limit' => array(), - 'offset' => array(), - 'group' => array() - ); - $this->assertEqual($queryData, $expected); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/', $result); - $this->assertPattern('/FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+`test_model4` AS `TestModel4Parent`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/', $result); - $this->assertPattern('/\s+WHERE\s+1 = 1\s+$/', $result); - - $params['assocData']['type'] = 'INNER'; - $this->Model->belongsTo['TestModel4Parent']['type'] = 'INNER'; - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $_queryData, $params['external'], $resultSet); - $this->assertTrue($result); - $this->assertEqual($_queryData['joins'][0]['type'], 'INNER'); - } - -/** - * testGenerateInnerJoinAssociationQuery method - * - * @access public - * @return void - */ - function testGenerateInnerJoinAssociationQuery() { - $this->Model = new TestModel9(); - $test = ConnectionManager::create('test2', $this->__config); - $this->Model->setDataSource('test2'); - $this->Model->TestModel8 = new TestModel8(); - $this->Model->TestModel8->setDataSource('test2'); - - $this->testDb->read($this->Model, array('recursive' => 1)); - $result = $this->testDb->getLastQuery(); - $this->assertPattern('/`TestModel9` LEFT JOIN `' . $this->testDb->fullTableName('test_model8', false) . '`/', $result); - - $this->Model->belongsTo['TestModel8']['type'] = 'INNER'; - $this->testDb->read($this->Model, array('recursive' => 1)); - $result = $this->testDb->getLastQuery(); - $this->assertPattern('/`TestModel9` INNER JOIN `' . $this->testDb->fullTableName('test_model8', false) . '`/', $result); - - } - -/** - * testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding method - * - * @access public - * @return void - */ - function testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding() { - $this->Model = new TestModel8(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'hasOne', 'model' => 'TestModel9'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - $_queryData = $queryData; - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertTrue($result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`, `TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`\s+/', $result); - $this->assertPattern('/FROM\s+`test_model8` AS `TestModel8`\s+LEFT JOIN\s+`test_model9` AS `TestModel9`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel9`\.`name` != \'mariano\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - } - -/** - * testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding method - * - * @access public - * @return void - */ - function testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding() { - $this->Model = new TestModel9(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'belongsTo', 'model' => 'TestModel8'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertTrue($result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`, `TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`\s+/', $result); - $this->assertPattern('/FROM\s+`test_model9` AS `TestModel9`\s+LEFT JOIN\s+`test_model8` AS `TestModel8`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel8`\.`name` != \'larry\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - } - -/** - * testGenerateAssociationQuerySelfJoinWithConditions method - * - * @access public - * @return void - */ - function testGenerateAssociationQuerySelfJoinWithConditions() { - $this->Model = new TestModel4(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent'); - $queryData = array('conditions' => array('TestModel4Parent.name !=' => 'mariano')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertTrue($result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/', $result); - $this->assertPattern('/FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+`test_model4` AS `TestModel4Parent`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?`TestModel4Parent`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result); - - $this->Featured2 = new Featured2(); - $this->Featured2->schema(); - - $this->Featured2->bindModel(array( - 'belongsTo' => array( - 'ArticleFeatured2' => array( - 'conditions' => 'ArticleFeatured2.published = \'Y\'', - 'fields' => 'id, title, user_id, published' - ) - ) - )); - - $this->_buildRelatedModels($this->Featured2); - - $binding = array('type' => 'belongsTo', 'model' => 'ArticleFeatured2'); - $queryData = array('conditions' => array()); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Featured2, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Featured2, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertTrue($result); - $result = $this->testDb->generateAssociationQuery($this->Featured2, $null, null, null, null, $queryData, false, $null); - - $this->assertPattern( - '/^SELECT\s+`Featured2`\.`id`, `Featured2`\.`article_id`, `Featured2`\.`category_id`, `Featured2`\.`name`,\s+'. - '`ArticleFeatured2`\.`id`, `ArticleFeatured2`\.`title`, `ArticleFeatured2`\.`user_id`, `ArticleFeatured2`\.`published`\s+' . - 'FROM\s+`featured2` AS `Featured2`\s+LEFT JOIN\s+`article_featured` AS `ArticleFeatured2`' . - '\s+ON\s+\(`ArticleFeatured2`.`published` = \'Y\'\s+AND\s+`Featured2`\.`article_featured2_id` = `ArticleFeatured2`\.`id`\)' . - '\s+WHERE\s+1\s+=\s+1\s*$/', - $result - ); - } - -/** - * testGenerateAssociationQueryHasOne method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasOne() { - $this->Model = new TestModel4(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'hasOne', 'model' => 'TestModel5'); - - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertTrue($result); - - $result = $this->testDb->buildJoinStatement($queryData['joins'][0]); - $expected = ' LEFT JOIN `test_model5` AS `TestModel5` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)'; - $this->assertEqual(trim($result), trim($expected)); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+/', $result); - $this->assertPattern('/`test_model5` AS `TestModel5`\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result); - } - -/** - * testGenerateAssociationQueryHasOneWithConditions method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasOneWithConditions() { - $this->Model = new TestModel4(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'hasOne', 'model' => 'TestModel5'); - - $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertTrue($result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - - $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+`test_model5` AS `TestModel5`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel5`.`test_model4_id`\s+=\s+`TestModel4`.`id`\)\s+WHERE/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?\s*`TestModel5`.`name`\s+!=\s+\'mariano\'\s*(?:\))?\s*$/', $result); - } - -/** - * testGenerateAssociationQueryBelongsTo method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryBelongsTo() { - $this->Model = new TestModel5(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type'=>'belongsTo', 'model'=>'TestModel4'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertTrue($result); - - $result = $this->testDb->buildJoinStatement($queryData['joins'][0]); - $expected = ' LEFT JOIN `test_model4` AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)'; - $this->assertEqual(trim($result), trim($expected)); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+`test_model4` AS `TestModel4`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result); - } - -/** - * testGenerateAssociationQueryBelongsToWithConditions method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryBelongsToWithConditions() { - $this->Model = new TestModel5(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'belongsTo', 'model' => 'TestModel4'); - $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertTrue($result); - - $result = $this->testDb->buildJoinStatement($queryData['joins'][0]); - $expected = ' LEFT JOIN `test_model4` AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)'; - $this->assertEqual(trim($result), trim($expected)); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+`test_model4` AS `TestModel4`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+`TestModel5`.`name` != \'mariano\'\s*$/', $result); - } - -/** - * testGenerateAssociationQueryHasMany method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasMany() { - $this->Model = new TestModel5(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - - $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE/', $result); - $this->assertPattern('/\s+WHERE\s+`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result); - } - -/** - * testGenerateAssociationQueryHasManyWithLimit method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasManyWithLimit() { - $this->Model = new TestModel5(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $this->Model->hasMany['TestModel6']['limit'] = 2; - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern( - '/^SELECT\s+' . - '`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+'. - 'FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+' . - '`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)\s*'. - 'LIMIT \d*'. - '\s*$/', $result - ); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern( - '/^SELECT\s+'. - '`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+'. - 'FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+'. - '(?:\()?\s*1 = 1\s*(?:\))?'. - '\s*$/', $result - ); - } - -/** - * testGenerateAssociationQueryHasManyWithConditions method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasManyWithConditions() { - $this->Model = new TestModel5(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); - $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?`TestModel5`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result); - } - -/** - * testGenerateAssociationQueryHasManyWithOffsetAndLimit method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasManyWithOffsetAndLimit() { - $this->Model = new TestModel5(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $__backup = $this->Model->hasMany['TestModel6']; - - $this->Model->hasMany['TestModel6']['offset'] = 2; - $this->Model->hasMany['TestModel6']['limit'] = 5; - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - - $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); - $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); - $this->assertPattern('/\s+LIMIT 2,\s*5\s*$/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - - $this->Model->hasMany['TestModel6'] = $__backup; - } - -/** - * testGenerateAssociationQueryHasManyWithPageAndLimit method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasManyWithPageAndLimit() { - $this->Model = new TestModel5(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $__backup = $this->Model->hasMany['TestModel6']; - - $this->Model->hasMany['TestModel6']['page'] = 2; - $this->Model->hasMany['TestModel6']['limit'] = 5; - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); - $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); - $this->assertPattern('/\s+LIMIT 5,\s*5\s*$/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - - $this->Model->hasMany['TestModel6'] = $__backup; - } - -/** - * testGenerateAssociationQueryHasManyWithFields method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasManyWithFields() { - $this->Model = new TestModel5(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array('fields' => array('`TestModel5`.`name`')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); - $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`id`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array('fields' => array('`TestModel5`.`id`, `TestModel5`.`name`')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); - $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array('fields' => array('`TestModel5`.`name`', '`TestModel5`.`created`')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); - $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`id`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - - $this->Model->hasMany['TestModel6']['fields'] = array('name'); - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); - $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - - unset($this->Model->hasMany['TestModel6']['fields']); - - $this->Model->hasMany['TestModel6']['fields'] = array('id', 'name'); - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); - $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - - unset($this->Model->hasMany['TestModel6']['fields']); - - $this->Model->hasMany['TestModel6']['fields'] = array('test_model5_id', 'name'); - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel6`\.`test_model5_id`, `TestModel6`\.`name`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result); - $this->assertPattern('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - - unset($this->Model->hasMany['TestModel6']['fields']); - } - -/** - * test generateAssociationQuery with a hasMany and an aggregate function. - * - * @return void - */ - function testGenerateAssociationQueryHasManyAndAggregateFunction() { - $this->Model = new TestModel5(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'hasMany', 'model' => 'TestModel6'); - $queryData = array('fields' => array('MIN(TestModel5.test_model4_id)')); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - $this->Model->recursive = 0; - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, $params['type'], $params['assoc'], $params['assocData'], $queryData, false, $resultSet); - $this->assertPattern('/^SELECT\s+MIN\(`TestModel5`\.`test_model4_id`\)\s+FROM/', $result); - } - -/** - * testGenerateAssociationQueryHasAndBelongsToMany method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasAndBelongsToMany() { - $this->Model = new TestModel4(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model7` AS `TestModel7`\s+JOIN\s+`' . $this->testDb->fullTableName('test_model4_test_model7', false) . '`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+AND/', $result); - $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)/', $result); - $this->assertPattern('/WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE/', $result); - $this->assertPattern('/\s+WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result); - } - -/** - * testGenerateAssociationQueryHasAndBelongsToManyWithConditions method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasAndBelongsToManyWithConditions() { - $this->Model = new TestModel4(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7'); - $queryData = array('conditions' => array('TestModel4.name !=' => 'mariano')); - $resultSet = null; - $null = null; - - $params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/', $result); - $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?`TestModel4`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result); - } - -/** - * testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit() { - $this->Model = new TestModel4(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $__backup = $this->Model->hasAndBelongsToMany['TestModel7']; - - $this->Model->hasAndBelongsToMany['TestModel7']['offset'] = 2; - $this->Model->hasAndBelongsToMany['TestModel7']['limit'] = 5; - - $binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+/', $result); - $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result); - $this->assertPattern('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 2,\s*5\s*$/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - - $this->Model->hasAndBelongsToMany['TestModel7'] = $__backup; - } - -/** - * testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit method - * - * @access public - * @return void - */ - function testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit() { - $this->Model = new TestModel4(); - $this->Model->schema(); - $this->_buildRelatedModels($this->Model); - - $__backup = $this->Model->hasAndBelongsToMany['TestModel7']; - - $this->Model->hasAndBelongsToMany['TestModel7']['page'] = 2; - $this->Model->hasAndBelongsToMany['TestModel7']['limit'] = 5; - - $binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7'); - $queryData = array(); - $resultSet = null; - $null = null; - - $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); - - $result = $this->testDb->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet); - $this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result); - $this->assertPattern('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/', $result); - $this->assertPattern('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result); - $this->assertPattern('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 5,\s*5\s*$/', $result); - - $result = $this->testDb->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); - $this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result); - $this->assertPattern('/\s+FROM\s+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); - - $this->Model->hasAndBelongsToMany['TestModel7'] = $__backup; - } - -/** - * buildRelatedModels method - * - * @param mixed $model - * @access protected - * @return void - */ - function _buildRelatedModels(&$model) { - foreach ($model->associations() as $type) { - foreach ($model->{$type} as $assoc => $assocData) { - if (is_string($assocData)) { - $className = $assocData; - } elseif (isset($assocData['className'])) { - $className = $assocData['className']; - } - $model->$className = new $className(); - $model->$className->schema(); - } - } - } - -/** - * &_prepareAssociationQuery method - * - * @param mixed $model - * @param mixed $queryData - * @param mixed $binding - * @access public - * @return void - */ - function &_prepareAssociationQuery(&$model, &$queryData, $binding) { - $type = $binding['type']; - $assoc = $binding['model']; - $assocData = $model->{$type}[$assoc]; - $className = $assocData['className']; - - $linkModel = $model->{$className}; - $external = isset($assocData['external']); - $queryData = $this->testDb->__scrubQueryData($queryData); - - $result = array_merge(array('linkModel' => &$linkModel), compact('type', 'assoc', 'assocData', 'external')); - return $result; - } - -/** - * testSelectDistict method - * - * @access public - * @return void - */ - function testSelectDistict() { - $result = $this->testDb->fields($this->Model, 'Vendor', "DISTINCT Vendor.id, Vendor.name"); - $expected = array('DISTINCT `Vendor`.`id`', '`Vendor`.`name`'); - $this->assertEqual($result, $expected); - } /** * test that booleans and null make logical condition strings. @@ -2276,716 +140,6 @@ class DboSourceTest extends CakeTestCase { $result = $this->testDb->conditions(' ', '" " conditions failed %s'); $this->assertEqual($result, ' WHERE 1 = 1'); } -/** - * testStringConditionsParsing method - * - * @access public - * @return void - */ - function testStringConditionsParsing() { - $result = $this->testDb->conditions("ProjectBid.project_id = Project.id"); - $expected = " WHERE `ProjectBid`.`project_id` = `Project`.`id`"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("Candy.name LIKE 'a' AND HardCandy.name LIKE 'c'"); - $expected = " WHERE `Candy`.`name` LIKE 'a' AND `HardCandy`.`name` LIKE 'c'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("HardCandy.name LIKE 'a' AND Candy.name LIKE 'c'"); - $expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("Post.title = '1.1'"); - $expected = " WHERE `Post`.`title` = '1.1'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("User.id != 0 AND User.user LIKE '%arr%'"); - $expected = " WHERE `User`.`id` != 0 AND `User`.`user` LIKE '%arr%'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("SUM(Post.comments_count) > 500"); - $expected = " WHERE SUM(`Post`.`comments_count`) > 500"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)"); - $expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("score BETWEEN 90.1 AND 95.7"); - $expected = " WHERE score BETWEEN 90.1 AND 95.7"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('score' => array(2=>1, 2, 10))); - $expected = " WHERE score IN (1, 2, 10)"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("Aro.rght = Aro.lft + 1.1"); - $expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)"); - $expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2'); - $expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2'; - $this->assertPattern('/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/', $result); - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"'); - $expected = ' WHERE `Sportstaette`.`sportstaettenart_id` = 2 AND `Sportstaette`.`sportstaette` LIKE "%ru%"'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('SUM(Post.comments_count) > 500 AND NOT Post.title IS NULL AND NOT Post.extended_title IS NULL'); - $expected = ' WHERE SUM(`Post`.`comments_count`) > 500 AND NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('NOT Post.title IS NULL AND NOT Post.extended_title IS NULL AND SUM(Post.comments_count) > 500'); - $expected = ' WHERE NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL AND SUM(`Post`.`comments_count`) > 500'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('NOT Post.extended_title IS NULL AND NOT Post.title IS NULL AND Post.title != "" AND SPOON(SUM(Post.comments_count) + 1.1) > 500'); - $expected = ' WHERE NOT `Post`.`extended_title` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title` != "" AND SPOON(SUM(`Post`.`comments_count`) + 1.1) > 500'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('NOT Post.title_extended IS NULL AND NOT Post.title IS NULL AND Post.title_extended != Post.title'); - $expected = ' WHERE NOT `Post`.`title_extended` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title_extended` != `Post`.`title`'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("Comment.id = 'a'"); - $expected = " WHERE `Comment`.`id` = 'a'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("lower(Article.title) LIKE 'a%'"); - $expected = " WHERE lower(`Article`.`title`) LIKE 'a%'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('((MATCH(Video.title) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(Video.description) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(Video.tags) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))'); - $expected = ' WHERE ((MATCH(`Video`.`title`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(`Video`.`description`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(`Video`.`tags`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('DATEDIFF(NOW(),Article.published) < 1 && Article.live=1'); - $expected = " WHERE DATEDIFF(NOW(),`Article`.`published`) < 1 && `Article`.`live`=1"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('file = "index.html"'); - $expected = ' WHERE file = "index.html"'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions("file = 'index.html'"); - $expected = " WHERE file = 'index.html'"; - $this->assertEqual($result, $expected); - - $letter = $letter = 'd.a'; - $conditions = array('Company.name like ' => $letter . '%'); - $result = $this->testDb->conditions($conditions); - $expected = " WHERE `Company`.`name` like 'd.a%'"; - $this->assertEqual($result, $expected); - - $conditions = array('Artist.name' => 'JUDY and MARY'); - $result = $this->testDb->conditions($conditions); - $expected = " WHERE `Artist`.`name` = 'JUDY and MARY'"; - $this->assertEqual($result, $expected); - - $conditions = array('Artist.name' => 'JUDY AND MARY'); - $result = $this->testDb->conditions($conditions); - $expected = " WHERE `Artist`.`name` = 'JUDY AND MARY'"; - $this->assertEqual($result, $expected); - } - -/** - * testQuotesInStringConditions method - * - * @access public - * @return void - */ - function testQuotesInStringConditions() { - $result = $this->testDb->conditions('Member.email = \'mariano@cricava.com\''); - $expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\''; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('Member.email = "mariano@cricava.com"'); - $expected = ' WHERE `Member`.`email` = "mariano@cricava.com"'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions('Member.email = \'mariano@cricava.com\' AND Member.user LIKE \'mariano.iglesias%\''); - $expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\' AND `Member`.`user` LIKE \'mariano.iglesias%\''; - $this->assertEqual($result, $expected); - - - $result = $this->testDb->conditions('Member.email = "mariano@cricava.com" AND Member.user LIKE "mariano.iglesias%"'); - $expected = ' WHERE `Member`.`email` = "mariano@cricava.com" AND `Member`.`user` LIKE "mariano.iglesias%"'; - $this->assertEqual($result, $expected); - } - -/** - * testParenthesisInStringConditions method - * - * @access public - * @return void - */ - function testParenthesisInStringConditions() { - $result = $this->testDb->conditions('Member.name = \'(lu\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \')lu\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'va(lu\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'va)lu\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'va(lu)\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'va(lu)e\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'(mariano)\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'(mariano)iglesias\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'(mariano) iglesias\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'(mariano word) iglesias\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'(mariano.iglesias)\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias)\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias) CakePHP\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result); - - $result = $this->testDb->conditions('Member.name = \'(mariano.iglesias) CakePHP\''); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result); - } - -/** - * testParenthesisInArrayConditions method - * - * @access public - * @return void - */ - function testParenthesisInArrayConditions() { - $result = $this->testDb->conditions(array('Member.name' => '(lu')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => ')lu')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => 'va(lu')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => 'va)lu')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => 'va(lu)')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => 'va(lu)e')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => '(mariano)')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => '(mariano)iglesias')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => '(mariano) iglesias')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => '(mariano word) iglesias')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => '(mariano.iglesias)')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias)')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias) CakePHP')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result); - - $result = $this->testDb->conditions(array('Member.name' => '(mariano.iglesias) CakePHP')); - $this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result); - } - -/** - * testArrayConditionsParsing method - * - * @access public - * @return void - */ - function testArrayConditionsParsing() { - $this->loadFixtures('Post', 'Author'); - $result = $this->testDb->conditions(array('Stereo.type' => 'in dash speakers')); - $this->assertPattern("/^\s+WHERE\s+`Stereo`.`type`\s+=\s+'in dash speakers'/", $result); - - $result = $this->testDb->conditions(array('Candy.name LIKE' => 'a', 'HardCandy.name LIKE' => 'c')); - $this->assertPattern("/^\s+WHERE\s+`Candy`.`name` LIKE\s+'a'\s+AND\s+`HardCandy`.`name`\s+LIKE\s+'c'/", $result); - - $result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'a', 'Candy.name LIKE' => 'c')); - $expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'a%', 'Candy.name LIKE' => '%c%')); - $expected = " WHERE `HardCandy`.`name` LIKE 'a%' AND `Candy`.`name` LIKE '%c%'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'to be or%', 'Candy.name LIKE' => '%not to be%')); - $expected = " WHERE `HardCandy`.`name` LIKE 'to be or%' AND `Candy`.`name` LIKE '%not to be%'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('score BETWEEN ? AND ?' => array(90.1, 95.7))); - $expected = " WHERE `score` BETWEEN 90.100000 AND 95.700000"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Post.title' => 1.1)); - $expected = " WHERE `Post`.`title` = 1.100000"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Post.title' => 1.1), true, true, new Post()); - $expected = " WHERE `Post`.`title` = '1.1'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('SUM(Post.comments_count) >' => '500')); - $expected = " WHERE SUM(`Post`.`comments_count`) > '500'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('MAX(Post.rating) >' => '50')); - $expected = " WHERE MAX(`Post`.`rating`) > '50'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('title LIKE' => '%hello')); - $expected = " WHERE `title` LIKE '%hello'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Post.name' => 'mad(g)ik')); - $expected = " WHERE `Post`.`name` = 'mad(g)ik'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('score' => array(1, 2, 10))); - $expected = " WHERE score IN (1, 2, 10)"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('score' => array())); - $expected = " WHERE `score` IS NULL"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('score !=' => array())); - $expected = " WHERE `score` IS NOT NULL"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('score !=' => '20')); - $expected = " WHERE `score` != '20'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('score >' => '20')); - $expected = " WHERE `score` > '20'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('client_id >' => '20'), true, true, new TestModel()); - $expected = " WHERE `client_id` > 20"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('OR' => array( - array('User.user' => 'mariano'), - array('User.user' => 'nate') - ))); - - $expected = " WHERE ((`User`.`user` = 'mariano') OR (`User`.`user` = 'nate'))"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('or' => array( - 'score BETWEEN ? AND ?' => array('4', '5'), 'rating >' => '20' - ))); - $expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`rating` > '20'))"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('or' => array( - 'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20') - ))); - $expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`score` > '20'))"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('and' => array( - 'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20') - ))); - $expected = " WHERE ((`score` BETWEEN '4' AND '5') AND (`score` > '20'))"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array( - 'published' => 1, 'or' => array('score >' => '2', array('score >' => '20')) - )); - $expected = " WHERE `published` = 1 AND ((`score` > '2') OR (`score` > '20'))"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array(array('Project.removed' => false))); - $expected = " WHERE `Project`.`removed` = 0"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array(array('Project.removed' => true))); - $expected = " WHERE `Project`.`removed` = 1"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array(array('Project.removed' => null))); - $expected = " WHERE `Project`.`removed` IS NULL"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array(array('Project.removed !=' => null))); - $expected = " WHERE `Project`.`removed` IS NOT NULL"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('(Usergroup.permissions) & 4' => 4)); - $expected = " WHERE (`Usergroup`.`permissions`) & 4 = 4"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('((Usergroup.permissions) & 4)' => 4)); - $expected = " WHERE ((`Usergroup`.`permissions`) & 4) = 4"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Post.modified >=' => 'DATE_SUB(NOW(), INTERVAL 7 DAY)')); - $expected = " WHERE `Post`.`modified` >= 'DATE_SUB(NOW(), INTERVAL 7 DAY)'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Post.modified >= DATE_SUB(NOW(), INTERVAL 7 DAY)')); - $expected = " WHERE `Post`.`modified` >= DATE_SUB(NOW(), INTERVAL 7 DAY)"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array( - 'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)), - 'Enrollment.yearcompleted >' => '0') - ); - $this->assertPattern('/^\s*WHERE\s+\(NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(level_of_education_id IN \(912, 999\)\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result); - - $result = $this->testDb->conditions(array('id <>' => '8')); - $this->assertPattern('/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/', $result); - - $result = $this->testDb->conditions(array('TestModel.field =' => 'gribe$@()lu')); - $expected = " WHERE `TestModel`.`field` = 'gribe$@()lu'"; - $this->assertEqual($result, $expected); - - $conditions['NOT'] = array('Listing.expiration BETWEEN ? AND ?' => array("1", "100")); - $conditions[0]['OR'] = array( - "Listing.title LIKE" => "%term%", - "Listing.description LIKE" => "%term%" - ); - $conditions[1]['OR'] = array( - "Listing.title LIKE" => "%term_2%", - "Listing.description LIKE" => "%term_2%" - ); - $result = $this->testDb->conditions($conditions); - $expected = " WHERE NOT (`Listing`.`expiration` BETWEEN '1' AND '100') AND" . - " ((`Listing`.`title` LIKE '%term%') OR (`Listing`.`description` LIKE '%term%')) AND" . - " ((`Listing`.`title` LIKE '%term_2%') OR (`Listing`.`description` LIKE '%term_2%'))"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('MD5(CONCAT(Reg.email,Reg.id))' => 'blah')); - $expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) = 'blah'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array( - 'MD5(CONCAT(Reg.email,Reg.id))' => array('blah', 'blahblah') - )); - $expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) IN ('blah', 'blahblah')"; - $this->assertEqual($result, $expected); - - $conditions = array('id' => array(2, 5, 6, 9, 12, 45, 78, 43, 76)); - $result = $this->testDb->conditions($conditions); - $expected = " WHERE id IN (2, 5, 6, 9, 12, 45, 78, 43, 76)"; - $this->assertEqual($result, $expected); - - $conditions = array('title' => 'user(s)'); - $result = $this->testDb->conditions($conditions); - $expected = " WHERE `title` = 'user(s)'"; - $this->assertEqual($result, $expected); - - $conditions = array('title' => 'user(s) data'); - $result = $this->testDb->conditions($conditions); - $expected = " WHERE `title` = 'user(s) data'"; - $this->assertEqual($result, $expected); - - $conditions = array('title' => 'user(s,arg) data'); - $result = $this->testDb->conditions($conditions); - $expected = " WHERE `title` = 'user(s,arg) data'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array("Book.book_name" => 'Java(TM)')); - $expected = " WHERE `Book`.`book_name` = 'Java(TM)'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array("Book.book_name" => 'Java(TM) ')); - $expected = " WHERE `Book`.`book_name` = 'Java(TM) '"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array("Book.id" => 0)); - $expected = " WHERE `Book`.`id` = 0"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array("Book.id" => NULL)); - $expected = " WHERE `Book`.`id` IS NULL"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Listing.beds >=' => 0)); - $expected = " WHERE `Listing`.`beds` >= 0"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array( - 'ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN ? AND ?' => array(65, 90) - )); - $expected = ' WHERE ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN 65 AND 90'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('or' => array( - '? BETWEEN Model.field1 AND Model.field2' => '2009-03-04' - ))); - $expected = " WHERE '2009-03-04' BETWEEN Model.field1 AND Model.field2"; - $this->assertEqual($result, $expected); - } - -/** - * testArrayConditionsParsingComplexKeys method - * - * @access public - * @return void - */ - function testArrayConditionsParsingComplexKeys() { - $result = $this->testDb->conditions(array( - 'CAST(Book.created AS DATE)' => '2008-08-02' - )); - $expected = " WHERE CAST(`Book`.`created` AS DATE) = '2008-08-02'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array( - 'CAST(Book.created AS DATE) <=' => '2008-08-02' - )); - $expected = " WHERE CAST(`Book`.`created` AS DATE) <= '2008-08-02'"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array( - '(Stats.clicks * 100) / Stats.views >' => 50 - )); - $expected = " WHERE (`Stats`.`clicks` * 100) / `Stats`.`views` > 50"; - $this->assertEqual($result, $expected); - } - -/** - * testMixedConditionsParsing method - * - * @access public - * @return void - */ - function testMixedConditionsParsing() { - $conditions[] = 'User.first_name = \'Firstname\''; - $conditions[] = array('User.last_name' => 'Lastname'); - $result = $this->testDb->conditions($conditions); - $expected = " WHERE `User`.`first_name` = 'Firstname' AND `User`.`last_name` = 'Lastname'"; - $this->assertEqual($result, $expected); - - $conditions = array( - 'Thread.project_id' => 5, - 'Thread.buyer_id' => 14, - '1=1 GROUP BY Thread.project_id' - ); - $result = $this->testDb->conditions($conditions); - $this->assertPattern('/^\s*WHERE\s+`Thread`.`project_id`\s*=\s*5\s+AND\s+`Thread`.`buyer_id`\s*=\s*14\s+AND\s+1\s*=\s*1\s+GROUP BY `Thread`.`project_id`$/', $result); - } - -/** - * testConditionsOptionalArguments method - * - * @access public - * @return void - */ - function testConditionsOptionalArguments() { - $result = $this->testDb->conditions( array('Member.name' => 'Mariano'), true, false); - $this->assertPattern('/^\s*`Member`.`name`\s*=\s*\'Mariano\'\s*$/', $result); - - $result = $this->testDb->conditions( array(), true, false); - $this->assertPattern('/^\s*1\s*=\s*1\s*$/', $result); - } - -/** - * testConditionsWithModel - * - * @access public - * @return void - */ - function testConditionsWithModel() { - $this->Model = new Article2(); - - $result = $this->testDb->conditions(array('Article2.viewed >=' => 0), true, true, $this->Model); - $expected = " WHERE `Article2`.`viewed` >= 0"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Article2.viewed >=' => '0'), true, true, $this->Model); - $expected = " WHERE `Article2`.`viewed` >= 0"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Article2.viewed >=' => '1'), true, true, $this->Model); - $expected = " WHERE `Article2`.`viewed` >= 1"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array(0, 10)), true, true, $this->Model); - $expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('0', '10')), true, true, $this->Model); - $expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('1', '10')), true, true, $this->Model); - $expected = " WHERE `Article2`.`rate_sum` BETWEEN 1 AND 10"; - $this->assertEqual($result, $expected); - } - -/** - * testFieldParsing method - * - * @access public - * @return void - */ - function testFieldParsing() { - $result = $this->testDb->fields($this->Model, 'Vendor', "Vendor.id, COUNT(Model.vendor_id) AS `Vendor`.`count`"); - $expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, 'Vendor', "`Vendor`.`id`, COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`"); - $expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, 'Post', "CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name, Node.created"); - $expected = array("CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", "`Node`.`created`"); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, 'round( (3.55441 * fooField), 3 ) AS test'); - $this->assertEqual($result, array('round( (3.55441 * fooField), 3 ) AS test')); - - $result = $this->testDb->fields($this->Model, null, 'ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating'); - $this->assertEqual($result, array('ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating')); - - $result = $this->testDb->fields($this->Model, null, 'ROUND(Rating.rate_total / Rating.rate_count,2) AS rating'); - $this->assertEqual($result, array('ROUND(Rating.rate_total / Rating.rate_count,2) AS rating')); - - $result = $this->testDb->fields($this->Model, 'Post', "Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name"); - $expected = array("`Node`.`created`", "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name"); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, 'Post', "2.2,COUNT(*), SUM(Something.else) as sum, Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name,Post.title,Post.1,1.1"); - $expected = array( - '2.2', 'COUNT(*)', 'SUM(`Something`.`else`) as sum', '`Node`.`created`', - "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", '`Post`.`title`', '`Post`.`1`', '1.1' - ); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, "(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`"); - $expected = array("(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`"); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, 'Post'); - $expected = array( - '`Post`.`id`', '`Post`.`client_id`', '`Post`.`name`', '`Post`.`login`', - '`Post`.`passwd`', '`Post`.`addr_1`', '`Post`.`addr_2`', '`Post`.`zip_code`', - '`Post`.`city`', '`Post`.`country`', '`Post`.`phone`', '`Post`.`fax`', - '`Post`.`url`', '`Post`.`email`', '`Post`.`comments`', '`Post`.`last_login`', - '`Post`.`created`', '`Post`.`updated`' - ); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, 'Other'); - $expected = array( - '`Other`.`id`', '`Other`.`client_id`', '`Other`.`name`', '`Other`.`login`', - '`Other`.`passwd`', '`Other`.`addr_1`', '`Other`.`addr_2`', '`Other`.`zip_code`', - '`Other`.`city`', '`Other`.`country`', '`Other`.`phone`', '`Other`.`fax`', - '`Other`.`url`', '`Other`.`email`', '`Other`.`comments`', '`Other`.`last_login`', - '`Other`.`created`', '`Other`.`updated`' - ); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, array(), false); - $expected = array('id', 'client_id', 'name', 'login', 'passwd', 'addr_1', 'addr_2', 'zip_code', 'city', 'country', 'phone', 'fax', 'url', 'email', 'comments', 'last_login', 'created', 'updated'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, 'COUNT(*)'); - $expected = array('COUNT(*)'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, 'SUM(Thread.unread_buyer) AS ' . $this->testDb->name('sum_unread_buyer')); - $expected = array('SUM(`Thread`.`unread_buyer`) AS `sum_unread_buyer`'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, 'name, count(*)'); - $expected = array('`TestModel`.`name`', 'count(*)'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, 'count(*), name'); - $expected = array('count(*)', '`TestModel`.`name`'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields( - $this->Model, null, 'field1, field2, field3, count(*), name' - ); - $expected = array( - '`TestModel`.`field1`', '`TestModel`.`field2`', - '`TestModel`.`field3`', 'count(*)', '`TestModel`.`name`' - ); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, array('dayofyear(now())')); - $expected = array('dayofyear(now())'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, array('MAX(Model.field) As Max')); - $expected = array('MAX(`Model`.`field`) As Max'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, array('Model.field AS AnotherName')); - $expected = array('`Model`.`field` AS `AnotherName`'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, array('field AS AnotherName')); - $expected = array('`field` AS `AnotherName`'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, null, array( - 'TestModel.field AS AnotherName' - )); - $expected = array('`TestModel`.`field` AS `AnotherName`'); - $this->assertEqual($result, $expected); - - $result = $this->testDb->fields($this->Model, 'Foo', array( - 'id', 'title', '(user_count + discussion_count + post_count) AS score' - )); - $expected = array( - '`Foo`.`id`', - '`Foo`.`title`', - '(user_count + discussion_count + post_count) AS score' - ); - $this->assertEqual($result, $expected); - } - -/** - * test that fields() will accept objects made from DboSource::expression - * - * @return void - */ - function testFieldsWithExpression() { - $expression = $this->testDb->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col"); - $result = $this->testDb->fields($this->Model, null, array("id", $expression)); - $expected = array( - '`TestModel`.`id`', - "CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col" - ); - $this->assertEqual($result, $expected); - } /** * test that order() will accept objects made from DboSource::expression @@ -3273,96 +427,6 @@ class DboSourceTest extends CakeTestCase { $this->assertEqual($data, $expected); } -/** - * testRenderStatement method - * - * @access public - * @return void - */ - function testRenderStatement() { - $result = $this->testDb->renderStatement('select', array( - 'fields' => 'id', 'table' => 'table', 'conditions' => 'WHERE 1=1', - 'alias' => '', 'joins' => '', 'order' => '', 'limit' => '', 'group' => '' - )); - $this->assertPattern('/^\s*SELECT\s+id\s+FROM\s+table\s+WHERE\s+1=1\s*$/', $result); - - $result = $this->testDb->renderStatement('update', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => '')); - $this->assertPattern('/^\s*UPDATE\s+table\s+SET\s+value=2\s+WHERE\s+1=1\s*$/', $result); - - $result = $this->testDb->renderStatement('update', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => 'alias', 'joins' => '')); - $this->assertPattern('/^\s*UPDATE\s+table\s+AS\s+alias\s+SET\s+value=2\s+WHERE\s+1=1\s*$/', $result); - - $result = $this->testDb->renderStatement('delete', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => '')); - $this->assertPattern('/^\s*DELETE\s+FROM\s+table\s+WHERE\s+1=1\s*$/', $result); - - $result = $this->testDb->renderStatement('delete', array('fields' => 'value=2', 'table' => 'table', 'conditions' => 'WHERE 1=1', 'alias' => 'alias', 'joins' => '')); - $this->assertPattern('/^\s*DELETE\s+alias\s+FROM\s+table\s+AS\s+alias\s+WHERE\s+1=1\s*$/', $result); - } - -/** - * testStatements method - * - * @access public - * @return void - */ - function testStatements() { - $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'Attachment', 'ArticlesTag'); - $Article = new Article(); - - $result = $this->testDb->update($Article, array('field1'), array('value1')); - $this->assertFalse($result); - $result = $this->testDb->getLastQuery(); - $this->assertPattern('/^\s*UPDATE\s+' . $this->testDb->fullTableName('articles') . '\s+SET\s+`field1`\s*=\s*\'value1\'\s+WHERE\s+1 = 1\s*$/', $result); - - $result = $this->testDb->update($Article, array('field1'), array('2'), '2=2'); - $this->assertFalse($result); - $result = $this->testDb->getLastQuery(); - $this->assertPattern('/^\s*UPDATE\s+' . $this->testDb->fullTableName('articles') . ' AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+SET\s+`Article`\.`field1`\s*=\s*2\s+WHERE\s+2\s*=\s*2\s*$/', $result); - - $result = $this->testDb->delete($Article); - $this->assertTrue($result); - $result = $this->testDb->getLastQuery(); - $this->assertPattern('/^\s*DELETE\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+WHERE\s+1 = 1\s*$/', $result); - - $result = $this->testDb->delete($Article, true); - $this->assertTrue($result); - $result = $this->testDb->getLastQuery(); - $this->assertPattern('/^\s*DELETE\s+`Article`\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+WHERE\s+1\s*=\s*1\s*$/', $result); - - $result = $this->testDb->delete($Article, '2=2'); - $this->assertTrue($result); - $result = $this->testDb->getLastQuery(); - $this->assertPattern('/^\s*DELETE\s+`Article`\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+WHERE\s+2\s*=\s*2\s*$/', $result); - - $result = $this->testDb->hasAny($Article, '1=2'); - $this->assertFalse($result); - - $result = $this->testDb->insertMulti('articles', array('field'), array('(1)', '(2)')); - $this->assertNull($result); - $result = $this->testDb->getLastQuery(); - $this->assertPattern('/^\s*INSERT INTO\s+' . $this->testDb->fullTableName('articles') . '\s+\(`field`\)\s+VALUES\s+\(1\),\s*\(2\)\s*$/', $result); - } - -/** - * testSchema method - * - * @access public - * @return void - */ - function testSchema() { - $Schema = new CakeSchema(); - $Schema->tables = array('table' => array(), 'anotherTable' => array()); - - $this->expectError(); - $result = $this->testDb->dropSchema(null); - $this->assertTrue($result === null); - - $result = $this->testDb->dropSchema($Schema, 'non_existing'); - $this->assertTrue(empty($result)); - - $result = $this->testDb->dropSchema($Schema, 'table'); - $this->assertPattern('/^\s*DROP TABLE IF EXISTS\s+' . $this->testDb->fullTableName('table') . ';\s*$/s', $result); - } /** * testMagicMethodQuerying method @@ -3436,496 +500,6 @@ class DboSourceTest extends CakeTestCase { $this->assertFalse($result); } -/** - * testOrderParsing method - * - * @access public - * @return void - */ - function testOrderParsing() { - $result = $this->testDb->order("ADDTIME(Event.time_begin, '-06:00:00') ASC"); - $expected = " ORDER BY ADDTIME(`Event`.`time_begin`, '-06:00:00') ASC"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->order("title, id"); - $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result); - - $result = $this->testDb->order("title desc, id desc"); - $this->assertPattern('/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/', $result); - - $result = $this->testDb->order(array("title desc, id desc")); - $this->assertPattern('/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/', $result); - - $result = $this->testDb->order(array("title", "id")); - $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result); - - $result = $this->testDb->order(array(array('title'), array('id'))); - $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result); - - $result = $this->testDb->order(array("Post.title" => 'asc', "Post.id" => 'desc')); - $this->assertPattern('/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/', $result); - - $result = $this->testDb->order(array(array("Post.title" => 'asc', "Post.id" => 'desc'))); - $this->assertPattern('/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/', $result); - - $result = $this->testDb->order(array("title")); - $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result); - - $result = $this->testDb->order(array(array("title"))); - $this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result); - - $result = $this->testDb->order("Dealer.id = 7 desc, Dealer.id = 3 desc, Dealer.title asc"); - $expected = " ORDER BY `Dealer`.`id` = 7 desc, `Dealer`.`id` = 3 desc, `Dealer`.`title` asc"; - $this->assertEqual($result, $expected); - - $result = $this->testDb->order(array("Page.name" => "='test' DESC")); - $this->assertPattern("/^\s*ORDER BY\s+`Page`\.`name`\s*='test'\s+DESC\s*$/", $result); - - $result = $this->testDb->order("Page.name = 'view' DESC"); - $this->assertPattern("/^\s*ORDER BY\s+`Page`\.`name`\s*=\s*'view'\s+DESC\s*$/", $result); - - $result = $this->testDb->order("(Post.views)"); - $this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\)\s+ASC\s*$/", $result); - - $result = $this->testDb->order("(Post.views)*Post.views"); - $this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\)\*`Post`\.`views`\s+ASC\s*$/", $result); - - $result = $this->testDb->order("(Post.views) * Post.views"); - $this->assertPattern("/^\s*ORDER BY\s+\(`Post`\.`views`\) \* `Post`\.`views`\s+ASC\s*$/", $result); - - $result = $this->testDb->order("(Model.field1 + Model.field2) * Model.field3"); - $this->assertPattern("/^\s*ORDER BY\s+\(`Model`\.`field1` \+ `Model`\.`field2`\) \* `Model`\.`field3`\s+ASC\s*$/", $result); - - $result = $this->testDb->order("Model.name+0 ASC"); - $this->assertPattern("/^\s*ORDER BY\s+`Model`\.`name`\+0\s+ASC\s*$/", $result); - - $result = $this->testDb->order("Anuncio.destaque & 2 DESC"); - $expected = ' ORDER BY `Anuncio`.`destaque` & 2 DESC'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->order("3963.191 * id"); - $expected = ' ORDER BY 3963.191 * id ASC'; - $this->assertEqual($result, $expected); - - $result = $this->testDb->order(array('Property.sale_price IS NULL')); - $expected = ' ORDER BY `Property`.`sale_price` IS NULL ASC'; - $this->assertEqual($result, $expected); - } - -/** - * testComplexSortExpression method - * - * @return void - */ - public function testComplexSortExpression() { - $result = $this->testDb->order(array('(Model.field > 100) DESC', 'Model.field ASC')); - $this->assertPattern("/^\s*ORDER BY\s+\(`Model`\.`field`\s+>\s+100\)\s+DESC,\s+`Model`\.`field`\s+ASC\s*$/", $result); - } - -/** - * testCalculations method - * - * @access public - * @return void - */ - function testCalculations() { - $result = $this->testDb->calculate($this->Model, 'count'); - $this->assertEqual($result, 'COUNT(*) AS `count`'); - - $result = $this->testDb->calculate($this->Model, 'count', array('id')); - $this->assertEqual($result, 'COUNT(`id`) AS `count`'); - - $result = $this->testDb->calculate( - $this->Model, - 'count', - array($this->testDb->expression('DISTINCT id')) - ); - $this->assertEqual($result, 'COUNT(DISTINCT id) AS `count`'); - - $result = $this->testDb->calculate($this->Model, 'count', array('id', 'id_count')); - $this->assertEqual($result, 'COUNT(`id`) AS `id_count`'); - - $result = $this->testDb->calculate($this->Model, 'count', array('Model.id', 'id_count')); - $this->assertEqual($result, 'COUNT(`Model`.`id`) AS `id_count`'); - - $result = $this->testDb->calculate($this->Model, 'max', array('id')); - $this->assertEqual($result, 'MAX(`id`) AS `id`'); - - $result = $this->testDb->calculate($this->Model, 'max', array('Model.id', 'id')); - $this->assertEqual($result, 'MAX(`Model`.`id`) AS `id`'); - - $result = $this->testDb->calculate($this->Model, 'max', array('`Model`.`id`', 'id')); - $this->assertEqual($result, 'MAX(`Model`.`id`) AS `id`'); - - $result = $this->testDb->calculate($this->Model, 'min', array('`Model`.`id`', 'id')); - $this->assertEqual($result, 'MIN(`Model`.`id`) AS `id`'); - - $result = $this->testDb->calculate($this->Model, 'min', 'left'); - $this->assertEqual($result, 'MIN(`left`) AS `left`'); - } - -/** - * testLength method - * - * @access public - * @return void - */ - function testLength() { - $result = $this->testDb->length('varchar(255)'); - $expected = 255; - $this->assertIdentical($result, $expected); - - $result = $this->testDb->length('int(11)'); - $expected = 11; - $this->assertIdentical($result, $expected); - - $result = $this->testDb->length('float(5,3)'); - $expected = '5,3'; - $this->assertIdentical($result, $expected); - - $result = $this->testDb->length('decimal(5,2)'); - $expected = '5,2'; - $this->assertIdentical($result, $expected); - - $result = $this->testDb->length("enum('test','me','now')"); - $expected = 4; - $this->assertIdentical($result, $expected); - - $result = $this->testDb->length("set('a','b','cd')"); - $expected = 2; - $this->assertIdentical($result, $expected); - - $this->expectError(); - $result = $this->testDb->length(false); - $this->assertTrue($result === null); - - $result = $this->testDb->length('datetime'); - $expected = null; - $this->assertIdentical($result, $expected); - - $result = $this->testDb->length('text'); - $expected = null; - $this->assertIdentical($result, $expected); - } - -/** - * testBuildIndex method - * - * @access public - * @return void - */ - function testBuildIndex() { - $data = array( - 'PRIMARY' => array('column' => 'id') - ); - $result = $this->testDb->buildIndex($data); - $expected = array('PRIMARY KEY (`id`)'); - $this->assertIdentical($result, $expected); - - $data = array( - 'MyIndex' => array('column' => 'id', 'unique' => true) - ); - $result = $this->testDb->buildIndex($data); - $expected = array('UNIQUE KEY `MyIndex` (`id`)'); - $this->assertEqual($result, $expected); - - $data = array( - 'MyIndex' => array('column' => array('id', 'name'), 'unique' => true) - ); - $result = $this->testDb->buildIndex($data); - $expected = array('UNIQUE KEY `MyIndex` (`id`, `name`)'); - $this->assertEqual($result, $expected); - } - -/** - * testBuildColumn method - * - * @access public - * @return void - */ - function testBuildColumn() { - $this->expectError(); - $data = array( - 'name' => 'testName', - 'type' => 'varchar(255)', - 'default', - 'null' => true, - 'key' - ); - $this->testDb->buildColumn($data); - - $data = array( - 'name' => 'testName', - 'type' => 'string', - 'length' => 255, - 'default', - 'null' => true, - 'key' - ); - $result = $this->testDb->buildColumn($data); - $expected = '`testName` varchar(255) DEFAULT NULL'; - $this->assertEqual($result, $expected); - - $data = array( - 'name' => 'int_field', - 'type' => 'integer', - 'default' => '', - 'null' => false, - ); - $restore = $this->testDb->columns; - - $this->testDb->columns = array('integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'), ); - $result = $this->testDb->buildColumn($data); - $expected = '`int_field` int(11) NOT NULL'; - $this->assertEqual($result, $expected); - - $this->testDb->fieldParameters['param'] = array( - 'value' => 'COLLATE', - 'quote' => false, - 'join' => ' ', - 'column' => 'Collate', - 'position' => 'beforeDefault', - 'options' => array('GOOD', 'OK') - ); - $data = array( - 'name' => 'int_field', - 'type' => 'integer', - 'default' => '', - 'null' => false, - 'param' => 'BAD' - ); - $result = $this->testDb->buildColumn($data); - $expected = '`int_field` int(11) NOT NULL'; - $this->assertEqual($result, $expected); - - $data = array( - 'name' => 'int_field', - 'type' => 'integer', - 'default' => '', - 'null' => false, - 'param' => 'GOOD' - ); - $result = $this->testDb->buildColumn($data); - $expected = '`int_field` int(11) COLLATE GOOD NOT NULL'; - $this->assertEqual($result, $expected); - - $this->testDb->columns = $restore; - - $data = array( - 'name' => 'created', - 'type' => 'timestamp', - 'default' => 'current_timestamp', - 'null' => false, - ); - $result = $this->db->buildColumn($data); - $expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL'; - $this->assertEqual($result, $expected); - - $data = array( - 'name' => 'created', - 'type' => 'timestamp', - 'default' => 'CURRENT_TIMESTAMP', - 'null' => true, - ); - $result = $this->db->buildColumn($data); - $expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP'; - $this->assertEqual($result, $expected); - - $data = array( - 'name' => 'modified', - 'type' => 'timestamp', - 'null' => true, - ); - $result = $this->db->buildColumn($data); - $expected = '`modified` timestamp NULL'; - $this->assertEqual($result, $expected); - - $data = array( - 'name' => 'modified', - 'type' => 'timestamp', - 'default' => null, - 'null' => true, - ); - $result = $this->db->buildColumn($data); - $expected = '`modified` timestamp NULL'; - $this->assertEqual($result, $expected); - } - -/** - * test hasAny() - * - * @return void - */ - function testHasAny() { - $this->testDb->hasAny($this->Model, array()); - $expected = 'SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE 1 = 1'; - $this->assertEqual(end($this->testDb->simulated), $expected); - - $this->testDb->hasAny($this->Model, array('TestModel.name' => 'harry')); - $expected = "SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE `TestModel`.`name` = 'harry'"; - $this->assertEqual(end($this->testDb->simulated), $expected); - } - -/** - * testIntrospectType method - * - * @access public - * @return void - */ - function testIntrospectType() { - $this->assertEqual($this->testDb->introspectType(0), 'integer'); - $this->assertEqual($this->testDb->introspectType(2), 'integer'); - $this->assertEqual($this->testDb->introspectType('2'), 'string'); - $this->assertEqual($this->testDb->introspectType('2.2'), 'string'); - $this->assertEqual($this->testDb->introspectType(2.2), 'float'); - $this->assertEqual($this->testDb->introspectType('stringme'), 'string'); - $this->assertEqual($this->testDb->introspectType('0stringme'), 'string'); - - $data = array(2.2); - $this->assertEqual($this->testDb->introspectType($data), 'float'); - - $data = array('2.2'); - $this->assertEqual($this->testDb->introspectType($data), 'float'); - - $data = array(2); - $this->assertEqual($this->testDb->introspectType($data), 'integer'); - - $data = array('2'); - $this->assertEqual($this->testDb->introspectType($data), 'integer'); - - $data = array('string'); - $this->assertEqual($this->testDb->introspectType($data), 'string'); - - $data = array(2.2, '2.2'); - $this->assertEqual($this->testDb->introspectType($data), 'float'); - - $data = array(2, '2'); - $this->assertEqual($this->testDb->introspectType($data), 'integer'); - - $data = array('string one', 'string two'); - $this->assertEqual($this->testDb->introspectType($data), 'string'); - - $data = array('2.2', 3); - $this->assertEqual($this->testDb->introspectType($data), 'integer'); - - $data = array('2.2', '0stringme'); - $this->assertEqual($this->testDb->introspectType($data), 'string'); - - $data = array(2.2, 3); - $this->assertEqual($this->testDb->introspectType($data), 'integer'); - - $data = array(2.2, '0stringme'); - $this->assertEqual($this->testDb->introspectType($data), 'string'); - - $data = array(2, 'stringme'); - $this->assertEqual($this->testDb->introspectType($data), 'string'); - - $data = array(2, '2.2', 'stringgme'); - $this->assertEqual($this->testDb->introspectType($data), 'string'); - - $data = array(2, '2.2'); - $this->assertEqual($this->testDb->introspectType($data), 'integer'); - - $data = array(2, 2.2); - $this->assertEqual($this->testDb->introspectType($data), 'integer'); - - - // NULL - $result = $this->testDb->value(null, 'boolean'); - $this->assertEqual($result, 'NULL'); - - // EMPTY STRING - $result = $this->testDb->value('', 'boolean'); - $this->assertEqual($result, 0); - - - // BOOLEAN - $result = $this->testDb->value('true', 'boolean'); - $this->assertEqual($result, 1); - - $result = $this->testDb->value('false', 'boolean'); - $this->assertEqual($result, 1); - - $result = $this->testDb->value(true, 'boolean'); - $this->assertEqual($result, 1); - - $result = $this->testDb->value(false, 'boolean'); - $this->assertEqual($result, 0); - - $result = $this->testDb->value(1, 'boolean'); - $this->assertEqual($result, 1); - - $result = $this->testDb->value(0, 'boolean'); - $this->assertEqual($result, 0); - - $result = $this->testDb->value('abc', 'boolean'); - $this->assertEqual($result, 1); - - $result = $this->testDb->value(1.234, 'boolean'); - $this->assertEqual($result, 1); - - $result = $this->testDb->value('1.234e05', 'boolean'); - $this->assertEqual($result, 1); - - // NUMBERS - $result = $this->testDb->value(123, 'integer'); - $this->assertEqual($result, 123); - - $result = $this->testDb->value('123', 'integer'); - $this->assertEqual($result, '123'); - - $result = $this->testDb->value('0123', 'integer'); - $this->assertEqual($result, "'0123'"); - - $result = $this->testDb->value('0x123ABC', 'integer'); - $this->assertEqual($result, "'0x123ABC'"); - - $result = $this->testDb->value('0x123', 'integer'); - $this->assertEqual($result, "'0x123'"); - - $result = $this->testDb->value(1.234, 'float'); - $this->assertEqual($result, 1.234); - - $result = $this->testDb->value('1.234', 'float'); - $this->assertEqual($result, '1.234'); - - $result = $this->testDb->value(' 1.234 ', 'float'); - $this->assertEqual($result, "' 1.234 '"); - - $result = $this->testDb->value('1.234e05', 'float'); - $this->assertEqual($result, "'1.234e05'"); - - $result = $this->testDb->value('1.234e+5', 'float'); - $this->assertEqual($result, "'1.234e+5'"); - - $result = $this->testDb->value('1,234', 'float'); - $this->assertEqual($result, "'1,234'"); - - $result = $this->testDb->value('FFF', 'integer'); - $this->assertEqual($result, "'FFF'"); - - $result = $this->testDb->value('abc', 'integer'); - $this->assertEqual($result, "'abc'"); - - // STRINGS - $result = $this->testDb->value('123', 'string'); - $this->assertEqual($result, "'123'"); - - $result = $this->testDb->value(123, 'string'); - $this->assertEqual($result, "'123'"); - - $result = $this->testDb->value(1.234, 'string'); - $this->assertEqual($result, "'1.234'"); - - $result = $this->testDb->value('abc', 'string'); - $this->assertEqual($result, "'abc'"); - - $result = $this->testDb->value(' abc ', 'string'); - $this->assertEqual($result, "' abc '"); - - $result = $this->testDb->value('a bc', 'string'); - $this->assertEqual($result, "'a bc'"); - } /** * testValue method @@ -3954,76 +528,6 @@ class DboSourceTest extends CakeTestCase { $this->assertEqual($this->testDb->config['prefix'], 'foo'); } -/** - * testRealQueries method - * - * @access public - * @return void - */ - function testRealQueries() { - $this->loadFixtures('Apple', 'Article', 'User', 'Comment', 'Tag', 'Sample'); - - $Apple = ClassRegistry::init('Apple'); - $Article = ClassRegistry::init('Article'); - - $result = $this->db->rawQuery('SELECT color, name FROM ' . $this->db->fullTableName('apples')); - $this->assertTrue(!empty($result)); - - $result = $this->db->fetchRow($result); - $expected = array($this->db->fullTableName('apples', false) => array( - 'color' => 'Red 1', - 'name' => 'Red Apple 1' - )); - $this->assertEqual($result, $expected); - - $result = $this->db->fetchAll('SELECT name FROM ' . $this->testDb->fullTableName('apples') . ' ORDER BY id'); - $expected = array( - array($this->db->fullTableName('apples', false) => array('name' => 'Red Apple 1')), - array($this->db->fullTableName('apples', false) => array('name' => 'Bright Red Apple')), - array($this->db->fullTableName('apples', false) => array('name' => 'green blue')), - array($this->db->fullTableName('apples', false) => array('name' => 'Test Name')), - array($this->db->fullTableName('apples', false) => array('name' => 'Blue Green')), - array($this->db->fullTableName('apples', false) => array('name' => 'My new apple')), - array($this->db->fullTableName('apples', false) => array('name' => 'Some odd color')) - ); - $this->assertEqual($result, $expected); - - $result = $this->db->field($this->testDb->fullTableName('apples', false), 'SELECT color, name FROM ' . $this->testDb->fullTableName('apples') . ' ORDER BY id'); - $expected = array( - 'color' => 'Red 1', - 'name' => 'Red Apple 1' - ); - $this->assertEqual($result, $expected); - - $Apple->unbindModel(array(), false); - $result = $this->db->read($Apple, array( - 'fields' => array($Apple->escapeField('name')), - 'conditions' => null, - 'recursive' => -1 - )); - $expected = array( - array('Apple' => array('name' => 'Red Apple 1')), - array('Apple' => array('name' => 'Bright Red Apple')), - array('Apple' => array('name' => 'green blue')), - array('Apple' => array('name' => 'Test Name')), - array('Apple' => array('name' => 'Blue Green')), - array('Apple' => array('name' => 'My new apple')), - array('Apple' => array('name' => 'Some odd color')) - ); - $this->assertEqual($result, $expected); - - $result = $this->db->read($Article, array( - 'fields' => array('id', 'user_id', 'title'), - 'conditions' => null, - 'recursive' => 1 - )); - - $this->assertTrue(Set::matches('/Article[id=1]', $result)); - $this->assertTrue(Set::matches('/Comment[id=1]', $result)); - $this->assertTrue(Set::matches('/Comment[id=2]', $result)); - $this->assertFalse(Set::matches('/Comment[id=10]', $result)); - } - /** * testName method * @@ -4189,26 +693,6 @@ class DboSourceTest extends CakeTestCase { $this->assertEqual($log['log'][2], $expected); } -/** - * test that execute runs queries. - * - * @return void - */ - function testExecute() { - $query = 'SELECT * FROM ' . $this->testDb->fullTableName('articles') . ' WHERE 1 = 1'; - $this->db->took = null; - $this->db->affected = null; - $result = $this->db->execute($query, array('stats' => false)); - $this->assertNotNull($result, 'No query performed! %s'); - $this->assertNull($this->db->took, 'Stats were set %s'); - $this->assertNull($this->db->affected, 'Stats were set %s'); - - $result = $this->db->execute($query); - $this->assertNotNull($result, 'No query performed! %s'); - $this->assertNotNull($this->db->took, 'Stats were not set %s'); - $this->assertNotNull($this->db->affected, 'Stats were not set %s'); - } - /** * test that query() returns boolean values from operations like CREATE TABLE * @@ -4218,11 +702,11 @@ class DboSourceTest extends CakeTestCase { $name = $this->db->fullTableName('test_query'); $query = "CREATE TABLE {$name} (name varchar(10));"; $result = $this->db->query($query); - $this->assertTrue($result, 'Query did not return a boolean. %s'); + $this->assertTrue($result, 'Query did not return a boolean'); $query = "DROP TABLE {$name};"; - $result = $this->db->fetchAll($query); - $this->assertTrue($result, 'Query did not return a boolean. %s'); + $result = $this->db->query($query); + $this->assertTrue($result, 'Query did not return a boolean'); } /** @@ -4241,140 +725,13 @@ class DboSourceTest extends CakeTestCase { $this->assertPattern('/Took:/s', $contents); } -/** - * test fields generating usable virtual fields to use in query - * - * @return void - */ - function testVirtualFields() { - $this->loadFixtures('Article'); - - $Article = ClassRegistry::init('Article'); - $Article->virtualFields = array( - 'this_moment' => 'NOW()', - 'two' => '1 + 1', - 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') . - ' WHERE Article.id = ' . $this->db->fullTableName('comments') . '.article_id' - ); - $result = $this->db->fields($Article); - $expected = array( - '`Article`.`id`', - '`Article`.`user_id`', - '`Article`.`title`', - '`Article`.`body`', - '`Article`.`published`', - '`Article`.`created`', - '`Article`.`updated`', - '(NOW()) AS `Article__this_moment`', - '(1 + 1) AS `Article__two`', - '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`' - ); - $this->assertEqual($expected, $result); - - $result = $this->db->fields($Article, null, array('this_moment', 'title')); - $expected = array( - '`Article`.`title`', - '(NOW()) AS `Article__this_moment`', - ); - $this->assertEqual($expected, $result); - - $result = $this->db->fields($Article, null, array('Article.title', 'Article.this_moment')); - $expected = array( - '`Article`.`title`', - '(NOW()) AS `Article__this_moment`', - ); - $this->assertEqual($expected, $result); - - $result = $this->db->fields($Article, null, array('Article.this_moment', 'Article.title')); - $expected = array( - '`Article`.`title`', - '(NOW()) AS `Article__this_moment`', - ); - $this->assertEqual($expected, $result); - - $result = $this->db->fields($Article, null, array('Article.*')); - $expected = array( - '`Article`.*', - '(NOW()) AS `Article__this_moment`', - '(1 + 1) AS `Article__two`', - '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`' - ); - $this->assertEqual($expected, $result); - - $result = $this->db->fields($Article, null, array('*')); - $expected = array( - '*', - '(NOW()) AS `Article__this_moment`', - '(1 + 1) AS `Article__two`', - '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`' - ); - $this->assertEqual($expected, $result); - } - -/** - * test conditions to generate query conditions for virtual fields - * - * @return void - */ - function testVirtualFieldsInConditions() { - $Article =& ClassRegistry::init('Article'); - $Article->virtualFields = array( - 'this_moment' => 'NOW()', - 'two' => '1 + 1', - 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') . - ' WHERE Article.id = ' . $this->db->fullTableName('comments') . '.article_id' - ); - $conditions = array('two' => 2); - $result = $this->db->conditions($conditions, true, false, $Article); - $expected = '(1 + 1) = 2'; - $this->assertEqual($expected, $result); - - $conditions = array('this_moment BETWEEN ? AND ?' => array(1,2)); - $expected = 'NOW() BETWEEN 1 AND 2'; - $result = $this->db->conditions($conditions, true, false, $Article); - $this->assertEqual($expected, $result); - - $conditions = array('comment_count >' => 5); - $expected = '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) > 5'; - $result = $this->db->conditions($conditions, true, false, $Article); - $this->assertEqual($expected, $result); - - $conditions = array('NOT' => array('two' => 2)); - $result = $this->db->conditions($conditions, true, false, $Article); - $expected = 'NOT ((1 + 1) = 2)'; - $this->assertEqual($expected, $result); - } - -/** - * test that virtualFields with complex functions and aliases work. - * - * @return void - */ - function testConditionsWithComplexVirtualFields() { - $Article =& ClassRegistry::init('Article'); - $Article->virtualFields = array( - 'distance' => 'ACOS(SIN(20 * PI() / 180) - * SIN(Article.latitude * PI() / 180) - + COS(20 * PI() / 180) - * COS(Article.latitude * PI() / 180) - * COS((50 - Article.longitude) * PI() / 180) - ) * 180 / PI() * 60 * 1.1515 * 1.609344' - ); - $conditions = array('distance >=' => 20); - $result = $this->db->conditions($conditions, true, true, $Article); - - $this->assertPattern('/\) >= 20/', $result); - $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result); - $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result); - } - /** * test order to generate query order clause for virtual fields * * @return void */ function testVirtualFieldsInOrder() { - $Article =& ClassRegistry::init('Article'); + $Article = ClassRegistry::init('Article'); $Article->virtualFields = array( 'this_moment' => 'NOW()', 'two' => '1 + 1', @@ -4390,147 +747,7 @@ class DboSourceTest extends CakeTestCase { $this->assertEqual($expected, $result); } -/** - * test calculate to generate claculate statements on virtual fields - * - * @return void - */ - function testVirtualFieldsInCalculate() { - $Article =& ClassRegistry::init('Article'); - $Article->virtualFields = array( - 'this_moment' => 'NOW()', - 'two' => '1 + 1', - 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') . - ' WHERE Article.id = ' . $this->db->fullTableName('comments'). '.article_id' - ); - $result = $this->db->calculate($Article, 'count', array('this_moment')); - $expected = 'COUNT(NOW()) AS `count`'; - $this->assertEqual($expected, $result); - - $result = $this->db->calculate($Article, 'max', array('comment_count')); - $expected = 'MAX(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `comment_count`'; - $this->assertEqual($expected, $result); - } - -/** - * test a full example of using virtual fields - * - * @return void - */ - function testVirtualFieldsFetch() { - $this->loadFixtures('Article', 'Comment'); - - $Article = ClassRegistry::init('Article'); - $Article->virtualFields = array( - 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') . - ' WHERE Article.id = ' . $this->db->fullTableName('comments') . '.article_id' - ); - - $conditions = array('comment_count >' => 2); - $query = 'SELECT ' . join(',',$this->db->fields($Article, null, array('id', 'comment_count'))) . - ' FROM ' . $this->db->fullTableName($Article) . ' Article ' . $this->db->conditions($conditions, true, true, $Article); - $result = $this->db->fetchAll($query); - $expected = array(array( - 'Article' => array('id' => 1, 'comment_count' => 4) - )); - $this->assertEqual($expected, $result); - } - -/** - * test reading complex virtualFields with subqueries. - * - * @return void - */ - function testVirtualFieldsComplexRead() { - $this->loadFixtures('DataTest', 'Article', 'Comment'); - - $Article =& ClassRegistry::init('Article'); - $commentTable = $this->db->fullTableName('comments'); - $Article =& ClassRegistry::init('Article'); - $Article->virtualFields = array( - 'comment_count' => 'SELECT COUNT(*) FROM ' . $commentTable . - ' AS Comment WHERE Article.id = Comment.article_id' - ); - $result = $Article->find('all'); - $this->assertTrue(count($result) > 0); - $this->assertTrue($result[0]['Article']['comment_count'] > 0); - - $DataTest =& ClassRegistry::init('DataTest'); - $DataTest->virtualFields = array( - 'complicated' => 'ACOS(SIN(20 * PI() / 180) - * SIN(DataTest.float * PI() / 180) - + COS(20 * PI() / 180) - * COS(DataTest.count * PI() / 180) - * COS((50 - DataTest.float) * PI() / 180) - ) * 180 / PI() * 60 * 1.1515 * 1.609344' - ); - $result = $DataTest->find('all'); - $this->assertTrue(count($result) > 0); - $this->assertTrue($result[0]['DataTest']['complicated'] > 0); - } - -/** - * test that virtualFields with complex functions and aliases work. - * - * @return void - */ - function testFieldsWithComplexVirtualFields() { - $Article = new Article(); - $Article->virtualFields = array( - 'distance' => 'ACOS(SIN(20 * PI() / 180) - * SIN(Article.latitude * PI() / 180) - + COS(20 * PI() / 180) - * COS(Article.latitude * PI() / 180) - * COS((50 - Article.longitude) * PI() / 180) - ) * 180 / PI() * 60 * 1.1515 * 1.609344' - ); - - $fields = array('id', 'distance'); - $result = $this->db->fields($Article, null, $fields); - $qs = $this->db->startQuote; - $qe = $this->db->endQuote; - - $this->assertEqual($result[0], "{$qs}Article{$qe}.{$qs}id{$qe}"); - $this->assertPattern('/Article__distance/', $result[1]); - $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result[1]); - $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result[1]); - } - -/** - * test reading virtual fields containing newlines when recursive > 0 - * - * @return void - */ - function testReadVirtualFieldsWithNewLines() { - $Article = new Article(); - $Article->recursive = 1; - $Article->virtualFields = array( - 'test' => ' - User.id + User.id - ' - ); - $result = $this->db->fields($Article, null, array()); - $result = $this->db->fields($Article, $Article->alias, $result); - $this->assertPattern('/[`\"]User[`\"]\.[`\"]id[`\"] \+ [`\"]User[`\"]\.[`\"]id[`\"]/', $result[7]); - } - -/** - * test group to generate GROUP BY statements on virtual fields - * - * @return void - */ - function testVirtualFieldsInGroup() { - $Article =& ClassRegistry::init('Article'); - $Article->virtualFields = array( - 'this_year' => 'YEAR(Article.created)' - ); - - $result = $this->db->group('this_year', $Article); - - $expected = " GROUP BY (YEAR(`Article`.`created`))"; - $this->assertEqual($expected, $result); - } /** * test the permutations of fullTableName() @@ -4538,7 +755,7 @@ class DboSourceTest extends CakeTestCase { * @return void */ function testFullTablePermutations() { - $Article =& ClassRegistry::init('Article'); + $Article = ClassRegistry::init('Article'); $result = $this->testDb->fullTableName($Article, false); $this->assertEqual($result, 'articles'); @@ -4558,10 +775,11 @@ class DboSourceTest extends CakeTestCase { * @return void */ function testReadOnlyCallingQueryAssociationWhenDefined() { + $this->loadFixtures('Article', 'User', 'ArticlesTag', 'Tag'); ConnectionManager::create('test_no_queryAssociation', array( 'datasource' => 'data' )); - $Article =& ClassRegistry::init('Article'); + $Article = ClassRegistry::init('Article'); $Article->Comment->useDbConfig = 'test_no_queryAssociation'; $result = $Article->find('all'); $this->assertTrue(is_array($result)); @@ -4576,8 +794,48 @@ class DboSourceTest extends CakeTestCase { $this->testDb->cacheMethods = false; $this->assertTrue(empty($this->testDb->methodCache['fields']), 'Cache not empty'); - $Article =& ClassRegistry::init('Article'); + $Article = ClassRegistry::init('Article'); $this->testDb->fields($Article, null, array('title', 'body', 'published')); $this->assertTrue(empty($this->testDb->methodCache['fields']), 'Cache not empty'); } + +/** + * testStatements method + * + * @access public + * @return void + */ + function testStatements() { + $this->skipIf(!$this->testDb instanceof DboMysql); + $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'Attachment', 'ArticlesTag'); + $Article = new Article(); + + $result = $this->testDb->update($Article, array('field1'), array('value1')); + $this->assertFalse($result); + $result = $this->testDb->getLastQuery(); + $this->assertPattern('/^\s*UPDATE\s+' . $this->testDb->fullTableName('articles') . '\s+SET\s+`field1`\s*=\s*\'value1\'\s+WHERE\s+1 = 1\s*$/', $result); + + $result = $this->testDb->update($Article, array('field1'), array('2'), '2=2'); + $this->assertFalse($result); + $result = $this->testDb->getLastQuery(); + $this->assertPattern('/^\s*UPDATE\s+' . $this->testDb->fullTableName('articles') . ' AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+SET\s+`Article`\.`field1`\s*=\s*2\s+WHERE\s+2\s*=\s*2\s*$/', $result); + + $result = $this->testDb->delete($Article); + $this->assertTrue($result); + $result = $this->testDb->getLastQuery(); + $this->assertPattern('/^\s*DELETE\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+WHERE\s+1 = 1\s*$/', $result); + + $result = $this->testDb->delete($Article, true); + $this->assertTrue($result); + $result = $this->testDb->getLastQuery(); + $this->assertPattern('/^\s*DELETE\s+`Article`\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+WHERE\s+1\s*=\s*1\s*$/', $result); + + $result = $this->testDb->delete($Article, '2=2'); + $this->assertTrue($result); + $result = $this->testDb->getLastQuery(); + $this->assertPattern('/^\s*DELETE\s+`Article`\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+WHERE\s+2\s*=\s*2\s*$/', $result); + + $result = $this->testDb->hasAny($Article, '1=2'); + $this->assertFalse($result); + } } diff --git a/cake/tests/cases/libs/model/model_integration.test.php b/cake/tests/cases/libs/model/model_integration.test.php index b3d78af1a..a8b5a6796 100644 --- a/cake/tests/cases/libs/model/model_integration.test.php +++ b/cake/tests/cases/libs/model/model_integration.test.php @@ -184,7 +184,7 @@ class ModelIntegrationTest extends BaseModelTest { */ function testPkInHabtmLinkModel() { //Test Nonconformant Models - $this->loadFixtures('Content', 'ContentAccount', 'Account', 'JoinC', 'JoinAC'); + $this->loadFixtures('Content', 'ContentAccount', 'Account', 'JoinC', 'JoinAC', 'ItemsPortfolio'); $TestModel = new Content(); $this->assertEqual($TestModel->ContentAccount->primaryKey, 'iContentAccountsId'); @@ -1867,7 +1867,7 @@ class ModelIntegrationTest extends BaseModelTest { * @return void */ function testCreation() { - $this->loadFixtures('Article', 'ArticleFeaturedsTags'); + $this->loadFixtures('Article', 'ArticleFeaturedsTags', 'User', 'Featured'); $TestModel = new Test(); $result = $TestModel->create(); $expected = array('Test' => array('notes' => 'write some notes here')); @@ -1882,9 +1882,10 @@ class ModelIntegrationTest extends BaseModelTest { } else { $intLength = 11; } - foreach (array('collate', 'charset') as $type) { - unset($result['user'][$type]); - unset($result['password'][$type]); + foreach (array('collate', 'charset', 'comment') as $type) { + foreach ($result as $i => $r) { + unset($result[$i][$type]); + } } $expected = array( @@ -1946,8 +1947,8 @@ class ModelIntegrationTest extends BaseModelTest { 'Featured' => array( 'article_featured_id' => 1, 'category_id' => 1, - 'published_date' => '2008-6-11 00:00:00', - 'end_date' => '2008-6-20 00:00:00' + 'published_date' => '2008-06-11 00:00:00', + 'end_date' => '2008-06-20 00:00:00' )); $this->assertEqual($FeaturedModel->create($data), $expected); @@ -1969,8 +1970,8 @@ class ModelIntegrationTest extends BaseModelTest { $expected = array( 'Featured' => array( - 'published_date' => '2008-6-11 00:00:00', - 'end_date' => '2008-6-20 00:00:00', + 'published_date' => '2008-06-11 00:00:00', + 'end_date' => '2008-06-20 00:00:00', 'article_featured_id' => 1, 'category_id' => 1 )); diff --git a/cake/tests/cases/libs/model/model_read.test.php b/cake/tests/cases/libs/model/model_read.test.php index 1a3118763..93808578c 100755 --- a/cake/tests/cases/libs/model/model_read.test.php +++ b/cake/tests/cases/libs/model/model_read.test.php @@ -52,15 +52,17 @@ class ModelReadTest extends BaseModelTest { ) ); + $Something->JoinThing->create($joinThingData); $Something->JoinThing->save(); $result = $Something->JoinThing->find('all', array('conditions' => array('something_else_id' => 2))); - $this->assertEqual($result[0]['JoinThing']['doomed'], '1'); - $this->assertEqual($result[1]['JoinThing']['doomed'], '0'); + $this->assertEqual((bool)$result[0]['JoinThing']['doomed'], true); + $this->assertEqual((bool)$result[1]['JoinThing']['doomed'], false); $result = $Something->find('first'); + $this->assertEqual(count($result['SomethingElse']), 2); $doomed = Set::extract('/JoinThing/doomed', $result['SomethingElse']); @@ -79,7 +81,7 @@ class ModelReadTest extends BaseModelTest { */ function testGroupBy() { $db = ConnectionManager::getDataSource('test'); - $isStrictGroupBy = in_array($db->config['driver'], array('postgres', 'oracle')); + $isStrictGroupBy = in_array($db->config['driver'], array('postgres', 'oracle', 'sqlite')); $message = '%s Postgres and Oracle have strict GROUP BY and are incompatible with this test.'; if ($this->skipIf($isStrictGroupBy, $message )) { @@ -279,13 +281,6 @@ class ModelReadTest extends BaseModelTest { $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag'); $Article = new Article(); - $finalQuery = 'SELECT title, published FROM '; - $finalQuery .= $this->db->fullTableName('articles'); - $finalQuery .= ' WHERE ' . $this->db->fullTableName('articles'); - $finalQuery .= '.id = ' . $this->db->value(1); - $finalQuery .= ' AND ' . $this->db->fullTableName('articles'); - $finalQuery .= '.published = ' . $this->db->value('Y'); - $query = 'SELECT title, published FROM '; $query .= $this->db->fullTableName('articles'); $query .= ' WHERE ' . $this->db->fullTableName('articles'); @@ -305,14 +300,9 @@ class ModelReadTest extends BaseModelTest { } $this->assertEqual($result, $expected); - $result = $this->db->getQueryCache($finalQuery); + $result = $this->db->getQueryCache($query, $params); $this->assertFalse(empty($result)); - $finalQuery = 'SELECT id, created FROM '; - $finalQuery .= $this->db->fullTableName('articles'); - $finalQuery .= ' WHERE ' . $this->db->fullTableName('articles'); - $finalQuery .= '.title = ' . $this->db->value('First Article'); - $query = 'SELECT id, created FROM '; $query .= $this->db->fullTableName('articles'); $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.title = ?'; @@ -324,7 +314,7 @@ class ModelReadTest extends BaseModelTest { isset($result[0][$this->db->fullTableName('articles', false)]) || isset($result[0][0]) ); - $result = $this->db->getQueryCache($finalQuery); + $result = $this->db->getQueryCache($query, $params); $this->assertTrue(empty($result)); $query = 'SELECT title FROM '; @@ -345,10 +335,7 @@ class ModelReadTest extends BaseModelTest { $params = array('First? Article', 'Y'); $Article->query($query, $params); - $expected = 'SELECT title FROM '; - $expected .= $this->db->fullTableName('articles'); - $expected .= " WHERE title = 'First? Article' AND published = 'Y'"; - $result = $this->db->getQueryCache($expected); + $result = $this->db->getQueryCache($query, $params); $this->assertFalse($result === false); } @@ -5091,7 +5078,7 @@ class ModelReadTest extends BaseModelTest { 'typ' => 2 ))); - $this->assertEqual($result, $expected); + $this->assertEqual($expected, $result); } /** @@ -5661,7 +5648,7 @@ class ModelReadTest extends BaseModelTest { 'name' => 'computer' )))))); - $this->assertIdentical($result, $expected); + $this->assertEquals($result, $expected); } /** @@ -7478,7 +7465,7 @@ class ModelReadTest extends BaseModelTest { $result = $Post->field('other_field'); $this->assertEqual($result, 4); - if ($this->skipIf($this->db->config['driver'] == 'postgres', 'The rest of virtualFieds test is not compatible with Postgres')) { + if ($this->skipIf($this->db->config['driver'] != 'mysql', 'The rest of virtualFieds test is not compatible with Postgres')) { return; } ClassRegistry::flush(); @@ -7486,20 +7473,20 @@ class ModelReadTest extends BaseModelTest { $Post->create(); $Post->virtualFields = array( - 'year' => 'YEAR(Post.created)', + 'low_title' => 'lower(Post.title)', 'unique_test_field' => 'COUNT(Post.id)' ); $expectation = array( 'Post' => array( - 'year' => 2007, - 'unique_test_field' => 3 + 'low_title' => 'first post', + 'unique_test_field' => 1 ) ); $result = $Post->find('first', array( 'fields' => array_keys($Post->virtualFields), - 'group' => array('year') + 'group' => array('low_title') )); $this->assertEqual($result, $expectation); diff --git a/cake/tests/cases/libs/model/model_write.test.php b/cake/tests/cases/libs/model/model_write.test.php index 744e50db5..4cac5959e 100644 --- a/cake/tests/cases/libs/model/model_write.test.php +++ b/cake/tests/cases/libs/model/model_write.test.php @@ -201,8 +201,8 @@ class ModelWriteTest extends BaseModelTest { $TestModel->create(array()); $TestModel->save(); $result = $TestModel->findById($TestModel->id); - $this->assertIdentical($result['DataTest']['count'], '0'); - $this->assertIdentical($result['DataTest']['float'], '0'); + $this->assertEquals($result['DataTest']['count'], 0); + $this->assertEquals($result['DataTest']['float'], 0); } /** @@ -286,20 +286,20 @@ class ModelWriteTest extends BaseModelTest { )); $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '2'); + $this->assertEquals($result['Syfile']['item_count'], 2); $TestModel2->delete(1); $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '1'); + $this->assertEquals($result['Syfile']['item_count'], 1); $TestModel2->id = 2; $TestModel2->saveField('syfile_id', 1); $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '2'); + $this->assertEquals($result['Syfile']['item_count'], 2); $result = $TestModel->findById(2); - $this->assertIdentical($result['Syfile']['item_count'], '0'); + $this->assertEquals($result['Syfile']['item_count'], 0); } /** @@ -437,7 +437,7 @@ class ModelWriteTest extends BaseModelTest { * @return void */ function testSaveWithCounterCacheScope() { - $this->loadFixtures('Syfile', 'Item'); + $this->loadFixtures('Syfile', 'Item', 'Image', 'ItemsPortfolio', 'Portfolio'); $TestModel = new Syfile(); $TestModel2 = new Item(); $TestModel2->belongsTo['Syfile']['counterCache'] = true; @@ -453,12 +453,13 @@ class ModelWriteTest extends BaseModelTest { )); $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '1'); + + $this->assertEquals($result['Syfile']['item_count'], 1); $TestModel2->id = 1; $TestModel2->saveField('published', true); $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '2'); + $this->assertEquals($result['Syfile']['item_count'], 2); $TestModel2->save(array( 'id' => 1, @@ -467,7 +468,7 @@ class ModelWriteTest extends BaseModelTest { )); $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '1'); + $this->assertEquals($result['Syfile']['item_count'], 1); } /** @@ -1013,6 +1014,7 @@ class ModelWriteTest extends BaseModelTest { * @return void */ function testSaveFromXml() { + $this->markTestSkipped('This feature needs to be fixed or dropped'); $this->loadFixtures('Article'); App::import('Core', 'Xml'); @@ -1579,7 +1581,7 @@ class ModelWriteTest extends BaseModelTest { 'DoomedSomethingElse' => array( 'className' => 'SomethingElse', 'joinTable' => 'join_things', - 'conditions' => 'JoinThing.doomed = true', + 'conditions' => array('JoinThing.doomed' => true), 'unique' => true ), 'NotDoomedSomethingElse' => array( @@ -2434,7 +2436,7 @@ class ModelWriteTest extends BaseModelTest { * @return void */ function testSaveAll() { - $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); + $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article', 'User'); $TestModel = new Post(); $result = $TestModel->find('all'); @@ -2574,7 +2576,7 @@ class ModelWriteTest extends BaseModelTest { * @return void */ function testSaveAllHabtm() { - $this->loadFixtures('Article', 'Tag', 'Comment', 'User'); + $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag'); $data = array( 'Article' => array( 'user_id' => 1, @@ -3839,8 +3841,8 @@ class ModelWriteTest extends BaseModelTest { */ function testProductUpdateAll() { $this->skipIf( - $this->db->config['driver'] == 'postgres', - '%s Currently, there is no way of doing joins in an update statement in postgresql' + $this->db->config['driver'] != 'mysql', + '%s Currently, there is no way of doing joins in an update statement in postgresql or sqlite' ); $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll'); $ProductUpdateAll = new ProductUpdateAll(); @@ -3890,7 +3892,7 @@ class ModelWriteTest extends BaseModelTest { */ function testProductUpdateAllWithoutForeignKey() { $this->skipIf( - $this->db->config['driver'] == 'postgres', + $this->db->config['driver'] != 'mysql', '%s Currently, there is no way of doing joins in an update statement in postgresql' ); $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll'); diff --git a/cake/tests/cases/libs/model/models.php b/cake/tests/cases/libs/model/models.php index b92543e49..8e20eb6be 100644 --- a/cake/tests/cases/libs/model/models.php +++ b/cake/tests/cases/libs/model/models.php @@ -3592,3 +3592,1309 @@ class TransactionTestModel extends CakeTestModel { $this->saveAll($data, array('atomic' => true, 'callbacks' => false)); } } + +/** + * TestModel class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class TestModel extends CakeTestModel { + +/** + * name property + * + * @var string 'TestModel' + * @access public + */ + public $name = 'TestModel'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * schema property + * + * @var array + * @access protected + */ + protected $_schema = array( + 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '11'), + 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), + 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), + 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'), + 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), + 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => '155'), + 'last_login' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''), + 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + ); + +/** + * find method + * + * @param mixed $conditions + * @param mixed $fields + * @param mixed $order + * @param mixed $recursive + * @access public + * @return void + */ + function find($conditions = null, $fields = null, $order = null, $recursive = null) { + return array($conditions, $fields); + } + +/** + * findAll method + * + * @param mixed $conditions + * @param mixed $fields + * @param mixed $order + * @param mixed $recursive + * @access public + * @return void + */ + function findAll($conditions = null, $fields = null, $order = null, $recursive = null) { + return $conditions; + } +} + +/** + * TestModel2 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class TestModel2 extends CakeTestModel { + +/** + * name property + * + * @var string 'TestModel2' + * @access public + */ + public $name = 'TestModel2'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; +} + +/** + * TestModel4 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class TestModel3 extends CakeTestModel { + +/** + * name property + * + * @var string 'TestModel3' + * @access public + */ + public $name = 'TestModel3'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; +} + +/** + * TestModel4 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class TestModel4 extends CakeTestModel { + +/** + * name property + * + * @var string 'TestModel4' + * @access public + */ + public $name = 'TestModel4'; + +/** + * table property + * + * @var string 'test_model4' + * @access public + */ + public $table = 'test_model4'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array( + 'TestModel4Parent' => array( + 'className' => 'TestModel4', + 'foreignKey' => 'parent_id' + ) + ); + +/** + * hasOne property + * + * @var array + * @access public + */ + public $hasOne = array( + 'TestModel5' => array( + 'className' => 'TestModel5', + 'foreignKey' => 'test_model4_id' + ) + ); + +/** + * hasAndBelongsToMany property + * + * @var array + * @access public + */ + public $hasAndBelongsToMany = array('TestModel7' => array( + 'className' => 'TestModel7', + 'joinTable' => 'test_model4_test_model7', + 'foreignKey' => 'test_model4_id', + 'associationForeignKey' => 'test_model7_id', + 'with' => 'TestModel4TestModel7' + )); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + ); + } + return $this->_schema; + } +} + +/** + * TestModel4TestModel7 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class TestModel4TestModel7 extends CakeTestModel { + +/** + * name property + * + * @var string 'TestModel4TestModel7' + * @access public + */ + public $name = 'TestModel4TestModel7'; + +/** + * table property + * + * @var string 'test_model4_test_model7' + * @access public + */ + public $table = 'test_model4_test_model7'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'test_model4_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'test_model7_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8') + ); + } + return $this->_schema; + } +} + +/** + * TestModel5 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class TestModel5 extends CakeTestModel { + +/** + * name property + * + * @var string 'TestModel5' + * @access public + */ + public $name = 'TestModel5'; + +/** + * table property + * + * @var string 'test_model5' + * @access public + */ + public $table = 'test_model5'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array('TestModel4' => array( + 'className' => 'TestModel4', + 'foreignKey' => 'test_model4_id' + )); + +/** + * hasMany property + * + * @var array + * @access public + */ + public $hasMany = array('TestModel6' => array( + 'className' => 'TestModel6', + 'foreignKey' => 'test_model5_id' + )); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'test_model4_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + ); + } + return $this->_schema; + } +} + +/** + * TestModel6 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class TestModel6 extends CakeTestModel { + +/** + * name property + * + * @var string 'TestModel6' + * @access public + */ + public $name = 'TestModel6'; + +/** + * table property + * + * @var string 'test_model6' + * @access public + */ + public $table = 'test_model6'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array('TestModel5' => array( + 'className' => 'TestModel5', + 'foreignKey' => 'test_model5_id' + )); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'test_model5_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + ); + } + return $this->_schema; + } +} + +/** + * TestModel7 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class TestModel7 extends CakeTestModel { + +/** + * name property + * + * @var string 'TestModel7' + * @access public + */ + public $name = 'TestModel7'; + +/** + * table property + * + * @var string 'test_model7' + * @access public + */ + public $table = 'test_model7'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + ); + } + return $this->_schema; + } +} + +/** + * TestModel8 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class TestModel8 extends CakeTestModel { + +/** + * name property + * + * @var string 'TestModel8' + * @access public + */ + public $name = 'TestModel8'; + +/** + * table property + * + * @var string 'test_model8' + * @access public + */ + public $table = 'test_model8'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * hasOne property + * + * @var array + * @access public + */ + public $hasOne = array( + 'TestModel9' => array( + 'className' => 'TestModel9', + 'foreignKey' => 'test_model8_id', + 'conditions' => 'TestModel9.name != \'mariano\'' + ) + ); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'test_model9_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + ); + } + return $this->_schema; + } +} + +/** + * TestModel9 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class TestModel9 extends CakeTestModel { + +/** + * name property + * + * @var string 'TestModel9' + * @access public + */ + public $name = 'TestModel9'; + +/** + * table property + * + * @var string 'test_model9' + * @access public + */ + public $table = 'test_model9'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array('TestModel8' => array( + 'className' => 'TestModel8', + 'foreignKey' => 'test_model8_id', + 'conditions' => 'TestModel8.name != \'larry\'' + )); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'test_model8_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '11'), + 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + ); + } + return $this->_schema; + } +} + +/** + * Level class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class Level extends CakeTestModel { + +/** + * name property + * + * @var string 'Level' + * @access public + */ + public $name = 'Level'; + +/** + * table property + * + * @var string 'level' + * @access public + */ + public $table = 'level'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * hasMany property + * + * @var array + * @access public + */ + public $hasMany = array( + 'Group'=> array( + 'className' => 'Group' + ), + 'User2' => array( + 'className' => 'User2' + ) + ); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), + 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'), + ); + } + return $this->_schema; + } +} + +/** + * Group class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class Group extends CakeTestModel { + +/** + * name property + * + * @var string 'Group' + * @access public + */ + public $name = 'Group'; + +/** + * table property + * + * @var string 'group' + * @access public + */ + public $table = 'group'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array('Level'); + +/** + * hasMany property + * + * @var array + * @access public + */ + public $hasMany = array('Category2', 'User2'); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), + 'level_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), + 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'), + ); + } + return $this->_schema; + } + +} + +/** + * User2 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class User2 extends CakeTestModel { + +/** + * name property + * + * @var string 'User2' + * @access public + */ + public $name = 'User2'; + +/** + * table property + * + * @var string 'user' + * @access public + */ + public $table = 'user'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array( + 'Group' => array( + 'className' => 'Group' + ), + 'Level' => array( + 'className' => 'Level' + ) + ); + +/** + * hasMany property + * + * @var array + * @access public + */ + public $hasMany = array( + 'Article2' => array( + 'className' => 'Article2' + ), + ); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), + 'group_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), + 'level_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), + 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'), + ); + } + return $this->_schema; + } +} + +/** + * Category2 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class Category2 extends CakeTestModel { + +/** + * name property + * + * @var string 'Category2' + * @access public + */ + public $name = 'Category2'; + +/** + * table property + * + * @var string 'category' + * @access public + */ + public $table = 'category'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array( + 'Group' => array( + 'className' => 'Group', + 'foreignKey' => 'group_id' + ), + 'ParentCat' => array( + 'className' => 'Category2', + 'foreignKey' => 'parent_id' + ) + ); + +/** + * hasMany property + * + * @var array + * @access public + */ + public $hasMany = array( + 'ChildCat' => array( + 'className' => 'Category2', + 'foreignKey' => 'parent_id' + ), + 'Article2' => array( + 'className' => 'Article2', + 'order'=>'Article2.published_date DESC', + 'foreignKey' => 'category_id', + 'limit'=>'3') + ); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), + 'group_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), + 'parent_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), + 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'), + 'icon' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'), + 'description' => array('type' => 'text', 'null' => false, 'default' => '', 'length' => null), + + ); + } + return $this->_schema; + } +} + +/** + * Article2 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class Article2 extends CakeTestModel { + +/** + * name property + * + * @var string 'Article2' + * @access public + */ + public $name = 'Article2'; + +/** + * table property + * + * @var string 'article' + * @access public + */ + public $table = 'articles'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array( + 'Category2' => array('className' => 'Category2'), + 'User2' => array('className' => 'User2') + ); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), + 'category_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'user_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'rate_count' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'rate_sum' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'viewed' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'version' => array('type' => 'string', 'null' => true, 'default' => '', 'length' => '45'), + 'title' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '200'), + 'intro' => array('text' => 'string', 'null' => true, 'default' => '', 'length' => null), + 'comments' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '4'), + 'body' => array('text' => 'string', 'null' => true, 'default' => '', 'length' => null), + 'isdraft' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'), + 'allow_comments' => array('type' => 'boolean', 'null' => false, 'default' => '1', 'length' => '1'), + 'moderate_comments' => array('type' => 'boolean', 'null' => false, 'default' => '1', 'length' => '1'), + 'published' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'), + 'multipage' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'), + 'published_date' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null), + 'created' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null), + 'modified' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null) + ); + } + return $this->_schema; + } +} + +/** + * CategoryFeatured2 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class CategoryFeatured2 extends CakeTestModel { + +/** + * name property + * + * @var string 'CategoryFeatured2' + * @access public + */ + public $name = 'CategoryFeatured2'; + +/** + * table property + * + * @var string 'category_featured' + * @access public + */ + public $table = 'category_featured'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), + 'parent_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '10'), + 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'), + 'icon' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'), + 'description' => array('text' => 'string', 'null' => false, 'default' => '', 'length' => null) + ); + } + return $this->_schema; + } +} + +/** + * Featured2 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class Featured2 extends CakeTestModel { + +/** + * name property + * + * @var string 'Featured2' + * @access public + */ + public $name = 'Featured2'; + +/** + * table property + * + * @var string 'featured2' + * @access public + */ + public $table = 'featured2'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array( + 'CategoryFeatured2' => array( + 'className' => 'CategoryFeatured2' + ) + ); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), + 'article_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'category_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20') + ); + } + return $this->_schema; + } +} + +/** + * Comment2 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class Comment2 extends CakeTestModel { + +/** + * name property + * + * @var string 'Comment2' + * @access public + */ + public $name = 'Comment2'; + +/** + * table property + * + * @var string 'comment' + * @access public + */ + public $table = 'comment'; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array('ArticleFeatured2', 'User2'); + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), + 'article_featured_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'user_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'name' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20') + ); + } + return $this->_schema; + } +} + +/** + * ArticleFeatured2 class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class ArticleFeatured2 extends CakeTestModel { + +/** + * name property + * + * @var string 'ArticleFeatured2' + * @access public + */ + public $name = 'ArticleFeatured2'; + +/** + * table property + * + * @var string 'article_featured' + * @access public + */ + public $table = 'article_featured'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * belongsTo property + * + * @var array + * @access public + */ + public $belongsTo = array( + 'CategoryFeatured2' => array('className' => 'CategoryFeatured2'), + 'User2' => array('className' => 'User2') + ); + +/** + * hasOne property + * + * @var array + * @access public + */ + public $hasOne = array( + 'Featured2' => array('className' => 'Featured2') + ); + +/** + * hasMany property + * + * @var array + * @access public + */ + public $hasMany = array( + 'Comment2' => array('className'=>'Comment2', 'dependent' => true) + ); + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + if (!isset($this->_schema)) { + $this->_schema = array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => '10'), + 'category_featured_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'user_id' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => '10'), + 'title' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => '20'), + 'body' => array('text' => 'string', 'null' => true, 'default' => '', 'length' => null), + 'published' => array('type' => 'boolean', 'null' => false, 'default' => '0', 'length' => '1'), + 'published_date' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null), + 'created' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null), + 'modified' => array('type' => 'datetime', 'null' => false, 'default' => '0000-00-00 00:00:00', 'length' => null) + ); + } + return $this->_schema; + } +} + +/** + * MysqlTestModel class + * + * @package cake + * @subpackage cake.tests.cases.libs.model.datasources + */ +class MysqlTestModel extends Model { + +/** + * name property + * + * @var string 'MysqlTestModel' + * @access public + */ + public $name = 'MysqlTestModel'; + +/** + * useTable property + * + * @var bool false + * @access public + */ + public $useTable = false; + +/** + * find method + * + * @param mixed $conditions + * @param mixed $fields + * @param mixed $order + * @param mixed $recursive + * @access public + * @return void + */ + function find($conditions = null, $fields = null, $order = null, $recursive = null) { + return $conditions; + } + +/** + * findAll method + * + * @param mixed $conditions + * @param mixed $fields + * @param mixed $order + * @param mixed $recursive + * @access public + * @return void + */ + function findAll($conditions = null, $fields = null, $order = null, $recursive = null) { + return $conditions; + } + +/** + * schema method + * + * @access public + * @return void + */ + function schema() { + return array( + 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'), + 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), + 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), + 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'), + 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), + 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''), + 'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''), + 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + ); + } +} diff --git a/cake/tests/fixtures/aco_fixture.php b/cake/tests/fixtures/aco_fixture.php index e6c649a15..be296aaf8 100644 --- a/cake/tests/fixtures/aco_fixture.php +++ b/cake/tests/fixtures/aco_fixture.php @@ -57,17 +57,17 @@ class AcoFixture extends CakeTestFixture { * @access public */ public $records = array( - array('parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'ROOT', 'lft' => 1, 'rght' => 24), - array('parent_id' => 1, 'model' => null, 'foreign_key' => null, 'alias' => 'Controller1', 'lft' => 2, 'rght' => 9), - array('parent_id' => 2, 'model' => null, 'foreign_key' => null, 'alias' => 'action1', 'lft' => 3, 'rght' => 6), - array('parent_id' => 3, 'model' => null, 'foreign_key' => null, 'alias' => 'record1', 'lft' => 4, 'rght' => 5), - array('parent_id' => 2, 'model' => null, 'foreign_key' => null, 'alias' => 'action2', 'lft' => 7, 'rght' => 8), - array('parent_id' => 1, 'model' => null, 'foreign_key' => null, 'alias' => 'Controller2','lft' => 10, 'rght' => 17), - array('parent_id' => 6, 'model' => null, 'foreign_key' => null, 'alias' => 'action1', 'lft' => 11, 'rght' => 14), - array('parent_id' => 7, 'model' => null, 'foreign_key' => null, 'alias' => 'record1', 'lft' => 12, 'rght' => 13), - array('parent_id' => 6, 'model' => null, 'foreign_key' => null, 'alias' => 'action2', 'lft' => 15, 'rght' => 16), - array('parent_id' => 1, 'model' => null, 'foreign_key' => null, 'alias' => 'Users', 'lft' => 18, 'rght' => 23), - array('parent_id' => 9, 'model' => null, 'foreign_key' => null, 'alias' => 'Users', 'lft' => 19, 'rght' => 22), - array('parent_id' => 10, 'model' => null, 'foreign_key' => null, 'alias' => 'view', 'lft' => 20, 'rght' => 21), + array('id' => 1, 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'ROOT', 'lft' => 1, 'rght' => 24), + array('id' => 2, 'parent_id' => 1, 'model' => null, 'foreign_key' => null, 'alias' => 'Controller1', 'lft' => 2, 'rght' => 9), + array('id' => 3, 'parent_id' => 2, 'model' => null, 'foreign_key' => null, 'alias' => 'action1', 'lft' => 3, 'rght' => 6), + array('id' => 4, 'parent_id' => 3, 'model' => null, 'foreign_key' => null, 'alias' => 'record1', 'lft' => 4, 'rght' => 5), + array('id' => 5, 'parent_id' => 2, 'model' => null, 'foreign_key' => null, 'alias' => 'action2', 'lft' => 7, 'rght' => 8), + array('id' => 6, 'parent_id' => 1, 'model' => null, 'foreign_key' => null, 'alias' => 'Controller2','lft' => 10, 'rght' => 17), + array('id' => 7, 'parent_id' => 6, 'model' => null, 'foreign_key' => null, 'alias' => 'action1', 'lft' => 11, 'rght' => 14), + array('id' => 8, 'parent_id' => 7, 'model' => null, 'foreign_key' => null, 'alias' => 'record1', 'lft' => 12, 'rght' => 13), + array('id' => 9, 'parent_id' => 6, 'model' => null, 'foreign_key' => null, 'alias' => 'action2', 'lft' => 15, 'rght' => 16), + array('id' => 10, 'parent_id' => 1, 'model' => null, 'foreign_key' => null, 'alias' => 'Users', 'lft' => 18, 'rght' => 23), + array('id' => 11, 'parent_id' => 9, 'model' => null, 'foreign_key' => null, 'alias' => 'Users', 'lft' => 19, 'rght' => 22), + array('id' => 12, 'parent_id' => 10, 'model' => null, 'foreign_key' => null, 'alias' => 'view', 'lft' => 20, 'rght' => 21), ); } diff --git a/cake/tests/fixtures/translate_fixture.php b/cake/tests/fixtures/translate_fixture.php index c056d3e8c..bc7926a4d 100644 --- a/cake/tests/fixtures/translate_fixture.php +++ b/cake/tests/fixtures/translate_fixture.php @@ -64,23 +64,23 @@ class TranslateFixture extends CakeTestFixture { * @access public */ public $records = array( - array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'), - array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'), - array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'), - array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'), - array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1'), - array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1'), - array('id' => 7, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Title #2'), - array('id' => 8, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'content', 'content' => 'Content #2'), - array('id' => 9, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Titel #2'), - array('id' => 10, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'content', 'content' => 'Inhalt #2'), - array('id' => 11, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Titulek #2'), - array('id' => 12, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'content', 'content' => 'Obsah #2'), - array('id' => 13, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Title #3'), - array('id' => 14, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'content', 'content' => 'Content #3'), - array('id' => 15, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Titel #3'), - array('id' => 16, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'content', 'content' => 'Inhalt #3'), - array('id' => 17, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Titulek #3'), - array('id' => 18, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'content', 'content' => 'Obsah #3') + array('locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'), + array('locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'), + array('locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'), + array('locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'), + array('locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1'), + array('locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1'), + array('locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Title #2'), + array('locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'content', 'content' => 'Content #2'), + array('locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Titel #2'), + array('locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'content', 'content' => 'Inhalt #2'), + array('locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Titulek #2'), + array('locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 2, 'field' => 'content', 'content' => 'Obsah #2'), + array('locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Title #3'), + array('locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'content', 'content' => 'Content #3'), + array('locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Titel #3'), + array('locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'content', 'content' => 'Inhalt #3'), + array('locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Titulek #3'), + array('locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 3, 'field' => 'content', 'content' => 'Obsah #3') ); } diff --git a/cake/tests/lib/cake_fixture_manager.php b/cake/tests/lib/cake_fixture_manager.php index 2643335f3..1c092c97b 100644 --- a/cake/tests/lib/cake_fixture_manager.php +++ b/cake/tests/lib/cake_fixture_manager.php @@ -177,8 +177,8 @@ class CakeFixtureManager { $cacheSources = $db->cacheSources; $db->cacheSources = false; - $db->cacheSources = $cacheSources; $sources = $db->listSources(); + $db->cacheSources = $cacheSources; $table = $db->config['prefix'] . $fixture->table; if ($drop && in_array($table, $sources)) { @@ -206,6 +206,7 @@ class CakeFixtureManager { return; } + $test->db->begin(); foreach ($fixtures as $f) { if (!empty($this->_loaded[$f])) { $fixture = $this->_loaded[$f]; @@ -213,6 +214,7 @@ class CakeFixtureManager { $fixture->insert($test->db); } } + $test->db->commit(); } /** diff --git a/cake/tests/lib/cake_test_fixture.php b/cake/tests/lib/cake_test_fixture.php index eeeae5c9d..b7676cf5c 100644 --- a/cake/tests/lib/cake_test_fixture.php +++ b/cake/tests/lib/cake_test_fixture.php @@ -165,11 +165,10 @@ class CakeTestFixture { public function insert(&$db) { if (!isset($this->_insert)) { $values = array(); - if (isset($this->records) && !empty($this->records)) { foreach ($this->records as $record) { $fields = array_keys($record); - $values[] = '(' . implode(', ', array_map(array(&$db, 'value'), array_values($record))) . ')'; + $values[] = array_values($record); } return $db->insertMulti($this->table, $fields, $values); } diff --git a/lib/Cake/Model/Datasource/DataSource.php b/lib/Cake/Model/Datasource/DataSource.php index 85c0b1f59..391e1bf65 100644 --- a/lib/Cake/Model/Datasource/DataSource.php +++ b/lib/Cake/Model/Datasource/DataSource.php @@ -263,7 +263,7 @@ class DataSource extends Object { * @param Model $model * @return array Array of Metadata for the $model */ - public function describe(&$model) { + public function describe($model) { if ($this->cacheSources === false) { return null; } @@ -556,7 +556,7 @@ class DataSource extends Object { * @param string $key Key name to make * @return string Key name for model. */ - public function resolveKey(&$model, $key) { + public function resolveKey($model, $key) { return $model->alias . $key; } diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index fbcf5c3af..884ea2d29 100755 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -88,6 +88,22 @@ class DboSource extends DataSource { */ private $__sqlOps = array('like', 'ilike', 'or', 'not', 'in', 'between', 'regexp', 'similar to'); +/** + * Indicates that a transaction have been started + * + * @var boolean + * @access protected + */ + protected $_transactionStarted = false; + +/** + * Indicates the level of nested transactions + * + * @var integer + * @access protected + */ + protected $_transactionNesting = 0; + /** * Index of basic SQL commands * @@ -160,18 +176,37 @@ class DboSource extends DataSource { } /** - * Prepares a value, or an array of values for database queries by quoting and escaping them. + * Disconnects from database. * - * @param mixed $data A value or an array of values to prepare. - * @param string $column The column into which this data will be inserted - * @param boolean $read Value to be used in READ or WRITE context - * @return mixed Prepared value or array of values. + * @return boolean True if the database could be disconnected, else false */ - public function value($data, $column = null, $read = true) { + function disconnect() { + if (is_a($this->_result, 'PDOStatement')) { + $this->_result->closeCursor(); + } + unset($this->_connection); + $this->connected = false; + return !$this->connected; + } + + public function getConnection() { + return $this->_connection; + } + + +/** + * Returns a quoted and escaped string of $data for use in an SQL statement. + * + * @param string $data String to be prepared for use in an SQL statement + * @param string $column The column into which this data will be inserted + * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided + * @return string Quoted and escaped data + */ + function value($data, $column = null, $safe = false) { if (is_array($data) && !empty($data)) { return array_map( array(&$this, 'value'), - $data, array_fill(0, count($data), $column), array_fill(0, count($data), $read) + $data, array_fill(0, count($data), $column), array_fill(0, count($data), $safe) ); } elseif (is_object($data) && isset($data->type)) { if ($data->type == 'identifier') { @@ -181,11 +216,45 @@ class DboSource extends DataSource { } } elseif (in_array($data, array('{$__cakeID__$}', '{$__cakeForeignKey__$}'), true)) { return $data; - } else { - return null; + } + + if ($data === null || (is_array($data) && empty($data))) { + return 'NULL'; + } + + if (empty($column)) { + $column = $this->introspectType($data); + } + + switch ($column) { + case 'binary': + return $this->_connection->quote($data, PDO::PARAM_LOB); + break; + case 'boolean': + return $this->_connection->quote($this->boolean($data), PDO::PARAM_BOOL); + break; + case 'string': + case 'text': + return $this->_connection->quote($data, PDO::PARAM_STR); + default: + if ($data === '') { + return 'NULL'; + } + if (is_float($data)) { + return sprintf('%F', $data); + } + if ((is_int($data) || $data === '0') || ( + is_numeric($data) && strpos($data, ',') === false && + $data[0] != '0' && strpos($data, 'e') === false) + ) { + return $data; + } + return $this->_connection->quote($data); + break; } } + /** * Returns an object to represent a database identifier in a query * @@ -218,9 +287,9 @@ class DboSource extends DataSource { * @param string $sql SQL statement * @return boolean */ - public function rawQuery($sql) { + public function rawQuery($sql, $params = array()) { $this->took = $this->error = $this->numRows = false; - return $this->execute($sql); + return $this->execute($sql, $params); } /** @@ -230,28 +299,23 @@ class DboSource extends DataSource { * * ### Options * - * - stats - Collect meta data stats for this query. Stats include time take, rows affected, - * any errors, and number of rows returned. Defaults to `true`. * - log - Whether or not the query should be logged to the memory log. * * @param string $sql * @param array $options + * @param array $params values to be bided to the query * @return mixed Resource or object representing the result set, or false on failure */ - public function execute($sql, $options = array()) { - $defaults = array('stats' => true, 'log' => $this->fullDebug); - $options = array_merge($defaults, $options); + public function execute($sql, $options = array(), $params = array()) { + $options = $options + array('log' => $this->fullDebug); + $this->error = null; $t = microtime(true); - $this->_result = $this->_execute($sql); - if ($options['stats']) { - $this->took = round((microtime(true) - $t) * 1000, 0); - $this->affected = $this->lastAffected(); - $this->error = $this->lastError(); - $this->numRows = $this->lastNumRows(); - } + $this->_result = $this->_execute($sql, $params); if ($options['log']) { + $this->took = round((microtime(true) - $t) * 1000, 0); + $this->numRows = $this->affected = $this->lastAffected(); $this->logQuery($sql); } @@ -262,6 +326,83 @@ class DboSource extends DataSource { return $this->_result; } +/** + * Executes given SQL statement. + * + * @param string $sql SQL statement + * @param array $params list of params to be bound to query + * @return PDOStatement if query executes with no problem, true as the result of a succesfull + * query returning no rows, suchs as a CREATE statement, false otherwise + */ + protected function _execute($sql, $params = array()) { + $sql = trim($sql); + if (preg_match('/^CREATE|^ALTER|^DROP/i', $sql)) { + $statements = array_filter(explode(';', $sql)); + if (count($statements) > 1) { + $result = array_map(array($this, '_execute'), $statements); + return array_search(false, $result) === false; + } + } + + try { + $query = $this->_connection->prepare($sql); + $query->setFetchMode(PDO::FETCH_LAZY); + if (!$query->execute($params)) { + $this->_results = $query; + $this->error = $this->lastError($query); + $query->closeCursor(); + return false; + } + if (!$query->columnCount()) { + $query->closeCursor(); + return true; + } + return $query; + } catch (PDOException $e) { + $this->_results = null; + $this->error = $e->getMessage(); + return false; + } + + } + +/** + * Returns a formatted error message from previous database operation. + * + * @param PDOStatement $query the query to extract the error from if any + * @return string Error message with error number + */ + function lastError(PDOStatement $query = null) { + $error = $query->errorInfo(); + if (empty($error[2])) { + return null; + } + return $error[1] . ': ' . $error[2]; + } + +/** + * Returns number of affected rows in previous database operation. If no previous operation exists, + * this returns false. + * + * @return integer Number of affected rows + */ + function lastAffected($source = null) { + if ($this->hasResult()) { + return $this->_result->rowCount(); + } + return null; + } + +/** + * Returns number of rows in previous resultset. If no previous resultset exists, + * this returns false. + * + * @return integer Number of rows in resultset + */ + function lastNumRows($source = null) { + return $this->lastAffected($source); + } + /** * DataSource Query abstraction * @@ -352,8 +493,7 @@ class DboSource extends DataSource { } else { $cache = true; } - $args[1] = array_map(array(&$this, 'value'), $args[1]); - return $this->fetchAll(String::insert($args[0], $args[1]), $cache); + return $this->fetchAll($args[0], $args[1], array('cache' => $cache)); } } } @@ -386,16 +526,31 @@ class DboSource extends DataSource { * Returns an array of all result rows for a given SQL query. * Returns false if no rows matched. * + * + * ### Options + * + * - cache - Returns the cached version of the query, if exists and stores the result in cache + * * @param string $sql SQL statement - * @param boolean $cache Enables returning/storing cached query results + * @param array $params parameters to be bound as values for the SQL statement + * @param array $options additional options for the query. * @return array Array of resultset rows, or false if no rows matched */ - public function fetchAll($sql, $cache = true, $modelName = null) { - if ($cache && ($cached = $this->getQueryCache($sql)) !== false) { + public function fetchAll($sql, $params = array(), $options = array()) { + if (is_string($options)) { + $options = array('modelName' => $options); + } + if (is_bool($params)) { + $options['cache'] = $params; + $params = array(); + } + $defaults = array('cache' => true); + $options = $options + $defaults; + $cache = $options['cache']; + if ($cache && ($cached = $this->getQueryCache($sql, $params)) !== false) { return $cached; } - - if ($this->execute($sql)) { + if ($result = $this->execute($sql, array(), $params)) { $out = array(); $first = $this->fetchRow(); @@ -407,9 +562,10 @@ class DboSource extends DataSource { $out[] = $item; } - if ($cache) { - $this->_writeQueryCache($sql, $out); + if (!is_bool($result) && $cache) { + $this->_writeQueryCache($sql, $out, $params); } + if (empty($out) && is_bool($this->_result)) { return $this->_result; } @@ -576,7 +732,7 @@ class DboSource extends DataSource { * @return boolean True if the result is valid else false */ public function hasResult() { - return is_resource($this->_result); + return is_a($this->_result, 'PDOStatement'); } /** @@ -784,7 +940,7 @@ class DboSource extends DataSource { foreach ($_associations as $type) { foreach ($model->{$type} as $assoc => $assocData) { - $linkModel =& $model->{$assoc}; + $linkModel = $model->{$assoc}; $external = isset($assocData['external']); if ($model->useDbConfig == $linkModel->useDbConfig) { @@ -795,9 +951,9 @@ class DboSource extends DataSource { } } - $query = $this->generateAssociationQuery($model, $null, null, null, null, $queryData, false, $null); + $query = trim($this->generateAssociationQuery($model, $null, null, null, null, $queryData, false, $null)); - $resultSet = $this->fetchAll($query, $model->cacheQueries, $model->alias); + $resultSet = $this->fetchAll($query, $model->cacheQueries); if ($resultSet === false) { $model->onError(); @@ -809,16 +965,16 @@ class DboSource extends DataSource { if ($model->recursive > -1) { foreach ($_associations as $type) { foreach ($model->{$type} as $assoc => $assocData) { - $linkModel =& $model->{$assoc}; + $linkModel = $model->{$assoc}; if (empty($linkedModels[$type . '/' . $assoc])) { if ($model->useDbConfig == $linkModel->useDbConfig) { - $db =& $this; + $db = $this; } else { - $db =& ConnectionManager::getDataSource($linkModel->useDbConfig); + $db = ConnectionManager::getDataSource($linkModel->useDbConfig); } } elseif ($model->recursive > 1 && ($type == 'belongsTo' || $type == 'hasOne')) { - $db =& $this; + $db = $this; } if (isset($db) && method_exists($db, 'queryAssociation')) { @@ -924,14 +1080,14 @@ class DboSource extends DataSource { if ($recursive > 0) { foreach ($linkModel->associations() as $type1) { foreach ($linkModel->{$type1} as $assoc1 => $assocData1) { - $deepModel =& $linkModel->{$assoc1}; + $deepModel = $linkModel->{$assoc1}; $tmpStack = $stack; $tmpStack[] = $assoc1; if ($linkModel->useDbConfig === $deepModel->useDbConfig) { - $db =& $this; + $db = $this; } else { - $db =& ConnectionManager::getDataSource($deepModel->useDbConfig); + $db = ConnectionManager::getDataSource($deepModel->useDbConfig); } $db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack); } @@ -966,7 +1122,7 @@ class DboSource extends DataSource { $q = $this->insertQueryData($query, null, $association, $assocData, $model, $linkModel, $stack); if ($q != false) { - $fetch = $this->fetchAll($q, $model->cacheQueries, $model->alias); + $fetch = $this->fetchAll($q, $model->cacheQueries); } else { $fetch = null; } @@ -978,7 +1134,7 @@ class DboSource extends DataSource { if ($type !== 'hasAndBelongsToMany') { $q = $this->insertQueryData($query, $resultSet[$i], $association, $assocData, $model, $linkModel, $stack); if ($q != false) { - $fetch = $this->fetchAll($q, $model->cacheQueries, $model->alias); + $fetch = $this->fetchAll($q, $model->cacheQueries); } else { $fetch = null; } @@ -993,15 +1149,15 @@ class DboSource extends DataSource { if ($recursive > 0) { foreach ($linkModel->associations() as $type1) { foreach ($linkModel->{$type1} as $assoc1 => $assocData1) { - $deepModel =& $linkModel->{$assoc1}; + $deepModel = $linkModel->{$assoc1}; if (($type1 === 'belongsTo') || ($deepModel->alias === $model->alias && $type === 'belongsTo') || ($deepModel->alias != $model->alias)) { $tmpStack = $stack; $tmpStack[] = $assoc1; if ($linkModel->useDbConfig == $deepModel->useDbConfig) { - $db =& $this; + $db = $this; } else { - $db =& ConnectionManager::getDataSource($deepModel->useDbConfig); + $db = ConnectionManager::getDataSource($deepModel->useDbConfig); } $db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack); } @@ -1053,7 +1209,7 @@ class DboSource extends DataSource { if (count($ids) > 1) { $query = str_replace('= (', 'IN (', $query); } - return $this->fetchAll($query, $model->cacheQueries, $model->alias); + return $this->fetchAll($query, $model->cacheQueries); } /** @@ -1764,14 +1920,14 @@ class DboSource extends DataSource { /** * Begin a transaction * - * @param model $model * @return boolean True on success, false on fail * (i.e. if the database/model does not support transactions, * or a transaction has not started). */ - public function begin(&$model) { - if (parent::begin($model) && $this->execute($this->_commands['begin'])) { + public function begin() { + if ($this->_transactionStarted || $this->_connection->beginTransaction()) { $this->_transactionStarted = true; + $this->_transactionNesting++; return true; } return false; @@ -1780,14 +1936,18 @@ class DboSource extends DataSource { /** * Commit a transaction * - * @param model $model * @return boolean True on success, false on fail * (i.e. if the database/model does not support transactions, * or a transaction has not started). */ - public function commit(&$model) { - if (parent::commit($model) && $this->execute($this->_commands['commit'])) { - $this->_transactionStarted = false; + public function commit() { + if ($this->_transactionStarted) { + $this->_transactionNesting--; + if ($this->_transactionNesting <= 0) { + $this->_transactionStarted = false; + $this->_transactionNesting = 0; + return $this->_connection->commit(); + } return true; } return false; @@ -1796,19 +1956,29 @@ class DboSource extends DataSource { /** * Rollback a transaction * - * @param model $model * @return boolean True on success, false on fail * (i.e. if the database/model does not support transactions, * or a transaction has not started). */ - public function rollback(&$model) { - if (parent::rollback($model) && $this->execute($this->_commands['rollback'])) { + public function rollback() { + if ($this->_transactionStarted && $this->_connection->rollBack()) { $this->_transactionStarted = false; + $this->_transactionNesting = 0; return true; } return false; } +/** + * Returns the ID generated from the previous INSERT operation. + * + * @param unknown_type $source + * @return in + */ + function lastInsertId($source = null) { + return $this->_connection->lastInsertId(); + } + /** * Creates a default set of conditions from the model if $conditions is null/empty. * If conditions are supplied then they will be returned. If a model doesn't exist and no conditions @@ -2524,7 +2694,7 @@ class DboSource extends DataSource { * Translates between PHP boolean values and Database (faked) boolean values * * @param mixed $data Value to be translated - * @return mixed Converted boolean value + * @return int Converted boolean value */ public function boolean($data) { if ($data === true || $data === false) { @@ -2533,7 +2703,7 @@ class DboSource extends DataSource { } return 0; } else { - return !empty($data); + return (int) !empty($data); } } @@ -2546,13 +2716,18 @@ class DboSource extends DataSource { */ public function insertMulti($table, $fields, $values) { $table = $this->fullTableName($table); - if (is_array($fields)) { - $fields = implode(', ', array_map(array(&$this, 'name'), $fields)); - } + $holder = implode(',', array_fill(0, count($fields), '?')); + $fields = implode(', ', array_map(array(&$this, 'name'), $fields)); + $count = count($values); + $sql = "INSERT INTO {$table} ({$fields}) VALUES ({$holder})"; + $statement = $this->_connection->prepare($sql); + $this->begin(); for ($x = 0; $x < $count; $x++) { - $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values[$x]}"); + $statement->execute($values[$x]); + $statement->closeCursor(); } + return $this->commit(); } /** @@ -2867,11 +3042,12 @@ class DboSource extends DataSource { * * @param string $sql SQL query * @param mixed $data result of $sql query + * @param array $params query params bound as values * @return void */ - protected function _writeQueryCache($sql, $data) { - if (strpos(trim(strtolower($sql)), 'select') !== false) { - $this->_queryCache[$sql] = $data; + protected function _writeQueryCache($sql, $data, $params = array()) { + if (preg_match('/^\s*select/i', $sql)) { + $this->_queryCache[$sql][serialize($params)] = $data; } } @@ -2879,11 +3055,15 @@ class DboSource extends DataSource { * Returns the result for a sql query if it is already cached * * @param string $sql SQL query + * @param array $params query params bound as values * @return mixed results for query if it is cached, false otherwise */ - public function getQueryCache($sql = null) { + public function getQueryCache($sql, $params = array()) { if (isset($this->_queryCache[$sql]) && preg_match('/^\s*select/i', $sql)) { - return $this->_queryCache[$sql]; + $serialized = serialize($params); + if (isset($this->_queryCache[$sql][$serialized])) { + return $this->_queryCache[$sql][$serialized]; + } } return false; } diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 1ea708a2b..77dbb5092 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -904,9 +904,6 @@ class Model extends Object { $dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec'); $timeFields = array('H' => 'hour', 'i' => 'min', 's' => 'sec'); - - $db = $this->getDataSource(); - $format = $db->columns[$type]['format']; $date = array(); if (isset($data['hour']) && isset($data['meridian']) && $data['hour'] != 12 && 'pm' == $data['meridian']) { @@ -949,9 +946,13 @@ class Model extends Object { } } } - $date = str_replace(array_keys($date), array_values($date), $format); + + $format = $this->getDataSource()->columns[$type]['format']; + $day = empty($date['Y']) ? null : $date['Y'] . '-' . $date['m'] . '-' . $date['d'] . ' '; + $hour = empty($date['H']) ? null : $date['H'] . ':' . $date['i'] . ':' . $date['s']; + $date = new DateTime($day . $hour); if ($useNewDate && !empty($date)) { - return $date; + return $date->format($format); } } return $data; @@ -1449,15 +1450,11 @@ class Model extends Object { foreach ((array)$data as $row) { if ((is_string($row) && (strlen($row) == 36 || strlen($row) == 16)) || is_numeric($row)) { - $values = array( - $db->value($id, $this->getColumnType($this->primaryKey)), - $db->value($row) - ); + $values = array($id, $row); if ($isUUID && $primaryAdded) { - $values[] = $db->value(String::uuid()); + $values[] = String::uuid(); } - $values = implode(',', $values); - $newValues[] = "({$values})"; + $newValues[] = $values; unset($values); } elseif (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) { $newData[] = $row; @@ -1496,7 +1493,6 @@ class Model extends Object { } if (!empty($newValues)) { - $fields = implode(',', $fields); $db->insertMulti($this->{$join}, $fields, $newValues); } }