Skip to content

Commit

Permalink
Merge pull request #6 from coreybutler/master
Browse files Browse the repository at this point in the history
Add field alias support.
  • Loading branch information
marianogappa authored Aug 2, 2021
2 parents 8e57d23 + 3adc75a commit c862319
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a]
Aliases: map[]
}
```

Expand All @@ -49,6 +50,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a]
Aliases: map[]
}
```

Expand All @@ -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]
}
```

Expand All @@ -86,6 +105,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a c d]
Aliases: map[]
}
```

Expand All @@ -108,6 +128,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a c d]
Aliases: map[]
}
```

Expand All @@ -130,6 +151,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a c d]
Aliases: map[]
}
```

Expand All @@ -152,6 +174,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a c d]
Aliases: map[]
}
```

Expand All @@ -174,6 +197,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a c d]
Aliases: map[]
}
```

Expand All @@ -196,6 +220,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a c d]
Aliases: map[]
}
```

Expand All @@ -218,6 +243,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a c d]
Aliases: map[]
}
```

Expand All @@ -233,6 +259,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [*]
Aliases: map[]
}
```

Expand All @@ -248,6 +275,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a *]
Aliases: map[]
}
```

Expand Down Expand Up @@ -277,6 +305,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: [a c d]
Aliases: map[]
}
```

Expand All @@ -299,6 +328,7 @@ query.Query {
Updates: map[b:hello]
Inserts: []
Fields: []
Aliases: map[]
}
```

Expand All @@ -321,6 +351,7 @@ query.Query {
Updates: map[b:hello\'world]
Inserts: []
Fields: []
Aliases: map[]
}
```

Expand All @@ -343,6 +374,7 @@ query.Query {
Updates: map[b:hello c:bye]
Inserts: []
Fields: []
Aliases: map[]
}
```

Expand Down Expand Up @@ -372,6 +404,7 @@ query.Query {
Updates: map[b:hello c:bye]
Inserts: []
Fields: []
Aliases: map[]
}
```

Expand All @@ -394,6 +427,7 @@ query.Query {
Updates: map[]
Inserts: []
Fields: []
Aliases: map[]
}
```

Expand All @@ -409,6 +443,7 @@ query.Query {
Updates: map[]
Inserts: [[1]]
Fields: [b]
Aliases: map[]
}
```

Expand All @@ -424,6 +459,7 @@ query.Query {
Updates: map[]
Inserts: [[1 2 3]]
Fields: [b c d]
Aliases: map[]
}
```

Expand All @@ -439,6 +475,7 @@ query.Query {
Updates: map[]
Inserts: [[1 2 3] [4 5 6]]
Fields: [b c d]
Aliases: map[]
}
```

Expand Down
1 change: 1 addition & 0 deletions README.template
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ query.Query {
Updates: {{.Expected.Updates}}
Inserts: {{.Expected.Inserts}}
Fields: {{.Expected.Fields}}
Aliases: {{.Expected.Aliases}}
}
```
{{end}}
Expand Down
1 change: 1 addition & 0 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c862319

Please sign in to comment.