Skip to content
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

Use WITHOUT ROWID table clustering for primary keys #44

Merged
merged 2 commits into from
Apr 11, 2024
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
31 changes: 31 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zetasqlite_test
import (
"context"
"database/sql"
"strings"
"testing"

zetasqlite "github.com/goccy/go-zetasqlite"
Expand Down Expand Up @@ -179,6 +180,36 @@ CREATE TABLE IF NOT EXISTS Singers (
})
}

func TestCreateTable(t *testing.T) {
t.Run("primary keys", func(t *testing.T) {
db, err := sql.Open("zetasqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS Singers (
SingerId INT64 NOT NULL PRIMARY KEY,
FirstName STRING(1024),
LastName STRING(1024)
)`); err != nil {
t.Fatal(err)
}
stmt, err := db.Prepare("INSERT Singers (SingerId, FirstName, LastName) VALUES (@SingerID, @FirstName, @LastName)")
if err != nil {
t.Fatal(err)
}
_, err = stmt.Exec(int64(1), "Kylie", "Minogue")
if err != nil {
t.Fatal(err)
}

_, err = stmt.Exec(int64(1), "Miss", "Kitten")
if !strings.HasSuffix(err.Error(), "UNIQUE constraint failed: Singers.SingerId") {
t.Fatalf("expected failed unique constraint err, got: %s", err)
}
})
}

func TestPreparedStatements(t *testing.T) {
t.Run("prepared select", func(t *testing.T) {
db, err := sql.Open("zetasqlite", ":memory:")
Expand Down
16 changes: 14 additions & 2 deletions internal/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,23 @@ func (s *TableSpec) SQLiteSchema() string {
columns = append(columns, c.SQLiteSchema())
}
if len(s.PrimaryKey) != 0 {
primaryKeys := make([]string, len(s.PrimaryKey))

for i, key := range s.PrimaryKey {
primaryKeys[i] = fmt.Sprintf("%s COLLATE zetasqlite_collate", key)
}

columns = append(
columns,
fmt.Sprintf("PRIMARY KEY (%s)", strings.Join(s.PrimaryKey, ",")),
fmt.Sprintf("PRIMARY KEY (%s)", strings.Join(primaryKeys, ",")),
)
}
var clustering string
if len(s.PrimaryKey) > 0 {
clustering = "WITHOUT ROWID"
} else {
clustering = ""
}
var stmt string
switch s.CreateMode {
case ast.CreateDefaultMode:
Expand All @@ -153,7 +165,7 @@ func (s *TableSpec) SQLiteSchema() string {
case ast.CreateIfNotExistsMode:
stmt = "CREATE TABLE IF NOT EXISTS"
}
return fmt.Sprintf("%s `%s` (%s)", stmt, s.TableName(), strings.Join(columns, ","))
return fmt.Sprintf("%s `%s` (%s) %s", stmt, s.TableName(), strings.Join(columns, ","), clustering)
}

func viewSQLiteSchema(s *TableSpec) string {
Expand Down
Loading