Skip to content

Commit b9a2dba

Browse files
committed
Refactor Args and ArgsWithDefaults methods to separate non-default and default parameters
1 parent 83355e8 commit b9a2dba

7 files changed

Lines changed: 123 additions & 14 deletions

File tree

internal/core/gen.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,39 @@ func (v Params) isEmpty() bool {
2222
return len(v.ModelClass.Fields) == 0
2323
}
2424

25+
func splitFieldsByDefault(fields []Field, withDefault func(Field) string, withoutDefault func(Field) string) (nonDefaults, defaults []string) {
26+
for _, f := range fields {
27+
if f.Default != "" {
28+
defaults = append(defaults, withDefault(f))
29+
continue
30+
}
31+
32+
nonDefaults = append(nonDefaults, withoutDefault(f))
33+
}
34+
return
35+
}
36+
37+
func withoutDefaultCallback(f Field) string {
38+
return f.Type.String() + " $" + f.Name
39+
}
40+
41+
func withDefaultCallback(f Field) string {
42+
return withoutDefaultCallback(f) + " = " + f.Default
43+
}
44+
2545
func (v Params) Args() string {
2646
if v.isEmpty() {
2747
return ""
2848
}
2949

30-
var out []string
3150
fields := v.ModelClass.Fields
32-
for _, f := range fields {
33-
sig := f.Type.String() + " $" + f.Name
34-
out = append(out, sig)
35-
}
51+
nonDefaults, defaults := splitFieldsByDefault(
52+
fields,
53+
withoutDefaultCallback,
54+
withoutDefaultCallback,
55+
)
3656

57+
out := append(nonDefaults, defaults...)
3758
return strings.Join(out, ", ")
3859
}
3960

@@ -42,17 +63,14 @@ func (v Params) ArgsWithDefaults() string {
4263
return ""
4364
}
4465

45-
var out []string
4666
fields := v.ModelClass.Fields
47-
for _, f := range fields {
48-
sig := f.Type.String() + " $" + f.Name
49-
if f.Default != "" {
50-
sig += " = " + f.Default
51-
}
52-
53-
out = append(out, sig)
54-
}
67+
nonDefaults, defaults := splitFieldsByDefault(
68+
fields,
69+
withDefaultCallback,
70+
withoutDefaultCallback,
71+
)
5572

73+
out := append(nonDefaults, defaults...)
5674
return strings.Join(out, ", ")
5775
}
5876

internal/tests/golden_test.go

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

6464
runGoldenTest(t, testCase)
6565
}
66+
67+
func TestArgsWithDefaults(t *testing.T) {
68+
testCase := TestCase{
69+
Name: "args_with_defaults",
70+
Engine: "sqlite",
71+
Package: "Test\\ArgsWithDefaults",
72+
}
73+
74+
runGoldenTest(t, testCase)
75+
}
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\ArgsWithDefaults;
10+
11+
final readonly class Author {
12+
public function __construct(
13+
public int $id,
14+
public string $name,
15+
public ?int $age,
16+
)
17+
{}
18+
}
19+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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\ArgsWithDefaults;
10+
11+
interface Queries {
12+
public function insertAuthor(int $id, int $age, string $name): void;
13+
14+
}
15+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\ArgsWithDefaults;
10+
11+
const insertAuthor = "-- name: insertAuthor :exec
12+
INSERT INTO
13+
author (id, name, age)
14+
VALUES
15+
(?1, ?2, ?3)
16+
";
17+
18+
final readonly class QueriesImpl implements Queries {
19+
public function __construct(private \PDO $pdo) {}
20+
21+
/**
22+
* @sqlc-param int $id
23+
* @sqlc-param string $name='hello'
24+
* @sqlc-param int $age
25+
* @throws \Exception
26+
*/
27+
public function insertAuthor(int $id, int $age, string $name = 'hello'): void
28+
{
29+
$stmt = $this->pdo->prepare(insertAuthor);
30+
$stmt->execute([$id, $name, $age]);
31+
}
32+
33+
}
34+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- name: InsertAuthor :exec
2+
-- @sqlc-param int $id
3+
-- @sqlc-param string $name='hello'
4+
-- @sqlc-param int $age
5+
INSERT INTO
6+
author (id, name, age)
7+
VALUES
8+
(:id, :name, :age);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE author (
2+
id INTEGER PRIMARY KEY,
3+
name TEXT NOT NULL,
4+
age INTEGER DEFAULT 42
5+
);

0 commit comments

Comments
 (0)