parser

package
v0.0.0-...-bbac96e Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2020 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

tongue-demon. A cli tool to generate go code from your sql code. Copyright (C) 2020 Rotten Network

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

tongue-demon. A cli tool to generate go code from your sql code. Copyright (C) 2020 Rotten Network

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

tongue-demon. A cli tool to generate go code from your sql code. Copyright (C) 2020 Rotten Network

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Index

Constants

This section is empty.

Variables

View Source
var DeleteTestCases = []TestCase{

	{
		Name: "DELETE with WHERE works",
		SQL:  "DELETE FROM 'a' WHERE b = '1'",
		Statement: &statement.Statement{
			Type:  statement.Delete,
			Table: &statement.Table{Name: "a"},
			Conditions: []*statement.Condition{
				0: {Operand1: "b", Operator: statement.Eq, Operand2: "1"},
			},
		},
		Err: nil,
	},
}
View Source
var InsertTestCases = []TestCase{

	{
		Name: "INSERT works",
		SQL:  "INSERT INTO 'a' (b) VALUES ('1')",
		Statement: &statement.Statement{
			Type:  statement.Insert,
			Table: &statement.Table{Name: "a"},
			Columns: []*statement.Column{
				0: {Name: "b"},
			},
			InsertSets: []*[]string{
				{"1"},
			},
		},
		Err: nil,
	},
	{
		Name: "INSERT with multiple fields works",
		SQL:  "INSERT INTO 'a' (b,c,d) VALUES ('1','2','3' )",
		Statement: &statement.Statement{
			Type:  statement.Insert,
			Table: &statement.Table{Name: "a"},
			Columns: []*statement.Column{
				{Name: "b"},
				{Name: "c"},
				{Name: "d"},
			},
			InsertSets: []*[]string{
				{"1", "2", "3"},
			},
		},
		Err: nil,
	},
	{
		Name: "INSERT with multiple fields and multiple values works",
		SQL:  "INSERT INTO 'a' (b,c,d) VALUES ('1','2' ,'3' ),('4','5' ,'6' )",
		Statement: &statement.Statement{
			Type:  statement.Insert,
			Table: &statement.Table{Name: "a"},
			Columns: []*statement.Column{
				{Name: "b"},
				{Name: "c"},
				{Name: "d"},
			},
			InsertSets: []*[]string{
				{"1", "2", "3"},
				{"4", "5", "6"},
			},
		},
		Err: nil,
	},
}
View Source
var SelectTestCases = []TestCase{

	{
		Name: "SELECT works",
		SQL:  "SELECT a FROM 'b'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Fields: []string{
				"a",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with field reference to table works",
		SQL:  "SELECT b.a FROM 'b'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Fields: []string{
				"b.a",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT works with As alias works",
		SQL:  "SELECT a FROM b AS B",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b", Alias: "B"},
			Fields: []string{
				"a",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT works without AS  works",
		SQL:  "SELECT a FROM b B",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b", Alias: "B"},
			Fields: []string{
				0: "a",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT works with lowercase",
		SQL:  "select a fRoM 'b'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Fields: []string{
				"a",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT many fields works",
		SQL:  "SELECT a, c, d FROM 'b'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT many fields from table without quotations works",
		SQL:  "SELECT a, c, d FROM b",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},

	{
		Name: "SELECT with WHERE with = works",
		SQL:  "SELECT a, c, d FROM 'b' WHERE a = ''",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Eq, Operand2: ""},
			},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with WHERE with < works",
		SQL:  "SELECT a, c, d FROM 'b' WHERE a < '1'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Lt, Operand2: "1"},
			},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with WHERE with <= works",
		SQL:  "SELECT a, c, d FROM 'b' WHERE a <= '1'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Lte, Operand2: "1"},
			},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with WHERE with > works",
		SQL:  "SELECT a, c, d FROM 'b' WHERE a > '1'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Gt, Operand2: "1"},
			},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with WHERE with >= works",
		SQL:  "SELECT a, c, d FROM 'b' WHERE a >= '1'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Gte, Operand2: "1"},
			},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with WHERE with != works",
		SQL:  "SELECT a, c, d FROM 'b' WHERE a != '1'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Ne, Operand2: "1"},
			},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT * works",
		SQL:  "SELECT * FROM 'b'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Fields: []string{
				"*",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT *,a works",
		SQL:  "SELECT *,a FROM 'b'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Fields: []string{
				"*",
				"a",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with WHERE with two conditions using AND works",
		SQL:  "SELECT a, c, d FROM 'b' WHERE a != '1' AND b = '2'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Ne, Operand2: "1"},
				{Operand1: "b", Operator: statement.Eq, Operand2: "2"},
			},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with WHERE with nested expression works",
		SQL:  "SELECT a, c, d FROM 'b' WHERE a != '1' AND (b = '2' OR c='3')",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Ne, Operand2: "1"},
				{Operand1: "b", Operator: statement.Eq, Operand2: "2"},
				{Operand1: "c", Operator: statement.Eq, Operand2: "3"},
			},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with WHERE with two conditions using OR works",
		SQL:  "SELECT a, c, d FROM 'b' WHERE a != '1' OR b = '2'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Ne, Operand2: "1"},
				{Operand1: "b", Operator: statement.Eq, Operand2: "2"},
			},
			Fields: []string{
				"a",
				"c",
				"d",
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with recursive SELECT",
		SQL:  "SELECT * FROM B WHERE A NOT IN (SELECT * FROM F WHERE C >9)",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "B"},
			Fields: []string{
				0: "*",
			},
			Conditions: []*statement.Condition{
				0: {Operand1: "A", Operator: statement.In},
			},
			Children: []*statement.Statement{
				{
					Type:  statement.Select,
					Table: &statement.Table{Name: "F"},
					Conditions: []*statement.Condition{
						{Operand1: "C", Operator: statement.Gt, Operand2: "9"},
					},
					Fields: []string{
						"*",
					},
				},
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with recursive SELECT withing recursive select",
		SQL:  "SELECT * FROM B WHERE A NOT IN (SELECT * FROM F WHERE J NOT IN (SELECT * FROM K WHERE L>8))",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "B"},
			Fields: []string{
				"*",
			},
			Conditions: []*statement.Condition{
				{Operand1: "A", Operator: statement.In},
			},
			Children: []*statement.Statement{
				{
					Type:  statement.Select,
					Table: &statement.Table{Name: "F"},
					Fields: []string{
						"*",
					},
					Conditions: []*statement.Condition{
						{Operand1: "J", Operator: statement.In},
					},
					Children: []*statement.Statement{
						{
							Type:  statement.Select,
							Table: &statement.Table{Name: "K"},
							Conditions: []*statement.Condition{
								{Operand1: "L", Operator: statement.Gt, Operand2: "8"},
							},
							Fields: []string{
								"*",
							},
						},
					},
				},
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with recursive SELECT with JOIN",
		SQL:  "SELECT * FROM B WHERE A NOT IN (SELECT * FROM F LEFT JOIN OTHER_TABLE OT ON OT.ID = F.OT WHERE C >9)",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "B"},
			Fields: []string{
				"*",
			},
			Conditions: []*statement.Condition{
				{Operand1: "A", Operator: statement.In},
			},
			Children: []*statement.Statement{
				{
					Type:  statement.Select,
					Table: &statement.Table{Name: "F", Alias: "LEFT"},
					Conditions: []*statement.Condition{
						{Operand1: "OT.ID", Operator: statement.Eq, Operand2: "F.OT"},
						{Operand1: "C", Operator: statement.Gt, Operand2: "9"},
					},
					Fields: []string{
						0: "*",
					},
					Joins: []*statement.Join{
						{
							Table: &statement.Table{
								Name:  "OTHER_TABLE",
								Alias: "OT",
							},
							Conditions: []*statement.Condition{
								{
									Operand1: "OT.ID",
									Operator: statement.Eq,
									Operand2: "F.OT",
								},
							},
						},
					},
				},
			},
		},

		Err: nil,
	},
	{
		Name: "SELECT with JOIN WORKS",
		SQL:  "SELECT a.f,b.ff FROM B AS b LEFT JOIN A a ON b.id=a.b WHERE a != '1'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "B", Alias: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "b.id", Operator: statement.Eq, Operand2: "a.b"},
				{Operand1: "a", Operator: statement.Ne, Operand2: "1"},
			},
			Fields: []string{
				"a.f",
				"b.ff",
			},
			Joins: []*statement.Join{
				{
					Table: &statement.Table{
						Name:  "A",
						Alias: "a",
					},
					Conditions: []*statement.Condition{
						{
							Operand1: "b.id",
							Operator: statement.Eq,
							Operand2: "a.b",
						},
					},
				},
			},
		},
		Err: nil,
	},
	{
		Name: "SELECT with two JOIN WORKS",
		SQL:  "SELECT a.f,b.ff,c.fff FROM B AS b LEFT JOIN A a ON b.id=a.b RIGHT JOIN C c ON b.id=c.b WHERE a != '1'",
		Statement: &statement.Statement{
			Type:  statement.Select,
			Table: &statement.Table{Name: "B", Alias: "b"},
			Conditions: []*statement.Condition{
				{Operand1: "b.id", Operator: statement.Eq, Operand2: "a.b"},
				{Operand1: "b.id", Operator: statement.Eq, Operand2: "c.b"},
				{Operand1: "a", Operator: statement.Ne, Operand2: "1"},
			},
			Fields: []string{
				"a.f",
				"b.ff",
				"c.fff",
			},
			Joins: []*statement.Join{
				{
					Table: &statement.Table{
						Name:  "A",
						Alias: "a",
					},
					Conditions: []*statement.Condition{
						{
							Operand1: "b.id",
							Operator: statement.Eq,
							Operand2: "a.b",
						},
					},
				},
				{
					Table: &statement.Table{
						Name:  "C",
						Alias: "c",
					},
					Conditions: []*statement.Condition{
						{
							Operand1: "b.id",
							Operator: statement.Eq,
							Operand2: "c.b",
						},
					},
				},
			},
		},
		Err: nil,
	},
}
View Source
var UpdateTestCases = []TestCase{

	{
		Name: "UPDATE works",
		SQL:  "UPDATE 't' SET b = 'hello' WHERE a = '1'",
		Statement: &statement.Statement{
			Type:  statement.Update,
			Table: &statement.Table{Name: "t"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Eq, Operand2: "1"},
			},
			Updates: []*statement.UpdateField{
				{Column: "b", Value: "hello"},
			},
		},
		Err: nil,
	},
	{
		Name: "UPDATE with multiple SETs works",
		SQL:  "UPDATE 'a' SET b = 'hello', c = 'bye' WHERE a = '1'",
		Statement: &statement.Statement{
			Type:  statement.Update,
			Table: &statement.Table{Name: "a"},
			Conditions: []*statement.Condition{
				0: {Operand1: "a", Operator: statement.Eq, Operand2: "1"},
			},
			Updates: []*statement.UpdateField{
				0: {Column: "b", Value: "hello"},
				1: {Column: "c", Value: "bye"},
			},
		},
		Err: nil,
	},
	{
		Name: "UPDATE with multiple SETs and multiple conditions works",
		SQL:  "UPDATE 'a' SET b = 'hello', c = 'bye' WHERE a = '1' AND b = '789'",
		Statement: &statement.Statement{
			Type:  statement.Update,
			Table: &statement.Table{Name: "a"},
			Conditions: []*statement.Condition{
				{Operand1: "a", Operator: statement.Eq, Operand2: "1"},
				{Operand1: "b", Operator: statement.Eq, Operand2: "789"},
			},
			Updates: []*statement.UpdateField{
				{Column: "b", Value: "hello"},
				{Column: "c", Value: "bye"},
			},
		},
		Err: nil,
	},
}

Functions

func RunTestCases

func RunTestCases(t *testing.T, ts []TestCase, parse Parse)

Types

type Error

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

func NewError

func NewError(line, col int, msg string) *Error

func (*Error) Error

func (e *Error) Error() string

type Parse

type Parse = func(string) (*statement.Statement, error)

type TestCase

type TestCase struct {
	Name      string
	SQL       string
	Statement *statement.Statement
	Err       *Error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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