• Stars
    star
    392
  • Rank 109,735 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple SQL parser meant for querying CSV files

sqlparser - meant for querying csv files

Build Status Coverage Status GitHub license Go Report Card GoDoc

Usage

package main

import (
	"fmt"
	"log"

	"github.com/marianogappa/sqlparser"
)

func main() {
	query, err := sqlparser.Parse("SELECT a, b, c FROM 'd' WHERE e = '1' AND f > '2'")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+#v", query)
}

Example: SELECT works

query, err := sqlparser.Parse(`SELECT a FROM 'b'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: []
	Updates: map[]
	Inserts: []
	Fields: [a]
	Aliases: map[]
}

Example: SELECT works with lowercase

query, err := sqlparser.Parse(`select a fRoM 'b'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: []
	Updates: map[]
	Inserts: []
	Fields: [a]
	Aliases: map[]
}

Example: SELECT many fields works

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: []
	Updates: map[]
	Inserts: []
	Fields: [a c d]
	Aliases: map[]
}

Example: SELECT with alias works

query, err := sqlparser.Parse(`SELECT a as z, b as y, c FROM 'b'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: []
	Updates: map[]
	Inserts: []
	Fields: [a b c]
	Aliases: map[a:z b:y]
}

Example: SELECT with WHERE with = works

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a = ''`)

query.Query {
	Type: Select
	TableName: b
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Eq,
            Operand2: ,
            Operand2IsField: false,
        }]
	Updates: map[]
	Inserts: []
	Fields: [a c d]
	Aliases: map[]
}

Example: SELECT with WHERE with < works

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a < '1'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Lt,
            Operand2: 1,
            Operand2IsField: false,
        }]
	Updates: map[]
	Inserts: []
	Fields: [a c d]
	Aliases: map[]
}

Example: SELECT with WHERE with <= works

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a <= '1'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Lte,
            Operand2: 1,
            Operand2IsField: false,
        }]
	Updates: map[]
	Inserts: []
	Fields: [a c d]
	Aliases: map[]
}

Example: SELECT with WHERE with > works

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a > '1'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Gt,
            Operand2: 1,
            Operand2IsField: false,
        }]
	Updates: map[]
	Inserts: []
	Fields: [a c d]
	Aliases: map[]
}

Example: SELECT with WHERE with >= works

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a >= '1'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Gte,
            Operand2: 1,
            Operand2IsField: false,
        }]
	Updates: map[]
	Inserts: []
	Fields: [a c d]
	Aliases: map[]
}

Example: SELECT with WHERE with != works

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a != '1'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Ne,
            Operand2: 1,
            Operand2IsField: false,
        }]
	Updates: map[]
	Inserts: []
	Fields: [a c d]
	Aliases: map[]
}

Example: SELECT with WHERE with != works (comparing field against another field)

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a != b`)

query.Query {
	Type: Select
	TableName: b
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Ne,
            Operand2: b,
            Operand2IsField: true,
        }]
	Updates: map[]
	Inserts: []
	Fields: [a c d]
	Aliases: map[]
}

Example: SELECT * works

query, err := sqlparser.Parse(`SELECT * FROM 'b'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: []
	Updates: map[]
	Inserts: []
	Fields: [*]
	Aliases: map[]
}

Example: SELECT a, * works

query, err := sqlparser.Parse(`SELECT a, * FROM 'b'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: []
	Updates: map[]
	Inserts: []
	Fields: [a *]
	Aliases: map[]
}

Example: SELECT with WHERE with two conditions using AND works

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a != '1' AND b = '2'`)

query.Query {
	Type: Select
	TableName: b
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Ne,
            Operand2: 1,
            Operand2IsField: false,
        }
        {
            Operand1: b,
            Operand1IsField: true,
            Operator: Eq,
            Operand2: 2,
            Operand2IsField: false,
        }]
	Updates: map[]
	Inserts: []
	Fields: [a c d]
	Aliases: map[]
}

Example: UPDATE works

query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello' WHERE a = '1'`)

query.Query {
	Type: Update
	TableName: a
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Eq,
            Operand2: 1,
            Operand2IsField: false,
        }]
	Updates: map[b:hello]
	Inserts: []
	Fields: []
	Aliases: map[]
}

Example: UPDATE works with simple quote inside

query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello\'world' WHERE a = '1'`)

query.Query {
	Type: Update
	TableName: a
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Eq,
            Operand2: 1,
            Operand2IsField: false,
        }]
	Updates: map[b:hello\'world]
	Inserts: []
	Fields: []
	Aliases: map[]
}

Example: UPDATE with multiple SETs works

query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello', c = 'bye' WHERE a = '1'`)

query.Query {
	Type: Update
	TableName: a
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Eq,
            Operand2: 1,
            Operand2IsField: false,
        }]
	Updates: map[b:hello c:bye]
	Inserts: []
	Fields: []
	Aliases: map[]
}

Example: UPDATE with multiple SETs and multiple conditions works

query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello', c = 'bye' WHERE a = '1' AND b = '789'`)

query.Query {
	Type: Update
	TableName: a
	Conditions: [
        {
            Operand1: a,
            Operand1IsField: true,
            Operator: Eq,
            Operand2: 1,
            Operand2IsField: false,
        }
        {
            Operand1: b,
            Operand1IsField: true,
            Operator: Eq,
            Operand2: 789,
            Operand2IsField: false,
        }]
	Updates: map[b:hello c:bye]
	Inserts: []
	Fields: []
	Aliases: map[]
}

Example: DELETE with WHERE works

query, err := sqlparser.Parse(`DELETE FROM 'a' WHERE b = '1'`)

query.Query {
	Type: Delete
	TableName: a
	Conditions: [
        {
            Operand1: b,
            Operand1IsField: true,
            Operator: Eq,
            Operand2: 1,
            Operand2IsField: false,
        }]
	Updates: map[]
	Inserts: []
	Fields: []
	Aliases: map[]
}

Example: INSERT works

query, err := sqlparser.Parse(`INSERT INTO 'a' (b) VALUES ('1')`)

query.Query {
	Type: Insert
	TableName: a
	Conditions: []
	Updates: map[]
	Inserts: [[1]]
	Fields: [b]
	Aliases: map[]
}

Example: INSERT with multiple fields works

query, err := sqlparser.Parse(`INSERT INTO 'a' (b,c,    d) VALUES ('1','2' ,  '3' )`)

query.Query {
	Type: Insert
	TableName: a
	Conditions: []
	Updates: map[]
	Inserts: [[1 2 3]]
	Fields: [b c d]
	Aliases: map[]
}

Example: INSERT with multiple fields and multiple values works

query, err := sqlparser.Parse(`INSERT INTO 'a' (b,c,    d) VALUES ('1','2' ,  '3' ),('4','5' ,'6' )`)

query.Query {
	Type: Insert
	TableName: a
	Conditions: []
	Updates: map[]
	Inserts: [[1 2 3] [4 5 6]]
	Fields: [b c d]
	Aliases: map[]
}

Example: empty query fails

query, err := sqlparser.Parse(``)

query type cannot be empty

Example: SELECT without FROM fails

query, err := sqlparser.Parse(`SELECT`)

table name cannot be empty

Example: SELECT without fields fails

query, err := sqlparser.Parse(`SELECT FROM 'a'`)

at SELECT: expected field to SELECT

Example: SELECT with comma and empty field fails

query, err := sqlparser.Parse(`SELECT b, FROM 'a'`)

at SELECT: expected field to SELECT

Example: SELECT with empty WHERE fails

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE`)

at WHERE: empty WHERE clause

Example: SELECT with WHERE with only operand fails

query, err := sqlparser.Parse(`SELECT a, c, d FROM 'b' WHERE a`)

at WHERE: condition without operator

Example: Empty UPDATE fails

query, err := sqlparser.Parse(`UPDATE`)

table name cannot be empty

Example: Incomplete UPDATE with table name fails

query, err := sqlparser.Parse(`UPDATE 'a'`)

at WHERE: WHERE clause is mandatory for UPDATE & DELETE

Example: Incomplete UPDATE with table name and SET fails

query, err := sqlparser.Parse(`UPDATE 'a' SET`)

at WHERE: WHERE clause is mandatory for UPDATE & DELETE

Example: Incomplete UPDATE with table name, SET with a field but no value and WHERE fails

query, err := sqlparser.Parse(`UPDATE 'a' SET b WHERE`)

at UPDATE: expected '='

Example: Incomplete UPDATE with table name, SET with a field and = but no value and WHERE fails

query, err := sqlparser.Parse(`UPDATE 'a' SET b = WHERE`)

at UPDATE: expected quoted value

Example: Incomplete UPDATE due to no WHERE clause fails

query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello' WHERE`)

at WHERE: empty WHERE clause

Example: Incomplete UPDATE due incomplete WHERE clause fails

query, err := sqlparser.Parse(`UPDATE 'a' SET b = 'hello' WHERE a`)

at WHERE: condition without operator

Example: Empty DELETE fails

query, err := sqlparser.Parse(`DELETE FROM`)

table name cannot be empty

Example: DELETE without WHERE fails

query, err := sqlparser.Parse(`DELETE FROM 'a'`)

at WHERE: WHERE clause is mandatory for UPDATE & DELETE

Example: DELETE with empty WHERE fails

query, err := sqlparser.Parse(`DELETE FROM 'a' WHERE`)

at WHERE: empty WHERE clause

Example: DELETE with WHERE with field but no operator fails

query, err := sqlparser.Parse(`DELETE FROM 'a' WHERE b`)

at WHERE: condition without operator

Example: Empty INSERT fails

query, err := sqlparser.Parse(`INSERT INTO`)

table name cannot be empty

Example: INSERT with no rows to insert fails

query, err := sqlparser.Parse(`INSERT INTO 'a'`)

at INSERT INTO: need at least one row to insert

Example: INSERT with incomplete value section fails

query, err := sqlparser.Parse(`INSERT INTO 'a' (`)

at INSERT INTO: need at least one row to insert

Example: INSERT with incomplete value section fails #2

query, err := sqlparser.Parse(`INSERT INTO 'a' (b`)

at INSERT INTO: need at least one row to insert

Example: INSERT with incomplete value section fails #3

query, err := sqlparser.Parse(`INSERT INTO 'a' (b)`)

at INSERT INTO: need at least one row to insert

Example: INSERT with incomplete value section fails #4

query, err := sqlparser.Parse(`INSERT INTO 'a' (b) VALUES`)

at INSERT INTO: need at least one row to insert

Example: INSERT with incomplete row fails

query, err := sqlparser.Parse(`INSERT INTO 'a' (b) VALUES (`)

at INSERT INTO: value count doesn't match field count

Example: INSERT * fails

query, err := sqlparser.Parse(`INSERT INTO 'a' (*) VALUES ('1')`)

at INSERT INTO: expected at least one field to insert

More Repositories

1

chart

Quick & smart charting for STDIN
Go
544
star
2

flowbro

Real-time flowchart visualisation for Kafka-based distributed systems.
Go
121
star
3

sql

MySQL & PostgreSQL pipe
Go
86
star
4

gomario

Basic Golang text-mode Mario-like game
Go
76
star
5

cheesse

Package, server, cli tool and WebAssembly binary for chess use cases.
Go
63
star
6

ostinato

A chess library that runs on the server (Scala) and on the browser (ScalaJS).
Scala
51
star
7

jira-cli

Lightweight bash script for easily querying your company's JIRA issues
Shell
42
star
8

crypto-candles

Universal crypto candlestick iterator library & CLI
Go
13
star
9

sd

Diffs two streams of newline-separated strings, timing them out if necessary.
Go
13
star
10

leetcode

Go
12
star
11

parseq

Parallel processing with sequential output, respecting order of input
Go
10
star
12

Spreadsheet-to-MediaWiki-table-Converter

Converts an Google Spreadsheet/Excel Sheet to MediaWiki table format, providing styling features.
PHP
10
star
13

mediawiki-table-utility

A handy tool to convert tab-separated tables (e.g. Excel, Google Spreadsheets) to the MediaWiki table format (e.g. Wikipedia)
HTML
7
star
14

kafka-examples

A suite of working test cases for getting started with Apache Kafka painlessly (in Scala)
Scala
5
star
15

bookie

Indexing for Kafka queues.
Go
4
star
16

ponganoid

A simple cli game reminiscent of Pong and Arkanoid.
Go
4
star
17

sctool

Starcraft: Remastered replay analyzer library and CLI tool
Go
3
star
18

hackerrank

Go
3
star
19

happiest-state-python

Determining the happiest USA state using Twitter streams and Sentiment Analysis in Python.
Python
3
star
20

blacklist

DNS-based blacklist checker
Go
3
star
21

fsm

Ridiculously hacky tool to help me keep in touch with my friends.
Go
2
star
22

iknowkungfoo

Website aiming to provide easiest way to learn Algos & DS for FAANGs
Go
2
star
23

bincli

Binance CLI
Go
2
star
24

cheesse-examples

JavaScript
2
star
25

crypto-predictions

State machine-based engine for tracking crypto-related predictions across social media posts.
Go
2
star
26

ostinato-examples

A quick and dirty demo to showcase the ostinato library
JavaScript
2
star
27

termserve

Go
1
star
28

chessboard-image-builder

Shell
1
star
29

kafkasimpletest

A working smoke int test for Apache Kafka to start up a Scala project
Scala
1
star
30

signal-checker

cli tool, server & library to check the results of crypto signals against an exchange's historical data.
Go
1
star
31

tablerenderer

A minimalist script to render an associative array as a MySQL result table
PHP
1
star
32

marianogappa.github.io

My blog about Software Engineering and Music Production
HTML
1
star