Skip to content
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
3 changes: 3 additions & 0 deletions examples/authors/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"schema": "sqlite/schema.sql",
"queries": "sqlite/query.sql",
"engine": "sqlite",
"database": {
"url": "'file:authors?mode=memory&cache=shared'"
},
"gen": {
"go": {
"package": "authors",
Expand Down
5 changes: 4 additions & 1 deletion examples/booktest/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"path": "sqlite",
"schema": "sqlite/schema.sql",
"queries": "sqlite/query.sql",
"engine": "sqlite"
"engine": "sqlite",
"database": {
"url": "'file:booktest?mode=memory&cache=shared'"
}
}
]
}
3 changes: 3 additions & 0 deletions examples/ondeck/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"schema": "sqlite/schema",
"queries": "sqlite/query",
"engine": "sqlite",
"database": {
"url": "'file:ondeck?mode=memory&cache=shared'"
},
"emit_json_tags": true,
"emit_prepared_queries": true,
"emit_interface": true
Expand Down
14 changes: 14 additions & 0 deletions internal/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/google/cel-go/cel"
"github.com/google/cel-go/ext"
"github.com/jackc/pgx/v5"
_ "github.com/mattn/go-sqlite3"
"github.com/spf13/cobra"

"github.com/kyleconroy/sqlc/internal/config"
Expand Down Expand Up @@ -165,6 +166,9 @@ func prepareable(sql config.SQL, raw *ast.RawStmt) bool {
if sql.Engine == config.EngineMySQL {
return true
}
if sql.Engine == config.EngineSQLite {
return true
}
return false
}

Expand Down Expand Up @@ -285,6 +289,16 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
}
defer db.Close()
prep = &dbPreparer{db}
case config.EngineSQLite:
db, err := sql.Open("sqlite3", dburl)
if err != nil {
return fmt.Errorf("database: connection error: %s", err)
}
if err := db.PingContext(ctx); err != nil {
return fmt.Errorf("database: connection error: %s", err)
}
defer db.Close()
prep = &dbPreparer{db}
default:
return fmt.Errorf("unsupported database url: %s", s.Engine)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/endtoend/vet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -60,6 +61,12 @@ func TestExamplesVet(t *testing.T) {
defer db.Close()
defer cleanup()
}
if s, found := findSchema(t, filepath.Join(path, "sqlite")); found {
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", tc)
db, cleanup := sqltest.CreateSQLiteDatabase(t, dsn, []string{s})
defer db.Close()
defer cleanup()
}
}

var stderr bytes.Buffer
Expand Down
2 changes: 2 additions & 0 deletions internal/sqltest/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func id() string {
}

func PostgreSQL(t *testing.T, migrations []string) (*sql.DB, func()) {
t.Helper()

// For each test, pick a new schema name at random.
schema := "sqltest_postgresql_" + id()
return CreatePostgreSQLDatabase(t, schema, true, migrations)
Expand Down
15 changes: 11 additions & 4 deletions internal/sqltest/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ import (

func SQLite(t *testing.T, migrations []string) (*sql.DB, func()) {
t.Helper()

// For each test, pick a new database name at random.
source, err := os.CreateTemp("", "sqltest_sqlite_")
if err != nil {
t.Fatal(err)
}
t.Logf("open %s\n", source.Name())
sdb, err := sql.Open("sqlite3", source.Name())
return CreateSQLiteDatabase(t, source.Name(), migrations)
}

func CreateSQLiteDatabase(t *testing.T, path string, migrations []string) (*sql.DB, func()) {
t.Helper()

t.Logf("open %s\n", path)
sdb, err := sql.Open("sqlite3", path)
if err != nil {
t.Fatal(err)
}
Expand All @@ -40,6 +45,8 @@ func SQLite(t *testing.T, migrations []string) (*sql.DB, func()) {
}

return sdb, func() {
os.Remove(source.Name())
if _, err := os.Stat(path); err == nil {
os.Remove(path)
}
}
}