Skip to content

Commit

Permalink
add bool better slice handling
Browse files Browse the repository at this point in the history
  • Loading branch information
delaneyj committed Oct 5, 2024
1 parent ba24092 commit 358b3ae
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 375 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version: "3"

vars:
VERSION: 0.3.1
VERSION: 0.3.2

interval: 200ms

Expand Down
16 changes: 14 additions & 2 deletions sqlc-gen-zombiezen/zombiezen/examples/migrations/0001.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
CREATE TABLE
nullableTestTable (id integer primary key, myBool boolean) strict;
CREATE TABLE nullableTestTable (id integer PRIMARY KEY, myBool boolean) strict;

CREATE TABLE NAMES (
id integer PRIMARY KEY,
name text NOT NULL UNIQUE
);

CREATE TABLE authors (
id integer PRIMARY KEY,
first_name_id integer NOT NULL,
last_name_id integer NOT NULL,
FOREIGN KEY (first_name_id) REFERENCES NAMES(id),
FOREIGN KEY (last_name_id) REFERENCES NAMES(id)
);
20 changes: 19 additions & 1 deletion sqlc-gen-zombiezen/zombiezen/examples/queries/nullable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,22 @@ SELECT
FROM
nullableTestTable
WHERE
myBool = @myBool;
myBool = @myBool;

-- name: DistinctAuthorNames :many
SELECT
DISTINCT na.name AS first_name,
nb.name AS last_name
FROM
authors a
INNER JOIN NAMES na ON a.first_name_id = na.id
INNER JOIN NAMES nb ON a.last_name_id = nb.id
ORDER BY
first_name,
last_name;

-- name: HasAuthors :one
SELECT
COUNT(*) > 0
FROM
authors;
7 changes: 5 additions & 2 deletions sqlc-gen-zombiezen/zombiezen/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ func toSQLType(c *plugin.Column) string {
return "bool"
case "blob":
return "bytes"
case "bool":
return "bool"
default:
panic(fmt.Sprintf("toSQLType unhandled type %s", c.Type.Name))
}

}

func toFieldName(c *plugin.Column) string {
Expand All @@ -123,12 +126,12 @@ func toGoType(c *plugin.Column) (val string, needsTime bool) {
return "int64", false
case "real":
return "float64", false
case "boolean":
case "boolean", "bool":
return "bool", false
case "blob":
return "[]byte", false
default:
panic(fmt.Sprintf("toGoType unhandled type %s for column %s ", c.Type.Name, c.Name))
panic(fmt.Sprintf("toGoType unhandled type '%s' for column '%s'", c.Type.Name, c.Name))
}
}
}
Expand Down
42 changes: 22 additions & 20 deletions sqlc-gen-zombiezen/zombiezen/queries.qtpl
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,27 @@ func (ps *{%s q.Name.Pascal %}Stmt) Run(
} else if !hasRow {
break
}
{%= fillResponse(q) -%}

{%- if q.ResponseIsSingularField -%}
{%s q.ResponseFields[0].Name.Camel %} := {%= fillResponse(q) -%}
res = append(res, {%s q.ResponseFields[0].Name.Camel %})
{%- else -%}
{%= fillResponse(q) -%}
res = append(res, row)
{%- endif -%}
}
{%- else -%}
if hasRow, err := ps.stmt.Step(); err != nil {
{%- if q.ResponseIsSingularField -%}
return {%s q.ResponseFields[0].Name.Camel %}, fmt.Errorf("failed to execute {{.Name.Lower}} SQL: %w", err)
return res, fmt.Errorf("failed to execute {{.Name.Lower}} SQL: %w", err)
{%- else -%}
return res, fmt.Errorf("failed to execute {{.Name.Lower}} SQL: %w", err)
{%- endif -%}
} else if hasRow {
{%- if q.ResponseIsSingularField -%}
res = {%= fillResponse(q) -%}
{%- else -%}
{%= fillResponse(q) -%}
{%- if !q.ResponseIsSingularField -%}
res = &row
{%- endif -%}
}
Expand All @@ -92,11 +99,7 @@ func (ps *{%s q.Name.Pascal %}Stmt) Run(
{%- endif -%}

{%- if q.HasResponse -%}
{%- if q.ResponseIsSingularField -%}
return {%s q.ResponseFields[0].Name.Camel %}, nil
{%- else -%}
return res, nil
{%- endif -%}
{%- else -%}
return nil
{%- endif -%}
Expand All @@ -123,37 +126,36 @@ func Once{%s q.Name.Pascal %}(

{% endfunc %}

{%- func fillResponse(q *GenerateQueryContext) %}
{%- func fillResponse(q *GenerateQueryContext) -%}
{%- if q.ResponseIsSingularField -%}
{%- code
f := q.ResponseFields[0]
-%}
{%- switch f.GoType.Original -%}
{%- case "time.Time" -%}
{%s f.Name.Camel %} = toolbelt.JulianDayToTime(ps.stmt.ColumnFloat({%d f.Offset %}))
toolbelt.JulianDayToTime(ps.stmt.ColumnFloat({%d f.Offset %}))
{%- case "time.Duration" -%}
{%s f.Name.Camel %} = toolbelt.MillisecondsToDuration(ps.stmt.ColumnInt64({%d f.Offset %}))
toolbelt.MillisecondsToDuration(ps.stmt.ColumnInt64({%d f.Offset %}))
{%- case "[]byte" -%}
{%s f.Name.Camel %} = toolbelt.StmtBytesByCol(ps.stmt, {%d f.Offset %}),
toolbelt.StmtBytesByCol(ps.stmt, {%d f.Offset %}),
{%- default -%}
{%s f.Name.Camel %} = ps.stmt.Column{%s f.SQLType.Pascal %}({%d f.Offset %})
ps.stmt.Column{%s f.SQLType.Pascal %}({%d f.Offset %})
{%- endswitch -%}
{%- else -%}
row := {%s q.Name.Pascal %}Res{}

{%- for _,f := range q.ResponseFields -%}
{% if f.IsNullable %}
{%- if f.IsNullable -%}
isNull{%s f.Name.Pascal %} := ps.stmt.ColumnIsNull({%d f.Offset %})
if !isNull{%s f.Name.Pascal %} {
tmp := {%= fillResponseField(f) -%}
row.{%s f.Name.Pascal %} = &tmp
}
{%- else %}
row.{%s f.Name.Pascal %} = {%= fillResponseField(f) %}
{% endif %}
{%- else -%}
row.{%s f.Name.Pascal %} = {%= fillResponseField(f) -%}
{%- endif -%}
{%- endfor -%}
{%- endif %}
{%- endfunc %}
{%- endif -%}
{%- endfunc -%}

{%- func fillResponseField(f GenerateField) -%}
{%- switch f.GoType.Original -%}
Expand Down Expand Up @@ -182,7 +184,7 @@ func Once{%s q.Name.Pascal %}(
{%- func fillReturns(q *GenerateQueryContext) -%}
{%- if q.HasResponse -%}
{%- if q.ResponseIsSingularField -%}
{%s q.ResponseFields[0].Name.Camel %} {%s q.ResponseFields[0].GoType.Original -%},
res {% if q.ResponseHasMultiple %}[]{% endif %}{%s q.ResponseFields[0].GoType.Original -%},
{%- else -%}
res {% if q.ResponseHasMultiple %}[]{% else %}*{% endif %}{%s q.Name.Pascal %}Res,
{%- endif -%}
Expand Down
Loading

0 comments on commit 358b3ae

Please sign in to comment.