jsqlite

package module
v0.0.0-...-7890c8b Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2020 License: MIT Imports: 11 Imported by: 0

README

go-jsqlite

SQL query runner for JSONL.

GoDoc

jsqlite uses go-sqlite3 internal.

Usage

jsqlite command
$ jsqlite "SELECT tag, time, message FROM records WHERE message LIKE '%error%' AND tag LIKE 'foo.%'" tests/logs.json
{"message":"[ERROR] hoge","tag":"foo.bar","time":"2020-07-06T17:49:37+0900"}
{"message":"[ERROR] hoge baz","tag":"foo.baz","time":"2020-07-06T17:49:43+0900"}

As library

func Select(r io.Reader, query string) error {
	runner, _ := jsqlite.Read(r)
	rows, err := runner.Select(query)
	if err != nil && !jsqlite.NoSuchColumnError(err) {
		return err
	}
	if len(rows) == 0 {
		return nil
	}
	for _, row := range rows {
		// ... map[string]interface{}
	}
	return nil
}

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NoSuchColumnError

func NoSuchColumnError(err error) bool

NoSuchColumnError detect error from SQLite which describe "no such column".

Types

type QueryRunner

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

QueryRunner represents a query runner of jsqlite.

func New

func New(memory bool) (*QueryRunner, error)

New creates a QueryRunner

func NewWithDB

func NewWithDB(db *sqlx.DB) *QueryRunner

NewWithDB creates a QueryRunner with the *sqlx.DB

func Read

func Read(r io.Reader) (*QueryRunner, error)

Read creates a QueryRunner and read JSONL from io.Reader

func (*QueryRunner) Close

func (r *QueryRunner) Close() error

Close closes a database and cleanup the database file if nessesary.

func (*QueryRunner) Read

func (r *QueryRunner) Read(src io.Reader) error

Read reads JSONL via io.Reader, creates table on in-memory SQLite and inserts records.

func (*QueryRunner) ReadWithContext

func (r *QueryRunner) ReadWithContext(ctx context.Context, src io.Reader) error

ReadWithContext reads JSONL via io.Reader, creates table on in-memory SQLite and inserts records with context.

func (*QueryRunner) Select

func (r *QueryRunner) Select(q string) ([]map[string]interface{}, error)

Select selects from table by a SQL query.

func (*QueryRunner) Table

func (r *QueryRunner) Table() string

Table returns a SQLite table name.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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