Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tblconv/csv): add sql driver for csv sources and outputs #11

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/tblconv/cmd/output/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io"

"github.com/Zaba505/tblconv"
"github.com/Zaba505/tblconv/csv"

"github.com/spf13/cobra"
)
Expand All @@ -35,6 +36,6 @@ func init() {
"csv",
"Write data formatted as CSV.",
func(_ *cobra.Command) {},
func(w io.Writer, _ *cobra.Command) tblconv.Writer { return tblconv.NewCSVWriter(w) },
func(w io.Writer, _ *cobra.Command) tblconv.Writer { return csv.NewWriter(w) },
)
}
5 changes: 3 additions & 2 deletions cmd/tblconv/cmd/output/excel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io"

"github.com/Zaba505/tblconv"
"github.com/Zaba505/tblconv/excel"

"github.com/spf13/cobra"
)
Expand All @@ -35,14 +36,14 @@ func init() {
"excel",
"Write data formatted as an Excel spreadsheet.",
func(cmd *cobra.Command) {
cmd.Flags().StringP("sheet", "s", tblconv.DefaultSheetName, "Excel sheet name write data to.")
cmd.Flags().StringP("sheet", "s", excel.DefaultSheetName, "Excel sheet name write data to.")
},
func(w io.Writer, cmd *cobra.Command) tblconv.Writer {
sheet, err := cmd.Flags().GetString("sheet")
if err != nil {
panic(err)
}
return tblconv.NewExcelWriter(w, tblconv.SheetName(sheet))
return excel.NewWriter(w, excel.SheetName(sheet))
},
)
}
3 changes: 2 additions & 1 deletion cmd/tblconv/cmd/output/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io"

"github.com/Zaba505/tblconv"
sqlconv "github.com/Zaba505/tblconv/sql"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -64,7 +65,7 @@ func init() {
panic(err)
}

return tblconv.NewSQLWriter(db, query)
return sqlconv.NewWriter(db, query)
},
)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/tblconv/cmd/source/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io"

"github.com/Zaba505/tblconv"
"github.com/Zaba505/tblconv/csv"

"github.com/spf13/cobra"
)
Expand All @@ -35,6 +36,6 @@ func init() {
"csv",
"Read data formatted as CSV.",
func(_ *cobra.Command) {},
func(r io.Reader, _ *cobra.Command) tblconv.Reader { return tblconv.NewCSVReader(r) },
func(r io.Reader, _ *cobra.Command) tblconv.Reader { return csv.NewReader(r) },
)
}
5 changes: 3 additions & 2 deletions cmd/tblconv/cmd/source/excel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io"

"github.com/Zaba505/tblconv"
"github.com/Zaba505/tblconv/excel"

"github.com/spf13/cobra"
)
Expand All @@ -35,14 +36,14 @@ func init() {
"excel",
"Read data formatted as CSV.",
func(cmd *cobra.Command) {
cmd.Flags().StringP("sheet", "s", tblconv.DefaultSheetName, "Excel sheet name to read values from.")
cmd.Flags().StringP("sheet", "s", excel.DefaultSheetName, "Excel sheet name to read values from.")
},
func(r io.Reader, cmd *cobra.Command) tblconv.Reader {
sheet, err := cmd.Flags().GetString("sheet")
if err != nil {
panic(err)
}
return tblconv.NewExcelReader(r, tblconv.SheetName(sheet))
return excel.NewReader(r, excel.SheetName(sheet))
},
)
}
3 changes: 2 additions & 1 deletion cmd/tblconv/cmd/source/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"io"

"github.com/Zaba505/tblconv"
sqlconv "github.com/Zaba505/tblconv/sql"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -67,7 +68,7 @@ func init() {
panic(err)
}

return tblconv.NewSQLReader(db, query, interfaceSlicize(args)...)
return sqlconv.NewReader(db, query, interfaceSlicize(args)...)
},
)
}
Expand Down
21 changes: 11 additions & 10 deletions csv.go → csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,38 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package tblconv
// Package csv provides a Reader and Writer for the CSV format.
package csv

import (
"encoding/csv"
"io"
)

// NewCSVReader
func NewCSVReader(r io.Reader) *csv.Reader {
// NewReader
func NewReader(r io.Reader) *csv.Reader {
return csv.NewReader(r)
}

// CSVWriter
type CSVWriter struct {
// Writer
type Writer struct {
CSV *csv.Writer
}

// NewCSVWriter
func NewCSVWriter(w io.Writer) *CSVWriter {
return &CSVWriter{
// NewWriter
func NewWriter(w io.Writer) *Writer {
return &Writer{
CSV: csv.NewWriter(w),
}
}

// Write
func (w *CSVWriter) Write(record []string) error {
func (w *Writer) Write(record []string) error {
return w.CSV.Write(record)
}

// Flush
func (w *CSVWriter) Flush() error {
func (w *Writer) Flush() error {
w.CSV.Flush()
return w.CSV.Error()
}
59 changes: 59 additions & 0 deletions csv/csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright © 2021 Zaba505

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package csv

import (
"bytes"
"strings"
"testing"

"github.com/Zaba505/tblconv"
)

func TestReader(t *testing.T) {
testData := `hello,goodbye
world,world
`

r := NewReader(strings.NewReader(testData))
w := tblconv.NewRecordsWriter()

err := tblconv.Copy(w, r)
if err != nil {
t.Error(err)
return
}
}

func TestWriter(t *testing.T) {
var b bytes.Buffer

r := tblconv.NewRecordsReader()
w := NewWriter(&b)

err := tblconv.Copy(w, r)
if err != nil {
t.Error(err)
return
}
}
142 changes: 142 additions & 0 deletions csv/driver/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Copyright © 2021 Zaba505

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// Package csv contains a sql.Driver implementation for CSV.
package csv

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

func init() {
sql.Register("csv", &Driver{})
}

// Driver
type Driver struct{}

// Open
func (d *Driver) Open(dsn string) (driver.Conn, error) {
c, err := d.OpenConnector(dsn)
if err != nil {
return nil, err
}
return c.Connect(context.Background())
}

func (d *Driver) OpenConnector(name string) (driver.Connector, error) {
return &connector{driver: d, fileName: name}, nil
}

type connector struct {
driver *Driver

fileName string
}

func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
return &conn{}, nil
}

func (c *connector) Driver() driver.Driver {
return c.driver
}

type conn struct{}

func (c *conn) Prepare(query string) (driver.Stmt, error) {
return nil, nil
}

func (c *conn) Close() error {
return nil
}

func (c *conn) Begin() (driver.Tx, error) {
return c.BeginTx(context.Background(), driver.TxOptions{})
}

func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return &tx{}, nil
}

type stmt struct{}

func (s *stmt) Close() error {
return nil
}

func (s *stmt) NumInput() int {
return 0
}

func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
return &result{}, nil
}

func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
return &result{}, nil
}

func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
return &rows{}, nil
}

func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
return &rows{}, nil
}

type tx struct{}

func (tx *tx) Commit() error {
return nil
}

func (tx *tx) Rollback() error {
return nil
}

type result struct{}

func (r *result) LastInsertId() (int64, error) {
return 0, nil
}

func (r *result) RowsAffected() (int64, error) {
return 0, nil
}

type rows struct{}

func (r *rows) Columns() []string {
return nil
}

func (r *rows) Close() error {
return nil
}

func (r *rows) Next(dest []driver.Value) error {
return nil
}
Loading