Skip to content

Commit

Permalink
Add slice read / writer (noborus#47)
Browse files Browse the repository at this point in the history
* Add slice read / writer
* Add go slice to import/export.
* Add test and example
  • Loading branch information
noborus authored Jun 18, 2019
1 parent 28c6e9f commit 92944df
Show file tree
Hide file tree
Showing 15 changed files with 700 additions and 84 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# trdsql

[![Build Status](https://travis-ci.org/noborus/trdsql.svg?branch=master)](https://travis-ci.org/noborus/trdsql)
[![GoDoc](https://godoc.org/github.com/noborus/trdsql?status.svg)](https://https://godoc.org/github.com/noborus/trdsql)
[![GoDoc](https://godoc.org/github.com/noborus/trdsql?status.svg)](https://godoc.org/github.com/noborus/trdsql)

A tool that can execute SQL queries on [CSV](https://tools.ietf.org/html/rfc4180), [LTSV](http://ltsv.org/), [JSON](https://tools.ietf.org/html/rfc7159) and [TBLN](https://tbln.dev/).

Expand Down
3 changes: 0 additions & 3 deletions _example/import/test.csv

This file was deleted.

41 changes: 6 additions & 35 deletions _example/simple/main.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,18 @@
package main

import (
"fmt"
"log"

"github.com/noborus/trdsql"
)

type ArrayTable struct {
table [][]string
}

func (a *ArrayTable) PreWrite(columns []string, types []string) error {
a.table = make([][]string, 0)
fmt.Println(columns, types)
return nil
}
func (a *ArrayTable) WriteRow(values []interface{}, columns []string) error {
row := make([]string, len(values))
for i, v := range values {
row[i] = trdsql.ValString(v)
}
a.table = append(a.table, row)
return nil
}
func (a *ArrayTable) PostWrite() error {
return nil
}

func exec(query string) [][]string {
writer := &ArrayTable{}
trd := trdsql.NewTRDSQL(trdsql.NewImporter(), trdsql.NewExporter(writer))
trd.Driver = "postgres"
trd.Dsn = ""
err := trd.Exec(query)
func main() {
trd := trdsql.NewTRDSQL(
trdsql.NewImporter(trdsql.InDelimiter(":")),
trdsql.NewExporter(trdsql.NewWriter()),
)
err := trd.Exec("SELECT c1 FROM /etc/passwd")
if err != nil {
log.Fatal(err)
}
return writer.table
}

func main() {
trdsql.EnableDebug()
table := exec("SELECT * FROM test")
fmt.Println(table)
}
35 changes: 35 additions & 0 deletions _example/slice/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"log"

"github.com/noborus/trdsql"
)

func sliceQuery(slice interface{}, tableName string, query string) ([][]interface{}, error) {
// trdsql.EnableDebug()
importer := trdsql.NewSliceImporter(tableName, slice)
writer := trdsql.NewSliceWriter()
trd := trdsql.NewTRDSQL(importer, trdsql.NewExporter(writer))
err := trd.Exec(query)
return writer.Table, err
}

func main() {
data := []struct {
id int
name string
}{
{id: 1, name: "Bod"},
{id: 2, name: "Alice"},
{id: 3, name: "Henry"},
}
table, err := sliceQuery(data, "slice", "SELECT name,id FROM slice ORDER BY id DESC")
if err != nil {
log.Fatal(err)
}
fmt.Println(table)
// Output:
// [[Henry 3] [Alice 2] [Bod 1]]
}
65 changes: 65 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package trdsql_test

import (
"fmt"
"io/ioutil"
"log"
"os"

"github.com/noborus/trdsql"
)

func Example() {
in := []byte(`"Rob","Pike",rob
Ken,Thompson,ken
"Robert","Griesemer","gri"
`)
tmpfile, err := ioutil.TempFile("/tmp", "xxx")
if err != nil {
log.Fatal(err)
}
defer func() {
defer os.Remove(tmpfile.Name())
}()
_, err = tmpfile.Write(in)
if err != nil {
log.Fatal(err)
}
trd := trdsql.NewTRDSQL(
trdsql.NewImporter(),
trdsql.NewExporter(trdsql.NewWriter()),
)
query := fmt.Sprintf("SELECT c1 FROM %s ORDER BY c1", tmpfile.Name())
err = trd.Exec(query)
if err != nil {
log.Fatal(err)
}
// Output:
// Ken
// Rob
// Robert
}

func ExampleSliceReader() {
data := []struct {
id int
name string
}{
{id: 1, name: "Bod"},
{id: 2, name: "Alice"},
{id: 3, name: "Henry"},
}
tableName := "slice"
importer := trdsql.NewSliceImporter(tableName, data)
writer := trdsql.NewSliceWriter()
trd := trdsql.NewTRDSQL(importer, trdsql.NewExporter(writer))

err := trd.Exec("SELECT name,id FROM slice ORDER BY id DESC")
if err != nil {
log.Fatal(err)
}
table := writer.Table
fmt.Print(table)
// Output:
// [[Henry 3] [Alice 2] [Bod 1]]
}
14 changes: 1 addition & 13 deletions importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,11 @@ type Importer interface {
}

// ReadFormat is a structure that includes ReadOpts,
// and is an implementation of the Importer interface.
// and is an implementation of the Importer interface.
type ReadFormat struct {
*ReadOpts
}

// NewReadOpts Returns ReadOpts.
func NewReadOpts() *ReadOpts {
return &ReadOpts{
InFormat: GUESS,
InPreRead: 1,
InSkip: 0,
InDelimiter: ",",
InHeader: false,
IsTemporary: true,
}
}

// NewImporter returns trdsql default Importer.
func NewImporter(options ...ReadOpt) *ReadFormat {
readOpts := NewReadOpts()
Expand Down
28 changes: 28 additions & 0 deletions importer_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package trdsql

type SliceImporter struct {
*SliceReader
}

func NewSliceImporter(tableName string, data interface{}) *SliceImporter {
return &SliceImporter{
SliceReader: NewSliceReader(tableName, data),
}
}

func (i *SliceImporter) Import(db *DB, query string) (string, error) {
names, err := i.Names()
if err != nil {
return query, err
}
types, err := i.Types()
if err != nil {
return query, err
}
err = db.CreateTable(i.tableName, names, types, true)
if err != nil {
return query, err
}
err = db.Import(i.tableName, names, i.SliceReader)
return query, err
}
54 changes: 34 additions & 20 deletions importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ func Test_sqlFields(t *testing.T) {
}
}

func newDBTestSqlite3(t *testing.T) *DB {
func newDBTestSqlite3() *DB {
db, err := Connect("sqlite3", "")
if err != nil {
t.Fatal(err)
return nil
}
return db
}
func newDBTestPostgres(t *testing.T) *DB {
func newDBTestPostgres() *DB {
db, err := Connect("postgres", "dbname=trdsql_test")
if err != nil {
return nil
Expand All @@ -122,7 +122,7 @@ func newDBTestPostgres(t *testing.T) *DB {
}
return db
}
func newDBTestMysql(t *testing.T) *DB {
func newDBTestMysql() *DB {
db, err := Connect("mysql", "root@/trdsql_test")
if err != nil {
return nil
Expand All @@ -133,7 +133,11 @@ func newDBTestMysql(t *testing.T) *DB {
}
return db
}

func csvReadOpts() *ReadOpts {
opts := NewReadOpts()
opts.InFormat = CSV
return opts
}
func TestImportFile(t *testing.T) {
type args struct {
db *DB
Expand All @@ -149,27 +153,17 @@ func TestImportFile(t *testing.T) {
{
name: "testNoFile",
args: args{
db: newDBTestSqlite3(t),
db: newDBTestSqlite3(),
fileName: "nofile",
opts: NewReadOpts(),
},
want: "",
wantErr: false,
},
{
name: "testSqlite",
args: args{
db: newDBTestSqlite3(t),
fileName: "testdata/test.csv",
opts: NewReadOpts(),
},
want: "`testdata/test.csv`",
wantErr: false,
},
{
name: "testGlobFile",
args: args{
db: newDBTestSqlite3(t),
db: newDBTestSqlite3(),
fileName: "testdata/test*.csv",
opts: NewReadOpts(),
},
Expand All @@ -179,17 +173,37 @@ func TestImportFile(t *testing.T) {
{
name: "testNoMatch",
args: args{
db: newDBTestSqlite3(t),
db: newDBTestSqlite3(),
fileName: "testdata/testtttttt*.csv",
opts: NewReadOpts(),
},
want: "",
wantErr: false,
},
{
name: "testCSV",
args: args{
db: newDBTestSqlite3(),
fileName: "testdata/test.csv",
opts: csvReadOpts(),
},
want: "`testdata/test.csv`",
wantErr: false,
},
{
name: "testSqlite",
args: args{
db: newDBTestSqlite3(),
fileName: "testdata/test.csv",
opts: NewReadOpts(),
},
want: "`testdata/test.csv`",
wantErr: false,
},
{
name: "testPostgres",
args: args{
db: newDBTestPostgres(t),
db: newDBTestPostgres(),
fileName: "testdata/test.csv",
opts: NewReadOpts(),
},
Expand All @@ -199,7 +213,7 @@ func TestImportFile(t *testing.T) {
{
name: "testMysql",
args: args{
db: newDBTestMysql(t),
db: newDBTestMysql(),
fileName: "testdata/test.csv",
opts: NewReadOpts(),
},
Expand Down
Loading

0 comments on commit 92944df

Please sign in to comment.