Skip to content

Commit

Permalink
Add Model::executeCountQuery() method (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Jun 17, 2022
1 parent 2c501c7 commit 28506be
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 54 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,7 @@ Now you can explore. Try typing:
> $m->loadBy('email', 'example@example.com')
> $m->get()
> $m->export(['email', 'name'])
> $m->action('count')
> $m->action('count')->getOne()
> $m->executeCountQuery()
```

## Agile Core and DSQL
Expand Down
6 changes: 3 additions & 3 deletions docs/persistence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ Create copy of existing record

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

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

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

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

and finally you can also use count::

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


SQL Actions on Linked Records
Expand Down
8 changes: 4 additions & 4 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ inside console::
$m->addCondition('address_1', 'not', null);
$m = $m->loadAny();
$m->get();
$m->action('count')->getOne();
$m->executeCountQuery(); // same as ((int) $m->action('count')->getOne())

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

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

$s->isLoaded();
$s->action('count')->getOne();
$s->executeCountQuery();
$s->export();
$s->action('count')->getDebugQuery();

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

$c->isLoaded();
$c->action('count')->getOne();
$c->executeCountQuery();
$c->export();
$c->action('count')->getDebugQuery();

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

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

Expand Down
2 changes: 2 additions & 0 deletions docs/sql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ Returns query for `count(*)`::

$action = $model->action('count');
$cnt = $action->getOne();
// for materialized count use:
$cnt = $model->executeCountQuery();

You can also specify alias::

Expand Down
12 changes: 12 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,18 @@ public function action(string $mode, array $args = [])
return $this->persistence->action($this, $mode, $args);
}

public function executeCountQuery(): int
{
$this->assertIsModel();

$res = $this->action('count')->getOne();
if (is_string($res) && $res === (string) (int) $res) {
$res = (int) $res;
}

return $res;
}

// }}}

// {{{ Expressions
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Sql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected function _render_field($add_alias = true): string
if (
$add_alias === false
|| (is_string($field) && $alias === $field)
|| is_numeric($alias)
|| is_int($alias)
) {
$alias = null;
}
Expand Down Expand Up @@ -225,7 +225,7 @@ protected function _render_table($add_alias = true): ?string
if (
$add_alias === false
|| (is_string($table) && $alias === $table)
|| is_numeric($alias)
|| is_int($alias)
) {
$alias = null;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,13 @@ public function testOrConditions(): void
['name', 'Peter'],
));

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

$u->addCondition(Model\Scope::createOr(
['name', 'Peter'],
['name', 'Joe'],
));
$this->assertSame('1', $u->action('count')->getOne());
$this->assertSame(1, $u->executeCountQuery());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/ModelWithoutIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function testInsert(): void
}

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

/**
Expand All @@ -101,7 +101,7 @@ public function testSave1(): void
$m = $this->m->tryLoadAny();
$m->saveAndUnload();

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

/**
Expand All @@ -116,7 +116,7 @@ public function testSave2(): void
$m = $this->m->tryLoadAny();
$m->save();

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

/**
Expand Down
37 changes: 18 additions & 19 deletions tests/Persistence/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public function testActionCount(): void
$m->addField('surname');

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

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

$this->assertSame(2, $m->action('count')->getOne());

// use alias as array key if it is set
$q = $m->action('field', ['name', 'alias' => 'first_name']);
$this->assertSame([
Expand Down Expand Up @@ -643,40 +642,40 @@ public function testImportAndAutoincrement(): void
['id' => 1, 'f1' => 'A'],
['id' => 2, 'f1' => 'B'],
]);
$this->assertSame(2, $m->action('count')->getOne());
$this->assertSame(2, $m->executeCountQuery());

$m->import([
['f1' => 'C'],
['f1' => 'D'],
]);
$this->assertSame(4, $m->action('count')->getOne());
$this->assertSame(4, $m->executeCountQuery());

$m->import([
['id' => 6, 'f1' => 'E'],
['id' => 7, 'f1' => 'F'],
]);
$this->assertSame(6, $m->action('count')->getOne());
$this->assertSame(6, $m->executeCountQuery());

$m->delete(6);
$this->assertSame(5, $m->action('count')->getOne());
$this->assertSame(5, $m->executeCountQuery());

$m->import([
['f1' => 'G'],
['f1' => 'H'],
]);
$this->assertSame(7, $m->action('count')->getOne());
$this->assertSame(7, $m->executeCountQuery());

$m->import([
['id' => 99, 'f1' => 'I'],
['id' => 20, 'f1' => 'J'],
]);
$this->assertSame(9, $m->action('count')->getOne());
$this->assertSame(9, $m->executeCountQuery());

$m->import([
['f1' => 'K'],
['f1' => 'L'],
]);
$this->assertSame(11, $m->action('count')->getOne());
$this->assertSame(11, $m->executeCountQuery());

$m->delete(100);
$m->createEntity()->set('f1', 'M')->save();
Expand Down Expand Up @@ -711,18 +710,18 @@ public function testLimit(): void
$m = new Model($p);
$m->addField('f1');

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

$m->setLimit(3);
$this->assertSame(3, $m->action('count')->getOne());
$this->assertSame(3, $m->executeCountQuery());
$this->assertSame([
['id' => 1, 'f1' => 'A'],
['id' => 2, 'f1' => 'D'],
['id' => 3, 'f1' => 'E'],
], array_values($m->export()));

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

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

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

$m->addCondition('name', 'Sarah');
$this->assertSame(3, $m->action('count')->getOne());
$this->assertSame(3, $m->executeCountQuery());

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

$m->addCondition('surname', 'Siiiith');
$this->assertSame(0, $m->action('count')->getOne());
$this->assertSame(0, $m->executeCountQuery());
}

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

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

$cc = $country->load(2);
$this->assertSame(1, $cc->ref('Users')->action('count')->getOne());
$this->assertSame(1, $cc->ref('Users')->executeCountQuery());
}

public function testLoadAnyThrowsExceptionOnRecordNotFound(): void
Expand Down
14 changes: 7 additions & 7 deletions tests/Persistence/Sql/WithDb/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,46 +326,46 @@ public function testImportAndAutoincrement(): void
['id' => 1, 'f1' => 'A'],
['id' => 2, 'f1' => 'B'],
]);
$this->assertSame('2', $m->action('count')->getOne());
$this->assertSame(2, $m->executeCountQuery());
$this->assertSame(2, $getLastAiFx());

$m->import([
['f1' => 'C'],
['f1' => 'D'],
]);
$this->assertSame('4', $m->action('count')->getOne());
$this->assertSame(4, $m->executeCountQuery());
$this->assertSame(4, $getLastAiFx());

$m->import([
['id' => 6, 'f1' => 'E'],
['id' => 7, 'f1' => 'F'],
]);
$this->assertSame('6', $m->action('count')->getOne());
$this->assertSame(6, $m->executeCountQuery());
$this->assertSame(7, $getLastAiFx());

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

$m->import([
['f1' => 'G'],
['f1' => 'H'],
]);
$this->assertSame('7', $m->action('count')->getOne());
$this->assertSame(7, $m->executeCountQuery());
$this->assertSame(9, $getLastAiFx());

$m->import([
['id' => 99, 'f1' => 'I'],
['id' => 20, 'f1' => 'J'],
]);
$this->assertSame('9', $m->action('count')->getOne());
$this->assertSame(9, $m->executeCountQuery());
$this->assertSame(99, $getLastAiFx());

$m->import([
['f1' => 'K'],
['f1' => 'L'],
]);
$this->assertSame('11', $m->action('count')->getOne());
$this->assertSame(11, $m->executeCountQuery());
$this->assertSame(101, $getLastAiFx());

$m->delete(100);
Expand Down
2 changes: 1 addition & 1 deletion tests/Persistence/SqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function testModelInsertRows(): void

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

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

public function testPersistenceDelete(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testRate(): void

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

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

public function testTitleImport(): void
Expand Down Expand Up @@ -234,7 +234,7 @@ public function testSameTable3(): void
$m->load(2)->get()
);

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

Expand Down
2 changes: 1 addition & 1 deletion tests/Schema/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private function makePseudoRandomString(bool $isBinary, int $length): string
for ($i = 0; $i <= 0x10FFFF; $i = $i * 1.001 + 1) {
$iInt = (int) $i;
if ($iInt < 0xD800 || $iInt > 0xDFFF) {
$baseChars[crc32($length . '_' . $i)] = mb_chr($iInt);
$baseChars[crc32($length . '_' . $iInt)] = mb_chr($iInt);
}
}
}
Expand Down
Loading

0 comments on commit 28506be

Please sign in to comment.