Skip to content

Commit e5b4e95

Browse files
committed
Distinguish between executeStatement and executeQuery
1 parent 4e6cc94 commit e5b4e95

16 files changed

+111
-61
lines changed

docs/persistence/sql/advanced.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ When it's time to execute you can specify your PDO manually::
3030

3131
With queries you might need to select mode first::
3232

33-
$stmt = $query->mode('delete')->execute($pdo);
33+
$stmt = $query->mode('delete')->executeStatement($pdo);
3434

3535
The :php:meth:`Expresssion::execute` is a convenient way to prepare query,
3636
bind all parameters and get `Doctrine\DBAL\Result`, but if you wish to do it manually,
@@ -47,7 +47,7 @@ by switching to DSQL::
4747
$c = new Connection(['connection' => $pdo]);
4848

4949
$user_ids = $c->dsql()->table('expired_users')->field('user_id');
50-
$c->dsql()->table('user')->where('id', 'in', $user_ids)->set('active', 0)->mode('update')->execute();
50+
$c->dsql()->table('user')->where('id', 'in', $user_ids)->set('active', 0)->mode('update')->executeStatement();
5151

5252
// Native Laravel Database Query Builder
5353
// $user_ids = DB::table('expired_users')->lists('user_id');

docs/persistence/sql/expressions.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,14 @@ you can simply change the value of :php:attr:`$connection` property::
175175

176176
$expr->connection = $pdo_dbh;
177177

178-
Finally, you can pass connection class into :php:meth:`execute` directly.
178+
Finally, you can pass connection class into :php:meth:`executeQuery` directly.
179179

180-
.. php:method:: execute($connection = null)
180+
.. php:method:: executeQuery($connection = null)
181181
182182
Executes expression using current database connection or the one you
183183
specify as the argument::
184184

185-
$stmt = $expr->execute($pdo_dbh);
185+
$stmt = $expr->executeQuery($pdo_dbh);
186186

187187
returns `Doctrine\DBAL\Result`.
188188

docs/persistence/sql/queries.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ to re-use your query efficiently::
9191
if ($row) {
9292
$query
9393
->set('revision', $query->expr('revision + 1'))
94-
->mode('update')->execute();
94+
->mode('update')->executeStatement();
9595
} else {
9696
$query
9797
->set('revision', 1)
98-
->mode('insert')->execute();
98+
->mode('insert')->executeStatement();
9999
}
100100

101101
The example above will perform a select query first:
@@ -665,10 +665,10 @@ Set value to a field
665665

666666
Example::
667667

668-
$q->table('user')->set('name', 'john')->mode('insert')->execute();
668+
$q->table('user')->set('name', 'john')->mode('insert')->executeStatement();
669669
// insert into user (name) values (john)
670670

671-
$q->table('log')->set('date', $q->expr('now()'))->mode('insert')->execute();
671+
$q->table('log')->set('date', $q->expr('now()'))->mode('insert')->executeStatement();
672672
// insert into log (date) values (now())
673673

674674
Method can be executed several times on the same Query object.
@@ -782,8 +782,8 @@ Other Methods
782782
->option('calc_found_rows') // for default select mode
783783
->option('ignore', 'insert') // for insert mode;
784784

785-
$q->execute(); // select calc_found_rows `name` from `test`
786-
$q->mode('insert')->execute(); // insert ignore into `test` (`name`) values (`name` = 'John')
785+
$q->executeQuery(); // select calc_found_rows `name` from `test`
786+
$q->mode('insert')->executeStatement(); // insert ignore into `test` (`name`) values (`name` = 'John')
787787

788788
.. php:method:: _set_args($what, $alias, $value)
789789

docs/persistence/sql/quickstart.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ DSQL classes are mindful about your SQL vendor and it's quirks, so when you're
162162
building sub-queries with :php:meth:`Query::dsql`, you can avoid some nasty
163163
problems::
164164

165-
$sqlite_c->dsql()->table('user')->mode('truncate')->execute();
165+
$sqlite_c->dsql()->table('user')->mode('truncate')->executeStatement();
166166

167167
The above code will work even though SQLite does not support truncate. That's
168168
because DSQL takes care of this.
@@ -191,15 +191,15 @@ You can actually perform multiple operations::
191191

192192
$q = $c->dsql()->table('employee')->where('emp_no', 1234);
193193
$backup_data = $q->getRows();
194-
$q->mode('delete')->execute();
194+
$q->mode('delete')->executeStatement();
195195

196196
A good practice is to re-use the same query object before you branch out and
197197
perform the action::
198198

199199
$q = $c->dsql()->table('employee')->where('emp_no', 1234);
200200

201201
if ($confirmed) {
202-
$q->mode('delete')->execute();
202+
$q->mode('delete')->executeStatement();
203203
} else {
204204
echo "Are you sure you want to delete ".$q->field('count(*)')." employees?";
205205
}

docs/persistence/sql/transactions.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ It is recommended to always use atomic() in your code.
2020
exception, whole transaction will be automatically rolled back::
2121

2222
$c->atomic(function () use ($c) {
23-
$c->dsql('user')->set('balance=balance+10')->where('id', 10)->mode('update')->execute();
24-
$c->dsql('user')->set('balance=balance-10')->where('id', 14)->mode('update')->execute();
23+
$c->dsql('user')->set('balance=balance+10')->where('id', 10)->mode('update')->executeStatement();
24+
$c->dsql('user')->set('balance=balance-10')->where('id', 14)->mode('update')->executeStatement();
2525
});
2626

2727
atomic() can be nested.

docs/sql.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ In short this should allow you to build and execute any SQL statement::
335335
$this->expr("call get_nominal_sheet([],[],'2014-10-01','2015-09-30',0)", [
336336
$this->getApp()->system->getId(),
337337
$this->getApp()->system['contractor_id']
338-
])->execute();
338+
])->executeQuery();
339339

340340
Depending on the statement you can also use your statement to retrieve data::
341341

@@ -469,7 +469,7 @@ procedure inside Model::init() then set $table property to a temporary table::
469469
$res = $this->expr("call get_nominal_sheet([],[],'2014-10-01','2015-09-30',0)", [
470470
$this->getApp()->system->getId(),
471471
$this->getApp()->system['contractor_id']
472-
])->execute();
472+
])->executeQuery();
473473

474474
$this->addField('date', ['type' => 'date']);
475475
$this->addField('items', ['type' => 'integer']);

src/Persistence/Sql.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ protected function insertRaw(Model $model, array $dataRaw)
551551

552552
try {
553553
$model->hook(self::HOOK_BEFORE_INSERT_QUERY, [$insert]);
554-
$c = $insert->execute()->rowCount();
554+
$c = $insert->executeStatement();
555555
} catch (SqlException $e) {
556556
throw (new Exception('Unable to execute insert query', 0, $e))
557557
->addMoreInfo('model', $model)
@@ -584,7 +584,7 @@ protected function updateRaw(Model $model, $idRaw, array $dataRaw): void
584584
$model->hook(self::HOOK_BEFORE_UPDATE_QUERY, [$update]);
585585

586586
try {
587-
$c = $update->execute()->rowCount();
587+
$c = $update->executeStatement();
588588
} catch (SqlException $e) {
589589
throw (new Exception('Unable to update due to query error', 0, $e))
590590
->addMoreInfo('model', $model)
@@ -622,7 +622,7 @@ protected function deleteRaw(Model $model, $idRaw): void
622622
$model->hook(self::HOOK_BEFORE_DELETE_QUERY, [$delete]);
623623

624624
try {
625-
$c = $delete->execute()->rowCount();
625+
$c = $delete->executeStatement();
626626
} catch (SqlException $e) {
627627
throw (new Exception('Unable to delete due to query error', 0, $e))
628628
->addMoreInfo('model', $model)

src/Persistence/Sql/Connection.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,29 @@ public function connection()
311311
}
312312

313313
/**
314-
* Execute Expression by using this connection.
314+
* Execute Expression by using this connection and return result.
315315
*/
316-
public function execute(Expression $expr): DbalResult
316+
public function executeQuery(Expression $expr): DbalResult
317317
{
318318
if ($this->connection === null) {
319319
throw new Exception('DBAL connection is not set');
320320
}
321321

322-
return $expr->execute($this->connection);
322+
return $expr->executeQuery($this->connection);
323+
}
324+
325+
/**
326+
* Execute Expression by using this connection and return affected rows.
327+
*
328+
* @phpstan-return int<0, max>
329+
*/
330+
public function executeStatement(Expression $expr): int
331+
{
332+
if ($this->connection === null) {
333+
throw new Exception('DBAL connection is not set');
334+
}
335+
336+
return $expr->executeStatement($this->connection);
323337
}
324338

325339
/**

src/Persistence/Sql/Expression.php

+42-6
Original file line numberDiff line numberDiff line change
@@ -530,15 +530,29 @@ function ($matches) use ($params, &$numParams, &$i, &$j) {
530530

531531
/**
532532
* @param DbalConnection|Connection $connection
533+
*
534+
* @return DbalResult|int<0, max>
535+
*
536+
* @deprecated Expression::execute() is deprecated and will be removed in v4.0, use Expression::executeQuery() or Expression::executeStatement() instead
533537
*/
534-
public function execute(object $connection = null): DbalResult
538+
public function execute(object $connection = null, bool $fromExecuteStatement = null)
535539
{
536540
if ($connection === null) {
537541
$connection = $this->connection;
538542
}
539543

544+
if ($fromExecuteStatement === null) {
545+
'trigger_error'('Method is deprecated. Use executeQuery() or executeStatement() instead', \E_USER_DEPRECATED);
546+
547+
$fromExecuteStatement = false;
548+
}
549+
540550
if (!$connection instanceof DbalConnection) {
541-
return $connection->execute($this);
551+
if ($fromExecuteStatement) {
552+
return $connection->executeStatement($this);
553+
}
554+
555+
return $connection->executeQuery($this);
542556
}
543557

544558
[$query, $params] = $this->updateRenderBeforeExecute($this->render());
@@ -597,7 +611,11 @@ public function execute(object $connection = null): DbalResult
597611
}
598612
}
599613

600-
$result = $statement->execute(); // @phpstan-ignore-line
614+
if ($fromExecuteStatement) {
615+
$result = $statement->executeStatement();
616+
} else {
617+
$result = $statement->executeQuery();
618+
}
601619

602620
return $result;
603621
} catch (DbalException $e) {
@@ -617,6 +635,24 @@ public function execute(object $connection = null): DbalResult
617635
}
618636
}
619637

638+
/**
639+
* @param DbalConnection|Connection $connection
640+
*/
641+
public function executeQuery(object $connection = null): DbalResult
642+
{
643+
return $this->execute($connection, false); // @phpstan-ignore-line
644+
}
645+
646+
/**
647+
* @param DbalConnection|Connection $connection
648+
*
649+
* @phpstan-return int<0, max>
650+
*/
651+
public function executeStatement(object $connection = null): int
652+
{
653+
return $this->execute($connection, true); // @phpstan-ignore-line
654+
}
655+
620656
// {{{ Result Querying
621657

622658
/**
@@ -648,7 +684,7 @@ public function getRowsIterator(): \Traversable
648684
{
649685
// DbalResult::iterateAssociative() is broken with streams with Oracle database
650686
// https://github.com/doctrine/dbal/issues/5002
651-
$iterator = $this->execute()->iterateAssociative();
687+
$iterator = $this->executeQuery()->iterateAssociative();
652688

653689
foreach ($iterator as $row) {
654690
yield array_map(function ($v) {
@@ -666,7 +702,7 @@ public function getRows(): array
666702
{
667703
// DbalResult::fetchAllAssociative() is broken with streams with Oracle database
668704
// https://github.com/doctrine/dbal/issues/5002
669-
$result = $this->execute();
705+
$result = $this->executeQuery();
670706

671707
$rows = [];
672708
while (($row = $result->fetchAssociative()) !== false) {
@@ -685,7 +721,7 @@ public function getRows(): array
685721
*/
686722
public function getRow(): ?array
687723
{
688-
$row = $this->execute()->fetchAssociative();
724+
$row = $this->executeQuery()->fetchAssociative();
689725

690726
if ($row === false) {
691727
return null;

src/Schema/Migrator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function dropIfExists(): self
123123
if ($this->getDatabasePlatform() instanceof OraclePlatform) {
124124
$dropTriggerSql = $this->getDatabasePlatform()->getDropAutoincrementSql($this->table->getName())[1];
125125
try {
126-
$this->connection->expr($dropTriggerSql)->execute();
126+
$this->connection->expr($dropTriggerSql)->executeStatement();
127127
} catch (Exception $e) {
128128
}
129129
}

src/Schema/TestCase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function setUp(): void
3838
if ($this->db->getDatabasePlatform() instanceof MySQLPlatform) {
3939
$this->db->connection->expr(
4040
'SET SESSION auto_increment_increment = 1, SESSION auto_increment_offset = 1'
41-
)->execute();
41+
)->executeStatement();
4242
}
4343

4444
$this->db->connection->connection()->getConfiguration()->setSQLLogger(
@@ -293,7 +293,7 @@ public function setDb(array $dbData, bool $importData = true): void
293293
$query->set($idColumnName, $id);
294294
}
295295

296-
$query->mode('insert')->execute();
296+
$query->mode('insert')->executeStatement();
297297
}
298298
}
299299
}

tests/JoinSqlTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function testJoinSaving2(): void
125125
],
126126
], $this->getDb(['user', 'contact']));
127127

128-
$this->db->connection->dsql()->table('contact')->where('id', 2)->mode('delete')->execute();
128+
$this->db->connection->dsql()->table('contact')->where('id', 2)->mode('delete')->executeStatement();
129129

130130
$m_u2->unload();
131131
$m_u2 = $m_u->createEntity();

tests/Persistence/Sql/ConnectionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,6 @@ public function testException4(): void
179179
);
180180

181181
$this->expectException(\Atk4\Data\Persistence\Sql\Exception::class);
182-
$q->execute();
182+
$q->executeQuery();
183183
}
184184
}

tests/Persistence/Sql/WithDb/SelectTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function testExpression(): void
158158
public function testOtherQueries(): void
159159
{
160160
// truncate table
161-
$this->q('employee')->mode('truncate')->execute();
161+
$this->q('employee')->mode('truncate')->executeStatement();
162162
$this->assertSame(
163163
'0',
164164
$this->q('employee')->field(new Expression('count(*)'))->getOne()
@@ -167,10 +167,10 @@ public function testOtherQueries(): void
167167
// insert
168168
$this->q('employee')
169169
->setMulti(['id' => 1, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
170-
->mode('insert')->execute();
170+
->mode('insert')->executeStatement();
171171
$this->q('employee')
172172
->setMulti(['id' => 2, 'name' => 'Jane', 'surname' => 'Doe', 'retired' => false])
173-
->mode('insert')->execute();
173+
->mode('insert')->executeStatement();
174174
$this->assertSame(
175175
[['id' => '1', 'name' => 'John'], ['id' => '2', 'name' => 'Jane']],
176176
$this->q('employee')->field('id')->field('name')->order('id')->getRows()
@@ -180,7 +180,7 @@ public function testOtherQueries(): void
180180
$this->q('employee')
181181
->where('name', 'John')
182182
->set('name', 'Johnny')
183-
->mode('update')->execute();
183+
->mode('update')->executeStatement();
184184
$this->assertSame(
185185
[['id' => '1', 'name' => 'Johnny'], ['id' => '2', 'name' => 'Jane']],
186186
$this->q('employee')->field('id')->field('name')->order('id')->getRows()
@@ -191,11 +191,11 @@ public function testOtherQueries(): void
191191
$this->q('employee')
192192
->setMulti(['name' => 'Peter', 'surname' => 'Doe', 'retired' => true])
193193
->where('id', 1)
194-
->mode('update')->execute();
194+
->mode('update')->executeStatement();
195195
} else {
196196
$this->q('employee')
197197
->setMulti(['id' => 1, 'name' => 'Peter', 'surname' => 'Doe', 'retired' => true])
198-
->mode('replace')->execute();
198+
->mode('replace')->executeStatement();
199199
}
200200

201201
// In SQLite replace is just like insert, it just checks if there is
@@ -217,7 +217,7 @@ public function testOtherQueries(): void
217217
// delete
218218
$this->q('employee')
219219
->where('retired', true)
220-
->mode('delete')->execute();
220+
->mode('delete')->executeStatement();
221221
$this->assertSame(
222222
[['id' => '2', 'name' => 'Jane']],
223223
$this->q('employee')->field('id')->field('name')->getRows()
@@ -227,7 +227,7 @@ public function testOtherQueries(): void
227227
public function testEmptyGetOne(): void
228228
{
229229
// truncate table
230-
$this->q('employee')->mode('truncate')->execute();
230+
$this->q('employee')->mode('truncate')->executeStatement();
231231
$this->expectException(Exception::class);
232232
$this->q('employee')->field('name')->getOne();
233233
}

0 commit comments

Comments
 (0)