Alias serialize simplified.

This commit is contained in:
go-jet 2019-07-06 14:26:25 +02:00
parent fb5bf7dd40
commit cd1d033ffb
3 changed files with 17 additions and 22 deletions

View file

@ -3,8 +3,6 @@ package jet
type alias struct {
expression Expression
alias string
subQuery ExpressionTable
}
func newAlias(expression Expression, aliasName string) projection {
@ -15,25 +13,22 @@ func newAlias(expression Expression, aliasName string) projection {
}
func (a *alias) from(subQuery ExpressionTable) projection {
newAlias := *a
newAlias.subQuery = subQuery
return &newAlias
column := newColumn(a.alias, "", nil)
column.parent = &column
column.subQuery = subQuery
return &column
}
func (a *alias) serializeForProjection(statement statementType, out *queryData) error {
if a.subQuery != nil {
out.writeIdentifier(a.subQuery.Alias())
out.writeByte('.')
out.writeQuotedString(a.alias)
} else {
err := a.expression.serialize(statement, out)
if err != nil {
return err
}
}
out.writeString(`AS "` + a.alias + `"`)
out.writeString("AS ")
out.writeQuotedString(a.alias)
return nil
}

View file

@ -89,7 +89,7 @@ func (c columnImpl) serialize(statement statementType, out *queryData, options .
if c.subQuery != nil {
out.writeIdentifier(c.subQuery.Alias())
out.writeByte('.')
out.writeString(`"` + c.defaultAlias() + `"`)
out.writeQuotedString(c.defaultAlias())
} else {
if c.tableName != "" {
out.writeIdentifier(c.tableName)

View file

@ -51,13 +51,13 @@ Every column is aliased by default. Format is "`table_name`.`column_name`"
Above statement will produce following result set:
|_row_| city.city_id | city.city | address.address_id | address.address | customer.customer_id | customer.last_name |
|---| ------------ | ------------- | ------------------- | -------------------- | -------------------- | ------------------ |
|--- | ------------ | ------------- | ------------------- | -------------------- | -------------------- | ------------------ |
| _1_| 312 | "London" | 256 | "1497 Yuzhou Drive" | 252 | "Hoffman"|
| _2_| 312 | "London" | 517 | "548 Uruapan Street"| 512 | "Vines" |
| _2_| 312 | "London" | 517 | "548 Uruapan Street" | 512 | "Vines" |
| _3_| 589 | "York" | 502 | "1515 Korla Way" | 497 | "Sledge" |
Lets execute statement and scan result set to destination `dest`:
```
```sql
var dest []struct {
model.City
@ -190,7 +190,7 @@ City of `London` has two customers, which is the product of object reuse in `ROW
### Custom model files
**Programmes are not limited to just model files, any destination will work, as long as camel case of result set column
**Destinations are not limited to just model files, any destination will work, as long as camel case of result set column
is equal to `model type name`.`field name`.**
#### Named types