Skip to content

Commit

Permalink
Merge branch 'main' of ssh://github.com/kyleconroy/sqlc into kyleconr…
Browse files Browse the repository at this point in the history
…oy-main
  • Loading branch information
victoraugustolls committed May 28, 2021
2 parents 2145685 + ad041d0 commit bab5cbf
Show file tree
Hide file tree
Showing 45 changed files with 916 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-kotlin.yml
Expand Up @@ -30,7 +30,7 @@ jobs:
- 3306:3306

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4
- uses: actions/setup-java@v2
with:
distribution: 'adopt'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-python.yml
Expand Up @@ -23,7 +23,7 @@ jobs:
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4
- uses: actions/setup-python@v2
with:
python-version: 3.9
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -30,7 +30,7 @@ jobs:
- 3306:3306

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4

- uses: actions/setup-go@v2
with:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/docker.yml
Expand Up @@ -12,13 +12,13 @@ jobs:
id: prep
run: |
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4
- uses: docker/setup-buildx-action@v1
- uses: docker/login-action@v1
- uses: docker/login-action@v1.9.0
with:
username: kjconroy
password: ${{ secrets.DOCKER_PASSWORD }}
- uses: docker/build-push-action@v2
- uses: docker/build-push-action@v2.4.0
with:
context: .
file: ./Dockerfile
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/equinox.yml
Expand Up @@ -11,7 +11,7 @@ jobs:
name: release --platforms windows
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4
- uses: actions/setup-go@v2
with:
go-version: '1.16'
Expand All @@ -26,7 +26,7 @@ jobs:
name: release --platforms darwin
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4
- uses: actions/setup-go@v2
with:
go-version: '1.16'
Expand All @@ -42,7 +42,7 @@ jobs:
runs-on: ubuntu-latest
needs: [macos, windows]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4
- uses: actions/setup-go@v2
with:
go-version: '1.16'
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
@@ -1,5 +1,5 @@
# STEP 1: Build sqlc
FROM golang:1.16.3 AS builder
FROM golang:1.16.4 AS builder

COPY . /workspace
WORKDIR /workspace
Expand Down
2 changes: 1 addition & 1 deletion examples/booktest/postgresql/db_test.go
Expand Up @@ -139,7 +139,7 @@ func TestBooks(t *testing.T) {
t.Fatal(err)
}
for _, ab := range res {
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tags: '%v'\n", ab.BookID, ab.Title, ab.Name, ab.Isbn, ab.Tags)
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tags: '%v'\n", ab.BookID, ab.Title, ab.Name.String, ab.Isbn, ab.Tags)
}

// TODO: call say_hello(varchar)
Expand Down
3 changes: 2 additions & 1 deletion examples/booktest/postgresql/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -23,7 +23,7 @@ WHERE tags && ?::varchar[]
data class BooksByTagsRow (
val bookId: Int,
val title: String,
val name: String,
val name: String?,
val isbn: String,
val tags: List<String>
)
Expand Down
2 changes: 1 addition & 1 deletion examples/python/src/booktest/query.py
Expand Up @@ -27,7 +27,7 @@
class BooksByTagsRow:
book_id: int
title: str
name: str
name: Optional[str]
isbn: str
tags: List[str]

Expand Down
57 changes: 55 additions & 2 deletions internal/compiler/output_columns.go
Expand Up @@ -206,9 +206,57 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
}
}

if n, ok := node.(*ast.SelectStmt); ok {
for _, col := range cols {
if !col.NotNull || col.Table == nil {
continue
}
for _, f := range n.FromClause.Items {
if res := isTableRequired(f, col.Table.Name, tableRequired); res != tableNotFound {
col.NotNull = res == tableRequired
break
}
}
}
}

return cols, nil
}

const (
tableNotFound = iota
tableRequired
tableOptional
)

func isTableRequired(n ast.Node, tableName string, prior int) int {
switch n := n.(type) {
case *ast.RangeVar:
if *n.Relname == tableName {
return prior
}
case *ast.JoinExpr:
helper := func(l, r int) int {
if res := isTableRequired(n.Larg, tableName, l); res != tableNotFound {
return res
}
if res := isTableRequired(n.Rarg, tableName, r); res != tableNotFound {
return res
}
return tableNotFound
}
switch n.Jointype {
case ast.JoinTypeLeft:
return helper(tableRequired, tableOptional)
case ast.JoinTypeRight:
return helper(tableOptional, tableRequired)
case ast.JoinTypeFull:
return helper(tableOptional, tableOptional)
}
}
return tableNotFound
}

// Compute the output columns for a statement.
//
// Return an error if column references are ambiguous
Expand Down Expand Up @@ -251,20 +299,24 @@ func sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
var tables []*Table
for _, item := range list.Items {
switch n := item.(type) {

case *ast.FuncName:
// If the function or table can't be found, don't error out. There
// are many queries that depend on functions unknown to sqlc.
fn, err := qc.GetFunc(n)
if err != nil {
return nil, err
continue
}
table, err := qc.GetTable(&ast.TableName{
Catalog: fn.ReturnType.Catalog,
Schema: fn.ReturnType.Schema,
Name: fn.ReturnType.Name,
})
if err != nil {
return nil, err
continue
}
tables = append(tables, table)

case *ast.RangeSubselect:
cols, err := outputColumns(qc, n.Subquery)
if err != nil {
Expand Down Expand Up @@ -297,6 +349,7 @@ func sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
}
}
tables = append(tables, table)

default:
return nil, fmt.Errorf("sourceTable: unsupported list item type: %T", n)
}
Expand Down
Expand Up @@ -2,3 +2,8 @@
SELECT *
FROM users_func()
WHERE first_name != '';

/* name: GenerateSeries :many */
SELECT ($1::inet) + i
FROM generate_series(0, $2::int) AS i
LIMIT 1;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions internal/endtoend/testdata/join_full/mysql/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions internal/endtoend/testdata/join_full/mysql/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions internal/endtoend/testdata/join_full/mysql/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/endtoend/testdata/join_full/mysql/query.sql
@@ -0,0 +1,8 @@
CREATE TABLE foo (id serial not null, bar_id int references bar(id));
CREATE TABLE bar (id serial not null);

-- name: FullJoin :many
SELECT f.id, f.bar_id, b.id
FROM foo f
FULL OUTER JOIN bar b ON b.id = f.bar_id
WHERE f.id = $1;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/join_full/mysql/sqlc.json
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}

0 comments on commit bab5cbf

Please sign in to comment.