Skip to content

Commit

Permalink
Use ExecContext for things other than SELECT
Browse files Browse the repository at this point in the history
Use ExecContext instead of QueryContext, other than SELECT in SQLite.

​Variable named sql was changed to sqlQuery.
  • Loading branch information
noborus committed Dec 11, 2023
1 parent c311d79 commit b35cc62
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 214 deletions.
18 changes: 8 additions & 10 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,17 @@ func (db *DB) Select(query string) (*sql.Rows, error) {
// SelectContext is executes SQL select statements with context.
// SelectContext is a wrapper for QueryContext.
func (db *DB) SelectContext(ctx context.Context, query string) (*sql.Rows, error) {
if db.Tx == nil {
return nil, ErrNoTransaction
}

query = strings.TrimSpace(query)
if query == "" {
return nil, ErrNoStatement
}
debug.Printf(query)

rows, err := db.Tx.QueryContext(ctx, query)
if err != nil {
return rows, fmt.Errorf("%w [%s]", err, query)
}
return rows, nil
}

func (db *DB) OtherExecContext(ctx context.Context, query string) error {
_, err := db.Tx.ExecContext(ctx, query)
if err != nil {
return fmt.Errorf("%w [%s]", err, query)
}
return nil
}
2 changes: 1 addition & 1 deletion database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestDB_Select(t *testing.T) {
{
name: "testErr",
fields: fields{driver: "sqlite", dsn: ""},
args: args{query: ""},
args: args{query: "ERR"},
wantErr: true,
},
{
Expand Down
30 changes: 27 additions & 3 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package trdsql
import (
"context"
"log"
"strings"

"github.com/noborus/sqlss"
)
Expand Down Expand Up @@ -35,10 +36,10 @@ func (e *WriteFormat) Export(db *DB, sql string) error {

// ExportContext is execute SQL(Select) and the result is written out by the writer.
// ExportContext is called from ExecContext.
func (e *WriteFormat) ExportContext(ctx context.Context, db *DB, sql string) error {
queries := sqlss.SplitQueries(sql)
func (e *WriteFormat) ExportContext(ctx context.Context, db *DB, sqlQuery string) error {
queries := sqlss.SplitQueries(sqlQuery)
if !multi || len(queries) == 1 {
return e.exportContext(ctx, db, false, sql)
return e.exportContext(ctx, db, false, sqlQuery)
}
for _, query := range queries {
if err := e.exportContext(ctx, db, true, query); err != nil {
Expand All @@ -49,6 +50,20 @@ func (e *WriteFormat) ExportContext(ctx context.Context, db *DB, sql string) err
}

func (e *WriteFormat) exportContext(ctx context.Context, db *DB, multi bool, query string) error {
if db.Tx == nil {
return ErrNoTransaction
}

query = strings.TrimSpace(query)
if query == "" {
return ErrNoStatement
}
debug.Printf(query)

if db.isExecContext(query) {
return db.OtherExecContext(ctx, query)
}

rows, err := db.SelectContext(ctx, query)
if err != nil {
return err
Expand Down Expand Up @@ -108,3 +123,12 @@ func (e *WriteFormat) exportContext(ctx context.Context, db *DB, multi bool, que

return e.Writer.PostWrite()
}

// isExecContext returns true if the query is not a SELECT statement.
// Queries that return no rows in SQlite should use ExecContext and therefore return true.
func (db *DB) isExecContext(query string) bool {
if db.driver == "sqlite3" || db.driver == "sqlite" {
return !strings.HasPrefix(strings.ToUpper(query), "SELECT")
}
return false
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.18
github.com/multiprocessio/go-sqlite3-stdlib v0.0.0-20220822170115-9f6825a1cd25
github.com/noborus/guesswidth v0.3.4
github.com/noborus/sqlss v0.0.0-20231204225649-d757e7878afc
github.com/noborus/sqlss v0.1.0
github.com/noborus/tbln v0.0.2
github.com/olekukonko/tablewriter v0.0.5
github.com/pierrec/lz4 v2.6.1+incompatible
Expand Down Expand Up @@ -44,7 +44,7 @@ require (
lukechampine.com/uint128 v1.3.0 // indirect
modernc.org/cc/v3 v3.41.0 // indirect
modernc.org/ccgo/v3 v3.16.15 // indirect
modernc.org/libc v1.36.0 // indirect
modernc.org/libc v1.37.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/opt v0.1.3 // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ github.com/multiprocessio/go-sqlite3-stdlib v0.0.0-20220822170115-9f6825a1cd25 h
github.com/multiprocessio/go-sqlite3-stdlib v0.0.0-20220822170115-9f6825a1cd25/go.mod h1:RrGEZqqiyEcLyTVLDSgtNZVLqJykj0F4vwuuqvMdT60=
github.com/noborus/guesswidth v0.3.4 h1:+iKmbm0iFTS3pksIOKQQvLVZVOKNZHavqJoFK2mPoTQ=
github.com/noborus/guesswidth v0.3.4/go.mod h1:2F1sqiazKIwuSRjQTweQHPFJcjV5375jYUrTik9/V5k=
github.com/noborus/sqlss v0.0.0-20231204225649-d757e7878afc h1:QqMWyLgZcwRfDJ85xqzZCRgJI5+rPeg93wddxZ6cOo8=
github.com/noborus/sqlss v0.0.0-20231204225649-d757e7878afc/go.mod h1:34KdYx3QxMFfD05RhUi7Uw5M1i6KOBQ1NHtMIuNVnWM=
github.com/noborus/sqlss v0.1.0 h1:GSrOeBuswHaBn6enegZeMVudPPeyVoYo+LCapTc+b7Q=
github.com/noborus/sqlss v0.1.0/go.mod h1:34KdYx3QxMFfD05RhUi7Uw5M1i6KOBQ1NHtMIuNVnWM=
github.com/noborus/tbln v0.0.2 h1:pQIv+ZO38KPz52FOuhs/W3inpgmd5qwL8XFDqI+KKyY=
github.com/noborus/tbln v0.0.2/go.mod h1:kS3WhEDRJhNwF3+aRGl9iaUzu/r3lExDagcPPENtNQ0=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
Expand Down Expand Up @@ -127,10 +127,8 @@ modernc.org/ccgo/v3 v3.16.15 h1:KbDR3ZAVU+wiLyMESPtbtE/Add4elztFyfsWoNTgxS0=
modernc.org/ccgo/v3 v3.16.15/go.mod h1:yT7B+/E2m43tmMOT51GMoM98/MtHIcQQSleGnddkUNI=
modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk=
modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM=
modernc.org/libc v1.35.0 h1:EQ4szx6Q/QLZuysmAnI4dfRnKbAbNlENp23ruvTJ2nE=
modernc.org/libc v1.35.0/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
modernc.org/libc v1.36.0 h1:J5UW1tOzu8d2NY8XtQ/tAHJuCcn0b062EkCY91drjX4=
modernc.org/libc v1.36.0/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
modernc.org/libc v1.37.0 h1:WerjebcsP6A7Jy+f2lCnHAkiSTLf7IaSftBYUtoswak=
modernc.org/libc v1.37.0/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
Expand Down
6 changes: 3 additions & 3 deletions trdsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (trd *TRDSQL) Exec(sql string) error {
}

// ExecContext is actually executed.
func (trd *TRDSQL) ExecContext(ctx context.Context, sql string) error {
func (trd *TRDSQL) ExecContext(ctx context.Context, sqlQuery string) error {
db, err := Connect(trd.Driver, trd.Dsn)
if err != nil {
return fmt.Errorf("connect: %w", err)
Expand All @@ -172,14 +172,14 @@ func (trd *TRDSQL) ExecContext(ctx context.Context, sql string) error {
}

if trd.Importer != nil {
sql, err = trd.Importer.ImportContext(ctx, db, sql)
sqlQuery, err = trd.Importer.ImportContext(ctx, db, sqlQuery)
if err != nil {
return fmt.Errorf("import: %w", err)
}
}

if trd.Exporter != nil {
if err := trd.Exporter.ExportContext(ctx, db, sql); err != nil {
if err := trd.Exporter.ExportContext(ctx, db, sqlQuery); err != nil {
return fmt.Errorf("export: %w", err)
}
}
Expand Down
Loading

0 comments on commit b35cc62

Please sign in to comment.