gosqlparser

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2021 License: MIT Imports: 4 Imported by: 1

README

gosqlparser

Build Status codecov Go Report Card GoDoc

gosqlparser is a simple SQL parser.

Use cases:

  • as part of the database engine;
  • as part of an application API instead of RPC or REST;
  • to query data from CSV and other table-like files.

Installation

As simple as:

go get github.com/krasun/gosqlparser

Usage

package gosqlparser_test

import (
	"fmt"

	"encoding/json"

	sql "github.com/krasun/gosqlparser"
)

func Example() {
	query, err := sql.Parse("SELECT col1, col2 FROM table1 WHERE col1 == \"abc\" AND col3 == 5 LIMIT 10")
	if err != nil {
		fmt.Printf("unexpected error: %s", err)
		return
	}

	json, err := json.Marshal(query)
	if err != nil {
		fmt.Printf("unexpected error: %s", err)
		return
	}

	fmt.Println(string(json))
	// Output:
	// {"Table":"table1","Columns":["col1","col2"],"Where":{"Expr":{"Left":{"Left":{"Name":"col1"},"Operator":0,"Right":{"Value":"\"abc\""}},"Operator":1,"Right":{"Left":{"Name":"col3"},"Operator":0,"Right":{"Value":"5"}}}},"Limit":"10"}
}

Supported Statements

CREATE:

CREATE TABLE table1 (c1 INTEGER, c2 STRING)

DROP:

DROP TABLE table1

SELECT:

SELECT c1, c2 FROM table1 WHERE c3 == c4 AND c5 == c6

INSERT:

INSERT INTO table1 (c1, c2, c3) VALUES (5, "some string", 10)

UPDATE:

UPDATE table1 SET c1 = 10 WHERE c1 == 5 AND c3 == "quoted string"

DELETE:

DELETE FROM table1 WHERE c1 == 5 AND c3 == "quoted string"

Tests

To make sure that the code is fully tested and covered:

$ go test .
ok  	github.com/krasun/gosqlparser	0.470s

Known Usages

  1. krasun/gosqldb - my experimental implementation of a simple database.

License

gosqlparser is released under the MIT license.

Documentation

Overview

Example
package main

import (
	"fmt"

	"encoding/json"

	sql "github.com/krasun/gosqlparser"
)

func main() {
	query, err := sql.Parse("SELECT col1, col2 FROM table1 WHERE col1 == \"abc\" AND col3 == 5 LIMIT 10")
	if err != nil {
		fmt.Printf("unexpected error: %s", err)
		return
	}

	json, err := json.Marshal(query)
	if err != nil {
		fmt.Printf("unexpected error: %s", err)
		return
	}

	fmt.Println(string(json))
}
Output:

{"Table":"table1","Columns":["col1","col2"],"Where":{"Expr":{"Left":{"Left":{"Name":"col1"},"Operator":0,"Right":{"Value":"\"abc\""}},"Operator":1,"Right":{"Left":{"Name":"col3"},"Operator":0,"Right":{"Value":"5"}}}},"Limit":"10"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColumnDefinition

type ColumnDefinition struct {
	Name string
	Type ColumnType
}

ColumnDefinition represents the column definition for CREATE TABLE query.

type ColumnType

type ColumnType int

ColumnType for predefined column types.

const (
	// TypeInteger is an integer type of the column.
	TypeInteger ColumnType = iota
	// TypeString is a string type of the column.
	TypeString
)

func (ColumnType) Name added in v1.0.4

func (t ColumnType) Name() string

Name returns the name of the type represented as a string.

type CreateTable

type CreateTable struct {
	Name    string
	Columns []ColumnDefinition
	Engine  EngineType
}

CreateTable represents CREATE TABLE statement.

func (*CreateTable) GetType added in v1.0.3

func (*CreateTable) GetType() StatementType

GetType returns the statement type.

type Delete

type Delete struct {
	Table string
	Where *Where
}

Delete represents DELETE query.

DELETE FROM Table
WHERE
	...

func (*Delete) GetType added in v1.0.3

func (*Delete) GetType() StatementType

GetType returns the statement type.

type DropTable

type DropTable struct {
	Table string
}

DropTable represents DROP TABLE statement.

func (*DropTable) GetType added in v1.0.3

func (*DropTable) GetType() StatementType

GetType returns the statement type.

type EngineType added in v1.0.5

type EngineType int

EngineType

const (
	// EngineDefault means that engine is not specified and default one can be used.
	EngineDefault EngineType = iota
	// EngineLSM defines LSM-tree engine.
	EngineLSM EngineType = iota
	// EngineBPTree defines B+ tree engine.
	EngineBPTree
)

type Expr

type Expr interface {
	// contains filtered or unexported methods
}

Expr represents expression that can be used in WHERE statement.

type ExprIdentifier

type ExprIdentifier struct {
	Name string
}

ExprIdentifier holds the name of the identifier.

type ExprOperation

type ExprOperation struct {
	Left     Expr
	Operator Operator
	Right    Expr
}

ExprOperation represents operation with == or AND operators.

type ExprValueInteger

type ExprValueInteger struct {
	Value string
}

ExprValueInteger holds the integer value.

type ExprValueString

type ExprValueString struct {
	Value string
}

ExprValueString holds the string value.

type Insert

type Insert struct {
	Table   string
	Columns []string
	Values  []string
}

Insert represents INSERT query.

func (*Insert) GetType added in v1.0.3

func (*Insert) GetType() StatementType

GetType returns the statement type.

type Operator

type Operator int

Operator for prefined operator types.

const (
	// OperatorEquals represents "=="
	OperatorEquals Operator = iota
	// OperatorLogicalAnd represents "AND"
	OperatorLogicalAnd
)

type Select

type Select struct {
	Table   string
	Columns []string
	Where   *Where
	Limit   string
}

Select represents parsed SELECT SQL statement.

func (*Select) GetType added in v1.0.3

func (*Select) GetType() StatementType

GetType returns the statement type.

type Statement

type Statement interface {
	// GetType returns the statement type.
	GetType() StatementType
}

Statement represents parsed SQL statement. Can be one of Select, Insert, Update, Delete, CreateTable or DropTable.

func Parse

func Parse(input string) (Statement, error)

Parse parses the input and returns the statement object or an error.

type StatementType added in v1.0.3

type StatementType int
const (
	// StatementSelect for SELECT query
	StatementSelect StatementType = iota
	// StatementUpdate for UPDATE query
	StatementUpdate
	// StatementInsert for INSERT query
	StatementInsert
	// StatementDelete for DELETE query
	StatementDelete
	// StatementCreateTable for CREATE TABLE query
	StatementCreateTable
	// StatementDropTable for DROP TABLE query
	StatementDropTable
)

type Update

type Update struct {
	Table   string
	Columns []string
	Values  []string
	Where   *Where
}

Update represents UPDATE query.

func (*Update) GetType added in v1.0.3

func (*Update) GetType() StatementType

GetType returns the statement type.

type Where

type Where struct {
	Expr Expr
}

Where represent conditional expressions.

Jump to

Keyboard shortcuts

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