Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ColumnAlias #29

Merged
merged 1 commit into from
Jan 2, 2019
Merged

added ColumnAlias #29

merged 1 commit into from
Jan 2, 2019

Conversation

derkan
Copy link
Contributor

@derkan derkan commented Dec 29, 2018

Hi Sam,

Given dummies table with column ATextnamed asa_text`:

type DummyAlias struct {
	ID              int            `db:"id,key,auto"`
	AText           string         `db:"new_name"`
}
singleDummy := DummyAlias{}
selectStmt := db.SelectFrom("dummies").
	Where("an_integer = ?", 13)
selectStmt.ColumnAlias("a_text", "new_name")
selectStmt.OrderBy("new_name")

In the result set column will be named as new_name.

OK. Why I need this?

When joinin tables, aliased table columns becomes weird. For following if ColumnAlias is not used, tag for AuthorName shoul be db:"a.name". And at result rows, column name will be also a.name.

type BookNamed struct {
	Book `db:",rel=b" json:""`   // Embeeding book struct
	AuthorName string `db:"author_name" json:"author_name"` 
}
q := db.SelectFrom("books as b").InnerJoin("authors","a",godb.Q("b.author_id=a.id"))
q.ColumnAlias("a.name", "author_name")

The result will be more convenient for SQL producing

  SELECT b.*, a.name as author_name
    FROM books as b
    JOIN authors as a ON b.author_id = a.id
ORDER BY author_name

Regards,
Erkan.

@derkan derkan mentioned this pull request Dec 29, 2018
@samonzeweb samonzeweb merged commit 76dc9ed into samonzeweb:master Jan 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants