Skip to content

Commit 77c3c2f

Browse files
committed
simplified filterParam to account for boolean and use placeholders for int
1 parent a262d99 commit 77c3c2f

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/ActiveRecord.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ abstract class ActiveRecord extends Base implements JsonSerializable
100100
/**
101101
* Database connection
102102
*
103-
* @var DatabaseInterface|null
103+
* @var DatabaseInterface|PDO|mysqli|null
104104
*/
105105
protected ?DatabaseInterface $databaseConnection;
106106

@@ -563,7 +563,6 @@ public function save(): ActiveRecord
563563
public function execute(string $sql, array $params = []): DatabaseStatementInterface
564564
{
565565
$statement = $this->databaseConnection->prepare($sql);
566-
567566
$statement->execute($params);
568567

569568
// Now that the query has run, reset the data in the object
@@ -755,15 +754,17 @@ protected function filterParam($value)
755754
{
756755
if (is_array($value)) {
757756
foreach ($value as $key => $val) {
758-
$this->params[$value[$key] = ActiveRecordData::PREFIX . ++$this->count] = $val;
757+
$value[$key] = $this->filterParam($val);
759758
}
760-
} elseif (is_string($value)) {
759+
} elseif ($value === null) {
760+
$value = 'NULL';
761+
} elseif (is_bool($value) === true && is_int($value) === false && is_float($value) === false) {
762+
$value = $value ? 'TRUE' : 'FALSE';
763+
} else {
761764
$ph = ActiveRecordData::PREFIX . ++$this->count;
762765
$this->params[$ph] = $value;
763766
$value = $ph;
764-
} elseif ($value === null) {
765-
$value = 'NULL';
766-
}
767+
}
767768
return $value;
768769
}
769770

tests/ActiveRecordPdoIntegrationTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function getDirty()
235235

236236
$user->isNotNull('id')->eq('id', 1)->lt('id', 2)->gt('id', 0)->find();
237237
$sql = $user->getBuiltSql();
238-
$this->assertStringContainsString('SELECT "user".* FROM "user" WHERE "user"."id" IS NOT NULL AND "user"."id" = 1 AND "user"."id" < 2 AND "user"."id" > 0', $sql);
238+
$this->assertStringContainsString('SELECT "user".* FROM "user" WHERE "user"."id" IS NOT NULL AND "user"."id" = :ph3 AND "user"."id" < :ph4 AND "user"."id" > :ph5', $sql);
239239
$this->assertGreaterThan(0, $user->id);
240240
$this->assertSame([], $user->getDirty());
241241
$user->name = 'testname';
@@ -685,7 +685,7 @@ public function testWrapMultipleOrStatements()
685685
->eq('id', 2)
686686
->find();
687687
$sql = $record->getBuiltSql();
688-
$this->assertEquals('SELECT "user".* FROM "user" WHERE "user"."name" IS NOT NULL AND ("user"."name" = :ph1 OR "user"."id" = 1 OR "user"."password" >= :ph2) AND "user"."id" = 2 LIMIT 1', $sql);
688+
$this->assertEquals('SELECT "user".* FROM "user" WHERE "user"."name" IS NOT NULL AND ("user"."name" = :ph1 OR "user"."id" = :ph2 OR "user"."password" >= :ph3) AND "user"."id" = :ph4 LIMIT 1', $sql);
689689
}
690690

691691
public function testWrapWithComplexLogic()
@@ -702,7 +702,7 @@ public function testWrapWithComplexLogic()
702702
->join('contact', '"contact"."user_id" = "user"."id"')
703703
->find();
704704
$sql = $record->getBuiltSql();
705-
$this->assertEquals('SELECT "user".* FROM "user" LEFT JOIN contact ON "contact"."user_id" = "user"."id" WHERE ("user"."name" = :ph1 OR "user"."id" IN (:ph2,:ph3,:ph4) OR "user"."id" = 1) AND "user"."name" IS NOT NULL AND "user"."id" BETWEEN :ph5 AND :ph6 LIMIT 1', $sql);
705+
$this->assertEquals('SELECT "user".* FROM "user" LEFT JOIN contact ON "contact"."user_id" = "user"."id" WHERE ("user"."name" = :ph1 OR "user"."id" IN (:ph2,:ph3,:ph4) OR "user"."id" = :ph5) AND "user"."name" IS NOT NULL AND "user"."id" BETWEEN :ph6 AND :ph7 LIMIT 1', $sql);
706706
}
707707

708708
public function testOrAsFinalParameter()
@@ -714,6 +714,26 @@ public function testOrAsFinalParameter()
714714
->eq('id', 1, 'or')
715715
->find();
716716
$sql = $record->getBuiltSql();
717-
$this->assertEquals('SELECT "user".* FROM "user" WHERE "user"."name" = :ph1 AND "user"."id" IN (:ph2,:ph3,:ph4) OR "user"."id" = 1 LIMIT 1', $sql);
718-
}
717+
$this->assertEquals('SELECT "user".* FROM "user" WHERE "user"."name" = :ph1 AND "user"."id" IN (:ph2,:ph3,:ph4) OR "user"."id" = :ph5 LIMIT 1', $sql);
718+
}
719+
720+
public function testBooleanParam()
721+
{
722+
$record = new User(new PDO('sqlite:test.db'));
723+
$record->eq('name', 'John');
724+
$record->eq('id', true);
725+
$record->find();
726+
$sql = $record->getBuiltSql();
727+
$this->assertEquals('SELECT "user".* FROM "user" WHERE "user"."name" = :ph1 AND "user"."id" = TRUE LIMIT 1', $sql);
728+
}
729+
730+
public function testBooleanParamWithArray()
731+
{
732+
$record = new User(new PDO('sqlite:test.db'));
733+
$record->eq('name', 'John');
734+
$record->in('id', [ true, false ]);
735+
$record->find();
736+
$sql = $record->getBuiltSql();
737+
$this->assertEquals('SELECT "user".* FROM "user" WHERE "user"."name" = :ph1 AND "user"."id" IN (TRUE,FALSE) LIMIT 1', $sql);
738+
}
719739
}

0 commit comments

Comments
 (0)