Skip to content

Commit

Permalink
Write initial README and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
chand1012 committed Feb 24, 2024
1 parent c85b317 commit 46a3603
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
61 changes: 54 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
# sq
<h1 align="center">sq</h1>
<h3 align="center">Convert and query JSON, JSONL, CSV, and SQLite with ease!</h3>

Like `jq` , but for SQLite.
`sq` is a simple yet powerful command line tool for query and converting from and to SQLite, CSV, JSON, and JSONL files, heavily inspired by [ `jq` ](https://jqlang.github.io/jq/).

## TODO
## Features

* [ ] Fix JSON input
* [ ] Finish README
* [ ] Write some docs
* [ ] Make a build for all major platforms
* Convert between SQLite, CSV, JSON, and JSONL files.
* Query CSV, JSON, and JSONL using real SQLite queries.
* Allows for rapid scripting and conversion of data.
* Pipe in data or read from files.

## Installation

```bash
go install github.com/chand1012/sq@latest
```

## Examples

Query some orders from a CSV file. Column names for CSV files are converted to lower case and spaces are replaced with underscores.

```bash
$ sq -r orders.csv 'select country from sq where seller_amount > 20 not null;'
United States of America
United States of America
United States of America
United States of America
United States of America
United States of America
United Kingdom
Canada
United States of America
United States of America
United States of America
United States of America
United States of America
Canada
United States of America
Canada
Canada
Australia
United States of America
...
```

Download and query some JSONL datasets.

```bash
$ curl https://raw.githubusercontent.com/TimeSurgeLabs/llm-finetuning/4e934ce602f34f62f4d803c40cd1e7825d216192/data/fingpt-sentiment-1k.jsonl | sq 'select * from sq where output = "positive";' -f jsonl > positive.jsonl
```

You can even use it with `jq` !

```bash
$ curl https://api.gogopool.com/stakers | jq '.stakers' | sq -t stakers 'SELECT stakerAddr,avaxValidating FROM stakers WHERE avaxValidating > 0;' -f json > stakers.json
```
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func run(cmd *cobra.Command, args []string) {
log.Debugf("Input file path: %s", inputFilePath)
d, _, err = db.LoadFile(inputFilePath)
if err != nil {
log.Debug("Input file is not a SQLite database, reading as a regular file")
file, err := os.ReadFile(inputFilePath)
if err != nil {
logger.HandlePanic(log, err, verbose)
Expand Down
4 changes: 2 additions & 2 deletions pkg/db/from_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func FromJSON(b []byte, tableName string) (*sql.DB, string, error) {

columns, types := utils.BreakOutMap(typeMap)

// preprocess the column names
columns = processColumnNames(columns)
// // preprocess the column names
// columns = processColumnNames(columns)

createQuery := genCreateTableQuery(tableName, columns, types)

Expand Down
4 changes: 2 additions & 2 deletions pkg/db/from_jsonl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func FromJSONL(b []byte, tableName string) (*sql.DB, string, error) {

columns, types := utils.BreakOutMap(typeMap)

// preprocess the column names
columns = processColumnNames(columns)
// // preprocess the column names
// columns = processColumnNames(columns)

createQuery := genCreateTableQuery(tableName, columns, types)

Expand Down
17 changes: 17 additions & 0 deletions pkg/db/load_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"database/sql"
"errors"
"os"

_ "github.com/glebarez/go-sqlite"
Expand All @@ -22,6 +23,19 @@ func createTempDB() (*sql.DB, string, error) {
return db, tmpFile.Name(), nil
}

// check if the file is a valid sqlite db
func IsValidDB(fileName string) bool {
db, err := sql.Open("sqlite", fileName)
if err != nil {
return false
}
defer db.Close()

// try to query the db
_, err = db.Query("SELECT * FROM sqlite_master")
return err == nil
}

// load a sql file from bytes
func LoadStdin(bytes []byte) (*sql.DB, string, error) {
tmpFile, err := os.CreateTemp(os.TempDir(), "sq-*.sql")
Expand All @@ -44,6 +58,9 @@ func LoadStdin(bytes []byte) (*sql.DB, string, error) {
}

func LoadFile(fileName string) (*sql.DB, string, error) {
if !IsValidDB(fileName) {
return nil, "", errors.New("file is not a valid SQLite database")
}
db, err := sql.Open("sqlite", fileName)
if err != nil {
return nil, fileName, err
Expand Down

0 comments on commit 46a3603

Please sign in to comment.