From 09cfaac73343f40a982e54835c2fbc747e46e5e7 Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Wed, 10 Apr 2024 14:45:29 -0700 Subject: [PATCH 1/2] Use `WITHOUT ROWID` table clustering for tables --- driver_test.go | 32 ++++++++++++++++++++++++++++++++ internal/spec.go | 16 ++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/driver_test.go b/driver_test.go index 1a866c5..607c753 100644 --- a/driver_test.go +++ b/driver_test.go @@ -3,6 +3,8 @@ package zetasqlite_test import ( "context" "database/sql" + "fmt" + "strings" "testing" zetasqlite "github.com/goccy/go-zetasqlite" @@ -179,6 +181,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.Fatal(fmt.Sprintf("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:") diff --git a/internal/spec.go b/internal/spec.go index c343f9e..41fd868 100644 --- a/internal/spec.go +++ b/internal/spec.go @@ -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: @@ -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 { From e20abff2377ea8d256587323be211e6691a1364f Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Wed, 10 Apr 2024 15:40:18 -0700 Subject: [PATCH 2/2] lint --- driver_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/driver_test.go b/driver_test.go index 607c753..8f02e68 100644 --- a/driver_test.go +++ b/driver_test.go @@ -3,7 +3,6 @@ package zetasqlite_test import ( "context" "database/sql" - "fmt" "strings" "testing" @@ -206,7 +205,7 @@ CREATE TABLE IF NOT EXISTS Singers ( _, err = stmt.Exec(int64(1), "Miss", "Kitten") if !strings.HasSuffix(err.Error(), "UNIQUE constraint failed: Singers.SingerId") { - t.Fatal(fmt.Sprintf("expected failed unique constraint err, got: %s", err)) + t.Fatalf("expected failed unique constraint err, got: %s", err) } }) }