Skip to content

Commit

Permalink
Add english translation of 24-27
Browse files Browse the repository at this point in the history
  • Loading branch information
noborus committed Apr 18, 2024
1 parent b96cf39 commit b6418a0
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 0 deletions.
69 changes: 69 additions & 0 deletions content/trdsql/24_graph.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
+++
author = "Noboru Saito"
title = "trdsql graph"
date = "2024-04-19"
description = "trdsql graph creation method."
weight = 24
tags = [
"trdsql",
"sql",
"graph",
]
categories = [
"trdsql",
]
+++

trdsql does not have a graph creation function, so when you want to create a graph, you will need to use another tool to create it.

Drawing with Excel or LibreOffice is the standard, but here we introduce how to draw a graph with [marianogappa/chart](

[marianogappa/chart](https://github.com/marianogappa/chart)は、Goで作られていて、や多くのプラットフォームで動作して、標準入力から受け取ったデータをブラウザに描画します。
en:[marianogappa/chart](https://github.com/marianogappa/chart) is made in Go and works on many platforms, drawing data received from standard input to the browser.

It is not suitable for complex graphs, but you can draw simple graphs with just a few options.

The data given to chart depends on the graph you want to display, but it is 1 or 2 columns of data.

For example, the default `pie` of chart aggregates data that looks like the following strings and creates a pie chart.

```CSV
aaa
bbb
ccc
aaa
aaa
aaa
```

```console
cat aaa.csv|chart
```

(The browser opens and displays)

![pie.png](../pie.png)

For example, using this to graph the requests of the logs used in [log aggregation](/trdsql/08_log) will look like this.

```console
trdsql "SELECT req FROM log.ltsv"|chart
```

![logpie.png](../logpie.png)

In other graphs, the first column is the x item name, and the second column is given as the value. The default is to receive tab-separated data, so output it in tab-separated format.

When you change the requests with the most [log aggregation](/trdsql/08_log) to the TOP 20 and output it, it will look like this.

```console
trdsql -od "\t" \
"SELECT req, count(req) as count " \
"FROM log.ltsv " \
"GROUP BY req " \
"ORDER BY count DESC LIMIT 20" |chart bar
```

![bar.png](../bar.png)

[marianogappa/chart](https://github.com/marianogappa/chart "github.com/marianogappa/chart") uses [Chart.js](https://www.chartjs.org/ "www.chartjs.org") to draw graphs. Chart.js itself will draw if you provide simple JavaScript, so it is better to use it directly if you want to draw a slightly more complex graph.
91 changes: 91 additions & 0 deletions content/trdsql/25_library.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
+++
author = "Noboru Saito"
title = "trdsql library"
date = "2024-04-19"
description = "trdsql library usage."
weight = 25
tags = [
"trdsql",
"sql",
"library",
]
categories = [
"trdsql",
]
+++


trdsql was initially composed of the main package, but now it is configured to call the trdsql package from main, and the trdsql package can be used as a library.

The trdsql package is configured as follows and can be called individually.

Here is a simple example.

```go
package main

import (
"log"

"github.com/noborus/trdsql"
)

func main() {
trd := trdsql.NewTRDSQL(
trdsql.NewImporter(trdsql.InDelimiter(":")),
trdsql.NewExporter(trdsql.NewWriter()),
)
err := trd.Exec("SELECT c1 FROM /etc/passwd")
if err != nil {
log.Fatal(err)
}
}
```

The above program executes an SQL statement against /etc/passwd.
Give the Importer (interface to import to the database) and Exporter (interface to output results from the database) to New TRDSQL and execute it with Exec.

```go
func NewTRDSQL(im Importer, ex Exporter) *TRDSQL
```

These Importer and Exporter can be replaced as long as they conform to the interface (for example, to import files not in SQL, create a function that conforms to the Importer interface).

## Importer

The default Importer can be created by calling trdsql.NewImporter().
The default Importer takes options from [ReadOpts](https://godoc.org/github.com/noborus/trdsql#ReadOpts) in trdsql.Import(). Here you pass the format and other options.

Importing and using "/etc/passwd" in SQL is the default behavior, so only the delimiter is changed to ":".

trdsql.Import() takes an SQL statement and imports the necessary files into the database. At that time, it is imported from the trdsql.Reader interface (Reader for each CSV, LTSV, JSON, TBLN) according to the file format.

Also, depending on the database to be imported, it selects bulk insert or COPY for import.

## Exporter

The default Exporter can be created by calling trdsql.NewExporter().
In SQL, there is only one output, so we pass the function to output (trdsql.NewWriter()).
trdsql.NewWriter() sets the format and operation options with [WriteOpts](https://godoc.org/github.com/noborus/trdsql#WriteOpts) and writes the results of executing SQL with the actual Writer function (CSV, LTSV, JSON, TBLN, AT, VF...).

## Exec

If the Importer and Exporter are ready, execute the SQL with Exec.

1. Database connection
2. Start transaction
3. Execute import with Importer
4. Execute the specified SQL with Exporter and output
5. End transaction
6. Disconnect database

## References

trdsql has functions to import from slices as well as from files.
There is a sample using it in [_example/slice/](https://github.com/noborus/trdsql/blob/master/_example/slice/main.go "github.com/noborus/trdsql/blob/master/_example/slice/main.go").

Using the trdsql package, you can get the results of [shirou/gopsutil](https://github.com/shirou/gopsutil "github.com/shirou/gopsutil")
with SQL in [noborus/psutilsql](https://github.com/noborus/psutilsql "github.com/noborus/psutilsql").

Please also refer to the [godoc](https://godoc.org/github.com/noborus/trdsql "godoc.org/github.com/noborus/trdsql") of trdsql.
[]
54 changes: 54 additions & 0 deletions content/trdsql/26_file_sql.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
+++
author = "Noboru Saito"
title = "trdsql SQL file"
date = "2024-04-19"
description = "trdsql SQL file"
weight = 26
tags = [
"trdsql",
"sql",
"file",
]
categories = [
"trdsql",
]
+++

## SQL file name option "-q"

When using the trdsql "SQL command" format, it can be difficult to write long SQL commands, and there may be characters that need to be escaped for the shell, making it difficult to read.

trdsql has an option to write SQL to a file and execute the SQL in that file.

Save the SQL written as follows in test.sql.

test.sql

```SQL
SELECT id, `name` FROM testsql.csv
```

When passing it as an argument to the command, you needed to escape the backticks like "\\\`", but you don't need to do that when executing SQL from a file.

testsql.csv is the target CSV file.

```CSV
id,name
1,tarou
2,jirou
```

Instead of "SQL command", execute it with "-q file name.sql". No other options are replaced.

```console
trdsql -ih -oat -q test.sql
```

```ascii table
+----+-------+
| id | name |
+----+-------+
| 1 | tarou |
| 2 | jirou |
+----+-------+
```
97 changes: 97 additions & 0 deletions content/trdsql/27_config.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
+++
author = "Noboru Saito"
title = "trdsql config"
date = "2024-04-19"
description = "trdsql config"
weight = 27
tags = [
"trdsql",
"config",
]
categories = [
"trdsql",
]
+++

trdsql works without a configuration file, but you can change the default database engine with a configuration file.

## Location of the config file

You can specify the location of the file directly with the `-config` option.

The default location is as follows if you do not use the `-config` option.

### Linux and other non-Windows

```path
${HOME}/.config/trdsql/config.json
```

### Windows

`%APPDATA%trdsql\config.json` is the default location. It is often located as follows.

```path
C:\Users\{"User"}\AppData\Roaming\trdsql\config.json
```

## Contents of the config file

The following is a sample.

```json
{
"db": "pdb",
"database": {
"sdb": {
"driver": "sqlite3",
"dsn": ""
},
"pdb": {
"driver": "postgres",
"dsn": "user=test dbname=test"
},
"mdb": {
"driver": "mysql",
"dsn": "user:password@/dbname"
}
}
}
```

Define the database in "name": {"driver": driver name (sqlite3 or postgres or mysql), "dsn": "DSN according to the driver"} in "database", and write the "name" defined in "db" at the beginning to change the default engine.

In the above example, "pdb" is the default, and the "postgres" engine is used.

Not only can you change the default, but by defining it here, you can easily switch to the mysql driver engine by specifying the trdsql option `-db mdb`.

## How to check

Since trdsql works without a configuration file, it can be difficult to tell if the engine has actually been changed.

When you start trdsql with the `-debug` option, detailed information is displayed, so please check there.

### If the configuration file is not found

```console
trdsql -debug -db pdb "SELECT * FROM testdata/test.csv"
2019/12/27 11:22:13 configOpen: open /home/noborus/.config/trdsql/config.json: no such file or directory
2019/12/27 11:22:13 ERROR: db[pdb] does not found
2019/12/27 11:22:13 driver: sqlite3, dsn:
2019/12/27 11:22:13 [SELECT][ ][*][ ][FROM][ ][testdata/test.csv]
... (omitted)
```

### If the configuration file is found

```console
trdsql -debug -db pdb "SELECT * FROM testdata/test.csv"
2019/12/27 11:30:18 config found: config.json.sample
2019/12/27 11:30:18 [driver: sdb:sqlite3:]
2019/12/27 11:30:18 >[driver: pdb:postgres:user=test dbname=test]
2019/12/27 11:30:18 [driver: mdb:mysql:user:password@/dbname]
2019/12/27 11:30:18 [SELECT][ ][*][ ][FROM][ ][testdata/test.csv]
... (omitted)
```

The ">" on the 4th line indicates that pdb is being used.

0 comments on commit b6418a0

Please sign in to comment.