Skip to content

Commit

Permalink
Merge pull request #42 from goccy/fix-temp-table
Browse files Browse the repository at this point in the history
Fix drop table query for create temp table
  • Loading branch information
goccy authored Nov 9, 2022
2 parents f0f2e28 + 9326816 commit 3addf38
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,26 @@ COMMIT TRANSACTION;
})
}
}

func TestCreateTempTable(t *testing.T) {
now := time.Now()
ctx := context.Background()
ctx = zetasqlite.WithCurrentTime(ctx, now)
db, err := sql.Open("zetasqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.ExecContext(ctx, "CREATE TEMP TABLE tmp_table (id INT64)"); err != nil {
t.Fatal(err)
}
if _, err := db.ExecContext(ctx, "CREATE TEMP TABLE tmp_table (id INT64)"); err != nil {
t.Fatal(err)
}
if _, err := db.ExecContext(ctx, "CREATE TABLE tmp_table (id INT64)"); err != nil {
t.Fatal(err)
}
if _, err := db.ExecContext(ctx, "CREATE TABLE tmp_table (id INT64)"); err == nil {
t.Fatal("expected error")
}
}
4 changes: 2 additions & 2 deletions internal/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (it *AnalyzerOutputIterator) analyzeCreateTableStmt(ctx context.Context, no
return nil, err
}
execContext := func(ctx context.Context, conn *Conn) (driver.Result, error) {
dropTableQuery := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", spec.TableName())
dropTableQuery := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", strings.Join(spec.NamePath, "."))
if spec.CreateMode == ast.CreateOrReplaceMode {
if _, err := conn.ExecContext(ctx, dropTableQuery); err != nil {
return nil, err
Expand Down Expand Up @@ -343,7 +343,7 @@ func (it *AnalyzerOutputIterator) analyzeCreateTableAsSelectStmt(ctx context.Con
return nil, err
}
execContext := func(ctx context.Context, conn *Conn) (driver.Result, error) {
dropTableQuery := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", spec.TableName())
dropTableQuery := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", strings.Join(spec.NamePath, "."))
if spec.CreateMode == ast.CreateOrReplaceMode {
if _, err := conn.ExecContext(ctx, dropTableQuery); err != nil {
return nil, err
Expand Down

0 comments on commit 3addf38

Please sign in to comment.