-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
thisisaaronland
committed
Aug 11, 2021
1 parent
e6e97b3
commit 01ff8b3
Showing
4 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |