csvq

package module
v1.4.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2021 License: MIT Imports: 16 Imported by: 0

README

csvq driver

A go database/sql driver for csvq.

Build Status codecov

csvq-driver allows you to operate csv files and other supported formats using csvq statements in Go. You can use the statements in the same way as other relational database systems.

Reference Manual - csvq

Requirements

Go 1.14 or later (ref. Getting Started - The Go Programming Language)

Supported features

  • Query
    • Only the result-sets in the top-level scope can be retrieved. You cannot refer the results of child scopes such as inside of IF, WHILE and user-defined functions.
  • Exec
    • Only the last result in the top-level scope can be retrieved.
    • LastInsertId is not supported.
  • Prepared Statement
    • Ordinal placeholders
    • Named placeholders
  • Transaction
    • Isolation Level is the default only.
    • Read-only transaction is not supported.
    • If you do not use the database/sql transaction feature, all execusions will commit at the end of the execusion automatically.

Usage

Data Source Name

A data source name to call sql.Open is a directory path. It is the same as "--repository" option of the csvq command.

Some parameters can be specified in a DSN string with a directory path. A directory path and parameters are separated by a question mark, and parameters are separated by ampersands.

db, err := sql.Open("csvq", "/path/to/data/directory?Param1=Value&Param2=Value")
Parameters that can be specified
Name Type Default
Timezone string "Local"
DatetimeFormat string empty string
AnsiQuotes bool false

Parameter names are case-insensitive.

See: csvq > Reference Manual > Command Usage > Options

Error Handling

If a received error is a returned error from csvq, you can cast the error to github.com/mithrandie/csvq/lib/query.Error interface. The error interface has following functions.

function description
Message() string Error message
Code() int Error code. The same as the exit code of the csvq command
Number() int Error number
Line() int Line number where the error occurred in the passed statement
Char() int Column number where the error occurred in the passed statement
Source() string File or statement name where the error occurred
Example
package main

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

	"github.com/mithrandie/csvq/lib/query"

	_ "github.com/mithrandie/csvq-driver"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	db, err := sql.Open("csvq", "/path/to/data/directory")
	if err != nil {
		panic(err)
	}
	defer func() {
		if err := db.Close(); err != nil {
			panic(err)
		}
	}()
	
	queryString := "SELECT id, first_name, country_code FROM `users.csv` WHERE id = '12'"
	r := db.QueryRowContext(ctx, queryString)

	var (
		id          int
		firstName   string
		countryCode string
	)

	if err := r.Scan(&id, &firstName, &countryCode); err != nil {
		if err == sql.ErrNoRows {
			fmt.Println("No Rows.")
		} else if csvqerr, ok := err.(query.Error); ok {
			panic(fmt.Sprintf("Unexpected error: Number: %d  Message: %s", csvqerr.Number(), csvqerr.Message()))
		} else {
			panic("Unexpected error: " + err.Error())
		}
	} else {
		fmt.Printf("Result: [id]%3d  [first_name]%10s  [country_code]%3s\n", id, firstName, countryCode)
	}
}

See: https://github.com/mithrandie/csvq-driver/blob/master/example/csvq-driver/csvq-driver-example.go

Replace I/O

You can replace input/output interface of the csvq to pass the data from inside of your go programs or retrive the result-sets as a another format.

function in the csvq-driver default description
SetStdin(r io.ReadCloser) error

SetStdinContext(ctx context.Context, r io.ReadCloser) error
os.Stdin Replace input interface. Passed data can be refered as a temporary table named "STDIN".
SetStdout(w io.WriteCloser) query.Discard Replace output interface. Logs and result-sets of select queries are written to stdout.
SetOutFile(w io.Writer) nil Put a writer for result-sets of select queries to write instead of stdout.

The following structs are available for replacement.

struct name initializer
query.Input query.NewInput(r io.Reader)
query.Output query.NewOutput()

"query" means the package "github.com/mithrandie/csvq/lib/query".

See: https://github.com/mithrandie/csvq-driver/blob/master/example/replace-io/csvq-replace-io-example.go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DSNParseErr = errors.New("incorrect data source name")

Functions

func GenerateStatementName

func GenerateStatementName() string

func IsCsvqValue

func IsCsvqValue(v interface{}) bool

func NewCompositeError

func NewCompositeError(errs []error) error

func NewStmt

func NewStmt(ctx context.Context, proc *query.Processor, queryString string) (driver.Stmt, error)

func NewTx

func NewTx(proc *query.Processor) (driver.Tx, error)

func SetOutFile

func SetOutFile(w io.Writer)

func SetStdin

func SetStdin(r io.ReadCloser) error

func SetStdinContext

func SetStdinContext(ctx context.Context, r io.ReadCloser) error

func SetStdout

func SetStdout(w io.WriteCloser)

Types

type Boolean

type Boolean struct {
	// contains filtered or unexported fields
}

func (Boolean) PrimitiveType

func (t Boolean) PrimitiveType() parser.PrimitiveType

func (Boolean) Value

func (t Boolean) Value() (driver.Value, error)

type CompositeError

type CompositeError struct {
	Errors []error
}

func (CompositeError) Error

func (e CompositeError) Error() string

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

func NewConn

func NewConn(ctx context.Context, dsnStr string, defaultWaitTimeout time.Duration, retryDelay time.Duration) (*Conn, error)

func (*Conn) Begin

func (c *Conn) Begin() (driver.Tx, error)

func (*Conn) BeginTx

func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) ExecContext

func (c *Conn) ExecContext(ctx context.Context, queryString string, args []driver.NamedValue) (driver.Result, error)

func (*Conn) Prepare

func (c *Conn) Prepare(queryString string) (driver.Stmt, error)

func (*Conn) PrepareContext

func (c *Conn) PrepareContext(ctx context.Context, queryString string) (driver.Stmt, error)

func (*Conn) QueryContext

func (c *Conn) QueryContext(ctx context.Context, queryString string, args []driver.NamedValue) (driver.Rows, error)

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

func (Connector) Connect

func (t Connector) Connect(ctx context.Context) (driver.Conn, error)

func (Connector) Driver

func (t Connector) Driver() driver.Driver

type DSN

type DSN struct {
	// contains filtered or unexported fields
}

func ParseDSN

func ParseDSN(dsnStr string) (DSN, error)

type Datetime

type Datetime struct {
	// contains filtered or unexported fields
}

func (Datetime) PrimitiveType

func (t Datetime) PrimitiveType() parser.PrimitiveType

func (Datetime) Value

func (t Datetime) Value() (driver.Value, error)

type Driver

type Driver struct {
}

func (Driver) Open

func (d Driver) Open(dsn string) (driver.Conn, error)

func (Driver) OpenConnector

func (d Driver) OpenConnector(dsn string) (driver.Connector, error)

func (Driver) OpenContext

func (d Driver) OpenContext(ctx context.Context, dsn string) (driver.Conn, error)

type Float

type Float struct {
	// contains filtered or unexported fields
}

func (Float) PrimitiveType

func (t Float) PrimitiveType() parser.PrimitiveType

func (Float) Value

func (t Float) Value() (driver.Value, error)

type Integer

type Integer struct {
	// contains filtered or unexported fields
}

func (Integer) PrimitiveType

func (t Integer) PrimitiveType() parser.PrimitiveType

func (Integer) Value

func (t Integer) Value() (driver.Value, error)

type Null

type Null struct {
}

func (Null) PrimitiveType

func (t Null) PrimitiveType() parser.PrimitiveType

func (Null) Value

func (t Null) Value() (driver.Value, error)

type Result

type Result struct {
	// contains filtered or unexported fields
}

func NewResult

func NewResult(rowsAffected int64) *Result

func (Result) LastInsertId

func (r Result) LastInsertId() (int64, error)

func (Result) RowsAffected

func (r Result) RowsAffected() (int64, error)

type Rows

type Rows struct {
	// contains filtered or unexported fields
}

func NewRows

func NewRows(selectedViews []*query.View) *Rows

func (*Rows) Close

func (r *Rows) Close() error

func (*Rows) Columns

func (r *Rows) Columns() []string

func (*Rows) HasNextResultSet

func (r *Rows) HasNextResultSet() bool

func (*Rows) Next

func (r *Rows) Next(dest []driver.Value) error

func (*Rows) NextResultSet

func (r *Rows) NextResultSet() error

type Stmt

type Stmt struct {
	// contains filtered or unexported fields
}

func (*Stmt) CheckNamedValue

func (stmt *Stmt) CheckNamedValue(nv *driver.NamedValue) error

func (*Stmt) Close

func (stmt *Stmt) Close() error

func (*Stmt) ColumnConverter

func (stmt *Stmt) ColumnConverter(_ int) driver.ValueConverter

func (*Stmt) Exec

func (stmt *Stmt) Exec(args []driver.Value) (driver.Result, error)

func (*Stmt) ExecContext

func (stmt *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)

func (*Stmt) NumInput

func (stmt *Stmt) NumInput() int

func (*Stmt) Query

func (stmt *Stmt) Query(args []driver.Value) (driver.Rows, error)

func (*Stmt) QueryContext

func (stmt *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)

type String

type String struct {
	// contains filtered or unexported fields
}

func (String) PrimitiveType

func (t String) PrimitiveType() parser.PrimitiveType

func (String) Value

func (t String) Value() (driver.Value, error)

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

func (Tx) Commit

func (tx Tx) Commit() error

func (Tx) Rollback

func (tx Tx) Rollback() error

type Value

type Value interface {
	Value() (driver.Value, error)
	PrimitiveType() parser.PrimitiveType
}

type ValueConverter

type ValueConverter struct {
}

func (ValueConverter) ConvertValue

func (c ValueConverter) ConvertValue(v interface{}) (driver.Value, error)

Directories

Path Synopsis
csvq-driver
Usage: $ cd $GOPATH/src/github.com/mithrandie/csvq-driver $ go build ./example/csvq-driver/csvq-driver-example.go $ ./csvq-driver-example -r ./example/data
Usage: $ cd $GOPATH/src/github.com/mithrandie/csvq-driver $ go build ./example/csvq-driver/csvq-driver-example.go $ ./csvq-driver-example -r ./example/data
replace-io
Usage: $ cd $GOPATH/src/github.com/mithrandie/csvq-driver $ go build ./example/csvq-driver/csvq-replace-io-example.go $ ./csvq-replace-io-example
Usage: $ cd $GOPATH/src/github.com/mithrandie/csvq-driver $ go build ./example/csvq-driver/csvq-replace-io-example.go $ ./csvq-replace-io-example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL