connection = ConnectionManager::get('test'); $this->autoQuote = $this->connection->getDriver()->isAutoQuotingEnabled(); } public function tearDown(): void { parent::tearDown(); $this->connection->getDriver()->enableAutoQuoting($this->autoQuote); unset($this->connection); } /** * Test a basic delete using from() */ public function testDeleteWithFrom(): void { $query = new DeleteQuery($this->connection); $query->delete() ->from('authors') ->where('1 = 1'); $result = $query->sql(); $this->assertQuotedQuery('DELETE FROM ', $result, !$this->autoQuote); $result = $query->execute(); $this->assertInstanceOf(StatementInterface::class, $result); $this->assertSame(self::AUTHOR_COUNT, $result->rowCount()); $result->closeCursor(); } /** * Test delete with from and alias. */ public function testDeleteWithAliasedFrom(): void { $query = new DeleteQuery($this->connection); $query->delete() ->from(['a ' => 'authors']) ->where(['a.id !=' => 99]); $result = $query->sql(); $this->assertQuotedQuery('DELETE FROM WHERE != :c0', $result, !$this->autoQuote); $result = $query->execute(); $this->assertInstanceOf(StatementInterface::class, $result); $this->assertSame(self::AUTHOR_COUNT, $result->rowCount()); $result->closeCursor(); } /** * Test a basic delete with no from() call. */ public function testDeleteNoFrom(): void { $query = new DeleteQuery($this->connection); $query->delete('authors') ->where('1 = 1'); $result = $query->sql(); $this->assertQuotedQuery('DELETE FROM ', $result, !$this->autoQuote); $result = $query->execute(); $this->assertInstanceOf(StatementInterface::class, $result); $this->assertSame(self::AUTHOR_COUNT, $result->rowCount()); $result->closeCursor(); } /** * Tests that delete queries that contain joins do trigger a notice, * warning about possible incompatibilities with aliases being removed * from the conditions. */ public function testDeleteRemovingAliasesCanBreakJoins(): void { $this->expectException(DatabaseException::class); $this->expectExceptionMessage('Aliases are being removed from conditions for UPDATE/DELETE queries, this can break references to joined tables.'); $query = new DeleteQuery($this->connection); $query ->delete('authors') ->from(['a ' => 'authors']) ->innerJoin('articles') ->where(['a.id' => 1]); $query->sql(); } /** * Tests that aliases are stripped from delete query conditions * where possible. */ public function testDeleteStripAliasesFromConditions(): void { $query = new DeleteQuery($this->connection); $query ->delete() ->from(['a' => 'authors']) ->where([ 'OR' => [ 'a.id' => 1, 'a.name IS' => null, 'a.email IS NOT' => null, 'AND' => [ 'b.name NOT IN' => ['foo', 'bar'], 'OR' => [ $query->newExpr()->eq(new IdentifierExpression('c.name'), 'zap'), 'd.name' => 'baz', (new SelectQuery($this->connection))->select(['e.name'])->where(['e.name' => 'oof']), ], ], ], ]); $this->assertQuotedQuery( 'DELETE FROM WHERE \(' . ' = :c0 OR \(\) IS NULL OR \(\) IS NOT NULL OR \(' . ' NOT IN \(:c1,:c2\) AND \(' . ' = :c3 OR = :c4 OR \(SELECT \. WHERE \. = :c5\)' . '\)' . '\)' . '\)', $query->sql(), !$this->autoQuote ); } /** * Test that epilog() will actually append a string to a delete query */ public function testAppendDelete(): void { $query = new DeleteQuery($this->connection); $sql = $query ->delete('articles') ->where(['id' => 1]) ->epilog('RETURNING id') ->sql(); $this->assertStringContainsString('DELETE FROM', $sql); $this->assertStringContainsString('WHERE', $sql); $this->assertSame(' RETURNING id', substr($sql, -13)); } /** * Test use of modifiers in a DELETE query * * Testing the generated SQL since the modifiers are usually different per driver */ public function testDeleteModifiers(): void { $query = new DeleteQuery($this->connection); $result = $query->delete() ->from('authors') ->where('1 = 1') ->modifier('IGNORE'); $this->assertQuotedQuery( 'DELETE IGNORE FROM WHERE 1 = 1', $result->sql(), !$this->autoQuote ); $query = new DeleteQuery($this->connection); $result = $query->delete() ->from('authors') ->where('1 = 1') ->modifier(['IGNORE', 'QUICK']); $this->assertQuotedQuery( 'DELETE IGNORE QUICK FROM WHERE 1 = 1', $result->sql(), !$this->autoQuote ); } }