Skip to content

Commit

Permalink
add query package and tool
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Aug 11, 2021
1 parent e6e97b3 commit 01ff8b3
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 4 deletions.
50 changes: 50 additions & 0 deletions cmd/query/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"context"
"flag"
"github.com/aaronland/go-sqlite/database"
"github.com/aaronland/go-sqlite/query"
"log"
"os"
)

func main() {

dsn := flag.String("dsn", "", "A valid SQLite DSN")
query_str := flag.String("query", "", "A valid SQL query")

flag.Parse()

ctx := context.Background()

db, err := database.NewDB(ctx, *dsn)

if err != nil {
log.Fatalf("Failed to create new database, %v", err)
}

conn, err := db.Conn()

if err != nil {
log.Fatalf("Failed to get database connection, %v", err)
}

rows, err := conn.Query(*query_str)

if err != nil {
log.Fatalf("query err: %s", err)
}

wr, err := query.NewCSVQueryWriter(ctx, os.Stdout)

if err != nil {
log.Fatalf("Failed to create query writer, %v", err)
}

err = query.WriteRows(ctx, wr, rows)

if err != nil {
log.Fatalf("Failed to write rows, %v", err)
}
}
8 changes: 4 additions & 4 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/psanford/sqlite3vfs"
"github.com/psanford/sqlite3vfshttp"
_ "log"
"strings"
"sync"
"github.com/psanford/sqlite3vfshttp"
"github.com/psanford/sqlite3vfs"
)

type SQLiteDatabase struct {
Expand Down Expand Up @@ -43,13 +43,13 @@ func NewDBWithDriver(ctx context.Context, driver string, dsn string) (*SQLiteDat
}

err := sqlite3vfs.RegisterVFS("httpvfs", &vfs)

if err != nil {
return nil, err
}

dsn = "not_a_real_name.db?vfs=httpvfs&mode=ro"

} else {

// https://github.com/mattn/go-sqlite3/issues/39
Expand Down
35 changes: 35 additions & 0 deletions query/csvwriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package query

import (
"context"
"encoding/csv"
"io"
)

type CSVQueryWriter struct {
QueryWriter
csv_writer *csv.Writer
}

func NewCSVQueryWriter(ctx context.Context, wr io.Writer) (QueryWriter, error) {

csv_wr := csv.NewWriter(wr)

query_wr := &CSVQueryWriter{
csv_writer: csv_wr,
}

return query_wr, nil
}

func (query_wr *CSVQueryWriter) WriteRow(ctx context.Context, row []string) error {

err := query_wr.csv_writer.Write(row)

if err != nil {
return err
}

query_wr.csv_writer.Flush()
return nil
}
66 changes: 66 additions & 0 deletions query/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package query

import (
"context"
"database/sql"
"fmt"
)

type QueryWriter interface {
WriteRow(context.Context, []string) error
}

// The guts of this (minus the QueryWriter stuff) are copied from:
// https://github.com/psanford/sqlite3vfshttp/blob/main/sqlitehttpcli/sqlitehttpcli.go

func WriteRows(ctx context.Context, wr QueryWriter, rows *sql.Rows) error {

cols, err := rows.Columns()

if err != nil {
return fmt.Errorf("Failed to determine columns for rows, %v", err)
}

for rows.Next() {

rows.Columns()

columns := make([]*string, len(cols))

columnPointers := make([]interface{}, len(cols))

for i := range columns {
columnPointers[i] = &columns[i]
}

err = rows.Scan(columnPointers...)

if err != nil {
return fmt.Errorf("Failed to scan row, %w", err)
}

names := make([]string, 0, len(columns))

for _, col := range columns {
if col == nil {
names = append(names, "NULL")
} else {
names = append(names, *col)
}
}

err = wr.WriteRow(ctx, names)

if err != nil {
return fmt.Errorf("Failed to write row, %v", err)
}
}

err = rows.Close()

if err != nil {
return fmt.Errorf("Failed to close rows, %v", err)
}

return nil
}

0 comments on commit 01ff8b3

Please sign in to comment.