diff --git a/.travis.yml b/.travis.yml index 09144156f0..d2b04eaac7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,16 +6,19 @@ php: env: - DB=mysql DB_USER=root + - DB=pgsql before_script: - # MySQL - - sh -c "if [ '$DB' = 'mysql' ]; then mysql -u$DB_USER -e 'SET FOREIGN_KEY_CHECKS = 0; DROP DATABASE IF EXISTS test; DROP SCHEMA IF EXISTS second_hand_books; DROP SCHEMA IF EXISTS contest; DROP SCHEMA IF EXISTS bookstore_schemas; SET FOREIGN_KEY_CHECKS = 1;'; fi" - - sh -c "if [ '$DB' = 'mysql' ]; then mysql -u$DB_USER -e 'CREATE DATABASE test; CREATE SCHEMA bookstore_schemas; CREATE SCHEMA contest; CREATE SCHEMA second_hand_books;'; fi" - # Composer - wget http://getcomposer.org/composer.phar - - php composer.phar install --dev --prefer-source + - php composer.phar install --prefer-source + + # MySQL + - sh -c "if [ '$DB' = 'mysql' ]; then ./tests/bin/setup.mysql.sh; fi" - - php bin/propel test:prepare --vendor="$DB" --dsn="$DB:dbname=test" --user="$DB_USER" + # PostgreSQL + - sh -c "if [ '$DB' = 'pgsql' ]; then ./tests/bin/setup.postgres.sh; fi" -script: phpunit +script: + - sh -c "if [ '$DB' = 'mysql' ]; then phpunit; fi" + - sh -c "if [ '$DB' = 'pgsql' ]; then phpunit --exclude-group mysql; fi" diff --git a/composer.json b/composer.json index a7595ef284..f29000d85b 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "symfony/yaml": "~2.2", "symfony/console": "~2.2", "symfony/finder": "~2.2", - "symfony/validator": "~2.2", + "symfony/validator": "2.2.*@dev", "symfony/filesystem": "~2.2", "psr/log": "~1.0" }, diff --git a/src/Propel/Generator/Command/SqlInsertCommand.php b/src/Propel/Generator/Command/SqlInsertCommand.php index 56f333b49a..4cba3f1fde 100644 --- a/src/Propel/Generator/Command/SqlInsertCommand.php +++ b/src/Propel/Generator/Command/SqlInsertCommand.php @@ -30,8 +30,8 @@ class SqlInsertCommand extends AbstractCommand protected function configure() { $this - ->addOption('output-dir', null, InputOption::VALUE_REQUIRED, 'The output directory', self::DEFAULT_OUTPUT_DIRECTORY) - ->addOption('connection', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Connection to use', array()) + ->addOption('input-dir', null, InputOption::VALUE_REQUIRED, 'The input directory', self::DEFAULT_OUTPUT_DIRECTORY) + ->addOption('connection', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Connection to use. Example: bookstore=mysql:host=127.0.0.1;dbname=test;user=root;password=foobar') ->setName('sql:insert') ->setAliases(array('insert-sql')) ->setDescription('Insert SQL statements') @@ -45,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $manager = new SqlManager(); - if (!$input->hasOption('connection')) { + if (!$input->hasOption('connection') || !$input->getOption('connection')) { throw new InvalidArgumentException(sprintf('At least one connection is required')); } @@ -61,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln($message); } }); - $manager->setWorkingDirectory($input->getOption('output-dir')); + $manager->setWorkingDirectory($input->getOption('input-dir')); $manager->insertSql(); } diff --git a/src/Propel/Generator/Command/TestPrepareCommand.php b/src/Propel/Generator/Command/TestPrepareCommand.php index bd5eb5cd00..e43c72fb10 100644 --- a/src/Propel/Generator/Command/TestPrepareCommand.php +++ b/src/Propel/Generator/Command/TestPrepareCommand.php @@ -159,7 +159,7 @@ protected function buildFixtures($fixturesDir, $connections, InputInterface $inp $in = new ArrayInput(array( 'command' => 'sql:insert', - '--output-dir' => 'build/sql/', + '--input-dir' => 'build/sql/', '--connection' => $conParams, '--verbose' => $input->getOption('verbose'), )); diff --git a/src/Propel/Generator/Manager/SqlManager.php b/src/Propel/Generator/Manager/SqlManager.php index d9ea526c10..67f4c952c8 100644 --- a/src/Propel/Generator/Manager/SqlManager.php +++ b/src/Propel/Generator/Manager/SqlManager.php @@ -171,8 +171,13 @@ public function insertSql($datasource = null) try { foreach ($sqls as $sql) { - $stmt = $con->prepare($sql); - $stmt->execute(); + try { + $stmt = $con->prepare($sql); + $stmt->execute(); + } catch (\Exception $e) { + $message = sprintf('SQL insert failed: %s', $sql); + throw new \Exception($message, 0, $e); + } } $con->commit(); @@ -180,6 +185,8 @@ public function insertSql($datasource = null) $con->rollback(); throw $e; } + + $this->log(sprintf('%d queries executed for %s database.', count($sqls), $database)); } return true; diff --git a/src/Propel/Generator/Model/ForeignKey.php b/src/Propel/Generator/Model/ForeignKey.php index e9ba402f0a..36f5c0bf53 100644 --- a/src/Propel/Generator/Model/ForeignKey.php +++ b/src/Propel/Generator/Model/ForeignKey.php @@ -33,17 +33,65 @@ class ForeignKey extends MappingModel const SETDEFAULT = 'SET DEFAULT'; const SETNULL = 'SET NULL'; + /** + * @var string + */ private $foreignTableCommonName; + + /** + * @var string + */ private $foreignSchemaName; + + /** + * @var string + */ private $name; + + /** + * @var string + */ private $phpName; + + /** + * @var string + */ private $refPhpName; + + /** + * @var string + */ private $defaultJoin; + + /** + * @var string + */ private $onUpdate = ''; + + /** + * @var string + */ private $onDelete = ''; + + /** + * @var Table + */ private $parentTable; + + /** + * @var string[] + */ private $localColumns; + + /** + * @var string[] + */ private $foreignColumns; + + + /** + * @var bool + */ private $skipSql; /** @@ -290,7 +338,9 @@ public function setForeignTableCommonName($tableName) */ public function getForeignTable() { - return $this->parentTable->getDatabase()->getTable($this->getForeignTableName()); + if ($this->parentTable->getDatabase()) { + return $this->parentTable->getDatabase()->getTable($this->getForeignTableName()); + } } /** diff --git a/src/Propel/Generator/Model/Index.php b/src/Propel/Generator/Model/Index.php index ecd1f27287..bde5824341 100644 --- a/src/Propel/Generator/Model/Index.php +++ b/src/Propel/Generator/Model/Index.php @@ -21,9 +21,24 @@ */ class Index extends MappingModel { + /** + * @var string + */ protected $name; + + /** + * @var string + */ protected $table; + + /** + * @var string[] + */ private $columns; + + /** + * @var string[] + */ private $columnsSize; /** @@ -150,6 +165,15 @@ public function addColumn($data) } } + /** + * @param string $name + * @return bool + */ + public function hasColumn($name) + { + return in_array($name, $this->columns); + } + /** * Sets an array of columns to use for the index. * diff --git a/src/Propel/Generator/Model/Table.php b/src/Propel/Generator/Model/Table.php index faea358876..26f5d02e56 100644 --- a/src/Propel/Generator/Model/Table.php +++ b/src/Propel/Generator/Model/Table.php @@ -29,10 +29,26 @@ */ class Table extends ScopedMappingModel implements IdMethod { + + /** + * @var Column[] + */ private $columns; + + /** + * @var ForeignKey[] + */ private $foreignKeys; private $foreignTableNames; + + /** + * @var Index[] + */ private $indices; + + /** + * @var Unique[] + */ private $unices; private $idMethodParameters; private $commonName; @@ -1344,7 +1360,7 @@ public function hasEnumColumns() /** * Returns the list of all foreign keys. * - * @return array + * @return ForeignKey[] */ public function getForeignKeys() { @@ -1380,6 +1396,71 @@ public function getUnices() return $this->unices; } + /** + * Checks if $keys are a unique constraint in the table. + * (through primaryKey, through a regular unices constraints or for single keys when it has isUnique=true) + * + * @param Column[]|string[] $keys + * @return bool + */ + public function isUnique(array $keys) + { + if (1 === count($keys)) { + $column = $keys[0] instanceof Column ? $keys[0] : $this->getColumn($keys[0]); + if ($column){ + if ($column->isUnique()) return true; + if ($column->isPrimaryKey() && 1 === count($column->getTable()->getPrimaryKey())) { + return true; + } + } + } + + // check if pk == $keys + if (count($this->getPrimaryKey()) === count($keys)) { + $allPk = true; + $stringArray = is_string($keys[0]); + foreach ($this->getPrimaryKey() as $pk) { + if ($stringArray) { + if (!in_array($pk->getName(), $keys)) { + $allPk = false; + break; + } + } else { + if (!in_array($pk, $keys)) { + $allPk = false; + break; + } + } + } + + if ($allPk) { + return true; + } + } + + // check if there is a unique constrains that contains exactly the $keys + if ($this->unices) { + foreach ($this->unices as $unique) { + if (count($unique->getColumns()) === count($keys)) { + $allAvailable = true; + foreach ($keys as $key) { + if (!$unique->hasColumn($key instanceof Column ? $key->getName() : $key)) { + $allAvailable = false; + break; + } + } + if ($allAvailable) { + return true; + } + } else { + continue; + } + } + } + + return false; + } + /** * Returns whether or not the table has a column. * diff --git a/src/Propel/Generator/Platform/PgsqlPlatform.php b/src/Propel/Generator/Platform/PgsqlPlatform.php index 01b06f0a5a..c9e3e94958 100644 --- a/src/Propel/Generator/Platform/PgsqlPlatform.php +++ b/src/Propel/Generator/Platform/PgsqlPlatform.php @@ -210,6 +210,29 @@ public function getAddTablesDDL(Database $database) return $ret; } + /** + * {@inheritDoc} + */ + public function getAddForeignKeysDDL(Table $table) + { + $ret = ''; + foreach ($table->getForeignKeys() as $fk) { + //PostgreSQL requires the keys of the foreignTable of a foreignKeys to be unique. + //check if there is already a unique constraint with exactly + //the keys of the FK, if not define it. + if ($fk->getForeignTable() && !$fk->getForeignTable()->isUnique($fk->getForeignColumnObjects())) { + $unique = new Unique(); + $unique->setTable($fk->getForeignTable()); + $unique->setColumns($fk->getForeignColumnObjects()); + $ret .= $this->getAddIndexDDL($unique); + } + + $ret .= $this->getAddForeignKeyDDL($fk); + } + + return $ret; + } + public function getAddTableDDL(Table $table) { $ret = ''; diff --git a/src/Propel/Generator/Reverse/AbstractSchemaParser.php b/src/Propel/Generator/Reverse/AbstractSchemaParser.php index 231b295345..55c1b8a329 100644 --- a/src/Propel/Generator/Reverse/AbstractSchemaParser.php +++ b/src/Propel/Generator/Reverse/AbstractSchemaParser.php @@ -13,6 +13,7 @@ use Propel\Generator\Config\GeneratorConfigInterface; use Propel\Generator\Model\VendorInfo; use Propel\Runtime\Connection\ConnectionInterface; +use Propel\Runtime\Connection\SqlConnectionInterface; /** * Base class for reverse engineering a database schema. diff --git a/src/Propel/Generator/Reverse/PgsqlSchemaParser.php b/src/Propel/Generator/Reverse/PgsqlSchemaParser.php index 2c09795618..806dc52f5b 100644 --- a/src/Propel/Generator/Reverse/PgsqlSchemaParser.php +++ b/src/Propel/Generator/Reverse/PgsqlSchemaParser.php @@ -98,16 +98,35 @@ public function parse(Database $database) // Clean up $stmt = null; - $stmt = $this->dbh->query("SELECT c.oid, + $searchPath = '?'; + $params = [$database->getSchema()]; + if (!$database->getSchema()) { + $stmt = $this->dbh->query('SHOW search_path'); + $searchPathString = $stmt->fetchColumn(); + + $params = []; + $searchPath = explode(',', $searchPathString); + + foreach ($searchPath as $n => &$path) { + $params[] = $path; + $path = '?'; + } + $searchPath = implode(', ', $searchPath); + } + + $stmt = $this->dbh->prepare("SELECT c.oid, c.relname, n.nspname FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid) WHERE c.relkind = 'r' AND n.nspname NOT IN ('information_schema','pg_catalog') AND n.nspname NOT LIKE 'pg_temp%' AND n.nspname NOT LIKE 'pg_toast%' + AND n.nspname IN ($searchPath) ORDER BY relname" ); + $stmt->execute($params); + $tableWraps = array(); // First load the tables (important that this happen before filling out details of tables) @@ -223,7 +242,7 @@ protected function addColumns(Table $table, $oid, $version) $autoincrement = null; // if column has a default - if ('t' === $boolHasDefault && (strlen(trim($default)) > 0)) { + if (true === $boolHasDefault && (strlen(trim($default)) > 0)) { if (!preg_match('/^nextval\(/', $default)) { $strDefault= preg_replace('/::[\W\D]*/', '', $default); $default = str_replace("'", '', $strDefault); @@ -335,7 +354,7 @@ private function processDomain($strDomain) $arrDomain['scale'] = $arrLengthPrecision['scale']; $arrDomain['notnull'] = $row['typnotnull']; $arrDomain['default'] = $row['typdefault']; - $arrDomain['hasdefault'] = (strlen(trim($row['typdefault'])) > 0) ? 't' : 'f'; + $arrDomain['hasdefault'] = (strlen(trim($row['typdefault'])) > 0) ? true : false; $stmt = null; // cleanup diff --git a/src/Propel/Runtime/ActiveQuery/Criteria.php b/src/Propel/Runtime/ActiveQuery/Criteria.php index 6d45964f3a..0b2c3d3be6 100644 --- a/src/Propel/Runtime/ActiveQuery/Criteria.php +++ b/src/Propel/Runtime/ActiveQuery/Criteria.php @@ -1799,7 +1799,6 @@ public function createSelectSql(&$params) $orderByClause = array(); $orderBy = $this->getOrderByColumns(); - $groupBy = $this->getGroupByColumns(); // get the first part of the SQL statement, the SELECT part $selectSql = $db->createSelectSqlPart($this, $fromClause); @@ -1861,9 +1860,6 @@ public function createSelectSql(&$params) } } - // Add the GROUP BY columns - $groupByClause = $groupBy; - $having = $this->getHaving(); $havingString = null; if (null !== $having) { @@ -1968,10 +1964,13 @@ public function createSelectSql(&$params) // Build the SQL from the arrays we compiled $sql = $selectSql .' FROM ' . $from - .($whereClause ? ' WHERE '.implode(' AND ', $whereClause) : '') - .($groupByClause ? ' GROUP BY '.implode(',', $groupByClause) : '') - .($havingString ? ' HAVING '.$havingString : '') - .($orderByClause ? ' ORDER BY '.implode(',', $orderByClause) : '') + .($whereClause ? ' WHERE '.implode(' AND ', $whereClause) : ''); + + + $db->applyGroupBy($sql, $this); + + $sql .= ($havingString ? ' HAVING '.$havingString : '') + .($orderByClause ? ' ORDER BY '.implode(',', $orderByClause) : '') ; // APPLY OFFSET & LIMIT to the query. @@ -2445,7 +2444,7 @@ public function doSelect($con = null) $stmt = null; // close } Propel::log($e->getMessage(), Propel::LOG_ERR); - throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql)); + throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), null, $e); } return $con->getDataFetcher($stmt); diff --git a/src/Propel/Runtime/Adapter/Pdo/PdoAdapter.php b/src/Propel/Runtime/Adapter/Pdo/PdoAdapter.php index 4e8db4633f..0b2ce4eba3 100644 --- a/src/Propel/Runtime/Adapter/Pdo/PdoAdapter.php +++ b/src/Propel/Runtime/Adapter/Pdo/PdoAdapter.php @@ -287,6 +287,18 @@ public function getTimestampFormatter() return 'Y-m-d H:i:s'; } + /** + * @param string $sql + * @param Criteria $criteria + */ + public function applyGroupBy(&$sql, Criteria $criteria) + { + $groupBy = $criteria->getGroupByColumns(); + if ($groupBy) { + $sql .= ' GROUP BY ' . implode(',', $groupBy); + } + } + /** * Returns date formatter string for use in date() function. * @@ -436,6 +448,30 @@ public function createSelectSqlPart(Criteria $criteria, &$fromClause, $aliasAll return $sql; } + /** + * Returns all selected columns that are selected without a aggregate function. + * + * @param Criteria $criteria + * @return string[] + */ + public function getPlainSelectedColumns(Criteria $criteria) + { + $selected = []; + foreach ($criteria->getSelectColumns() as $columnName) { + if (false === strpos($columnName, '(')) { + $selected[] = $columnName; + } + } + + foreach ($criteria->getAsColumns() as $alias => $col) { + if (!in_array($col, $selected)) { + $selected[] = $col; + } + } + + return $selected; + } + /** * Ensures uniqueness of select column names by turning them all into aliases * This is necessary for queries on more than one table when the tables share a column name diff --git a/src/Propel/Runtime/Adapter/Pdo/PgsqlAdapter.php b/src/Propel/Runtime/Adapter/Pdo/PgsqlAdapter.php index be48a8aac2..ec861ea402 100644 --- a/src/Propel/Runtime/Adapter/Pdo/PgsqlAdapter.php +++ b/src/Propel/Runtime/Adapter/Pdo/PgsqlAdapter.php @@ -10,6 +10,7 @@ namespace Propel\Runtime\Adapter\Pdo; +use Propel\Runtime\Adapter\AdapterInterface; use Propel\Runtime\Adapter\SqlAdapterInterface; use Propel\Runtime\Connection\ConnectionInterface; use Propel\Runtime\Exception\InvalidArgumentException; @@ -126,6 +127,38 @@ public function applyLimit(&$sql, $offset, $limit) } } + /** + * @param string $sql + * @param Criteria $criteria + */ + public function applyGroupBy(&$sql, Criteria $criteria) + { + $groupBy = $criteria->getGroupByColumns(); + + if ($groupBy) { + //check if all selected columns are groupBy'ed. + $selected = $this->getPlainSelectedColumns($criteria); + $asSelects = $criteria->getAsColumns(); + + foreach ($selected as $colName) { + if (!in_array($colName, $groupBy)) { + + //is a alias there that is grouped? + if ($alias = array_search($colName, $asSelects)) { + if (in_array($alias, $groupBy)) { + continue; //yes, alias is selected. + } + } + $groupBy[] = $colName; + } + } + + if ($groupBy) { + $sql .= ' GROUP BY ' . implode(',', $groupBy); + } + } + } + /** * @see AdapterInterface::random() * diff --git a/tests/Fixtures/namespaced/schema.xml b/tests/Fixtures/namespaced/schema.xml index eaf81346e1..66e036d1da 100644 --- a/tests/Fixtures/namespaced/schema.xml +++ b/tests/Fixtures/namespaced/schema.xml @@ -1,7 +1,8 @@ + xsi:noNamespaceSchemaLocation="../../../resources/xsd/database.xsd"> diff --git a/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php b/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php index e344edbb68..bac9907cfb 100644 --- a/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php +++ b/tests/Propel/Tests/Generator/Behavior/Timestampable/TimestampableBehaviorTest.php @@ -45,6 +45,9 @@ public function testParameters() $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table1', 'getUpdatedOn'), 'Timestampable allows customization of update_column name'); } + /** + * @TODO on slow CPU machines or a high load this will fail. Fix it. + */ public function testPreSave() { $t1 = new Table2(); diff --git a/tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectRelTest.php b/tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectRelTest.php index 58ab0f1f28..d1dd181c52 100644 --- a/tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectRelTest.php +++ b/tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectRelTest.php @@ -741,6 +741,7 @@ public function testSetterCollectionWithManyToManyModifiedByReferenceWithANewObj $book = new Book(); $book->setTitle('foo'); + $book->setISBN('01234'); // The object is "new" $this->assertTrue($book->isNew()); @@ -751,6 +752,7 @@ public function testSetterCollectionWithManyToManyModifiedByReferenceWithANewObj $books[] = $book; $bookClubList->setBooks($books); + $bookClubList->setGroupLeader('TestLeader'); $bookClubList->save(); $this->assertEquals(1, BookQuery::create()->count()); @@ -767,6 +769,7 @@ public function testSetterCollectionWithManyToManyModifiedByReferenceWithAnExist $book = new Book(); $book->setTitle('foo'); + $book->setISBN('01234'); $book->save(); // The object isn't "new" @@ -778,6 +781,7 @@ public function testSetterCollectionWithManyToManyModifiedByReferenceWithAnExist $books[] = $book; $bookClubList->setBooks($books); + $bookClubList->setGroupLeader('TestLeader'); $bookClubList->save(); $this->assertEquals(1, BookQuery::create()->count()); @@ -845,9 +849,11 @@ public function testRemoveObjectOneToMany() AuthorQuery::create()->deleteAll(); $book = new Book(); + $book->setISBN('012345'); $book->setTitle('Propel Book'); $book2 = new Book(); + $book2->setISBN('6789'); $book2->setTitle('Propel2 Book'); $author = new Author(); @@ -898,6 +904,7 @@ public function testRemoveObjectOneToManyWithFkRequired() $book = new Book(); $book->setTitle('Propel Book'); + $book->setISBN('01234'); $book->addBookSummary($bookSummary); $book->addBookSummary($bookSummary2); diff --git a/tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectTest.php b/tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectTest.php index 43fb37d1f9..609a792faa 100644 --- a/tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectTest.php +++ b/tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectTest.php @@ -1355,13 +1355,16 @@ public function testSetterOneToManyWithFkRequired() $coll->setModel('BookSummary'); for ($i = 0; $i < 3; $i++) { - $coll[] = new BookSummary(); + $summary = new BookSummary(); + $summary->setSummary('Was Great!'); + $coll[] = $summary; } $this->assertEquals(3, $coll->count()); $b = new Book(); $b->setTitle('myBook'); + $b->setISBN('01234'); $b->setBookSummaries($coll); $b->save(); @@ -1437,6 +1440,7 @@ public function testSetterOneToManyReplacesOldObjectsByNewObjectsWithFkRequired( $b = new Book(); $b->setTitle('Hello'); + $b->setISBN('01234'); $b->setBookSummaries($bookSummaries); $b->save(); diff --git a/tests/Propel/Tests/Generator/Builder/Om/PoisonedCacheBugTest.php b/tests/Propel/Tests/Generator/Builder/Om/PoisonedCacheBugTest.php index 5e198c14ff..224a10e13a 100644 --- a/tests/Propel/Tests/Generator/Builder/Om/PoisonedCacheBugTest.php +++ b/tests/Propel/Tests/Generator/Builder/Om/PoisonedCacheBugTest.php @@ -42,10 +42,12 @@ public function setUp() $b1 = new Book(); $b1->setTitle("The Hitchhikers Guide To The Galaxy"); + $b1->setISBN('01234'); $a->addBook($b1); $b2 = new Book(); $b2->setTitle("The Restaurant At The End Of The Universe"); + $b2->setISBN('5678'); $a->addBook($b2); $a->save(); diff --git a/tests/Propel/Tests/Generator/Builder/Om/QueryBuilderTest.php b/tests/Propel/Tests/Generator/Builder/Om/QueryBuilderTest.php index 62de62646a..abd114c9a9 100644 --- a/tests/Propel/Tests/Generator/Builder/Om/QueryBuilderTest.php +++ b/tests/Propel/Tests/Generator/Builder/Om/QueryBuilderTest.php @@ -236,7 +236,7 @@ public function testFindPkSimpleAddsObjectToInstancePool() public function testFindPkUsesFindPkComplexOnNonEmptyQueries() { BookQuery::create('b')->findPk(123, $this->con); - $expected = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.ID=123'; + $expected = $this->getSql('SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.ID=123'); $this->assertEquals($expected, $this->con->getLastExecutedQuery()); } diff --git a/tests/Propel/Tests/Generator/Platform/PgsqlPlatformTest.php b/tests/Propel/Tests/Generator/Platform/PgsqlPlatformTest.php index 590355d53f..061ac71781 100644 --- a/tests/Propel/Tests/Generator/Platform/PgsqlPlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/PgsqlPlatformTest.php @@ -585,7 +585,7 @@ public function testAddIndexDDL($index) $expected = " CREATE INDEX babar ON foo (bar1,bar2); "; - $this->assertEquals($expected, $this->getPLatform()->getAddIndexDDL($index)); + $this->assertEquals($expected, $this->getPlatform()->getAddIndexDDL($index)); } /** @@ -598,7 +598,7 @@ public function testAddIndicesDDL($table) CREATE INDEX foo_index ON foo (bar1); "; - $this->assertEquals($expected, $this->getPLatform()->getAddIndicesDDL($table)); + $this->assertEquals($expected, $this->getPlatform()->getAddIndicesDDL($table)); } /** @@ -609,7 +609,7 @@ public function testDropIndexDDL($index) $expected = " DROP INDEX babar; "; - $this->assertEquals($expected, $this->getPLatform()->getDropIndexDDL($index)); + $this->assertEquals($expected, $this->getPlatform()->getDropIndexDDL($index)); } /** @@ -618,7 +618,7 @@ public function testDropIndexDDL($index) public function testGetIndexDDL($index) { $expected = 'INDEX babar (bar1,bar2)'; - $this->assertEquals($expected, $this->getPLatform()->getIndexDDL($index)); + $this->assertEquals($expected, $this->getPlatform()->getIndexDDL($index)); } /** @@ -646,7 +646,7 @@ public function testGetAddForeignKeysDDL($table) REFERENCES baz (id) ON DELETE SET NULL; "; - $this->assertEquals($expected, $this->getPLatform()->getAddForeignKeysDDL($table)); + $this->assertEquals($expected, $this->getPlatform()->getAddForeignKeysDDL($table)); } /** @@ -660,7 +660,7 @@ public function testGetAddForeignKeyDDL($fk) REFERENCES bar (id) ON DELETE CASCADE; "; - $this->assertEquals($expected, $this->getPLatform()->getAddForeignKeyDDL($fk)); + $this->assertEquals($expected, $this->getPlatform()->getAddForeignKeyDDL($fk)); } /** @@ -669,7 +669,7 @@ public function testGetAddForeignKeyDDL($fk) public function testGetAddForeignKeySkipSqlDDL($fk) { $expected = ''; - $this->assertEquals($expected, $this->getPLatform()->getAddForeignKeyDDL($fk)); + $this->assertEquals($expected, $this->getPlatform()->getAddForeignKeyDDL($fk)); } /** @@ -680,7 +680,7 @@ public function testGetDropForeignKeyDDL($fk) $expected = " ALTER TABLE foo DROP CONSTRAINT foo_bar_FK; "; - $this->assertEquals($expected, $this->getPLatform()->getDropForeignKeyDDL($fk)); + $this->assertEquals($expected, $this->getPlatform()->getDropForeignKeyDDL($fk)); } /** @@ -689,7 +689,7 @@ public function testGetDropForeignKeyDDL($fk) public function testGetDropForeignKeySkipSqlDDL($fk) { $expected = ''; - $this->assertEquals($expected, $this->getPLatform()->getDropForeignKeyDDL($fk)); + $this->assertEquals($expected, $this->getPlatform()->getDropForeignKeyDDL($fk)); } /** @@ -701,7 +701,7 @@ public function testGetForeignKeyDDL($fk) FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE"; - $this->assertEquals($expected, $this->getPLatform()->getForeignKeyDDL($fk)); + $this->assertEquals($expected, $this->getPlatform()->getForeignKeyDDL($fk)); } /** @@ -710,7 +710,7 @@ public function testGetForeignKeyDDL($fk) public function testGetForeignKeySkipSqlDDL($fk) { $expected = ''; - $this->assertEquals($expected, $this->getPLatform()->getForeignKeyDDL($fk)); + $this->assertEquals($expected, $this->getPlatform()->getForeignKeyDDL($fk)); } public function testGetCommentBlockDDL() @@ -720,7 +720,7 @@ public function testGetCommentBlockDDL() -- foo bar ----------------------------------------------------------------------- "; - $this->assertEquals($expected, $this->getPLatform()->getCommentBlockDDL('foo bar')); + $this->assertEquals($expected, $this->getPlatform()->getCommentBlockDDL('foo bar')); } } diff --git a/tests/Propel/Tests/Generator/Reverse/PgsqlSchemaParserTest.php b/tests/Propel/Tests/Generator/Reverse/PgsqlSchemaParserTest.php index ec788488de..654482e93f 100644 --- a/tests/Propel/Tests/Generator/Reverse/PgsqlSchemaParserTest.php +++ b/tests/Propel/Tests/Generator/Reverse/PgsqlSchemaParserTest.php @@ -22,6 +22,7 @@ * Tests for Pgsql database schema parser. * * @author Alan Pinstein + * @group pgsql */ class PgsqlSchemaParserTest extends TestCase { @@ -72,10 +73,8 @@ public function testParse($columnDDL, $expectedColumnPhpName, $expectedColumnDef $database->setPlatform(new DefaultPlatform()); // make sure our DDL insert produced exactly the SQL we inserted - $this->assertEquals(1, $parser->parse($database), 'One table and one view defined should return one as we exclude views'); - $tables = $database->getTables(); - $this->assertEquals(1, count($tables)); - $table = $tables[0]; + $this->assertGreaterThanOrEqual(1, $parser->parse($database), 'We parsed at least one table.'); + $table = $database->getTable('foo'); $columns = $table->getColumns(); $this->assertEquals(1, count($columns)); diff --git a/tests/Propel/Tests/Helpers/Bookstore/BookstoreTestBase.php b/tests/Propel/Tests/Helpers/Bookstore/BookstoreTestBase.php index a84e53a16f..2b16e71b55 100644 --- a/tests/Propel/Tests/Helpers/Bookstore/BookstoreTestBase.php +++ b/tests/Propel/Tests/Helpers/Bookstore/BookstoreTestBase.php @@ -62,6 +62,27 @@ protected function tearDown() } } + /** + * Makes the sql compatible with the current database. + * Means: replaces ` etc. + * + * @param string $sql + * @param string $source + * @return mixed + */ + protected function getSql($sql, $source = 'mysql') + { + if (!$this->isDb('mysql') && 'mysql' === $source) { + return str_replace('`', '', $sql); + } + return $sql; + } + + protected function isDb($db = 'mysql') + { + return in_array($this->getDriver(), array($db)); + } + protected function getDriver() { return $this->con->getAttribute(\PDO::ATTR_DRIVER_NAME); diff --git a/tests/Propel/Tests/Issues/Issue617Test.php b/tests/Propel/Tests/Issues/Issue617Test.php index 06c41fdaa2..e6ec8dfe16 100644 --- a/tests/Propel/Tests/Issues/Issue617Test.php +++ b/tests/Propel/Tests/Issues/Issue617Test.php @@ -12,6 +12,7 @@ * not return the `Engine` of the table. Since we depend on that information in `MysqlPlatform`, * we really need that kind of information. * + * @group mysql */ class Issue617Test extends PlatformDatabaseBuildTimeBase { diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaMergeTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaMergeTest.php index 99a2c6acf9..568a6a15ce 100644 --- a/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaMergeTest.php +++ b/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaMergeTest.php @@ -228,27 +228,27 @@ public function testMergeWithWhereConditions() $c1->add(BookTableMap::TITLE, 'foo'); $c2 = new Criteria(); $c1->mergeWith($c2); - $sql = 'SELECT FROM `book` WHERE book.TITLE=:p1'; + $sql = $this->getSql('SELECT FROM `book` WHERE book.TITLE=:p1'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() does not remove an existing where condition'); $c1 = new Criteria(); $c2 = new Criteria(); $c2->add(BookTableMap::TITLE, 'foo'); $c1->mergeWith($c2); - $sql = 'SELECT FROM `book` WHERE book.TITLE=:p1'; + $sql = $this->getSql('SELECT FROM `book` WHERE book.TITLE=:p1'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to an empty condition'); $c1 = new Criteria(); $c1->add(BookTableMap::ID, 123); $c2 = new Criteria(); $c2->add(BookTableMap::TITLE, 'foo'); $c1->mergeWith($c2); - $sql = 'SELECT FROM `book` WHERE book.ID=:p1 AND book.TITLE=:p2'; + $sql = $this->getSql('SELECT FROM `book` WHERE book.ID=:p1 AND book.TITLE=:p2'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to existing conditions'); $c1 = new Criteria(); $c1->add(BookTableMap::TITLE, 'foo'); $c2 = new Criteria(); $c2->add(BookTableMap::TITLE, 'bar'); $c1->mergeWith($c2); - $sql = 'SELECT FROM `book` WHERE (book.TITLE=:p1 AND book.TITLE=:p2)'; + $sql = $this->getSql('SELECT FROM `book` WHERE (book.TITLE=:p1 AND book.TITLE=:p2)'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to existing conditions on the same column'); $c1 = new Criteria(); $c1->add(BookTableMap::TITLE, 'foo'); @@ -256,37 +256,38 @@ public function testMergeWithWhereConditions() $c2 = new Criteria(); $c2->add(AuthorTableMap::FIRST_NAME, 'bar'); $c1->mergeWith($c2); - $sql = 'SELECT FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE book.TITLE=:p1 AND author.FIRST_NAME=:p2'; + $sql = $this->getSql('SELECT FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE book.TITLE=:p1 AND author.FIRST_NAME=:p2'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to existing conditions on the different tables'); } + public function testMergeOrWithWhereConditions() { $c1 = new Criteria(); $c1->add(BookTableMap::TITLE, 'foo'); $c2 = new Criteria(); $c1->mergeWith($c2, Criteria::LOGICAL_OR); - $sql = 'SELECT FROM `book` WHERE book.TITLE=:p1'; + $sql = $this->getSql('SELECT FROM `book` WHERE book.TITLE=:p1'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() does not remove an existing where condition'); $c1 = new Criteria(); $c2 = new Criteria(); $c2->add(BookTableMap::TITLE, 'foo'); $c1->mergeWith($c2, Criteria::LOGICAL_OR); - $sql = 'SELECT FROM `book` WHERE book.TITLE=:p1'; + $sql = $this->getSql('SELECT FROM `book` WHERE book.TITLE=:p1'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to an empty condition'); $c1 = new Criteria(); $c1->add(BookTableMap::ID, 123); $c2 = new Criteria(); $c2->add(BookTableMap::TITLE, 'foo'); $c1->mergeWith($c2, Criteria::LOGICAL_OR); - $sql = 'SELECT FROM `book` WHERE (book.ID=:p1 OR book.TITLE=:p2)'; + $sql = $this->getSql('SELECT FROM `book` WHERE (book.ID=:p1 OR book.TITLE=:p2)'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to existing conditions'); $c1 = new Criteria(); $c1->add(BookTableMap::TITLE, 'foo'); $c2 = new Criteria(); $c2->add(BookTableMap::TITLE, 'bar'); $c1->mergeWith($c2, Criteria::LOGICAL_OR); - $sql = 'SELECT FROM `book` WHERE (book.TITLE=:p1 OR book.TITLE=:p2)'; + $sql = $this->getSql('SELECT FROM `book` WHERE (book.TITLE=:p1 OR book.TITLE=:p2)'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to existing conditions on the same column'); $c1 = new Criteria(); $c1->add(BookTableMap::TITLE, 'foo'); @@ -294,7 +295,7 @@ public function testMergeOrWithWhereConditions() $c2 = new Criteria(); $c2->add(AuthorTableMap::FIRST_NAME, 'bar'); $c1->mergeWith($c2, Criteria::LOGICAL_OR); - $sql = 'SELECT FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE (book.TITLE=:p1 OR author.FIRST_NAME=:p2)'; + $sql = $this->getSql('SELECT FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE (book.TITLE=:p1 OR author.FIRST_NAME=:p2)'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to existing conditions on the different tables'); } @@ -305,14 +306,14 @@ public function testMerge_OrWithWhereConditions() $c2 = new Criteria(); $c1->_or(); $c1->mergeWith($c2); - $sql = 'SELECT FROM `book` WHERE book.TITLE=:p1'; + $sql = $this->getSql('SELECT FROM `book` WHERE book.TITLE=:p1'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() does not remove an existing where condition'); $c1 = new Criteria(); $c2 = new Criteria(); $c2->add(BookTableMap::TITLE, 'foo'); $c1->_or(); $c1->mergeWith($c2); - $sql = 'SELECT FROM `book` WHERE book.TITLE=:p1'; + $sql = $this->getSql('SELECT FROM `book` WHERE book.TITLE=:p1'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to an empty condition'); $c1 = new Criteria(); $c1->add(BookTableMap::ID, 123); @@ -320,7 +321,7 @@ public function testMerge_OrWithWhereConditions() $c2 = new Criteria(); $c2->add(BookTableMap::TITLE, 'foo'); $c1->mergeWith($c2); - $sql = 'SELECT FROM `book` WHERE (book.ID=:p1 OR book.TITLE=:p2)'; + $sql = $this->getSql('SELECT FROM `book` WHERE (book.ID=:p1 OR book.TITLE=:p2)'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to existing conditions'); $c1 = new Criteria(); $c1->add(BookTableMap::TITLE, 'foo'); @@ -328,7 +329,7 @@ public function testMerge_OrWithWhereConditions() $c2 = new Criteria(); $c2->add(BookTableMap::TITLE, 'bar'); $c1->mergeWith($c2); - $sql = 'SELECT FROM `book` WHERE (book.TITLE=:p1 OR book.TITLE=:p2)'; + $sql = $this->getSql('SELECT FROM `book` WHERE (book.TITLE=:p1 OR book.TITLE=:p2)'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to existing conditions on the same column'); $c1 = new Criteria(); $c1->add(BookTableMap::TITLE, 'foo'); @@ -337,9 +338,10 @@ public function testMerge_OrWithWhereConditions() $c2 = new Criteria(); $c2->add(AuthorTableMap::FIRST_NAME, 'bar'); $c1->mergeWith($c2); - $sql = 'SELECT FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE (book.TITLE=:p1 OR author.FIRST_NAME=:p2)'; + $sql = $this->getSql('SELECT FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE (book.TITLE=:p1 OR author.FIRST_NAME=:p2)'); $this->assertCriteriaTranslation($c1, $sql, 'mergeWith() merges where condition to existing conditions on the different tables'); } + public function testMergeWithHavingConditions() { $c1 = new Criteria(); diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaTest.php index 453f7a8bb6..2876313a77 100644 --- a/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaTest.php +++ b/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaTest.php @@ -887,7 +887,10 @@ public function testAliasInCriterion() $this->assertEquals("column_alias", $crit->getColumn()); } - public function testHaving() + /** + * @group mysql + */ + public function testHavingAlias() { $c = new Criteria(); $c->addSelectColumn(BookTableMap::TITLE); @@ -903,7 +906,28 @@ public function testHaving() $this->assertEquals($expected, $this->con->getLastExecutedQuery()); } - public function testHavingRaw() + public function testHaving() + { + $c = new Criteria(); + $c->addSelectColumn(BookTableMap::TITLE); + $c->addSelectColumn(BookTableMap::ISBN); + $crit = $c->getNewCriterion('ISBN', '1234567890123'); + $c->addHaving($crit); + $c->addGroupByColumn(BookTableMap::TITLE); + $c->addGroupByColumn(BookTableMap::ISBN); + $expected = 'SELECT book.TITLE, book.ISBN FROM book GROUP BY book.TITLE,book.ISBN HAVING ISBN=:p1'; + $params = array(); + $result = $c->createSelectSql($params); + $this->assertEquals($expected, $result); + $c->doSelect($this->con); + $expected = 'SELECT book.TITLE, book.ISBN FROM book GROUP BY book.TITLE,book.ISBN HAVING ISBN=\'1234567890123\''; + $this->assertEquals($expected, $this->con->getLastExecutedQuery()); + } + + /** + * @group mysql + */ + public function testHavingAliasRaw() { $c = new Criteria(); $c->addSelectColumn(BookTableMap::TITLE); diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaSelectTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaSelectTest.php index b08088558e..fc1b1e2352 100644 --- a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaSelectTest.php +++ b/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaSelectTest.php @@ -50,7 +50,7 @@ public function testSelectStringNoResult() $c->select('Title'); $titles = $c->find($this->con); - $expectedSQL = 'SELECT book.TITLE AS "Title" FROM `book` WHERE book.TITLE = \'kdjfhlkdsh\''; + $expectedSQL = $this->getSql('SELECT book.TITLE AS "Title" FROM `book` WHERE book.TITLE = \'kdjfhlkdsh\''); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(string) selects a single column'); $this->assertInstanceOf('Propel\Runtime\Collection\ArrayCollection', $titles, 'find() called after select(string) returns a PropelArrayCollection object'); $this->assertTrue(is_array($titles->getData()), 'find() called after select(string) returns an empty ArrayCollection object'); @@ -68,19 +68,19 @@ public function testSelectStringAcceptsColumnNames() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->select('Title'); $titles = $c->find(); - $expectedSQL = 'SELECT book.TITLE AS "Title" FROM `book`'; + $expectedSQL = $this->getSql('SELECT book.TITLE AS "Title" FROM `book`'); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select() accepts short column names'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->select('Propel\Tests\Bookstore\Book.Title'); $titles = $c->find(); - $expectedSQL = 'SELECT book.TITLE AS "Propel\Tests\Bookstore\Book.Title" FROM `book`'; + $expectedSQL = $this->getSql('SELECT book.TITLE AS "Propel\Tests\Bookstore\Book.Title" FROM `book`'); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select() accepts complete column names'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->select('b.Title'); $titles = $c->find(); - $expectedSQL = 'SELECT book.TITLE AS "b.Title" FROM `book`'; + $expectedSQL = $this->getSql('SELECT book.TITLE AS "b.Title" FROM `book`'); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select() accepts complete column names with table aliases'); } @@ -101,7 +101,7 @@ public function testSelectStringFind() $c->select('FirstName'); $authors = $c->find($this->con); $this->assertEquals($authors->count(), 1, 'find() called after select(string) allows for where() statements'); - $expectedSQL = "SELECT author.FIRST_NAME AS \"FirstName\" FROM `author` WHERE author.FIRST_NAME = 'Neal'"; + $expectedSQL = $this->getSql("SELECT author.FIRST_NAME AS \"FirstName\" FROM `author` WHERE author.FIRST_NAME = 'Neal'"); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(string) allows for where() statements'); } @@ -113,7 +113,7 @@ public function testSelectStringFindOne() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->select('Title'); $title = $c->findOne($this->con); - $expectedSQL = 'SELECT book.TITLE AS "Title" FROM `book` LIMIT 1'; + $expectedSQL = $this->getSql('SELECT book.TITLE AS "Title" FROM `book` LIMIT 1'); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(string) selects a single column and requests a single row'); $this->assertTrue(is_string($title),'findOne() called after select(string) returns a string'); $this->assertEquals($title, 'Harry Potter and the Order of the Phoenix', 'findOne() called after select(string) returns the column value of the first row matching the query'); @@ -123,13 +123,13 @@ public function testSelectStringFindOne() $c->select('FirstName'); $author = $c->findOne($this->con); $this->assertEquals(count($author), 1, 'findOne() called after select(string) allows for where() statements'); - $expectedSQL = "SELECT author.FIRST_NAME AS \"FirstName\" FROM `author` WHERE author.FIRST_NAME = 'Neal' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT author.FIRST_NAME AS \"FirstName\" FROM `author` WHERE author.FIRST_NAME = 'Neal' LIMIT 1"); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(string) allows for where() statements'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Author'); $c->select(AuthorTableMap::FIRST_NAME); $author = $c->find($this->con); - $expectedSQL = "SELECT author.FIRST_NAME AS \"author.FIRST_NAME\" FROM `author`"; + $expectedSQL = $this->getSql("SELECT author.FIRST_NAME AS \"author.FIRST_NAME\" FROM `author`"); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select(string) accepts model TableMap Constants'); } @@ -160,7 +160,7 @@ public function testSelectStringJoin() $c->select('Title'); $titles = $c->find($this->con); $this->assertEquals($titles->count(), 1, 'find() called after select(string) allows for join() statements'); - $expectedSQL = "SELECT book.TITLE AS \"Title\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal'"; + $expectedSQL = $this->getSql("SELECT book.TITLE AS \"Title\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal'"); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(string) allows for join() statements'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); @@ -176,7 +176,7 @@ public function testSelectStringJoin() $c->select('Title'); $title = $c->findOne($this->con); $this->assertEquals(count($title), 1, 'findOne() called after select(string) allows for join() statements'); - $expectedSQL = "SELECT book.TITLE AS \"Title\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.TITLE AS \"Title\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal' LIMIT 1"); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(string) allows for where() statements'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); @@ -195,7 +195,7 @@ public function testSelectStringWildcard() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->select('*'); $book = $c->findOne($this->con); - $expectedSQL = 'SELECT book.ID AS "Propel\Tests\Bookstore\Book.Id", book.TITLE AS "Propel\Tests\Bookstore\Book.Title", book.ISBN AS "Propel\Tests\Bookstore\Book.ISBN", book.PRICE AS "Propel\Tests\Bookstore\Book.Price", book.PUBLISHER_ID AS "Propel\Tests\Bookstore\Book.PublisherId", book.AUTHOR_ID AS "Propel\Tests\Bookstore\Book.AuthorId" FROM `book` LIMIT 1'; + $expectedSQL = $this->getSql('SELECT book.ID AS "Propel\Tests\Bookstore\Book.Id", book.TITLE AS "Propel\Tests\Bookstore\Book.Title", book.ISBN AS "Propel\Tests\Bookstore\Book.ISBN", book.PRICE AS "Propel\Tests\Bookstore\Book.Price", book.PUBLISHER_ID AS "Propel\Tests\Bookstore\Book.PublisherId", book.AUTHOR_ID AS "Propel\Tests\Bookstore\Book.AuthorId" FROM `book` LIMIT 1'); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select(\'*\') selects all the columns from the main object'); $this->assertTrue(is_array($book), 'findOne() called after select(\'*\') returns an array'); $this->assertEquals(array('Propel\Tests\Bookstore\Book.Id', 'Propel\Tests\Bookstore\Book.Title', 'Propel\Tests\Bookstore\Book.ISBN', 'Propel\Tests\Bookstore\Book.Price', 'Propel\Tests\Bookstore\Book.PublisherId', 'Propel\Tests\Bookstore\Book.AuthorId'), array_keys($book), 'select(\'*\') returns all the columns from the main object, in complete form'); @@ -217,7 +217,7 @@ public function testSelectArrayFind() $c->select(array('FirstName', 'LastName')); $authors = $c->find($this->con); $this->assertEquals($authors->count(), 1, 'find() called after select(array) allows for where() statements'); - $expectedSQL = "SELECT author.FIRST_NAME AS \"FirstName\", author.LAST_NAME AS \"LastName\" FROM `author` WHERE author.FIRST_NAME = 'Neal'"; + $expectedSQL = $this->getSql("SELECT author.FIRST_NAME AS \"FirstName\", author.LAST_NAME AS \"LastName\" FROM `author` WHERE author.FIRST_NAME = 'Neal'"); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) allows for where() statements'); } @@ -231,7 +231,7 @@ public function testSelectArrayFindOne() $c->select(array('FirstName', 'LastName')); $author = $c->findOne($this->con); $this->assertEquals(count($author), 2, 'findOne() called after select(array) allows for where() statements'); - $expectedSQL = "SELECT author.FIRST_NAME AS \"FirstName\", author.LAST_NAME AS \"LastName\" FROM `author` WHERE author.FIRST_NAME = 'Neal' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT author.FIRST_NAME AS \"FirstName\", author.LAST_NAME AS \"LastName\" FROM `author` WHERE author.FIRST_NAME = 'Neal' LIMIT 1"); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(array) allows for where() statements'); } @@ -246,7 +246,7 @@ public function testSelectArrayJoin() $c->select(array('Title', 'ISBN')); $titles = $c->find($this->con); $this->assertEquals($titles->count(), 1, 'find() called after select(array) allows for join() statements'); - $expectedSQL = "SELECT book.TITLE AS \"Title\", book.ISBN AS \"ISBN\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal'"; + $expectedSQL = $this->getSql("SELECT book.TITLE AS \"Title\", book.ISBN AS \"ISBN\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal'"); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) allows for join() statements'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); @@ -262,7 +262,7 @@ public function testSelectArrayJoin() $c->select(array('Title', 'ISBN')); $title = $c->findOne($this->con); $this->assertEquals(count($title), 2, 'findOne() called after select(array) allows for join() statements'); - $expectedSQL = "SELECT book.TITLE AS \"Title\", book.ISBN AS \"ISBN\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.TITLE AS \"Title\", book.ISBN AS \"ISBN\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal' LIMIT 1"); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(array) allows for join() statements'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); @@ -283,7 +283,7 @@ public function testSelectArrayRelation() $c->orderBy('Propel\Tests\Bookstore\Book.Title'); $c->select(array('Propel\Tests\Bookstore\Author.LastName', 'Propel\Tests\Bookstore\Book.Title')); $rows = $c->find($this->con); - $expectedSQL = 'SELECT author.LAST_NAME AS "Propel\Tests\Bookstore\Author.LastName", book.TITLE AS "Propel\Tests\Bookstore\Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.TITLE ASC'; + $expectedSQL = $this->getSql('SELECT author.LAST_NAME AS "Propel\Tests\Bookstore\Author.LastName", book.TITLE AS "Propel\Tests\Bookstore\Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.TITLE ASC'); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select(array) can select columns from several tables (many-to-one)'); $expectedRows = array( @@ -312,7 +312,7 @@ public function testSelectArrayRelation() $c->orderBy('Book.Id'); $c->orderBy('Author.Id'); $rows = $c->find($this->con); - $expectedSQL = 'SELECT author.LAST_NAME AS "Author.LastName", book.TITLE AS "Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.ID ASC,author.ID ASC'; + $expectedSQL = $this->getSql('SELECT author.LAST_NAME AS "Author.LastName", book.TITLE AS "Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.ID ASC,author.ID ASC'); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select(array) can select columns from several tables (many-to-one)'); $expectedRows = array ( @@ -347,7 +347,7 @@ public function testSelectArrayWithColumn() $c->select(array('LowercaseTitle', 'Propel\Tests\Bookstore\Book.Title')); $c->orderBy('Propel\Tests\Bookstore\Book.Title'); $rows = $c->find($this->con); - $expectedSQL = 'SELECT LOWER(book.TITLE) AS LowercaseTitle, book.TITLE AS "Propel\Tests\Bookstore\Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.TITLE ASC'; + $expectedSQL = $this->getSql('SELECT LOWER(book.TITLE) AS LowercaseTitle, book.TITLE AS "Propel\Tests\Bookstore\Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.TITLE ASC'); $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) can cope with a column added with withColumn()'); $expectedRows = array ( diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaTest.php index d6273ee84e..3123332e10 100644 --- a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaTest.php +++ b/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaTest.php @@ -186,7 +186,7 @@ public function testTableAlias() $c->setModelAlias('b'); $c->where('b.Title = ?', 'foo'); - $sql = "SELECT FROM `book` WHERE book.TITLE = :p1"; + $sql = $this->getSql("SELECT FROM `book` WHERE book.TITLE = :p1"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), ); @@ -195,7 +195,7 @@ public function testTableAlias() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->where('b.Title = ?', 'foo'); - $sql = "SELECT FROM `book` WHERE book.TITLE = :p1"; + $sql = $this->getSql("SELECT FROM `book` WHERE book.TITLE = :p1"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), ); @@ -210,11 +210,7 @@ public function testTrueTableAlias() $c->join('b.Author a'); $c->where('a.FirstName = ?', 'john'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT FROM `book` `b` INNER JOIN `author` `a` ON (b.AUTHOR_ID=a.ID) WHERE b.TITLE = :p1 AND a.FIRST_NAME = :p2"; - } else { - $sql = "SELECT FROM book b INNER JOIN author a ON (b.AUTHOR_ID=a.ID) WHERE b.TITLE = :p1 AND a.FIRST_NAME = :p2"; - } + $sql = $this->getSql("SELECT FROM `book` `b` INNER JOIN `author` `a` ON (b.AUTHOR_ID=a.ID) WHERE b.TITLE = :p1 AND a.FIRST_NAME = :p2"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -230,11 +226,7 @@ public function testCondition() $c->condition('cond2', 'Propel\Tests\Bookstore\Book.Title like ?', '%bar%'); $c->combine(array('cond1', 'cond2'), 'or'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT FROM `book` WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)"; - } else { - $sql = "SELECT FROM book WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)"; - } + $sql = $this->getSql("SELECT FROM `book` WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -251,11 +243,7 @@ public function testConditionCustomOperator() $c->condition('cond2', 'title_start like ?', '%bar%', \PDO::PARAM_STR); $c->combine(array('cond1', 'cond2'), 'or'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM `book` WHERE (book.TITLE <> :p1 OR title_start like :p2)"; - } else { - $sql = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM book WHERE (book.TITLE <> :p1 OR title_start like :p2)"; - } + $sql = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM `book` WHERE (book.TITLE <> :p1 OR title_start like :p2)"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -289,11 +277,7 @@ public function testWhere($clause, $value, $sql, $params) $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->where($clause, $value); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = 'SELECT FROM `book` WHERE ' . $sql; - } else { - $sql = 'SELECT FROM book WHERE ' . $sql; - } + $sql = $this->getSql('SELECT FROM `book` WHERE ' . $sql); $this->assertCriteriaTranslation($c, $sql, $params, 'where() accepts a string clause'); } @@ -305,11 +289,7 @@ public function testWhereUsesDefaultOperator() $c->_or(); $c->where('Propel\Tests\Bookstore\Book.Title = ?', 'foo'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = 'SELECT FROM `book` WHERE (book.ID = :p1 OR book.TITLE = :p2)'; - } else { - $sql = 'SELECT FROM book WHERE (book.ID = :p1 OR book.TITLE = :p2)'; - } + $sql = $this->getSql('SELECT FROM `book` WHERE (book.ID = :p1 OR book.TITLE = :p2)'); $params = array( array('table' => 'book', 'column' => 'ID', 'value' => '12'), @@ -330,11 +310,7 @@ public function testWhereTwiceSameColumn() array('table' => 'book', 'column' => 'ID', 'value' => '5'), ); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = 'SELECT FROM `book` WHERE (book.ID IN (:p1,:p2,:p3) AND book.ID <> :p4)'; - } else { - $sql = 'SELECT FROM book WHERE (book.ID IN (:p1,:p2,:p3) AND book.ID <> :p4)'; - } + $sql = $this->getSql('SELECT FROM `book` WHERE (book.ID IN (:p1,:p2,:p3) AND book.ID <> :p4)'); $this->assertCriteriaTranslation($c, $sql, $params, 'where() adds clauses on the same column correctly'); } @@ -346,11 +322,7 @@ public function testWhereConditions() $c->condition('cond2', 'Propel\Tests\Bookstore\Book.Title like ?', '%bar%'); $c->where(array('cond1', 'cond2')); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT FROM `book` WHERE (book.TITLE <> :p1 AND book.TITLE like :p2)"; - } else { - $sql = "SELECT FROM book WHERE (book.TITLE <> :p1 AND book.TITLE like :p2)"; - } + $sql = $this->getSql("SELECT FROM `book` WHERE (book.TITLE <> :p1 AND book.TITLE like :p2)"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -363,11 +335,7 @@ public function testWhereConditions() $c->condition('cond2', 'Propel\Tests\Bookstore\Book.Title like ?', '%bar%'); $c->where(array('cond1', 'cond2'), Criteria::LOGICAL_OR); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT FROM `book` WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)"; - } else { - $sql = "SELECT FROM book WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)"; - } + $sql = $this->getSql("SELECT FROM `book` WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)"); $this->assertCriteriaTranslation($c, $sql, $params, 'where() accepts an array of named conditions with operator'); } @@ -378,11 +346,7 @@ public function testWhereNoReplacement() $c->where('b.Title = ?', 'foo'); $c->where('1=1'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT FROM `book` WHERE book.TITLE = :p1 AND 1=1"; - } else { - $sql = "SELECT FROM book WHERE book.TITLE = :p1 AND 1=1"; - } + $sql = $this->getSql("SELECT FROM `book` WHERE book.TITLE = :p1 AND 1=1"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -403,11 +367,7 @@ public function testWhereFunction() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->where('UPPER(b.Title) = ?', 'foo'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT FROM `book` WHERE UPPER(book.TITLE) = :p1"; - } else { - $sql = "SELECT FROM book WHERE UPPER(book.TITLE) = :p1"; - } + $sql = $this->getSql("SELECT FROM `book` WHERE UPPER(book.TITLE) = :p1"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -415,16 +375,15 @@ public function testWhereFunction() $this->assertCriteriaTranslation($c, $sql, $params, 'where() accepts a complex calculation'); } + /** + * @group mysql + */ public function testWhereTypeValue() { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->where('LOCATE(\'foo\', b.Title) = ?', true, \PDO::PARAM_BOOL); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT FROM `book` WHERE LOCATE('foo', book.TITLE) = :p1"; - } else { - $sql = "SELECT FROM book WHERE LOCATE('foo', book.TITLE) = :p1"; - } + $sql = $this->getSql("SELECT FROM `book` WHERE LOCATE('foo', book.TITLE) = :p1"); $params = array( array('table' => null, 'type' => \PDO::PARAM_BOOL, 'value' => true), @@ -432,11 +391,7 @@ public function testWhereTypeValue() $this->assertCriteriaTranslation($c, $sql, $params, 'where() accepts a complex calculation'); $c->find($this->con); - if (in_array($this->getDriver(), array('mysql'))) { - $expected = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE LOCATE('foo', book.TITLE) = true"; - } else { - $expected = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book WHERE LOCATE('foo', book.TITLE) = true"; - } + $expected = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE LOCATE('foo', book.TITLE) = true"); $this->assertEquals($expected, $this->con->getLastExecutedQuery()); } @@ -447,11 +402,7 @@ public function testOrWhere() $c->where('Propel\Tests\Bookstore\Book.Title <> ?', 'foo'); $c->_or()->where('Propel\Tests\Bookstore\Book.Title like ?', '%bar%'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT FROM `book` WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)"; - } else { - $sql = "SELECT FROM book WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)"; - } + $sql = $this->getSql("SELECT FROM `book` WHERE (book.TITLE <> :p1 OR book.TITLE like :p2)"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -468,11 +419,7 @@ public function testOrWhereConditions() $c->condition('cond2', 'Propel\Tests\Bookstore\Book.Title like ?', '%bar%'); $c->_or()->where(array('cond1', 'cond2')); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT FROM `book` WHERE (book.ID = :p1 OR (book.TITLE <> :p2 AND book.TITLE like :p3))"; - } else { - $sql = "SELECT FROM book WHERE (book.ID = :p1 OR (book.TITLE <> :p2 AND book.TITLE like :p3))"; - } + $sql = $this->getSql("SELECT FROM `book` WHERE (book.ID = :p1 OR (book.TITLE <> :p2 AND book.TITLE like :p3))"); $params = array( array('table' => 'book', 'column' => 'ID', 'value' => 12), @@ -487,11 +434,7 @@ public function testOrWhereConditions() $c->condition('cond2', 'Propel\Tests\Bookstore\Book.Title like ?', '%bar%'); $c->_or()->where(array('cond1', 'cond2'), Criteria::LOGICAL_OR); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT FROM `book` WHERE (book.ID = :p1 OR (book.TITLE <> :p2 OR book.TITLE like :p3))"; - } else { - $sql = "SELECT FROM book WHERE (book.ID = :p1 OR (book.TITLE <> :p2 OR book.TITLE like :p3))"; - } + $sql = $this->getSql("SELECT FROM `book` WHERE (book.ID = :p1 OR (book.TITLE <> :p2 OR book.TITLE like :p3))"); $this->assertCriteriaTranslation($c, $sql, $params, 'orWhere() accepts an array of named conditions with operator'); } @@ -502,11 +445,7 @@ public function testMixedCriteria() $c->where('Propel\Tests\Bookstore\Book.Title = ?', 'foo'); $c->add(BookTableMap::ID, array(1, 2), Criteria::IN); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = 'SELECT FROM `book` WHERE book.TITLE = :p1 AND book.ID IN (:p2,:p3)'; - } else { - $sql = 'SELECT FROM book WHERE book.TITLE = :p1 AND book.ID IN (:p2,:p3)'; - } + $sql = $this->getSql('SELECT FROM `book` WHERE book.TITLE = :p1 AND book.ID IN (:p2,:p3)'); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -521,11 +460,7 @@ public function testFilterBy() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->filterBy('Title', 'foo'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = 'SELECT FROM `book` WHERE book.TITLE=:p1'; - } else { - $sql = 'SELECT FROM book WHERE book.TITLE=:p1'; - } + $sql = $this->getSql('SELECT FROM `book` WHERE book.TITLE=:p1'); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -535,11 +470,7 @@ public function testFilterBy() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->filterBy('Title', 'foo', Criteria::NOT_EQUAL); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = 'SELECT FROM `book` WHERE book.TITLE<>:p1'; - } else { - $sql = 'SELECT FROM book WHERE book.TITLE<>:p1'; - } + $sql = $this->getSql('SELECT FROM `book` WHERE book.TITLE<>:p1'); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -549,11 +480,7 @@ public function testFilterBy() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->filterBy('Title', 'foo'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = 'SELECT FROM `book` WHERE book.TITLE=:p1'; - } else { - $sql = 'SELECT FROM book WHERE book.TITLE=:p1'; - } + $sql = $this->getSql('SELECT FROM `book` WHERE book.TITLE=:p1'); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'foo'), @@ -596,17 +523,16 @@ public function testHavingConditions() $this->assertCriteriaTranslation($c, $sql, $params, 'having() accepts an array of named conditions with an operator'); } + /** + * @group mysql + */ public function testHavingWithColumn() { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->withColumn('SUBSTRING(Book.Title, 1, 4)', 'title_start'); $c->having('title_start = ?', 'foo', \PDO::PARAM_STR); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM `book` HAVING title_start = :p1'; - } else { - $sql = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM book HAVING title_start = :p1'; - } + $sql = $this->getSql('SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM `book` HAVING title_start = :p1'); $params = array( array('table' => null, 'type' => 2, 'value' => 'foo'), @@ -614,11 +540,7 @@ public function testHavingWithColumn() $this->assertCriteriaTranslation($c, $sql, $params, 'having() accepts a string clause'); $c->find($this->con); - if (in_array($this->getDriver(), array('mysql'))) { - $expected = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM `book` HAVING title_start = \'foo\''; - } else { - $expected = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM book HAVING title_start = \'foo\''; - } + $expected = $this->getSql('SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, SUBSTRING(book.TITLE, 1, 4) AS title_start FROM `book` HAVING title_start = \'foo\''); $this->assertEquals($expected, $this->con->getLastExecutedQuery()); } @@ -760,7 +682,7 @@ public function testGroupByClassJoinedModel() $c->join('Propel\Tests\Bookstore\Author.Book'); $c->groupByClass('Book'); - $sql = 'SELECT FROM `author` INNER JOIN `book` ON (author.ID=book.AUTHOR_ID) GROUP BY book.ID,book.TITLE,book.ISBN,book.PRICE,book.PUBLISHER_ID,book.AUTHOR_ID'; + $sql = $this->getSql('SELECT FROM `author` INNER JOIN `book` ON (author.ID=book.AUTHOR_ID) GROUP BY book.ID,book.TITLE,book.ISBN,book.PRICE,book.PUBLISHER_ID,book.AUTHOR_ID'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'groupByClass() accepts the class name of a joined model'); } @@ -771,7 +693,7 @@ public function testGroupByClassJoinedModelWithAlias() $c->join('Propel\Tests\Bookstore\Author.Book b'); $c->groupByClass('b'); - $sql = 'SELECT FROM `author` INNER JOIN `book` `b` ON (author.ID=b.AUTHOR_ID) GROUP BY b.ID,b.TITLE,b.ISBN,b.PRICE,b.PUBLISHER_ID,b.AUTHOR_ID'; + $sql = $this->getSql('SELECT FROM `author` INNER JOIN `book` `b` ON (author.ID=b.AUTHOR_ID) GROUP BY b.ID,b.TITLE,b.ISBN,b.PRICE,b.PUBLISHER_ID,b.AUTHOR_ID'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'groupByClass() accepts the alias of a joined model'); } @@ -799,7 +721,11 @@ public function testOffset() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->limit(50); $c->offset(10); - $sql = 'SELECT FROM LIMIT 10, 50'; + if ($this->isDb('mysql')) { + $sql = 'SELECT FROM LIMIT 10, 50'; + } else { + $sql = 'SELECT FROM LIMIT 50 OFFSET 10'; + } $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'offset() adds an OFFSET clause'); } @@ -809,7 +735,7 @@ public function testAddJoin() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->addJoin(BookTableMap::AUTHOR_ID, AuthorTableMap::ID); $c->addJoin(BookTableMap::PUBLISHER_ID, PublisherTableMap::ID); - $sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) INNER JOIN `publisher` ON (book.PUBLISHER_ID=publisher.ID)'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) INNER JOIN `publisher` ON (book.PUBLISHER_ID=publisher.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'addJoin() works the same as in Criteria'); } @@ -818,7 +744,7 @@ public function testJoin() { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author'); - $sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() uses a relation to guess the columns'); @@ -833,7 +759,7 @@ public function testJoin() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author'); $c->where('Author.FirstName = ?', 'Leo'); - $sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = :p1'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = :p1'); $params = array( array('table' => 'author', 'column' => 'FIRST_NAME', 'value' => 'Leo'), ); @@ -842,7 +768,7 @@ public function testJoin() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Author'); $c->where('Author.FirstName = ?', 'Leo'); - $sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = :p1'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = :p1'); $params = array( array('table' => 'author', 'column' => 'FIRST_NAME', 'value' => 'Leo'), ); @@ -859,7 +785,7 @@ public function testJoinQuery() $c->join('Propel\Tests\Bookstore\Book.Author'); $c->where('Author.FirstName = ?', 'Neal'); $books = BookQuery::create(null, $c)->find(); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal'"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() issues a real JOIN query'); $this->assertEquals(1, count($books), 'join() issues a real JOIN query'); } @@ -868,7 +794,7 @@ public function testJoinRelationName() { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\BookstoreEmployee'); $c->join('Propel\Tests\Bookstore\BookstoreEmployee.Supervisor'); - $sql = 'SELECT FROM INNER JOIN `bookstore_employee` ON (bookstore_employee.SUPERVISOR_ID=bookstore_employee.ID)'; + $sql = $this->getSql('SELECT FROM INNER JOIN `bookstore_employee` ON (bookstore_employee.SUPERVISOR_ID=bookstore_employee.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() uses relation names as defined in schema.xml'); } @@ -877,7 +803,7 @@ public function testJoinComposite() { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\ReaderFavorite'); $c->join('Propel\Tests\Bookstore\ReaderFavorite.BookOpinion'); - $sql = 'SELECT FROM `reader_favorite` INNER JOIN `book_opinion` ON (reader_favorite.BOOK_ID=book_opinion.BOOK_ID AND reader_favorite.READER_ID=book_opinion.READER_ID)'; + $sql = $this->getSql('SELECT FROM `reader_favorite` INNER JOIN `book_opinion` ON (reader_favorite.BOOK_ID=book_opinion.BOOK_ID AND reader_favorite.READER_ID=book_opinion.READER_ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() knows how to create a JOIN clause for relationships with composite fkeys'); } @@ -886,31 +812,31 @@ public function testJoinType() { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author'); - $sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() adds an INNER JOIN by default'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author', Criteria::INNER_JOIN); - $sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() adds an INNER JOIN by default'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author', Criteria::LEFT_JOIN); - $sql = 'SELECT FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() can add a LEFT JOIN'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author', Criteria::RIGHT_JOIN); - $sql = 'SELECT FROM `book` RIGHT JOIN `author` ON (book.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` RIGHT JOIN `author` ON (book.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() can add a RIGHT JOIN'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author', 'incorrect join'); - $sql = 'SELECT FROM `book` incorrect join `author` ON (book.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` incorrect join `author` ON (book.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() accepts any join string'); } @@ -919,25 +845,25 @@ public function testJoinDirection() { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author'); - $sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() adds a JOIN clause correctly for many to one relationship'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Author'); $c->join('Propel\Tests\Bookstore\Author.Book'); - $sql = 'SELECT FROM `author` INNER JOIN `book` ON (author.ID=book.AUTHOR_ID)'; + $sql = $this->getSql('SELECT FROM `author` INNER JOIN `book` ON (author.ID=book.AUTHOR_ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() adds a JOIN clause correctly for one to many relationship'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\BookstoreEmployee'); $c->join('Propel\Tests\Bookstore\BookstoreEmployee.BookstoreEmployeeAccount'); - $sql = 'SELECT FROM `bookstore_employee` INNER JOIN `bookstore_employee_account` ON (bookstore_employee.ID=bookstore_employee_account.EMPLOYEE_ID)'; + $sql = $this->getSql('SELECT FROM `bookstore_employee` INNER JOIN `bookstore_employee_account` ON (bookstore_employee.ID=bookstore_employee_account.EMPLOYEE_ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() adds a JOIN clause correctly for one to one relationship'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\BookstoreEmployeeAccount'); $c->join('Propel\Tests\Bookstore\BookstoreEmployeeAccount.BookstoreEmployee'); - $sql = 'SELECT FROM `bookstore_employee_account` INNER JOIN `bookstore_employee` ON (bookstore_employee_account.EMPLOYEE_ID=bookstore_employee.ID)'; + $sql = $this->getSql('SELECT FROM `bookstore_employee_account` INNER JOIN `bookstore_employee` ON (bookstore_employee_account.EMPLOYEE_ID=bookstore_employee.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() adds a JOIN clause correctly for one to one relationship'); } @@ -948,7 +874,7 @@ public function testJoinSeveral() $c->join('Propel\Tests\Bookstore\Author.Book'); $c->join('Book.Publisher'); $c->where('Publisher.Name = ?', 'foo'); - $sql = 'SELECT FROM `author` INNER JOIN `book` ON (author.ID=book.AUTHOR_ID) INNER JOIN `publisher` ON (book.PUBLISHER_ID=publisher.ID) WHERE publisher.NAME = :p1'; + $sql = $this->getSql('SELECT FROM `author` INNER JOIN `book` ON (author.ID=book.AUTHOR_ID) INNER JOIN `publisher` ON (book.PUBLISHER_ID=publisher.ID) WHERE publisher.NAME = :p1'); $params = array( array('table' => 'publisher', 'column' => 'NAME', 'value' => 'foo'), ); @@ -959,25 +885,25 @@ public function testJoinAlias() { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->join('b.Author'); - $sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() supports relation on main alias'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->join('Author'); - $sql = 'SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() can use a simple relation name when the model has an alias'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author a'); - $sql = 'SELECT FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID)'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() supports relation alias'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->join('b.Author a'); - $sql = 'SELECT FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID)'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() supports relation alias on main alias'); @@ -985,7 +911,7 @@ public function testJoinAlias() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->join('b.Author a'); $c->where('a.FirstName = ?', 'Leo'); - $sql = 'SELECT FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = :p1'; + $sql = $this->getSql('SELECT FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = :p1'); $params = array( array('table' => 'author', 'column' => 'FIRST_NAME', 'value' => 'Leo'), ); @@ -995,7 +921,7 @@ public function testJoinAlias() $c->join('a.Book b'); $c->join('b.Publisher p'); $c->where('p.Name = ?', 'foo'); - $sql = 'SELECT FROM `author` INNER JOIN `book` `b` ON (author.ID=b.AUTHOR_ID) INNER JOIN `publisher` `p` ON (b.PUBLISHER_ID=p.ID) WHERE p.NAME = :p1'; + $sql = $this->getSql('SELECT FROM `author` INNER JOIN `book` `b` ON (author.ID=b.AUTHOR_ID) INNER JOIN `publisher` `p` ON (b.PUBLISHER_ID=p.ID) WHERE p.NAME = :p1'); $params = array( array('table' => 'publisher', 'column' => 'NAME', 'value' => 'foo'), ); @@ -1007,14 +933,14 @@ public function testJoinTrueTableAlias() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->setModelAlias('b', true); $c->join('b.Author'); - $sql = 'SELECT FROM `book` `b` INNER JOIN `author` ON (b.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` `b` INNER JOIN `author` ON (b.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() supports relation on true table alias'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->setModelAlias('b', true); $c->join('Author'); - $sql = 'SELECT FROM `book` `b` INNER JOIN `author` ON (b.AUTHOR_ID=author.ID)'; + $sql = $this->getSql('SELECT FROM `book` `b` INNER JOIN `author` ON (b.AUTHOR_ID=author.ID)'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'join() supports relation without alias name on true table alias'); } @@ -1025,7 +951,7 @@ public function testJoinOnSameTable() $c->join('be.Supervisor sup'); $c->join('sup.Subordinate sub'); $c->where('sub.Name = ?', 'Foo'); - $sql = 'SELECT FROM `bookstore_employee` INNER JOIN `bookstore_employee` `sup` ON (bookstore_employee.SUPERVISOR_ID=sup.ID) INNER JOIN `bookstore_employee` `sub` ON (sup.ID=sub.SUPERVISOR_ID) WHERE sub.NAME = :p1'; + $sql = $this->getSql('SELECT FROM `bookstore_employee` INNER JOIN `bookstore_employee` `sup` ON (bookstore_employee.SUPERVISOR_ID=sup.ID) INNER JOIN `bookstore_employee` `sub` ON (sup.ID=sub.SUPERVISOR_ID) WHERE sub.NAME = :p1'); $params = array( array('table' => 'bookstore_employee', 'column' => 'NAME', 'value' => 'Foo'), ); @@ -1039,7 +965,7 @@ public function testJoinAliasQuery() $c->join('b.Author a'); $c->where('a.FirstName = ?', 'Leo'); $books = BookQuery::create(null, $c)->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo'"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in where()'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\BookstoreEmployee', 'be'); @@ -1047,7 +973,7 @@ public function testJoinAliasQuery() $c->join('sup.Subordinate sub'); $c->where('sub.Name = ?', 'Foo'); $employees = BookstoreEmployeeQuery::create(null, $c)->find($con); - $expectedSQL = "SELECT bookstore_employee.ID, bookstore_employee.CLASS_KEY, bookstore_employee.NAME, bookstore_employee.JOB_TITLE, bookstore_employee.SUPERVISOR_ID FROM `bookstore_employee` INNER JOIN `bookstore_employee` `sup` ON (bookstore_employee.SUPERVISOR_ID=sup.ID) INNER JOIN `bookstore_employee` `sub` ON (sup.ID=sub.SUPERVISOR_ID) WHERE sub.NAME = 'Foo'"; + $expectedSQL = $this->getSql("SELECT bookstore_employee.ID, bookstore_employee.CLASS_KEY, bookstore_employee.NAME, bookstore_employee.JOB_TITLE, bookstore_employee.SUPERVISOR_ID FROM `bookstore_employee` INNER JOIN `bookstore_employee` `sup` ON (bookstore_employee.SUPERVISOR_ID=sup.ID) INNER JOIN `bookstore_employee` `sub` ON (sup.ID=sub.SUPERVISOR_ID) WHERE sub.NAME = 'Foo'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in further joins()'); } @@ -1058,7 +984,7 @@ public function testAddJoinConditionSimple() $c->join('Propel\Tests\Bookstore\Book.Author', Criteria::INNER_JOIN); $c->addJoinCondition('Author', 'Propel\Tests\Bookstore\Book.Title IS NOT NULL'); $books = BookQuery::create(null, $c)->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID AND book.TITLE IS NOT NULL)"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID AND book.TITLE IS NOT NULL)"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of custom conditions'); } @@ -1069,7 +995,7 @@ public function testAddJoinConditionBinding() $c->join('Propel\Tests\Bookstore\Book.Author', Criteria::INNER_JOIN); $c->addJoinCondition('Author', 'Propel\Tests\Bookstore\Book.Title = ?', 'foo'); $books = BookQuery::create(null, $c)->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID AND book.TITLE = 'foo')"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID AND book.TITLE = 'foo')"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of custom conditions with values to bind'); } @@ -1081,7 +1007,7 @@ public function testAddJoinConditionSeveral() $c->addJoinCondition('Author', 'Propel\Tests\Bookstore\Book.Title = ?', 'foo'); $c->addJoinCondition('Author', 'Propel\Tests\Bookstore\Book.ISBN IS NOT NULL'); $books = BookQuery::create(null, $c)->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON ((book.AUTHOR_ID=author.ID AND book.TITLE = 'foo') AND book.ISBN IS NOT NULL)"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON ((book.AUTHOR_ID=author.ID AND book.TITLE = 'foo') AND book.ISBN IS NOT NULL)"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of several custom conditions'); } @@ -1093,7 +1019,7 @@ public function testAddJoinConditionBindingAndWhere() $c->join('Propel\Tests\Bookstore\Book.Author', Criteria::INNER_JOIN); $c->addJoinCondition('Author', 'Propel\Tests\Bookstore\Book.Title = ?', 'foo'); $books = BookQuery::create(null, $c)->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID AND book.TITLE = 'foo') WHERE book.TITLE LIKE 'foo%'"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID AND book.TITLE = 'foo') WHERE book.TITLE LIKE 'foo%'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of custom conditions with values and lives well with WHERE conditions'); } @@ -1104,7 +1030,7 @@ public function testAddJoinConditionAlias() $c->join('Propel\Tests\Bookstore\Book.Author a', Criteria::INNER_JOIN); $c->addJoinCondition('a', 'Book.Title IS NOT NULL'); $books = BookQuery::create(null, $c)->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID AND book.TITLE IS NOT NULL)"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID AND book.TITLE IS NOT NULL)"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of custom conditions even on aliased relations'); } @@ -1115,7 +1041,7 @@ public function testAddJoinConditionOperator() $c->join('Propel\Tests\Bookstore\Book.Author', Criteria::INNER_JOIN); $c->addJoinCondition('Author', 'Propel\Tests\Bookstore\Book.Title IS NOT NULL', null, Criteria::LOGICAL_OR); $books = BookQuery::create(null, $c)->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID OR book.TITLE IS NOT NULL)"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID OR book.TITLE IS NOT NULL)"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'addJoinCondition() allows the use of custom conditions with a custom operator'); } @@ -1127,7 +1053,7 @@ public function testSetJoinConditionCriterion() $criterion = $c->getNewCriterion(BookTableMap::TITLE, BookTableMap::TITLE . ' = ' . AuthorTableMap::FIRST_NAME, Criteria::CUSTOM); $c->setJoinCondition('Author', $criterion); $books = BookQuery::create(null, $c)->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON book.TITLE = author.FIRST_NAME"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON book.TITLE = author.FIRST_NAME"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'setJoinCondition() can override a previous join condition with a Criterion'); } @@ -1139,7 +1065,7 @@ public function testSetJoinConditionNamedCondition() $c->condition('cond1', 'Propel\Tests\Bookstore\Book.Title = Author.FirstName'); $c->setJoinCondition('Author', 'cond1'); $books = BookQuery::create(null, $c)->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON book.TITLE = author.FIRST_NAME"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` ON book.TITLE = author.FIRST_NAME"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'setJoinCondition() can override a previous join condition with a named condition'); } @@ -1432,7 +1358,7 @@ public function testWithColumn($clause, $alias, $selectTranslation) { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->withColumn($clause, $alias); - $sql = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, ' . $selectTranslation . ' FROM `book`'; + $sql = $this->getSql('SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, ' . $selectTranslation . ' FROM `book`'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() adds a calculated column to the select clause'); } @@ -1460,7 +1386,7 @@ public function testWithColumnAndQuotes($clause, $alias, $selectTranslation) { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Author'); $c->withColumn($clause, $alias); - $sql = 'SELECT author.ID, author.FIRST_NAME, author.LAST_NAME, author.EMAIL, author.AGE, ' . $selectTranslation . ' FROM `author`'; + $sql = $this->getSql('SELECT author.ID, author.FIRST_NAME, author.LAST_NAME, author.EMAIL, author.AGE, ' . $selectTranslation . ' FROM `author`'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() adds a calculated column using quotes to the select clause'); } @@ -1469,14 +1395,14 @@ public function testWithColumnAndSelectColumns() { $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->withColumn('UPPER(Propel\Tests\Bookstore\Book.Title)', 'foo'); - $sql = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, UPPER(book.TITLE) AS foo FROM `book`'; + $sql = $this->getSql('SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, UPPER(book.TITLE) AS foo FROM `book`'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() adds the object columns if the criteria has no select columns'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->addSelectColumn('book.ID'); $c->withColumn('UPPER(Propel\Tests\Bookstore\Book.Title)', 'foo'); - $sql = 'SELECT book.ID, UPPER(book.TITLE) AS foo FROM `book`'; + $sql = $this->getSql('SELECT book.ID, UPPER(book.TITLE) AS foo FROM `book`'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() does not add the object columns if the criteria already has select columns'); @@ -1484,7 +1410,7 @@ public function testWithColumnAndSelectColumns() $c->addSelectColumn('book.ID'); $c->withColumn('UPPER(Propel\Tests\Bookstore\Book.Title)', 'foo'); $c->addSelectColumn('book.TITLE'); - $sql = 'SELECT book.ID, book.TITLE, UPPER(book.TITLE) AS foo FROM `book`'; + $sql = $this->getSql('SELECT book.ID, book.TITLE, UPPER(book.TITLE) AS foo FROM `book`'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() does adds as column after the select columns even though the withColumn() method was called first'); @@ -1492,7 +1418,7 @@ public function testWithColumnAndSelectColumns() $c->addSelectColumn('book.ID'); $c->withColumn('UPPER(Propel\Tests\Bookstore\Book.Title)', 'foo'); $c->withColumn('UPPER(Propel\Tests\Bookstore\Book.ISBN)', 'isbn'); - $sql = 'SELECT book.ID, UPPER(book.TITLE) AS foo, UPPER(book.ISBN) AS isbn FROM `book`'; + $sql = $this->getSql('SELECT book.ID, UPPER(book.TITLE) AS foo, UPPER(book.ISBN) AS isbn FROM `book`'); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() called repeatedly adds several as columns'); } @@ -1587,7 +1513,7 @@ public function testFindAddsSelectColumns() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $books = $c->find($con); - $sql = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`"; + $sql = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`"); $this->assertEquals($sql, $con->getLastExecutedQuery(), 'find() adds the select columns of the current model'); } @@ -1597,7 +1523,7 @@ public function testFindTrueAliasAddsSelectColumns() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->setModelAlias('b', true); $books = $c->find($con); - $sql = "SELECT b.ID, b.TITLE, b.ISBN, b.PRICE, b.PUBLISHER_ID, b.AUTHOR_ID FROM `book` `b`"; + $sql = $this->getSql("SELECT b.ID, b.TITLE, b.ISBN, b.PRICE, b.PUBLISHER_ID, b.AUTHOR_ID FROM `book` `b`"); $this->assertEquals($sql, $con->getLastExecutedQuery(), 'find() uses the true model alias if available'); } @@ -1842,7 +1768,7 @@ public function testFindBy() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $books = $c->findBy('Title', 'Don Juan', $con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan'"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findBy() adds simple column conditions'); $this->assertTrue($books instanceof Collection, 'findBy() issues a find()'); $this->assertEquals(1, count($books), 'findBy() adds simple column conditions'); @@ -1856,7 +1782,7 @@ public function testFindByArray() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $books = $c->findByArray(array('Title' => 'Don Juan', 'ISBN' => 12345), $con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' AND book.ISBN=12345"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' AND book.ISBN=12345"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByArray() adds multiple column conditions'); } @@ -1873,7 +1799,7 @@ public function testFindOneBy() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $book = $c->findOneBy('Title', 'Don Juan', $con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findOneBy() adds simple column conditions'); $this->assertTrue($book instanceof Book, 'findOneBy() returns a Model object by default'); $this->assertEquals('Don Juan', $book->getTitle(), 'findOneBy() returns the model object matching the query'); @@ -1884,7 +1810,7 @@ public function testFindOneByArray() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $book = $c->findOneByArray(array('Title' => 'Don Juan', 'ISBN' => 12345), $con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' AND book.ISBN=12345 LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' AND book.ISBN=12345 LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findOneBy() adds multiple column conditions'); } @@ -2055,14 +1981,18 @@ public function testDeleteUsingTableAlias() $c->setModelAlias('b', false); $c->where('b.Title = ?', 'foo'); $c->delete(); - $expectedSQL = "DELETE FROM `book` WHERE book.TITLE = 'foo'"; + $expectedSQL = $this->getSql("DELETE FROM `book` WHERE book.TITLE = 'foo'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'delete() also works on tables with table alias'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->setModelAlias('b', true); $c->where('b.Title = ?', 'foo'); $c->delete(); - $expectedSQL = "DELETE b FROM `book` AS b WHERE b.TITLE = 'foo'"; + if ($this->isDb('pgsql')) { + $expectedSQL = $this->getSql("DELETE FROM `book` AS b WHERE b.TITLE = 'foo'"); + } else { + $expectedSQL = $this->getSql("DELETE b FROM `book` AS b WHERE b.TITLE = 'foo'"); + } $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'delete() also works on tables with true table alias'); } @@ -2126,14 +2056,14 @@ public function testUpdateUsingTableAlias() $c->setModelAlias('b', false); $c->where('b.Title = ?', 'foo'); $c->update(array('Title' => 'foo2'), $con); - $expectedSQL = "UPDATE `book` SET `TITLE`='foo2' WHERE book.TITLE = 'foo'"; + $expectedSQL = $this->getSql("UPDATE `book` SET `TITLE`='foo2' WHERE book.TITLE = 'foo'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'update() also works on tables with table alias'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->setModelAlias('b', true); $c->where('b.Title = ?', 'foo'); $c->update(array('Title' => 'foo2'), $con); - $expectedSQL = "UPDATE `book` `b` SET `TITLE`='foo2' WHERE b.TITLE = 'foo'"; + $expectedSQL = $this->getSql("UPDATE `book` `b` SET `TITLE`='foo2' WHERE b.TITLE = 'foo'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'update() also works on tables with true table alias'); } @@ -2210,49 +2140,49 @@ public function testMagicJoin() $c->leftJoin('b.Author a'); $c->where('a.FirstName = ?', 'Leo'); $books = $c->findOne($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'leftJoin($x) is turned into join($x, Criteria::LEFT_JOIN)'); $books = BookQuery::create() ->leftJoinAuthor('a') ->where('a.FirstName = ?', 'Leo') ->findOne($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'leftJoinX() is turned into join($x, Criteria::LEFT_JOIN)'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->innerJoin('b.Author a'); $c->where('a.FirstName = ?', 'Leo'); $books = $c->findOne($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'innerJoin($x) is turned into join($x, Criteria::INNER_JOIN)'); $books = BookQuery::create() ->innerJoinAuthor('a') ->where('a.FirstName = ?', 'Leo') ->findOne($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'innerJoinX() is turned into join($x, Criteria::INNER_JOIN)'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book', 'b'); $c->rightJoin('b.Author a'); $c->where('a.FirstName = ?', 'Leo'); $books = $c->findOne($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` RIGHT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` RIGHT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'rightJoin($x) is turned into join($x, Criteria::RIGHT_JOIN)'); $books = BookQuery::create() ->rightJoinAuthor('a') ->where('a.FirstName = ?', 'Leo') ->findOne($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` RIGHT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` RIGHT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'rightJoinX() is turned into join($x, Criteria::RIGHT_JOIN)'); $books = BookQuery::create() ->leftJoinAuthor() ->where('Author.FirstName = ?', 'Leo') ->findOne($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Leo' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Leo' LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'leftJoinX() is turned into join($x, Criteria::LEFT_JOIN)'); } @@ -2331,22 +2261,22 @@ public function testMagicFind() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $books = $c->findByTitle('Don Juan'); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan'"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXX($value) is turned into findBy(XXX, $value)'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $books = $c->findByTitleAndISBN('Don Juan', 1234); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' AND book.ISBN=1234"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' AND book.ISBN=1234"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXXAndYYY($value) is turned into findBy(array(XXX,YYY), $value)'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $book = $c->findOneByTitle('Don Juan'); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findOneByXXX($value) is turned into findOneBy(XXX, $value)'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $book = $c->findOneByTitleAndISBN('Don Juan', 1234); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' AND book.ISBN=1234 LIMIT 1"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan' AND book.ISBN=1234 LIMIT 1"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findOneByXXX($value) is turned into findOneBy(XXX, $value)'); } @@ -2356,7 +2286,7 @@ public function testMagicFilterBy() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $books = $c->filterByTitle('Don Juan')->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan'"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE='Don Juan'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'filterByXXX($value) is turned into filterBy(XXX, $value)'); } @@ -2366,12 +2296,12 @@ public function testMagicOrderBy() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $books = $c->orderByTitle()->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` ORDER BY book.TITLE ASC"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` ORDER BY book.TITLE ASC"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'orderByXXX() is turned into orderBy(XXX)'); $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $books = $c->orderByTitle(Criteria::DESC)->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` ORDER BY book.TITLE DESC"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` ORDER BY book.TITLE DESC"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'orderByXXX($direction) is turned into orderBy(XXX, $direction)'); } @@ -2381,7 +2311,13 @@ public function testMagicGroupBy() $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $books = $c->groupByTitle()->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` GROUP BY book.TITLE"; + + if ($this->isDb('pgsql')) { + $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book GROUP BY book.TITLE,book.ID,book.ISBN,book.PRICE,book.PUBLISHER_ID,book.AUTHOR_ID"; + } else { + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` GROUP BY book.TITLE"); + } + $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'groupByXXX() is turned into groupBy(XXX)'); } @@ -2405,7 +2341,14 @@ public function testUseQuery() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE book.TITLE = 'foo' AND author.FIRST_NAME = 'john' LIMIT 10, 5"; + + if ($this->isDb('pgsql')) { + $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book LEFT JOIN author ON (book.AUTHOR_ID=author.ID) WHERE book.TITLE = 'foo' AND author.FIRST_NAME = 'john' LIMIT 5 OFFSET 10"; + } else { + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE book.TITLE = 'foo' AND author.FIRST_NAME = 'john' LIMIT 10, 5"); + } + + $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'useQuery() and endUse() allow to merge a secondary criteria'); } @@ -2430,7 +2373,13 @@ public function testUseQueryAlias() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE book.TITLE = 'foo' AND a.FIRST_NAME = 'john' LIMIT 10, 5"; + + if ($this->isDb('pgsql')) { + $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book LEFT JOIN author a ON (book.AUTHOR_ID=a.ID) WHERE book.TITLE = 'foo' AND a.FIRST_NAME = 'john' LIMIT 5 OFFSET 10"; + } else { + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE book.TITLE = 'foo' AND a.FIRST_NAME = 'john' LIMIT 10, 5"); + } + $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'useQuery() and endUse() allow to merge a secondary criteria'); } @@ -2449,7 +2398,7 @@ public function testUseQueryCustomClass() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c->find($con); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE book.TITLE = 'foo' AND a.FIRST_NAME IS NOT NULL AND a.LAST_NAME IS NOT NULL LIMIT 10"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE book.TITLE = 'foo' AND a.FIRST_NAME IS NOT NULL AND a.LAST_NAME IS NOT NULL LIMIT 10"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'useQuery() and endUse() allow to merge a custom secondary criteria'); } @@ -2472,11 +2421,7 @@ public function testUseQueryJoinWithFind() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c->find($con); - if (in_array($this->getDriver(), array('mysql'))) { - $expectedSQL = "SELECT review.ID, review.REVIEWED_BY, review.REVIEW_DATE, review.RECOMMENDED, review.STATUS, review.BOOK_ID, book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, author.ID, author.FIRST_NAME, author.LAST_NAME, author.EMAIL, author.AGE FROM `review` INNER JOIN `book` ON (review.BOOK_ID=book.ID) INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)"; - } else { - $expectedSQL = "SELECT review.ID, review.REVIEWED_BY, review.REVIEW_DATE, review.RECOMMENDED, review.STATUS, review.BOOK_ID, book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, author.ID, author.FIRST_NAME, author.LAST_NAME, author.EMAIL, author.AGE FROM review INNER JOIN book ON (review.BOOK_ID=book.ID) INNER JOIN author ON (book.AUTHOR_ID=author.ID)"; - } + $expectedSQL = $this->getSql("SELECT review.ID, review.REVIEWED_BY, review.REVIEW_DATE, review.RECOMMENDED, review.STATUS, review.BOOK_ID, book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, author.ID, author.FIRST_NAME, author.LAST_NAME, author.EMAIL, author.AGE FROM `review` INNER JOIN `book` ON (review.BOOK_ID=book.ID) INNER JOIN `author` ON (book.AUTHOR_ID=author.ID)"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'useQuery() and joinWith() can be used together and form a correct query'); } @@ -2497,11 +2442,7 @@ public function testUseQueryCustomRelationPhpName() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c->find($con); - if (in_array($this->getDriver(), array('mysql'))) { - $expectedSQL = "SELECT bookstore_contest.BOOKSTORE_ID, bookstore_contest.CONTEST_ID, bookstore_contest.PRIZE_BOOK_ID FROM `bookstore_contest` LEFT JOIN `book` ON (bookstore_contest.PRIZE_BOOK_ID=book.ID) WHERE book.TITLE = 'War And Peace'"; - } else { - $expectedSQL = "SELECT bookstore_contest.BOOKSTORE_ID, bookstore_contest.CONTEST_ID, bookstore_contest.PRIZE_BOOK_ID FROM bookstore_contest LEFT JOIN book ON (bookstore_contest.PRIZE_BOOK_ID=book.ID) WHERE book.TITLE = 'War And Peace'"; - } + $expectedSQL = $this->getSql("SELECT bookstore_contest.BOOKSTORE_ID, bookstore_contest.CONTEST_ID, bookstore_contest.PRIZE_BOOK_ID FROM `bookstore_contest` LEFT JOIN `book` ON (bookstore_contest.PRIZE_BOOK_ID=book.ID) WHERE book.TITLE = 'War And Peace'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'useQuery() and endUse() allow to merge a secondary criteria'); } @@ -2522,11 +2463,7 @@ public function testUseQueryCustomRelationPhpNameAndAlias() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c->find($con); - if (in_array($this->getDriver(), array('mysql'))) { - $expectedSQL = "SELECT bookstore_contest.BOOKSTORE_ID, bookstore_contest.CONTEST_ID, bookstore_contest.PRIZE_BOOK_ID FROM `bookstore_contest` LEFT JOIN `book` `w` ON (bookstore_contest.PRIZE_BOOK_ID=w.ID) WHERE w.TITLE = 'War And Peace'"; - } else { - $expectedSQL = "SELECT bookstore_contest.BOOKSTORE_ID, bookstore_contest.CONTEST_ID, bookstore_contest.PRIZE_BOOK_ID FROM bookstore_contest LEFT JOIN book w ON (bookstore_contest.PRIZE_BOOK_ID=w.ID) WHERE w.TITLE = 'War And Peace'"; - } + $expectedSQL = $this->getSql("SELECT bookstore_contest.BOOKSTORE_ID, bookstore_contest.CONTEST_ID, bookstore_contest.PRIZE_BOOK_ID FROM `bookstore_contest` LEFT JOIN `book` `w` ON (bookstore_contest.PRIZE_BOOK_ID=w.ID) WHERE w.TITLE = 'War And Peace'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'useQuery() and endUse() allow to merge a secondary criteria'); } @@ -2674,11 +2611,7 @@ public function testCloneCopiesConditions() $params = array(); $sql = $bookQuery1->createSelectSql($params); - if (in_array($this->getDriver(), array('mysql'))) { - $expected = 'SELECT FROM `book` WHERE book.PRICE=:p1'; - } else { - $expected = 'SELECT FROM book WHERE book.PRICE=:p1'; - } + $expected = $this->getSql('SELECT FROM `book` WHERE book.PRICE=:p1'); $this->assertEquals($expected, $sql, 'conditions applied on a cloned query don\'t get applied on the original query'); } @@ -2713,11 +2646,7 @@ public function testMagicFindByObject() $q = BookQuery::create() ->findByAuthor($testAuthor); - if (in_array($this->getDriver(), array('mysql'))) { - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=" . $testAuthor->getId(); - } else { - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book WHERE book.AUTHOR_ID=" . $testAuthor->getId(); - } + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=" . $testAuthor->getId()); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXX($value) is turned into findBy(XXX, $value)'); @@ -2725,7 +2654,7 @@ public function testMagicFindByObject() $testAuthor = $c->findOne(); $q = BookQuery::create() ->findByAuthorAndISBN($testAuthor, 1234); - $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=" . $testAuthor->getId() . " AND book.ISBN=1234"; + $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=" . $testAuthor->getId() . " AND book.ISBN=1234"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXXAndYYY($value) is turned into findBy(array(XXX, YYY), $value)'); } } diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithNamespaceTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithNamespaceTest.php index a36e7e9b1f..fff23dfe6d 100644 --- a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithNamespaceTest.php +++ b/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithNamespaceTest.php @@ -24,16 +24,16 @@ class ModelCriteriaWithNamespaceTest extends NamespacesTestBase public static function conditionsForTestReplaceNamesWithNamespaces() { return array( - array('Foo\\Bar\\NamespacedBook.Title = ?', 'Title', 'book.TITLE = ?'), // basic case - array('Foo\\Bar\\NamespacedBook.Title=?', 'Title', 'book.TITLE=?'), // without spaces - array('Foo\\Bar\\NamespacedBook.Id<= ?', 'Id', 'book.ID<= ?'), // with non-equal comparator - array('Foo\\Bar\\NamespacedBook.AuthorId LIKE ?', 'AuthorId', 'book.AUTHOR_ID LIKE ?'), // with SQL keyword separator - array('(Foo\\Bar\\NamespacedBook.AuthorId) LIKE ?', 'AuthorId', '(book.AUTHOR_ID) LIKE ?'), // with parenthesis - array('(Foo\\Bar\\NamespacedBook.Id*1.5)=1', 'Id', '(book.ID*1.5)=1'), // ignore numbers + array('Foo\\Bar\\NamespacedBook.Title = ?', 'Title', 'namespaced_book.TITLE = ?'), // basic case + array('Foo\\Bar\\NamespacedBook.Title=?', 'Title', 'namespaced_book.TITLE=?'), // without spaces + array('Foo\\Bar\\NamespacedBook.Id<= ?', 'Id', 'namespaced_book.ID<= ?'), // with non-equal comparator + array('Foo\\Bar\\NamespacedBook.AuthorId LIKE ?', 'AuthorId', 'namespaced_book.AUTHOR_ID LIKE ?'), // with SQL keyword separator + array('(Foo\\Bar\\NamespacedBook.AuthorId) LIKE ?', 'AuthorId', '(namespaced_book.AUTHOR_ID) LIKE ?'), // with parenthesis + array('(Foo\\Bar\\NamespacedBook.Id*1.5)=1', 'Id', '(namespaced_book.ID*1.5)=1'), // ignore numbers // dealing with quotes - array("Foo\\Bar\\NamespacedBook.Id + ' ' + Foo\\Bar\\NamespacedBook.AuthorId", null, "book.ID + ' ' + book.AUTHOR_ID"), - array("'Foo\\Bar\\NamespacedBook.Id' + Foo\\Bar\\NamespacedBook.AuthorId", null, "'Foo\\Bar\\NamespacedBook.Id' + book.AUTHOR_ID"), - array("Foo\\Bar\\NamespacedBook.Id + 'Foo\\Bar\\NamespacedBook.AuthorId'", null, "book.ID + 'Foo\\Bar\\NamespacedBook.AuthorId'"), + array("Foo\\Bar\\NamespacedBook.Id + ' ' + Foo\\Bar\\NamespacedBook.AuthorId", null, "namespaced_book.ID + ' ' + namespaced_book.AUTHOR_ID"), + array("'Foo\\Bar\\NamespacedBook.Id' + Foo\\Bar\\NamespacedBook.AuthorId", null, "'Foo\\Bar\\NamespacedBook.Id' + namespaced_book.AUTHOR_ID"), + array("Foo\\Bar\\NamespacedBook.Id + 'Foo\\Bar\\NamespacedBook.AuthorId'", null, "namespaced_book.ID + 'Foo\\Bar\\NamespacedBook.AuthorId'"), ); } diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/SubQueryTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/SubQueryTest.php index 08d6e7b12f..0350b67cc0 100644 --- a/tests/Propel/Tests/Runtime/ActiveQuery/SubQueryTest.php +++ b/tests/Propel/Tests/Runtime/ActiveQuery/SubQueryTest.php @@ -45,10 +45,10 @@ public function testSubQueryExplicit() $c->addSelectQuery($subCriteria, 'subCriteriaAlias', false); $c->groupBy('subCriteriaAlias.AuthorId'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` ORDER BY book.TITLE ASC) AS subCriteriaAlias GROUP BY subCriteriaAlias.AUTHOR_ID"; + if ($this->isDb('pgsql')) { + $sql = $this->getSql("SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book ORDER BY book.TITLE ASC) AS subCriteriaAlias GROUP BY subCriteriaAlias.AUTHOR_ID,subCriteriaAlias.ID,subCriteriaAlias.TITLE,subCriteriaAlias.ISBN,subCriteriaAlias.PRICE,subCriteriaAlias.PUBLISHER_ID"); } else { - $sql = "SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book ORDER BY book.TITLE ASC) AS subCriteriaAlias GROUP BY subCriteriaAlias.AUTHOR_ID"; + $sql = $this->getSql("SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` ORDER BY book.TITLE ASC) AS subCriteriaAlias GROUP BY subCriteriaAlias.AUTHOR_ID"); } $params = array(); @@ -64,11 +64,7 @@ public function testSubQueryWithoutSelect() $c->addSelectQuery($subCriteria, 'subCriteriaAlias'); $c->filterByPrice(20, Criteria::LESS_THAN); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS subCriteriaAlias WHERE subCriteriaAlias.PRICE<:p1"; - } else { - $sql = "SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book) AS subCriteriaAlias WHERE subCriteriaAlias.PRICE<:p1"; - } + $sql = $this->getSql("SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS subCriteriaAlias WHERE subCriteriaAlias.PRICE<:p1"); $params = array( array('table' => 'book', 'column' => 'PRICE', 'value' => 20), @@ -85,11 +81,7 @@ public function testSubQueryWithoutAlias() $c->addSelectQuery($subCriteria); // no alias $c->filterByPrice(20, Criteria::LESS_THAN); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS alias_1 WHERE alias_1.PRICE<:p1"; - } else { - $sql = "SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book) AS alias_1 WHERE alias_1.PRICE<:p1"; - } + $sql = $this->getSql("SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS alias_1 WHERE alias_1.PRICE<:p1"); $params = array( array('table' => 'book', 'column' => 'PRICE', 'value' => 20), @@ -106,11 +98,7 @@ public function testSubQueryWithoutAliasAndSelect() $c->addSelectQuery($subCriteria); // no alias $c->filterByPrice(20, Criteria::LESS_THAN); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS alias_1 WHERE alias_1.PRICE<:p1"; - } else { - $sql = "SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book) AS alias_1 WHERE alias_1.PRICE<:p1"; - } + $sql = $this->getSql("SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS alias_1 WHERE alias_1.PRICE<:p1"); $params = array( array('table' => 'book', 'column' => 'PRICE', 'value' => 20), @@ -131,11 +119,7 @@ public function testSubQueryWithoutAliasSeveral() $c3->addSelectQuery($c2); // no alias $c3->filterByTitle('War%'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID, alias_2.ID, alias_2.TITLE, alias_2.ISBN, alias_2.PRICE, alias_2.PUBLISHER_ID, alias_2.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.PRICE>:p2) AS alias_1, (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.PRICE<:p3) AS alias_2 WHERE alias_2.TITLE LIKE :p1"; - } else { - $sql = "SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID, alias_2.ID, alias_2.TITLE, alias_2.ISBN, alias_2.PRICE, alias_2.PUBLISHER_ID, alias_2.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book WHERE book.PRICE>:p2) AS alias_1, (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book WHERE book.PRICE<:p3) AS alias_2 WHERE alias_2.TITLE LIKE :p1"; - } + $sql = $this->getSql("SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID, alias_2.ID, alias_2.TITLE, alias_2.ISBN, alias_2.PRICE, alias_2.PUBLISHER_ID, alias_2.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.PRICE>:p2) AS alias_1, (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.PRICE<:p3) AS alias_2 WHERE alias_2.TITLE LIKE :p1"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'War%'), @@ -157,11 +141,7 @@ public function testSubQueryWithoutAliasRecursive() $c3->addSelectQuery($c2); // no alias $c3->filterByTitle('War%'); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT alias_2.ID, alias_2.TITLE, alias_2.ISBN, alias_2.PRICE, alias_2.PUBLISHER_ID, alias_2.AUTHOR_ID FROM (SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS alias_1 WHERE alias_1.PRICE<:p2) AS alias_2 WHERE alias_2.TITLE LIKE :p1"; - } else { - $sql = "SELECT alias_2.ID, alias_2.TITLE, alias_2.ISBN, alias_2.PRICE, alias_2.PUBLISHER_ID, alias_2.AUTHOR_ID FROM (SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book) AS alias_1 WHERE alias_1.PRICE<:p2) AS alias_2 WHERE alias_2.TITLE LIKE :p1"; - } + $sql = $this->getSql("SELECT alias_2.ID, alias_2.TITLE, alias_2.ISBN, alias_2.PRICE, alias_2.PUBLISHER_ID, alias_2.AUTHOR_ID FROM (SELECT alias_1.ID, alias_1.TITLE, alias_1.ISBN, alias_1.PRICE, alias_1.PUBLISHER_ID, alias_1.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS alias_1 WHERE alias_1.PRICE<:p2) AS alias_2 WHERE alias_2.TITLE LIKE :p1"); $params = array( array('table' => 'book', 'column' => 'TITLE', 'value' => 'War%'), @@ -181,11 +161,7 @@ public function testSubQueryWithJoin() $c2->addSelectQuery($c1, 'subQuery'); $c2->filterByPrice(20, Criteria::LESS_THAN); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT subQuery.ID, subQuery.TITLE, subQuery.ISBN, subQuery.PRICE, subQuery.PUBLISHER_ID, subQuery.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.LAST_NAME=:p2) AS subQuery WHERE subQuery.PRICE<:p1"; - } else { - $sql = "SELECT subQuery.ID, subQuery.TITLE, subQuery.ISBN, subQuery.PRICE, subQuery.PUBLISHER_ID, subQuery.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book LEFT JOIN author ON (book.AUTHOR_ID=author.ID) WHERE author.LAST_NAME=:p2) AS subQuery WHERE subQuery.PRICE<:p1"; - } + $sql = $this->getSql("SELECT subQuery.ID, subQuery.TITLE, subQuery.ISBN, subQuery.PRICE, subQuery.PUBLISHER_ID, subQuery.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.LAST_NAME=:p2) AS subQuery WHERE subQuery.PRICE<:p1"); $params = array( array('table' => 'book', 'column' => 'PRICE', 'value' => 20), @@ -204,11 +180,7 @@ public function testSubQueryParameters() // and use filterByPrice method! $c->filterByPrice(20, Criteria::LESS_THAN); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=:p2) AS subCriteriaAlias WHERE subCriteriaAlias.PRICE<:p1"; - } else { - $sql = "SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book WHERE book.AUTHOR_ID=:p2) AS subCriteriaAlias WHERE subCriteriaAlias.PRICE<:p1"; - } + $sql = $this->getSql("SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=:p2) AS subCriteriaAlias WHERE subCriteriaAlias.PRICE<:p1"); $params = array( array('table' => 'book', 'column' => 'PRICE', 'value' => 20), @@ -236,24 +208,20 @@ public function testSubQueryRecursive() $c->addSelectQuery($latestBookQuery, 'latestBookQuery'); $c->filterByPrice(12, Criteria::LESS_THAN); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT latestBookQuery.ID, latestBookQuery.TITLE, latestBookQuery.ISBN, latestBookQuery.PRICE, latestBookQuery.PUBLISHER_ID, latestBookQuery.AUTHOR_ID ". - "FROM (SELECT sortedBookQuery.ID, sortedBookQuery.TITLE, sortedBookQuery.ISBN, sortedBookQuery.PRICE, sortedBookQuery.PUBLISHER_ID, sortedBookQuery.AUTHOR_ID ". - "FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID ". - "FROM `book` ". - "WHERE book.PUBLISHER_ID=:p2 ". - "ORDER BY book.TITLE DESC,book.ID DESC) AS sortedBookQuery ". - "GROUP BY sortedBookQuery.AUTHOR_ID) AS latestBookQuery ". - "WHERE latestBookQuery.PRICE<:p1"; + if ($this->isDb('pgsql')) { + $sql = $this->getSql("SELECT latestBookQuery.ID, latestBookQuery.TITLE, latestBookQuery.ISBN, latestBookQuery.PRICE, latestBookQuery.PUBLISHER_ID, latestBookQuery.AUTHOR_ID ". + "FROM (SELECT sortedBookQuery.ID, sortedBookQuery.TITLE, sortedBookQuery.ISBN, sortedBookQuery.PRICE, sortedBookQuery.PUBLISHER_ID, sortedBookQuery.AUTHOR_ID FROM ". + "(SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book WHERE book.PUBLISHER_ID=:p2 ORDER BY book.TITLE DESC,book.ID DESC) AS sortedBookQuery ". + "GROUP BY sortedBookQuery.AUTHOR_ID,sortedBookQuery.ID,sortedBookQuery.TITLE,sortedBookQuery.ISBN,sortedBookQuery.PRICE,sortedBookQuery.PUBLISHER_ID) AS latestBookQuery WHERE latestBookQuery.PRICE<:p1"); } else { - $sql = "SELECT latestBookQuery.ID, latestBookQuery.TITLE, latestBookQuery.ISBN, latestBookQuery.PRICE, latestBookQuery.PUBLISHER_ID, latestBookQuery.AUTHOR_ID ". - "FROM (SELECT sortedBookQuery.ID, sortedBookQuery.TITLE, sortedBookQuery.ISBN, sortedBookQuery.PRICE, sortedBookQuery.PUBLISHER_ID, sortedBookQuery.AUTHOR_ID ". - "FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID ". - "FROM book ". - "WHERE book.PUBLISHER_ID=:p2 ". - "ORDER BY book.TITLE DESC,book.ID DESC) AS sortedBookQuery ". - "GROUP BY sortedBookQuery.AUTHOR_ID) AS latestBookQuery ". - "WHERE latestBookQuery.PRICE<:p1"; + $sql = $this->getSql("SELECT latestBookQuery.ID, latestBookQuery.TITLE, latestBookQuery.ISBN, latestBookQuery.PRICE, latestBookQuery.PUBLISHER_ID, latestBookQuery.AUTHOR_ID ". + "FROM (SELECT sortedBookQuery.ID, sortedBookQuery.TITLE, sortedBookQuery.ISBN, sortedBookQuery.PRICE, sortedBookQuery.PUBLISHER_ID, sortedBookQuery.AUTHOR_ID ". + "FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID ". + "FROM `book` ". + "WHERE book.PUBLISHER_ID=:p2 ". + "ORDER BY book.TITLE DESC,book.ID DESC) AS sortedBookQuery ". + "GROUP BY sortedBookQuery.AUTHOR_ID) AS latestBookQuery ". + "WHERE latestBookQuery.PRICE<:p1"); } $params = array( @@ -272,11 +240,7 @@ public function testSubQueryWithSelectColumns() $c->select(array('alias1.Id')); $c->configureSelectColumns(); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT alias1.ID AS \"alias1.Id\" FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS alias1"; - } else { - $sql = "SELECT alias1.ID AS \"alias1.Id\" FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book) AS alias1"; - } + $sql = $this->getSql("SELECT alias1.ID AS \"alias1.Id\" FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS alias1"); $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'addSelectQuery() forges a unique alias and adds select columns by default'); @@ -293,11 +257,7 @@ public function testSubQueryCount() $query = Propel::getConnection()->getLastExecutedQuery(); - if (in_array($this->getDriver(), array('mysql'))) { - $sql = "SELECT COUNT(*) FROM (SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS subCriteriaAlias WHERE subCriteriaAlias.PRICE<20) propelmatch4cnt"; - } else { - $sql = "SELECT COUNT(*) FROM (SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM book) AS subCriteriaAlias WHERE subCriteriaAlias.PRICE<20) propelmatch4cnt"; - } + $sql = $this->getSql("SELECT COUNT(*) FROM (SELECT subCriteriaAlias.ID, subCriteriaAlias.TITLE, subCriteriaAlias.ISBN, subCriteriaAlias.PRICE, subCriteriaAlias.PUBLISHER_ID, subCriteriaAlias.AUTHOR_ID FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book`) AS subCriteriaAlias WHERE subCriteriaAlias.PRICE<20) propelmatch4cnt"); $this->assertEquals($sql, $query, 'addSelectQuery() doCount is defined as complexQuery'); } diff --git a/tests/Propel/Tests/Runtime/Collection/ObjectCollectionTest.php b/tests/Propel/Tests/Runtime/Collection/ObjectCollectionTest.php index 4cb01eff72..d81b309a66 100644 --- a/tests/Propel/Tests/Runtime/Collection/ObjectCollectionTest.php +++ b/tests/Propel/Tests/Runtime/Collection/ObjectCollectionTest.php @@ -210,6 +210,7 @@ public function testContainsMatchesSimilarObjects() $col = new ObjectCollection(); $b1 = new Book(); $b1->setTitle('Bar'); + $b1->setISBN('012345'); $b1->save(); $b2 = clone $b1; @@ -227,6 +228,7 @@ public function testSearchMatchesSimilarObjects() $col = new ObjectCollection(); $b1 = new Book(); $b1->setTitle('Bar'); + $b1->setISBN('012345'); $b1->save(); $b2 = clone $b1; @@ -243,6 +245,7 @@ public function testContainsMatchesSimilarNewObjects() $col = new ObjectCollection(); $b1 = new Book(); $b1->setTitle('Bar'); + $b1->setISBN('012345'); $b2 = clone $b1; $this->assertFalse($col->contains($b1), 'contains() returns false on an empty collection'); @@ -258,6 +261,7 @@ public function testSearchMatchesSimilarNewObjects() $col = new ObjectCollection(); $b1 = new Book(); $b1->setTitle('Bar'); + $b1->setISBN('012345'); $b2 = clone $b1; $this->assertFalse($col->search($b1), 'search() returns false on an empty collection'); diff --git a/tests/Propel/Tests/Runtime/Connection/PropelPDOTest.php b/tests/Propel/Tests/Runtime/Connection/PropelPDOTest.php index 6f7ca50fd0..d1d9ddf439 100644 --- a/tests/Propel/Tests/Runtime/Connection/PropelPDOTest.php +++ b/tests/Propel/Tests/Runtime/Connection/PropelPDOTest.php @@ -35,6 +35,7 @@ class PropelPDOTest extends BookstoreTestBase { protected function setUp() { + $this->con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); } protected function tearDown() @@ -215,7 +216,8 @@ public function testNestedTransactionRollBackRethrow() } /** - * @link http://propel.phpdb.org/trac/ticket/699 + * @link http://trac.propelorm.org/ticket/699 + * @group mysql */ public function testNestedTransactionRollBackSwallow() { @@ -323,7 +325,7 @@ public function testLatestQueryMoreThanTenArgs() $c = new Criteria(); $c->add(BookTableMap::ID, array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Criteria::IN); $books = BookQuery::create(null, $c)->find($con); - $expected = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.ID IN (1,1,1,1,1,1,1,1,1,1,1,1)"; + $expected = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.ID IN (1,1,1,1,1,1,1,1,1,1,1,1)"); $this->assertEquals($expected, $con->getLastExecutedQuery(), 'PropelPDO correctly replaces arguments in queries'); } @@ -360,14 +362,11 @@ public function testDebugLatestQuery() $con->useDebug(true); $books = BookQuery::create(null, $c)->find($con); - $latestExecutedQuery = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE LIKE 'Harry%s'"; - if (!Propel::getServiceContainer()->getAdapter(BookTableMap::DATABASE_NAME)->useQuoteIdentifier()) { - $latestExecutedQuery = str_replace('`', '', $latestExecutedQuery); - } + $latestExecutedQuery = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE LIKE 'Harry%s'"); $this->assertEquals($latestExecutedQuery, $con->getLastExecutedQuery(), 'PropelPDO updates the last executed query when useLogging is true'); BookTableMap::doDeleteAll($con); - $latestExecutedQuery = "DELETE FROM `book`"; + $latestExecutedQuery = $this->getSql("DELETE FROM `book`"); $this->assertEquals($latestExecutedQuery, $con->getLastExecutedQuery(), 'PropelPDO updates the last executed query on delete operations'); $sql = 'DELETE FROM book WHERE 1=1'; @@ -478,11 +477,11 @@ public function testDebugLog() $c->add(BookTableMap::TITLE, 'Harry%s', Criteria::LIKE); $books = BookQuery::create(null, $c)->find($con); - $latestExecutedQuery = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE LIKE 'Harry%s'"; + $latestExecutedQuery = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.TITLE LIKE 'Harry%s'"); $this->assertEquals($latestExecutedQuery, $handler->latestMessage, 'PropelPDO logs queries and populates bound parameters in debug mode'); BookTableMap::doDeleteAll($con); - $latestExecutedQuery = "DELETE FROM `book`"; + $latestExecutedQuery = $this->getSql("DELETE FROM `book`"); $this->assertEquals($latestExecutedQuery, $handler->latestMessage, 'PropelPDO logs deletion queries in debug mode'); $latestExecutedQuery = 'DELETE FROM book WHERE 1=1'; diff --git a/tests/Propel/Tests/Runtime/Formatter/ArrayFormatterTest.php b/tests/Propel/Tests/Runtime/Formatter/ArrayFormatterTest.php index 9f3c36c0bf..d365ab383b 100644 --- a/tests/Propel/Tests/Runtime/Formatter/ArrayFormatterTest.php +++ b/tests/Propel/Tests/Runtime/Formatter/ArrayFormatterTest.php @@ -67,7 +67,7 @@ public function testFormatOneResult() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $dataFetcher = $con->query('SELECT * FROM book WHERE book.TITLE = "Quicksilver"'); + $dataFetcher = $con->query("SELECT * FROM book WHERE book.TITLE = 'Quicksilver'"); $formatter = new ArrayFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $books = $formatter->format($dataFetcher); @@ -85,7 +85,7 @@ public function testFormatNoResult() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $dataFetcher = $con->query('SELECT * FROM book WHERE book.TITLE = "foo"'); + $dataFetcher = $con->query("SELECT * FROM book WHERE book.TITLE = 'foo'"); $formatter = new ArrayFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $books = $formatter->format($dataFetcher); @@ -125,7 +125,7 @@ public function testFormatOneNoResult() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $dataFetcher = $con->query('SELECT * FROM book WHERE book.TITLE = "foo"'); + $dataFetcher = $con->query("SELECT * FROM book WHERE book.TITLE = 'foo'"); $formatter = new ArrayFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $book = $formatter->formatOne($dataFetcher); diff --git a/tests/Propel/Tests/Runtime/Formatter/ObjectFormatterTest.php b/tests/Propel/Tests/Runtime/Formatter/ObjectFormatterTest.php index 467500dc73..b3ac4cc5d4 100644 --- a/tests/Propel/Tests/Runtime/Formatter/ObjectFormatterTest.php +++ b/tests/Propel/Tests/Runtime/Formatter/ObjectFormatterTest.php @@ -81,7 +81,7 @@ public function testFormatOneResult() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "Quicksilver"'); + $stmt = $con->query("SELECT * FROM book WHERE book.TITLE = 'Quicksilver'"); $formatter = new ObjectFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $books = $formatter->format($stmt); @@ -97,7 +97,7 @@ public function testFormatNoResult() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "foo"'); + $stmt = $con->query("SELECT * FROM book WHERE book.TITLE = 'foo'"); $formatter = new ObjectFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $books = $formatter->format($stmt); @@ -136,7 +136,7 @@ public function testFormatOneNoResult() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "foo"'); + $stmt = $con->query("SELECT * FROM book WHERE book.TITLE = 'foo'"); $formatter = new ObjectFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $book = $formatter->formatOne($stmt); diff --git a/tests/Propel/Tests/Runtime/Formatter/OnDemandFormatterTest.php b/tests/Propel/Tests/Runtime/Formatter/OnDemandFormatterTest.php index 115380bcd3..77f4a86e57 100644 --- a/tests/Propel/Tests/Runtime/Formatter/OnDemandFormatterTest.php +++ b/tests/Propel/Tests/Runtime/Formatter/OnDemandFormatterTest.php @@ -166,7 +166,7 @@ public function testFormatOneResult() $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); BookstoreDataPopulator::populate($con); - $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "Quicksilver"'); + $stmt = $con->query("SELECT * FROM book WHERE book.TITLE = 'Quicksilver'"); $formatter = new OnDemandFormatter(); $formatter->init(new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book')); $books = $formatter->format($stmt); @@ -183,7 +183,7 @@ public function testFormatNoResult() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "foo"'); + $stmt = $con->query("SELECT * FROM book WHERE book.TITLE = 'foo'"); $formatter = new OnDemandFormatter(); $formatter->init(new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book')); $books = $formatter->format($stmt); diff --git a/tests/Propel/Tests/Runtime/Formatter/SimpleArrayFormatterTest.php b/tests/Propel/Tests/Runtime/Formatter/SimpleArrayFormatterTest.php index ca09acd3a4..6a152463cb 100644 --- a/tests/Propel/Tests/Runtime/Formatter/SimpleArrayFormatterTest.php +++ b/tests/Propel/Tests/Runtime/Formatter/SimpleArrayFormatterTest.php @@ -36,7 +36,7 @@ public function testFormatWithOneRowAndValueIsNotZero() $books = $formatter->format($stmt); $this->assertInstanceOf('\Propel\Runtime\Collection\Collection', $books); $this->assertCount(4, $books); - $this->assertSame('1', $books[0]); + $this->assertSame($this->isDb('mysql') ? '1' : 1, $books[0]); } public function testFormatWithOneRowAndValueEqualsZero() @@ -50,30 +50,38 @@ public function testFormatWithOneRowAndValueEqualsZero() $books = $formatter->format($stmt); $this->assertInstanceOf('\Propel\Runtime\Collection\Collection', $books); $this->assertCount(4, $books); - $this->assertSame('0', $books[0]); + $this->assertSame($this->isDb('mysql') ? '0' : 0, $books[0]); } public function testFormatOneWithOneRowAndValueIsNotZero() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $stmt = $con->query('SELECT 1 FROM book LIMIT 0, 1'); + if ($this->isDb('mysql')) { + $stmt = $con->query('SELECT 1 FROM book LIMIT 0, 1'); + } else { + $stmt = $con->query('SELECT 1 FROM book LIMIT 1'); + } $formatter = new SimpleArrayFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $book = $formatter->formatOne($stmt); - $this->assertSame('1', $book); + $this->assertSame($this->isDb('mysql') ? '1' : 1, $book); } public function testFormatOneWithOneRowAndValueEqualsZero() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $stmt = $con->query('SELECT 0 FROM book LIMIT 0, 1'); + if ($this->isDb('mysql')) { + $stmt = $con->query('SELECT 0 FROM book LIMIT 0, 1'); + } else { + $stmt = $con->query('SELECT 0 FROM book LIMIT 1'); + } $formatter = new SimpleArrayFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $book = $formatter->formatOne($stmt); - $this->assertSame('0', $book); + $this->assertSame($this->isDb('mysql') ? '0' : 0, $book); } } diff --git a/tests/Propel/Tests/Runtime/Formatter/StatementFormatterTest.php b/tests/Propel/Tests/Runtime/Formatter/StatementFormatterTest.php index 94e7c4c6c3..20434fbba7 100644 --- a/tests/Propel/Tests/Runtime/Formatter/StatementFormatterTest.php +++ b/tests/Propel/Tests/Runtime/Formatter/StatementFormatterTest.php @@ -73,7 +73,7 @@ public function testFormatOneResult() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "Quicksilver"'); + $stmt = $con->query("SELECT * FROM book WHERE book.TITLE = 'Quicksilver'"); $formatter = new StatementFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $books = $formatter->format($stmt); @@ -88,7 +88,7 @@ public function testFormatNoResult() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "foo"'); + $stmt = $con->query("SELECT * FROM book WHERE book.TITLE = 'foo'"); $formatter = new StatementFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $books = $formatter->format($stmt); @@ -127,7 +127,7 @@ public function testFormatOneNoResult() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); - $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "foo"'); + $stmt = $con->query("SELECT * FROM book WHERE book.TITLE = 'foo'"); $formatter = new StatementFormatter(); $formatter->init(new ModelCriteria('bookstore', '\Propel\Tests\Bookstore\Book')); $book = $formatter->formatOne($stmt); diff --git a/tests/Propel/Tests/Runtime/Util/TableMapExceptionsTest.php b/tests/Propel/Tests/Runtime/Util/TableMapExceptionsTest.php index 96f3696cc9..13ae412719 100644 --- a/tests/Propel/Tests/Runtime/Util/TableMapExceptionsTest.php +++ b/tests/Propel/Tests/Runtime/Util/TableMapExceptionsTest.php @@ -34,7 +34,7 @@ public function testDoSelect() $c->doSelect(); $this->fail('Missing expected exception on BAD SQL'); } catch (PropelException $e) { - $this->assertContains('[SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.ID BAD SQL:p1]', $e->getMessage(), 'SQL query is written in the exception message'); + $this->assertContains($this->getSql('[SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.ID BAD SQL:p1]'), $e->getMessage(), 'SQL query is written in the exception message'); } } @@ -47,7 +47,7 @@ public function testDoCount() $c->doCount(); $this->fail('Missing expected exception on BAD SQL'); } catch (PropelException $e) { - $this->assertContains('[SELECT COUNT(*) FROM `book` WHERE book.ID BAD SQL:p1]', $e->getMessage(), 'SQL query is written in the exception message'); + $this->assertContains($this->getSql('[SELECT COUNT(*) FROM `book` WHERE book.ID BAD SQL:p1]'), $e->getMessage(), 'SQL query is written in the exception message'); } } @@ -60,7 +60,7 @@ public function testDoDelete() $c->doDelete(Propel::getServiceContainer()->getWriteConnection(BookTableMap::DATABASE_NAME)); $this->fail('Missing expected exception on BAD SQL'); } catch (PropelException $e) { - $this->assertContains('[DELETE FROM `book` WHERE book.ID BAD SQL:p1]', $e->getMessage(), 'SQL query is written in the exception message'); + $this->assertContains($this->getSql('[DELETE FROM `book` WHERE book.ID BAD SQL:p1]'), $e->getMessage(), 'SQL query is written in the exception message'); } } @@ -76,20 +76,30 @@ public function testDoUpdate() $c1->doUpdate($c2, Propel::getServiceContainer()->getWriteConnection(BookTableMap::DATABASE_NAME)); $this->fail('Missing expected exception on BAD SQL'); } catch (PropelException $e) { - $this->assertContains('[UPDATE `book` SET `TITLE`=:p1 WHERE book.ID BAD SQL:p2]', $e->getMessage(), 'SQL query is written in the exception message'); + $this->assertContains($this->getSql('[UPDATE `book` SET `TITLE`=:p1 WHERE book.ID BAD SQL:p2]'), $e->getMessage(), 'SQL query is written in the exception message'); } } public function testDoInsert() { + $con = Propel::getServiceContainer()->getWriteConnection(BookTableMap::DATABASE_NAME); + try { $c = new Criteria(); $c->setPrimaryTableName(BookTableMap::TABLE_NAME); $c->add(BookTableMap::AUTHOR_ID, 'lkhlkhj'); - $c->doInsert(Propel::getServiceContainer()->getWriteConnection(BookTableMap::DATABASE_NAME)); + + $db = Propel::getServiceContainer()->getAdapter($c->getDbName()); + + $c->doInsert($con); $this->fail('Missing expected exception on BAD SQL'); } catch (PropelException $e) { - $this->assertContains('[INSERT INTO `book` (`AUTHOR_ID`) VALUES (:p1)]', $e->getMessage(), 'SQL query is written in the exception message'); + if ($db->isGetIdBeforeInsert()) { + $this->assertContains($this->getSql('[INSERT INTO book (AUTHOR_ID,ID) VALUES (:p1,:p2)]'), $e->getMessage(), 'SQL query is written in the exception message'); + } else { + $this->assertContains($this->getSql('[INSERT INTO `book` (`AUTHOR_ID`) VALUES (:p1)]'), $e->getMessage(), 'SQL query is written in the exception message'); + } + } } } diff --git a/tests/Propel/Tests/Runtime/Util/TableMapTest.php b/tests/Propel/Tests/Runtime/Util/TableMapTest.php index 08fcfe3e9f..21a3b4f2b2 100644 --- a/tests/Propel/Tests/Runtime/Util/TableMapTest.php +++ b/tests/Propel/Tests/Runtime/Util/TableMapTest.php @@ -260,7 +260,7 @@ public function testDoDeleteSimpleCondition() $c = new Criteria(BookTableMap::DATABASE_NAME); $c->add(BookTableMap::TITLE, 'War And Peace'); $c->doDelete($con); - $expectedSQL = "DELETE FROM `book` WHERE book.TITLE='War And Peace'"; + $expectedSQL = $this->getSql("DELETE FROM `book` WHERE book.TITLE='War And Peace'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'doDelete() translates a condition into a WHERE'); } @@ -271,7 +271,7 @@ public function testDoDeleteSeveralConditions() $c->add(BookTableMap::TITLE, 'War And Peace'); $c->add(BookTableMap::ID, 12); $c->doDelete($con); - $expectedSQL = "DELETE FROM `book` WHERE book.TITLE='War And Peace' AND book.ID=12"; + $expectedSQL = $this->getSql("DELETE FROM `book` WHERE book.TITLE='War And Peace' AND book.ID=12"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'doDelete() combines conditions in WHERE with an AND'); } @@ -282,7 +282,13 @@ public function testDoDeleteTableAlias() $c->addAlias('b', BookTableMap::TABLE_NAME); $c->add('b.TITLE', 'War And Peace'); $c->doDelete($con); - $expectedSQL = "DELETE b FROM `book` AS b WHERE b.TITLE='War And Peace'"; + + if ($this->isDb('pgsql')) { + $expectedSQL = $this->getSql("DELETE FROM `book` AS b WHERE b.TITLE='War And Peace'"); + } else { + $expectedSQL = $this->getSql("DELETE b FROM `book` AS b WHERE b.TITLE='War And Peace'"); + } + $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'doDelete() accepts a Criteria with a table alias'); } @@ -298,7 +304,7 @@ public function testDoDeleteSeveralTables() $c->add(BookTableMap::TITLE, 'War And Peace'); $c->add(AuthorTableMap::FIRST_NAME, 'Leo'); $c->doDelete($con); - $expectedSQL = "DELETE FROM `author` WHERE author.FIRST_NAME='Leo'"; + $expectedSQL = $this->getSql("DELETE FROM `author` WHERE author.FIRST_NAME='Leo'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'doDelete() issues two DELETE queries when passed conditions on two tables'); $this->assertEquals($count + 2, $con->getQueryCount(), 'doDelete() issues two DELETE queries when passed conditions on two tables'); @@ -306,7 +312,7 @@ public function testDoDeleteSeveralTables() $c->add(AuthorTableMap::FIRST_NAME, 'Leo'); $c->add(BookTableMap::TITLE, 'War And Peace'); $c->doDelete($con); - $expectedSQL = "DELETE FROM `book` WHERE book.TITLE='War And Peace'"; + $expectedSQL = $this->getSql("DELETE FROM `book` WHERE book.TITLE='War And Peace'"); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'doDelete() issues two DELETE queries when passed conditions on two tables'); $this->assertEquals($count + 4, $con->getQueryCount(), 'doDelete() issues two DELETE queries when passed conditions on two tables'); } @@ -316,7 +322,7 @@ public function testCommentDoSelect() $c = new Criteria(); $c->setComment('Foo'); $c->addSelectColumn(BookTableMap::ID); - $expected = 'SELECT /* Foo */ book.ID FROM `book`'; + $expected = $this->getSql('SELECT /* Foo */ book.ID FROM `book`'); $params = array(); $this->assertEquals($expected, $c->createSelectSQL($params), 'Criteria::setComment() adds a comment to select queries'); } @@ -330,7 +336,7 @@ public function testCommentDoUpdate() $c2->add(BookTableMap::TITLE, 'Updated Title'); $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c1->doUpdate($c2, $con); - $expected = 'UPDATE /* Foo */ `book` SET `TITLE`=\'Updated Title\''; + $expected = $this->getSql('UPDATE /* Foo */ `book` SET `TITLE`=\'Updated Title\''); $this->assertEquals($expected, $con->getLastExecutedQuery(), 'Criteria::setComment() adds a comment to update queries'); } @@ -341,7 +347,7 @@ public function testCommentDoDelete() $c->add(BookTableMap::TITLE, 'War And Peace'); $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $c->doDelete($con); - $expected = 'DELETE /* Foo */ FROM `book` WHERE book.TITLE=\'War And Peace\''; + $expected = $this->getSql('DELETE /* Foo */ FROM `book` WHERE book.TITLE=\'War And Peace\''); $this->assertEquals($expected, $con->getLastExecutedQuery(), 'Criteria::setComment() adds a comment to delete queries'); } diff --git a/tests/bin/setup.base.sh b/tests/bin/setup.base.sh new file mode 100644 index 0000000000..caa6b58e58 --- /dev/null +++ b/tests/bin/setup.base.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +if [ "$DB" = "" ]; then + echo "\$DB is not defined." + exit; +fi + +check() { + if [ $? != 0 ]; then + echo "Aborted."; exit 1; + fi +} \ No newline at end of file diff --git a/tests/bin/setup.mysql.sh b/tests/bin/setup.mysql.sh new file mode 100755 index 0000000000..ff6893adee --- /dev/null +++ b/tests/bin/setup.mysql.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +DIR=`dirname $0`; +. $DIR/setup.base.sh; + +mysql=`which mysql`; +if [ "$mysql" = "" ]; then + mysql=`which mysql5`; +fi + +if [ "$mysql" = "" ]; then + echo "Can not find mysql binary. Is it installed?"; + exit 1; +fi + +if [ "$DB_USER" = "" ]; then + DB_USER="root"; +fi + +$mysql -u$DB_USER -e '\ +SET FOREIGN_KEY_CHECKS = 0; \ +DROP DATABASE IF EXISTS test; \ +DROP SCHEMA IF EXISTS second_hand_books; \ +DROP SCHEMA IF EXISTS contest; \ +DROP SCHEMA IF EXISTS bookstore_schemas; \ +SET FOREIGN_KEY_CHECKS = 1; \ +'; +check; + +$mysql -u$DB_USER -e '\ +CREATE DATABASE test; \ +CREATE SCHEMA bookstore_schemas; \ +CREATE SCHEMA contest; \ +CREATE SCHEMA second_hand_books; \ +'; +check; + +if [ "$DB_PW" = "" ]; then + php $DIR/../../bin/propel test:prepare --vendor="$DB" --dsn="$DB:dbname=test" --user="$DB_USER"; +else + php $DIR/../../bin/propel test:prepare --vendor="$DB" --dsn="$DB:dbname=test" --user="$DB_USER" --password="$DB_PW"; +fi \ No newline at end of file diff --git a/tests/bin/setup.postgres.sh b/tests/bin/setup.postgres.sh new file mode 100755 index 0000000000..e63f0d98a1 --- /dev/null +++ b/tests/bin/setup.postgres.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +DIR=`dirname $0`; +. $DIR/setup.base.sh; + +if [ "$DB_USER" = "" ]; then + DB_USER="postgres"; +fi + +dropdb -U $DB_USER postgres; +check; + +createdb -U $DB_USER postgres; +check; + +psql --username=$DB_USER -c 'CREATE SCHEMA bookstore_schemas; CREATE SCHEMA contest; CREATE SCHEMA second_hand_books'; +check; + +if [ "$DB_PW" = "" ]; then + php $DIR/../../bin/propel test:prepare --vendor="$DB" --dsn="$DB:dbname=postgres" --user="$DB_USER"; +else + php $DIR/../../bin/propel test:prepare --vendor="$DB" --dsn="$DB:dbname=postgres" --user="$DB_USER" --password="$DB_PW"; +fi