diff --git a/README.md b/README.md index e340217..6e66d54 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a] + Aliases: map[] } ``` @@ -49,6 +50,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a] + Aliases: map[] } ``` @@ -64,6 +66,23 @@ query.Query { 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] } ``` @@ -86,6 +105,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a c d] + Aliases: map[] } ``` @@ -108,6 +128,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a c d] + Aliases: map[] } ``` @@ -130,6 +151,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a c d] + Aliases: map[] } ``` @@ -152,6 +174,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a c d] + Aliases: map[] } ``` @@ -174,6 +197,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a c d] + Aliases: map[] } ``` @@ -196,6 +220,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a c d] + Aliases: map[] } ``` @@ -218,6 +243,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a c d] + Aliases: map[] } ``` @@ -233,6 +259,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [*] + Aliases: map[] } ``` @@ -248,6 +275,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a *] + Aliases: map[] } ``` @@ -277,6 +305,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [a c d] + Aliases: map[] } ``` @@ -299,6 +328,7 @@ query.Query { Updates: map[b:hello] Inserts: [] Fields: [] + Aliases: map[] } ``` @@ -321,6 +351,7 @@ query.Query { Updates: map[b:hello\'world] Inserts: [] Fields: [] + Aliases: map[] } ``` @@ -343,6 +374,7 @@ query.Query { Updates: map[b:hello c:bye] Inserts: [] Fields: [] + Aliases: map[] } ``` @@ -372,6 +404,7 @@ query.Query { Updates: map[b:hello c:bye] Inserts: [] Fields: [] + Aliases: map[] } ``` @@ -394,6 +427,7 @@ query.Query { Updates: map[] Inserts: [] Fields: [] + Aliases: map[] } ``` @@ -409,6 +443,7 @@ query.Query { Updates: map[] Inserts: [[1]] Fields: [b] + Aliases: map[] } ``` @@ -424,6 +459,7 @@ query.Query { Updates: map[] Inserts: [[1 2 3]] Fields: [b c d] + Aliases: map[] } ``` @@ -439,6 +475,7 @@ query.Query { Updates: map[] Inserts: [[1 2 3] [4 5 6]] Fields: [b c d] + Aliases: map[] } ``` diff --git a/README.template b/README.template index 0e35490..9dfb2bd 100644 --- a/README.template +++ b/README.template @@ -43,6 +43,7 @@ query.Query { Updates: {{.Expected.Updates}} Inserts: {{.Expected.Inserts}} Fields: {{.Expected.Fields}} + Aliases: {{.Expected.Aliases}} } ``` {{end}} diff --git a/query/query.go b/query/query.go index 4cb28c4..bfada6a 100644 --- a/query/query.go +++ b/query/query.go @@ -8,6 +8,7 @@ type Query struct { Updates map[string]string Inserts [][]string Fields []string // Used for SELECT (i.e. SELECTed field names) and INSERT (INSERTEDed field names) + Aliases map[string]string } // Type is the type of SQL query, e.g. SELECT/UPDATE diff --git a/sql.go b/sql.go index 7da4ea9..2d457bc 100644 --- a/sql.go +++ b/sql.go @@ -121,6 +121,19 @@ func (p *parser) doParse() (query.Query, error) { p.query.Fields = append(p.query.Fields, identifier) p.pop() maybeFrom := p.peek() + if strings.ToUpper(maybeFrom) == "AS" { + p.pop() + alias := p.peek() + if !isIdentifier(alias) { + return p.query, fmt.Errorf("at SELECT: expected field alias for \"" + identifier + " as\" to SELECT") + } + if p.query.Aliases == nil { + p.query.Aliases = make(map[string]string) + } + p.query.Aliases[identifier] = alias + p.pop() + maybeFrom = p.peek() + } if strings.ToUpper(maybeFrom) == "FROM" { p.step = stepSelectFrom continue @@ -370,7 +383,7 @@ func (p *parser) popWhitespace() { var reservedWords = []string{ "(", ")", ">=", "<=", "!=", ",", "=", ">", "<", "SELECT", "INSERT INTO", "VALUES", "UPDATE", "DELETE FROM", - "WHERE", "FROM", "SET", + "WHERE", "FROM", "SET", "AS", } func (p *parser) peekWithLength() (string, int) { diff --git a/sql_test.go b/sql_test.go index f15cde3..4155302 100644 --- a/sql_test.go +++ b/sql_test.go @@ -70,6 +70,21 @@ func TestSQL(t *testing.T) { Expected: query.Query{Type: query.Select, TableName: "b", Fields: []string{"a", "c", "d"}}, Err: nil, }, + { + Name: "SELECT with alias works", + SQL: "SELECT a as z, b as y, c FROM 'b'", + Expected: query.Query{ + Type: query.Select, + TableName: "b", + Fields: []string{"a", "b", "c"}, + Aliases: map[string]string{ + "a": "z", + "b": "y", + }, + }, + Err: nil, + }, + { Name: "SELECT with empty WHERE fails", SQL: "SELECT a, c, d FROM 'b' WHERE",