Skip to content

Commit

Permalink
Updated alias generation to maintain an alias map separately from the…
Browse files Browse the repository at this point in the history
… list of queried fields.
  • Loading branch information
coreybutler committed Feb 14, 2021
1 parent 077d207 commit 3adc75a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
26 changes: 24 additions & 2 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,21 +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 FROM 'b'`)
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: [z y]
Fields: [a b c]
Aliases: map[a:z b:y]
}
```

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand All @@ -454,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
7 changes: 5 additions & 2 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ func (p *parser) doParse() (query.Query, error) {
if !isIdentifier(alias) {
return p.query, fmt.Errorf("at SELECT: expected field alias for \"" + identifier + " as\" to SELECT")
}
p.query.Fields[len(p.query.Fields)-1] = alias
if p.query.Aliases == nil {
p.query.Aliases = make(map[string]string)
}
p.query.Aliases[identifier] = alias
p.pop()
maybeFrom = p.peek()
}
Expand Down Expand Up @@ -380,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
8 changes: 6 additions & 2 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ func TestSQL(t *testing.T) {
},
{
Name: "SELECT with alias works",
SQL: "SELECT a as z, b as y FROM 'b'",
SQL: "SELECT a as z, b as y, c FROM 'b'",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"z", "y"},
Fields: []string{"a", "b", "c"},
Aliases: map[string]string{
"a": "z",
"b": "y",
},
},
Err: nil,
},
Expand Down

0 comments on commit 3adc75a

Please sign in to comment.