Skip to content

Commit 28506be

Browse files
authored
Add Model::executeCountQuery() method (#1008)
1 parent 2c501c7 commit 28506be

15 files changed

+66
-54
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,7 @@ Now you can explore. Try typing:
609609
> $m->loadBy('email', 'example@example.com')
610610
> $m->get()
611611
> $m->export(['email', 'name'])
612-
> $m->action('count')
613-
> $m->action('count')->getOne()
612+
> $m->executeCountQuery()
614613
```
615614

616615
## Agile Core and DSQL

docs/persistence.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ Create copy of existing record
415415

416416
// Now you have 2 records:
417417
// one with ID = 123 and another with ID = {next db generated id}
418-
echo $m->action('count')->getOne();
418+
echo $m->executeCountQuery();
419419

420420
Duplicate then save under a new ID
421421
----------------------------------
@@ -757,7 +757,7 @@ rows of data.
757757
Action can be executed at any time and that will return an expected result::
758758

759759
$m = Model_Invoice();
760-
$val = $m->action('count')->getOne();
760+
$val = (int) $m->action('count')->getOne(); // same as $val = $m->executeCountQuery()
761761

762762
Most actions are sufficiently smart to understand what type of result you are
763763
expecting, so you can have the following code::
@@ -801,7 +801,7 @@ There are ability to execute aggregation functions::
801801

802802
and finally you can also use count::
803803

804-
echo $m->action('count')->getOne();
804+
echo $m->executeCountQuery(); // same as echo $m->action('count')->getOne()
805805

806806

807807
SQL Actions on Linked Records

docs/quickstart.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ inside console::
138138
$m->addCondition('address_1', 'not', null);
139139
$m = $m->loadAny();
140140
$m->get();
141-
$m->action('count')->getOne();
141+
$m->executeCountQuery(); // same as ((int) $m->action('count')->getOne())
142142

143143
Next, exit and create file `src/Model_ContactInfo.php`::
144144

@@ -454,7 +454,7 @@ corresponding to all Systems that belong to user john. You can use the following
454454
to see number of records in DataSet or export DataSet::
455455

456456
$s->isLoaded();
457-
$s->action('count')->getOne();
457+
$s->executeCountQuery();
458458
$s->export();
459459
$s->action('count')->getDebugQuery();
460460

@@ -471,7 +471,7 @@ the Clients that are contained in all of the Systems that belong to user john.
471471
You can examine the this model further::
472472

473473
$c->isLoaded();
474-
$c->action('count')->getOne();
474+
$c->executeCountQuery();
475475
$c->export();
476476
$c->action('count')->getDebugQuery();
477477

@@ -529,7 +529,7 @@ basic aggregation without grouping. This type of aggregation provides some
529529
specific value from a data-set. SQL persistence implements some of the operations::
530530

531531
$m = new Model_Invoice($db);
532-
$m->action('count')->getOne();
532+
$m->executeCountQuery();
533533
$m->action('fx', ['sum', 'total'])->getOne();
534534
$m->action('fx', ['max', 'shipping'])->getOne();
535535

docs/sql.rst

+2
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ Returns query for `count(*)`::
247247

248248
$action = $model->action('count');
249249
$cnt = $action->getOne();
250+
// for materialized count use:
251+
$cnt = $model->executeCountQuery();
250252

251253
You can also specify alias::
252254

src/Model.php

+12
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,18 @@ public function action(string $mode, array $args = [])
19261926
return $this->persistence->action($this, $mode, $args);
19271927
}
19281928

1929+
public function executeCountQuery(): int
1930+
{
1931+
$this->assertIsModel();
1932+
1933+
$res = $this->action('count')->getOne();
1934+
if (is_string($res) && $res === (string) (int) $res) {
1935+
$res = (int) $res;
1936+
}
1937+
1938+
return $res;
1939+
}
1940+
19291941
// }}}
19301942

19311943
// {{{ Expressions

src/Persistence/Sql/Query.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function _render_field($add_alias = true): string
116116
if (
117117
$add_alias === false
118118
|| (is_string($field) && $alias === $field)
119-
|| is_numeric($alias)
119+
|| is_int($alias)
120120
) {
121121
$alias = null;
122122
}
@@ -225,7 +225,7 @@ protected function _render_table($add_alias = true): ?string
225225
if (
226226
$add_alias === false
227227
|| (is_string($table) && $alias === $table)
228-
|| is_numeric($alias)
228+
|| is_int($alias)
229229
) {
230230
$alias = null;
231231
}

tests/ConditionSqlTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,13 @@ public function testOrConditions(): void
383383
['name', 'Peter'],
384384
));
385385

386-
$this->assertSame('2', $u->action('count')->getOne());
386+
$this->assertSame(2, $u->executeCountQuery());
387387

388388
$u->addCondition(Model\Scope::createOr(
389389
['name', 'Peter'],
390390
['name', 'Joe'],
391391
));
392-
$this->assertSame('1', $u->action('count')->getOne());
392+
$this->assertSame(1, $u->executeCountQuery());
393393
}
394394

395395
/**

tests/ModelWithoutIdTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function testInsert(): void
8686
}
8787

8888
$this->m->insert(['name' => 'Joe']);
89-
$this->assertSame('3', $this->m->action('count')->getOne());
89+
$this->assertSame(3, $this->m->executeCountQuery());
9090
}
9191

9292
/**
@@ -101,7 +101,7 @@ public function testSave1(): void
101101
$m = $this->m->tryLoadAny();
102102
$m->saveAndUnload();
103103

104-
$this->assertSame('3', $this->m->action('count')->getOne());
104+
$this->assertSame(3, $this->m->executeCountQuery());
105105
}
106106

107107
/**
@@ -116,7 +116,7 @@ public function testSave2(): void
116116
$m = $this->m->tryLoadAny();
117117
$m->save();
118118

119-
$this->assertSame('3', $this->m->action('count')->getOne());
119+
$this->assertSame(3, $this->m->executeCountQuery());
120120
}
121121

122122
/**

tests/Persistence/ArrayTest.php

+18-19
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ public function testActionCount(): void
244244
$m->addField('surname');
245245

246246
$this->assertSame(2, $m->action('count')->getOne());
247+
$this->assertSame(2, $m->executeCountQuery());
247248
}
248249

249250
/**
@@ -259,8 +260,6 @@ public function testActionField(): void
259260
$m->addField('name');
260261
$m->addField('surname');
261262

262-
$this->assertSame(2, $m->action('count')->getOne());
263-
264263
// use alias as array key if it is set
265264
$q = $m->action('field', ['name', 'alias' => 'first_name']);
266265
$this->assertSame([
@@ -643,40 +642,40 @@ public function testImportAndAutoincrement(): void
643642
['id' => 1, 'f1' => 'A'],
644643
['id' => 2, 'f1' => 'B'],
645644
]);
646-
$this->assertSame(2, $m->action('count')->getOne());
645+
$this->assertSame(2, $m->executeCountQuery());
647646

648647
$m->import([
649648
['f1' => 'C'],
650649
['f1' => 'D'],
651650
]);
652-
$this->assertSame(4, $m->action('count')->getOne());
651+
$this->assertSame(4, $m->executeCountQuery());
653652

654653
$m->import([
655654
['id' => 6, 'f1' => 'E'],
656655
['id' => 7, 'f1' => 'F'],
657656
]);
658-
$this->assertSame(6, $m->action('count')->getOne());
657+
$this->assertSame(6, $m->executeCountQuery());
659658

660659
$m->delete(6);
661-
$this->assertSame(5, $m->action('count')->getOne());
660+
$this->assertSame(5, $m->executeCountQuery());
662661

663662
$m->import([
664663
['f1' => 'G'],
665664
['f1' => 'H'],
666665
]);
667-
$this->assertSame(7, $m->action('count')->getOne());
666+
$this->assertSame(7, $m->executeCountQuery());
668667

669668
$m->import([
670669
['id' => 99, 'f1' => 'I'],
671670
['id' => 20, 'f1' => 'J'],
672671
]);
673-
$this->assertSame(9, $m->action('count')->getOne());
672+
$this->assertSame(9, $m->executeCountQuery());
674673

675674
$m->import([
676675
['f1' => 'K'],
677676
['f1' => 'L'],
678677
]);
679-
$this->assertSame(11, $m->action('count')->getOne());
678+
$this->assertSame(11, $m->executeCountQuery());
680679

681680
$m->delete(100);
682681
$m->createEntity()->set('f1', 'M')->save();
@@ -711,18 +710,18 @@ public function testLimit(): void
711710
$m = new Model($p);
712711
$m->addField('f1');
713712

714-
$this->assertSame(4, $m->action('count')->getOne());
713+
$this->assertSame(4, $m->executeCountQuery());
715714

716715
$m->setLimit(3);
717-
$this->assertSame(3, $m->action('count')->getOne());
716+
$this->assertSame(3, $m->executeCountQuery());
718717
$this->assertSame([
719718
['id' => 1, 'f1' => 'A'],
720719
['id' => 2, 'f1' => 'D'],
721720
['id' => 3, 'f1' => 'E'],
722721
], array_values($m->export()));
723722

724723
$m->setLimit(2, 1);
725-
$this->assertSame(2, $m->action('count')->getOne());
724+
$this->assertSame(2, $m->executeCountQuery());
726725
$this->assertSame([
727726
['id' => 2, 'f1' => 'D'],
728727
['id' => 3, 'f1' => 'E'],
@@ -731,7 +730,7 @@ public function testLimit(): void
731730
// well, this is strange, that you can actually change limit on-the-fly and then previous
732731
// limit is not taken into account, but most likely you will never set it multiple times
733732
$m->setLimit(3);
734-
$this->assertSame(3, $m->action('count')->getOne());
733+
$this->assertSame(3, $m->executeCountQuery());
735734
}
736735

737736
/**
@@ -749,19 +748,19 @@ public function testCondition(): void
749748
$m->addField('name');
750749
$m->addField('surname');
751750

752-
$this->assertSame(4, $m->action('count')->getOne());
751+
$this->assertSame(4, $m->executeCountQuery());
753752
$this->assertSame(['data' => $dbData], $this->getInternalPersistenceData($p));
754753

755754
$m->addCondition('name', 'Sarah');
756-
$this->assertSame(3, $m->action('count')->getOne());
755+
$this->assertSame(3, $m->executeCountQuery());
757756

758757
$m->addCondition('surname', 'Smith');
759-
$this->assertSame(1, $m->action('count')->getOne());
758+
$this->assertSame(1, $m->executeCountQuery());
760759
$this->assertSame([4 => ['id' => 4, 'name' => 'Sarah', 'surname' => 'Smith']], $m->export());
761760
$this->assertSame([4 => ['id' => 4, 'name' => 'Sarah', 'surname' => 'Smith']], $m->action('select')->getRows());
762761

763762
$m->addCondition('surname', 'Siiiith');
764-
$this->assertSame(0, $m->action('count')->getOne());
763+
$this->assertSame(0, $m->executeCountQuery());
765764
}
766765

767766
public function testUnsupportedAction(): void
@@ -863,10 +862,10 @@ public function testHasMany(): void
863862
$user->hasOne('country_id', ['model' => $country]);
864863

865864
$cc = $country->load(1);
866-
$this->assertSame(2, $cc->ref('Users')->action('count')->getOne());
865+
$this->assertSame(2, $cc->ref('Users')->executeCountQuery());
867866

868867
$cc = $country->load(2);
869-
$this->assertSame(1, $cc->ref('Users')->action('count')->getOne());
868+
$this->assertSame(1, $cc->ref('Users')->executeCountQuery());
870869
}
871870

872871
public function testLoadAnyThrowsExceptionOnRecordNotFound(): void

tests/Persistence/Sql/WithDb/SelectTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -326,46 +326,46 @@ public function testImportAndAutoincrement(): void
326326
['id' => 1, 'f1' => 'A'],
327327
['id' => 2, 'f1' => 'B'],
328328
]);
329-
$this->assertSame('2', $m->action('count')->getOne());
329+
$this->assertSame(2, $m->executeCountQuery());
330330
$this->assertSame(2, $getLastAiFx());
331331

332332
$m->import([
333333
['f1' => 'C'],
334334
['f1' => 'D'],
335335
]);
336-
$this->assertSame('4', $m->action('count')->getOne());
336+
$this->assertSame(4, $m->executeCountQuery());
337337
$this->assertSame(4, $getLastAiFx());
338338

339339
$m->import([
340340
['id' => 6, 'f1' => 'E'],
341341
['id' => 7, 'f1' => 'F'],
342342
]);
343-
$this->assertSame('6', $m->action('count')->getOne());
343+
$this->assertSame(6, $m->executeCountQuery());
344344
$this->assertSame(7, $getLastAiFx());
345345

346346
$m->delete(6);
347-
$this->assertSame('5', $m->action('count')->getOne());
347+
$this->assertSame(5, $m->executeCountQuery());
348348
$this->assertSame(7, $getLastAiFx());
349349

350350
$m->import([
351351
['f1' => 'G'],
352352
['f1' => 'H'],
353353
]);
354-
$this->assertSame('7', $m->action('count')->getOne());
354+
$this->assertSame(7, $m->executeCountQuery());
355355
$this->assertSame(9, $getLastAiFx());
356356

357357
$m->import([
358358
['id' => 99, 'f1' => 'I'],
359359
['id' => 20, 'f1' => 'J'],
360360
]);
361-
$this->assertSame('9', $m->action('count')->getOne());
361+
$this->assertSame(9, $m->executeCountQuery());
362362
$this->assertSame(99, $getLastAiFx());
363363

364364
$m->import([
365365
['f1' => 'K'],
366366
['f1' => 'L'],
367367
]);
368-
$this->assertSame('11', $m->action('count')->getOne());
368+
$this->assertSame(11, $m->executeCountQuery());
369369
$this->assertSame(101, $getLastAiFx());
370370

371371
$m->delete(100);

tests/Persistence/SqlTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function testModelInsertRows(): void
177177

178178
$this->assertSame('1', $m->action('exists')->getOne());
179179

180-
$this->assertSame('2', $m->action('count')->getOne());
180+
$this->assertSame(2, $m->executeCountQuery());
181181
}
182182

183183
public function testPersistenceDelete(): void

tests/RandomTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testRate(): void
8888

8989
$m = new Model_Rate($this->db);
9090

91-
$this->assertSame('2', $m->action('count')->getOne());
91+
$this->assertSame(2, $m->executeCountQuery());
9292
}
9393

9494
public function testTitleImport(): void
@@ -234,7 +234,7 @@ public function testSameTable3(): void
234234
$m->load(2)->get()
235235
);
236236

237-
$this->assertSame('1', $m->load(2)->ref('Child', ['table_alias' => 'pp'])->action('count')->getOne());
237+
$this->assertSame(1, $m->load(2)->ref('Child', ['table_alias' => 'pp'])->executeCountQuery());
238238
$this->assertSame('John', $m->load(2)->ref('parent_item_id', ['table_alias' => 'pp'])->get('name'));
239239
}
240240

tests/Schema/ModelTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private function makePseudoRandomString(bool $isBinary, int $length): string
158158
for ($i = 0; $i <= 0x10FFFF; $i = $i * 1.001 + 1) {
159159
$iInt = (int) $i;
160160
if ($iInt < 0xD800 || $iInt > 0xDFFF) {
161-
$baseChars[crc32($length . '_' . $i)] = mb_chr($iInt);
161+
$baseChars[crc32($length . '_' . $iInt)] = mb_chr($iInt);
162162
}
163163
}
164164
}

0 commit comments

Comments
 (0)