Categorygithub.com/vkuznet/sqlshell
modulepackage
0.0.0-20220823200032-fd12d0f8bfd9
Repository: https://github.com/vkuznet/sqlshell.git
Documentation: pkg.go.dev

# README

sqlshell

sqlshell a (better) replacement of database (DB) shell(s)

Build Go Report Card GoDoc

Introduction

Each database has its own Command Line (CLI) tool, e.g. sqlite3 for SQLite database, sqlplus for ORACLE one. All of these tools have their own syntax and behavior and sometime lack of certain features found in other tools. For instance, the sqlplus does not support proper cursor movement (it is not based on ncurses), and therefore lack of useful features such as history, in-place query editing, etc.

The sqlshell provides uniform shell for different database, and therefore it works identically regardless of underlying DB. The DB access is provided via Go sql module and can support any database throught native database libraries.

Build

To build sqlshell you'll need specific DB libraries, e.g.

  • for SQLite you do not need anything to do
  • for ORACLE please obtain ORACLE SDK and adjust accordingly oci8.pc file
  • for MySQL ...
  • for Postgress ... After that, just run make to make a build on your architecture or use make build_linux, etc. for other platforms

Usage

The sqlshell provides the following set of features:

  • full access to SQL commands
    • different output formatting options, e.g. columns, rows or json views
  • persistent history
  • uniform access to different database backend
    • currently sqlshell supports access to SQLite, MySQL, ORACLE, Postgres databases
  • full access to UNIX commands, yes you can execute your favorite UNIX command, e.g. ls or pwd or even vim :)

Here is a preview of sqlshell session:

# start from any UNIX shell
# sqlshell sqlite:///tmp/file.db

# now we are in sqlshell
sqlsh >

# any unix command is supported, e.g.
sqlsh > ls
file1 file2

# use SQL command, by default it will use pairs format which shows
# given DB record as key:value pair printed separately
sqlsh > select * from table;

id   : 1
name : value1

id   : 2
name : value2
...

# change database format
sqlsh > set format
format  : json,pairs,rows or rows:minwidth:tabwidth:padding:padchar
Example : dbformat=rows:4:16:0

# setup db output as rows data-format
sqlsh > set format=rows

# execute query
sqlsh > select * from table;
1 value1
2 value2

# show history
sqlsh > history
ls
pwd
select * from table;

# execute certain command from the history
sqlsh > !3

id   : 1
name : value1

id   : 2
name : value2

The sqlshell also adds useful index,limit commands to manage output from database. Since different DBs use different methods (in MySQL you need to use LIMIT X, Y while in ORALCE you need to wrap your SQL statement into another one to use ROWNU) we provide this function to manage this use-cases. For example, to limit your DB results to a specific range just do the following:

sqlsh > set index=10
sqlsh > set limit=100
sqlsh > select * from table;

and it will show only results within index-limit range (in this case between 10 and 100).

# Functions

helper function to create dbs error.
Flush writes history commands to a file.
No description provided by the author
Read reads content from history file.

# Constants

106 db commit error.
101 database error.
115 decode error.
116 encode error.
generic DBS error.
109 get id db error.
110 db insert error.
112 db last insert error.
123 marshal error.
119 not implemented API error.
107 parser error.
103 query error.
120 io reader error.
104 row scan error.
105 db session error.
102 transaction error.
122 json unmarshal error.
111 update error.
113 validation error.
121 io writer error.

# Variables

COLOR set color output.
DatabaseErr represents generic database error.
DB represents sql DB pointer.
DBFORMAT defines how to print DB records, e.g.
DBOWNER represents DBS DB owner.
DBSQL represents DBS SQL record.
DBTYPE represents DBS DB type, e.g.
No description provided by the author
GenericErr represents generic dbs error.
HIST_LIMIT defines number of line we write to history file.
INDEX represents index value to use when printing DB records by default we use first record.
InvalidParamErr represents generic error for invalid input parameter.
LIMIT represents limit value to use when printing DB records by default there is no limit.
MinWidth used by tabwriter.
NotImplementedApiErr represents generic not implemented api error.
Padding used by tabwriter.
PROMPT represents shell prompt.
TabWidth used by tabwriter.
DB transaction object.

# Structs

DBConfif represents DB configuration file.
DBError represents common structure for DB errors.

# Type aliases

Record represents DBS record.