Skip to content

parser: Add support for mathmatical operators #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/dinosql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ func parseQuery(c core.Catalog, stmt nodes.Node, source string) (*Query, error)
default:
return nil, nil
}

rawSQL, err := pluckQuery(source, raw)
if err != nil {
return nil, err
Expand Down Expand Up @@ -562,9 +563,15 @@ func outputColumns(c core.Catalog, node nodes.Node) ([]core.Column, error) {
if res.Name != nil {
name = *res.Name
}
if postgres.IsComparisonOperator(join(n.Name, "")) {
switch {
case postgres.IsComparisonOperator(join(n.Name, "")):
// TODO: Generate a name for these operations
cols = append(cols, core.Column{Name: name, DataType: "bool", NotNull: true})
case postgres.IsMathematicalOperator(join(n.Name, "")):
// TODO: Generate correct numeric type
cols = append(cols, core.Column{Name: name, DataType: "pg_catalog.int4", NotNull: true})
default:
cols = append(cols, core.Column{Name: name, DataType: "any", NotNull: false})
}

case nodes.CoalesceExpr:
Expand Down
13 changes: 13 additions & 0 deletions internal/dinosql/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,19 @@ func TestQueries(t *testing.T) {
},
},
},
{
"mathmatical-operator",
`
CREATE TABLE foo (num integer not null);
SELECT *, num / 1024 as division FROM foo;
`,
Query{
Columns: []core.Column{
{Name: "num", DataType: "pg_catalog.int4", NotNull: true, Table: public("foo")},
{Name: "division", DataType: "pg_catalog.int4", NotNull: true},
},
},
},
} {
test := tc
t.Run(test.name, func(t *testing.T) {
Expand Down
25 changes: 25 additions & 0 deletions internal/postgres/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,28 @@ func IsComparisonOperator(s string) bool {
}
return true
}

func IsMathematicalOperator(s string) bool {
switch s {
case "+":
case "-":
case "*":
case "/":
case "%":
case "^":
case "|/":
case "||/":
case "!":
case "!!":
case "@":
case "&":
case "|":
case "#":
case "~":
case "<<":
case ">>":
default:
return false
}
return true
}