sqlbless

package module
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 26 Imported by: 0

README

SQL-Bless

<English> / <Japanese>

The SQL-Bless is a command-line database client like SQL*Plus or psql.

  • Emacs-like keybindings for inline editing of multiple lines of SQL.
    • The action of Enter key will only insert a line feed code.
    • Press Ctrl-J or Ctrl-Enter to execute the input.
  • Save the result of SELECT in CSV format
  • Supported RDBMS
    • SQLite3
    • Oracle
    • PostgreSQL
    • Microsoft SQL Server
    • MySQL
  • Allows editing database records directly, similar to a spreadsheet (with the EDIT command)
  • Auto commit is disabled.

image

Video by @emisjerry

Key Binding
Enter, Ctrl-M Insert a linefeed
Ctrl-Enter/J or ;+Enter[^semicolon] Execute SQL
Ctrl-F/B Move Cursor forward or backward
Ctrl-N/P Move Cursor or refer history
Ctrl-C Exit with rollback
Ctrl-D Delete character or submit EOF (exit with rollback)
ALT-P, Ctrl-Up, PageUp Insert the previous SQL (history)
ALT-N, Ctrl-Down, PageDown Insert the next SQL (history)

[^semicolon]: ; or the string specfied with the option -term string

Supported commands

  • SELECT / INSERT / UPDATE / DELETE

    • INSERT, UPDATE and DELETE begin the transaction automatically.
  • COMMIT / ROLLBACK

  • SPOOL

    • spool FILENAME .. open FILENAME and write log and output.
    • spool off .. stop spooling and close.
  • EXIT / QUIT

    • Rollback a transaction and exit SQL-Bless.
  • START filename

    • Start the SQL script given with filename
  • REM comments

  • DESC [tablename] / \D [tablename]

    • When the tablename is given, show the specification of the the table
    • Without the tablename, show the list of tables.
  • HISTORY

    • Show the history of input SQLs
  • EDIT tablename [WHERE conditions...]

    • Start an editor to modify the selected records of the table.
    • In the editor, these keys are bound.
      • x or d: set NULL to the current cell
      • c: apply changes
      • q or ESC: quit without applying changes
    • Because the EDIT statement automatically generates SQL from data changed in the editor, it may not be able to properly represent SQL data for special types specific to individual databases. If you find it, we would appreciate it if you could contact us.
  • ; (or the string specified with -term string) is a statement seperator when script is executed

  • When sql is input interactively, terminator string (; or the string specified with -term string) is ignored

Example of a spooled file

# (2023-04-17 22:52:16)
# select *
#   from tab
#  where rownum < 5
TNAME,TABTYPE,CLUSTERID
AQ$_INTERNET_AGENTS,TABLE,<NULL>
AQ$_INTERNET_AGENT_PRIVS,TABLE,<NULL>
AQ$_KEY_SHARD_MAP,TABLE,<NULL>
AQ$_QUEUES,TABLE,<NULL>
# (2023-04-17 22:52:20)
# history
0,2023-04-17 22:52:05,spool hoge
1,2023-04-17 22:52:16,"select *
  from tab
 where rownum < 5"
2,2023-04-17 22:52:20,history

Install

Download the binary package from Releases and extract the executable.

via Scoop-installer (Windows)
scoop install https://raw.githubusercontent.com/hymkor/sqlbless/master/sqlbless.json

or

scoop bucket add hymkor https://github.com/hymkor/scoop-bucket
scoop install sqlbless
Installing via Go
go install github.com/hymkor/sqlbless/cmd/sqlbless@latest

How to start

$ sqlbless {options} [DRIVERNAME] DATASOURCENAME

DRIVERNAME can be omitted when DATASOURCENAME contains DRIVERNAME.

SQLite3
$ sqlbless sqlite3 :memory:
$ sqlbless sqlite3 path/to/file.db
Oracle
$ sqlbless oracle oracle://USERNAME:PASSWORD@HOSTNAME:PORT/SERVICE
$ sqlbless oracle://USERNAME:PASSWORD@HOSTNAME:PORT/SERVICE
PostgreSQL
$ sqlbless postgres host=127.0.0.1 port=5555 user=USERNAME password=PASSWORD dbname=DBNAME sslmode=disable
$ sqlbless postgres postgres://USERNAME:PASSWORD@127.0.0.1:5555/DBNAME?sslmode=verify-full
$ sqlbless postgres://USERNAME:PASSWORD@127.0.0.1:5555/DBNAME?sslmode=verify-full
SQL Server
$ sqlbless sqlserver sqlserver://@localhost?database=master
$ sqlbless sqlserver://@localhost?database=master
( Windows authentication )
MySQL
$ sqlbless.exe mysql user:password@/database

Common Options

  • -crlf
    • Use CRLF
  • -fs string
    • Set a field separator (default ",")
  • -null string
    • Set a string representing NULL (default "<NULL>")
  • -tsv
    • Use TAB as seperator
  • -f string
    • Start the script
  • -submit-enter
    • Submit by [Enter] and insert a new line by [Ctrl]-[Enter]
  • -debug
    • Print type-information in the header of SELECT and EDIT
  • -help
    • Help

Documentation

Index

Constants

View Source
const (
	DateTimeTzLayout = "2006-01-02 15:04:05.999999999 -07:00"
	DateTimeLayout   = "2006-01-02 15:04:05.999999999"
	DateOnlyLayout   = "2006-01-02"
	TimeOnlyLayout   = "15:04:05.999999999"
	TimeTzLayout     = "15:04:05.999999999 -07:00"
)

Variables

View Source
var Version string

Functions

func Main

func Main() error

func NewConfigFromFlag

func NewConfigFromFlag() func() *Config

NewConfigFromFlag returns the constructor of Config from flag variables.

cfgSetup := NewConfigFromFlag()
flag.Parse()
cfg := cfgSetup()

func ParseAnyDateTime

func ParseAnyDateTime(s string) (time.Time, error)

func RegisterDB

func RegisterDB(name string, setting *DBDialect)

Types

type Coloring

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

func (*Coloring) Init

func (c *Coloring) Init() readline.ColorSequence

func (*Coloring) Next

func (c *Coloring) Next(r rune) readline.ColorSequence

type CommandIn

type CommandIn interface {
	Read(context.Context) ([]string, error)
	GetKey() (string, error)

	// AutoPilotForCsvi returns Tty object when AutoPilot is enabled.
	// When disabled, it MUST return nil.
	AutoPilotForCsvi() getKeyAndSize
}

type Config

type Config struct {
	Auto           string
	Term           string
	CrLf           bool
	Null           string
	Tsv            bool
	FieldSeperator string
	Debug          bool
	SubmitByEnter  bool
	Script         string
}

func (Config) Run

func (cfg Config) Run(driver, dataSourceName string, dbDialect *DBDialect) error

type DBDialect

type DBDialect struct {
	Usage                 string
	SqlForDesc            string
	SqlForTab             string
	DisplayDateTimeLayout string
	TypeNameToConv        func(string) func(string) (string, error)
	DSNFilter             func(string) (string, error)
}

func (*DBDialect) TryTypeNameToConv

func (D *DBDialect) TryTypeNameToConv(typeName string) func(string) (string, error)

type Filter

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

func (*Filter) Close

func (s *Filter) Close() error

func (*Filter) Name

func (s *Filter) Name() string

func (*Filter) Write

func (s *Filter) Write(b []byte) (int, error)

type FilterSource

type FilterSource interface {
	Write([]byte) (int, error)
	Name() string
	Close() error
}

type History

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

func (*History) Add

func (h *History) Add(text string)

func (*History) At

func (h *History) At(n int) string

func (*History) Len

func (h *History) Len() int

type InteractiveIn

type InteractiveIn struct {
	*multiline.Editor
	// contains filtered or unexported fields
}

func (*InteractiveIn) AutoPilotForCsvi

func (i *InteractiveIn) AutoPilotForCsvi() getKeyAndSize

func (*InteractiveIn) GetKey

func (i *InteractiveIn) GetKey() (string, error)

type RowToCsv

type RowToCsv struct {
	Comma      rune
	UseCRLF    bool
	Null       string
	PrintType  bool
	TimeLayout string
}

func (RowToCsv) Dump

func (cfg RowToCsv) Dump(ctx context.Context, rows _RowsI, w io.Writer) error

type Script

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

func (*Script) AutoPilotForCsvi

func (script *Script) AutoPilotForCsvi() getKeyAndSize

func (*Script) GetKey

func (script *Script) GetKey() (string, error)

func (*Script) Read

func (script *Script) Read(context.Context) ([]string, error)

type Session

type Session struct {
	DumpConfig RowToCsv
	// contains filtered or unexported fields
}

func (*Session) Close

func (ss *Session) Close()

func (*Session) Loop

func (ss *Session) Loop(ctx context.Context, commandIn CommandIn, onErrorAbort bool) error

func (*Session) Start

func (ss *Session) Start(ctx context.Context, fname string) error

func (*Session) StartFromStdin

func (ss *Session) StartFromStdin(ctx context.Context) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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