mdtsql

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 14 Imported by: 0

README

mdtsql

A CLI tool that executes SQL queries and converts the results into a Markdown table.

install

Go install
go install github.com/noborus/mdtsql/cmd/mdtsql@latest
Homebrew
brew install noborus/tap/mdtsql

Usage

Executes SQL for markdown containing table. The result can be output to CSV, JSON, LTSV, YAML, Markdown, etc.

mdtsql query "SELECT * FROM file" file.md
mdtsql table file.md
option
mdtsql -h
Execute SQL for table in markdown.
The result can be output to CSV, JSON, LTSV, YAML, Markdown, etc.

Usage:
  mdtsql [flags]
  mdtsql [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  list        List and analyze SQL dumps
  query       Execute SQL queries on markdown table and tabular data
  table       SQL(SELECT * FROM table) for markdown table and tabular data

Flags:
  -d, --Delimiter string   output delimiter (CSV only) (default ",")
  -O, --Header             output header (CSV only)
  -o, --OutFormat string   output format=at|csv|ltsv|json|jsonl|tbln|raw|md|vf|yaml (default "md")
  -c, --caption            caption table name
      --config string      config file (default is $HOME/.mdtsql.yaml)
      --debug              debug print
  -h, --help               help for mdtsql
  -q, --query string       SQL query
  -t, --toggle             Help message for toggle
  -v, --version            display version information

Use "mdtsql [command] --help" for more information about a command.
Example
mdtsql query "SELECT * FROM file.md"
c1 a b c
1 a1 b1 c1
2 a2 b2 c2
3 a3 b3 c3

If the markdown includes multiple tables, the second and subsequent tables are marked with ::number.

mdtsql query "SELECT * FROM file.md::1"

Specify the output format with option -o. -o csv, -o ltsv, -ojson ...

mdtsql -o csv query "SELECT * FROM file.md"
1,a1,b1,c1
2,a2,b2,c2
3,a3,b3,c3
List Command

The list command displays all the tables in the specified markdown file.

mdtsql list file.md
mdtsql list abc.md
Table Name: [0]
+-------------+------+
| column name | type |
+-------------+------+
| c1          | text |
| a           | text |
| b           | text |
| c           | text |
+-------------+------+

Table Name: [1]
+-------------+------+
| column name | type |
+-------------+------+
| c1          | text |
| a           | text |
| b           | text |
| c           | text |
+-------------+------+

Table Name: [2]
+-------------+------+
| column name | type |
+-------------+------+
| c1          | text |
| a           | text |
| b           | text |
| c           | text |
+-------------+------+

Table Command

The table command executes SQL(SELECT * FROM table) for markdown table and tabular data.

mdtsql table file.md
mdtsql table file.md::1

Caption option

The --caption or -c option specifies a caption name, not a sequential number. This allows you to specify the same table even if the order changes.

mdtsql --caption list testdata/abc.md
Table Name: [header]
+-------------+------+
| column name | type |
+-------------+------+
| c1          | text |
| a           | text |
| b           | text |
| c           | text |
+-------------+------+

Table Name: [caption]
+-------------+------+
| column name | type |
+-------------+------+
| c1          | text |
| a           | text |
| b           | text |
| c           | text |
+-------------+------+

Table Name: [caption_1]
+-------------+------+
| column name | type |
+-------------+------+
| c1          | text |
| a           | text |
| b           | text |
| c           | text |
+-------------+------+
mdtsql --caption query "SELECT * FROM testdata/abc.md::caption_1"
| c1 | a  | b  | c  |
|----|----|----|----|
|  1 | a1 | b1 | c1 |
|  2 | a2 | b2 | c2 |
|  3 | a3 | b3 | c3 |

Multiple queries

You can specify multiple queries with the ; separator.

$ mdtsql query "INSERT INTO abc.md::2 (c1, a, b, c) VALUES ('4', 'a4', 'b4', 'c4');SELECT * FROM abc.md::2"
c1 a b c
1 a1 b1 c1
2 a2 b2 c2
3 a3 b3 c3
4 a4 b4 c4
$ mdtsql query "UPDATE abc.md::2 SET c='u4' WHERE c1=3;SELECT * FROM abc.md::2"
c1 a b c
1 a1 b1 c1
2 a2 b2 c2
3 a3 b3 u4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Caption bool

Caption is a caption specification.

Functions

func Analyze

func Analyze(fileName string) ([]table, error)

func Dump added in v0.1.0

func Dump(w io.Writer, tables []table)

func NewMDTReader added in v0.1.0

func NewMDTReader(reader io.Reader, opts *trdsql.ReadOpts) (trdsql.Reader, error)

NewMDTReader returns a new MDTReader.

Types

type MDTReader added in v0.1.0

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

MDTReader is a reader for markdown table.

func (MDTReader) Names added in v0.1.0

func (t MDTReader) Names() ([]string, error)

func (MDTReader) PreReadRow added in v0.1.0

func (t MDTReader) PreReadRow() [][]interface{}

func (MDTReader) ReadRow added in v0.1.0

func (t MDTReader) ReadRow(row []interface{}) ([]interface{}, error)

ReadRow only returns EOF.

func (MDTReader) Types added in v0.1.0

func (t MDTReader) Types() ([]string, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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