Skip to content

Commit 5d20c20

Browse files
committed
Pass as numbers when using booleans
1 parent b9a2dba commit 5d20c20

8 files changed

Lines changed: 136 additions & 0 deletions

File tree

internal/core/gen.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ func (v Params) Bindings() string {
9696
continue
9797
}
9898

99+
if f.Type.IsBoolean() {
100+
out = append(out, fmt.Sprintf("($%s ? 1 : 0)", f.Name))
101+
continue
102+
}
103+
99104
out = append(out, fmt.Sprintf("$%s", f.Name))
100105
}
101106

internal/core/models.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func (t phpType) String() string {
9797
return v
9898
}
9999

100+
func (t phpType) IsBoolean() bool {
101+
return t.Name == "bool"
102+
}
103+
100104
func (t phpType) IsDateTimeImmutable() bool {
101105
return t.Name == "\\DateTimeImmutable"
102106
}

internal/tests/golden_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,13 @@ func TestArgsWithDefaults(t *testing.T) {
7373

7474
runGoldenTest(t, testCase)
7575
}
76+
77+
func TestBooleanBindings(t *testing.T) {
78+
testCase := TestCase{
79+
Name: "boolean_bindings",
80+
Engine: "sqlite",
81+
Package: "Test\\BooleanBindings",
82+
}
83+
84+
runGoldenTest(t, testCase)
85+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
// @formatter:off
3+
// Code generated by sqlc. DO NOT EDIT.
4+
// versions:
5+
// sqlc v1.30.0
6+
7+
declare(strict_types=1);
8+
9+
namespace Test\BooleanBindings;
10+
11+
final readonly class FeatureFlags {
12+
public function __construct(
13+
public int $id,
14+
public string $name,
15+
public bool $enabled,
16+
)
17+
{}
18+
}
19+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
// @formatter:off
3+
// Code generated by sqlc. DO NOT EDIT.
4+
// versions:
5+
// sqlc v1.30.0
6+
7+
declare(strict_types=1);
8+
9+
namespace Test\BooleanBindings;
10+
11+
interface Queries {
12+
/**
13+
* @return FeatureFlags[]
14+
*/
15+
public function listFlags(): array;
16+
17+
public function setFlag(int $id, string $name, bool $enabled): void;
18+
19+
}
20+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
// @formatter:off
3+
// Code generated by sqlc. DO NOT EDIT.
4+
// versions:
5+
// sqlc v1.30.0
6+
7+
declare(strict_types=1);
8+
9+
namespace Test\BooleanBindings;
10+
11+
const listFlags = "-- name: listFlags :many
12+
SELECT
13+
id,
14+
name,
15+
enabled
16+
FROM
17+
feature_flags
18+
ORDER BY
19+
id
20+
";
21+
22+
const setFlag = "-- name: setFlag :exec
23+
INSERT INTO
24+
feature_flags (id, name, enabled)
25+
VALUES
26+
(?, ?, ?)
27+
";
28+
29+
final readonly class QueriesImpl implements Queries {
30+
public function __construct(private \PDO $pdo) {}
31+
32+
/**
33+
* @return FeatureFlags[]
34+
* @throws \Exception
35+
*/
36+
public function listFlags(): array
37+
{
38+
$stmt = $this->pdo->prepare(listFlags);
39+
$stmt->execute();
40+
$results = $stmt->fetchAll(\PDO::FETCH_NUM);
41+
$ret = [];
42+
foreach ($results as $row) {
43+
$ret[] = new FeatureFlags($row[0], $row[1], $row[2]);
44+
}
45+
return $ret;
46+
}
47+
48+
/**
49+
* @throws \Exception
50+
*/
51+
public function setFlag(int $id, string $name, bool $enabled): void
52+
{
53+
$stmt = $this->pdo->prepare(setFlag);
54+
$stmt->execute([$id, $name, ($enabled ? 1 : 0)]);
55+
}
56+
57+
}
58+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- name: setFlag :exec
2+
INSERT INTO
3+
feature_flags (id, name, enabled)
4+
VALUES
5+
(?, ?, ?);
6+
7+
-- name: listFlags :many
8+
SELECT
9+
id,
10+
name,
11+
enabled
12+
FROM
13+
feature_flags
14+
ORDER BY
15+
id;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE feature_flags (
2+
id INTEGER PRIMARY KEY,
3+
name TEXT NOT NULL,
4+
enabled BOOLEAN NOT NULL DEFAULT 0
5+
);

0 commit comments

Comments
 (0)