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

Directly selecting values from struct #104

Closed
ssttevee opened this issue Jul 17, 2019 · 1 comment
Closed

Directly selecting values from struct #104

ssttevee opened this issue Jul 17, 2019 · 1 comment

Comments

@ssttevee
Copy link

Given the following struct

type Person struct {
	Name string `db:"name"`
	Age int `db:"age"`
}

p := Person{
	Name: "Jon",
	Age: 30,
}

There doesn't seem to be a quick way to directly select all of the data from a struct:

SELECT 'Jon' AS "name", 30 AS "age"

Similar to the way it is interpreted in Dataset.Insert():

goqu.From("table").Insert(&p)
// INSERT INTO "table" ("name", "age") VALUES ("Jon", 30)

The current behaviour is:

goqu.From().Select(&p) // SELECT "name", "age"

and

goqu.From(&p) // SELECT * FROM "name", "age"

The best way I can come up with currently, is something like this:

goqu.From().Select(
	goqu.L("?", p.Name).As("name"),
	goqu.L("?", p.Age).As("age"),
) // SELECT 'Jon' AS "name", 30 AS "age"

I'd like to propose a way to more easily build such a query.

I think there should be a new function, perhaps goqu.V, for explicitly stating that I want to use the value as a literal.

goqu.From().Select(
	goqu.V("Jon").As("name"),
	goqu.V(30).As("age"),
) // SELECT 'Jon' AS "name", 30 AS "age"

goqu.From().Select(goqu.V(p)) // SELECT 'Jon' AS "name", 30 AS "age"

goqu.From().Select(goqu.V(p).As("person")) // SELECT ('Jon', 30) AS "person"

I'm not sure about the interactions with other expressions, or other databases, esp. the tuple syntax. It works in postgres though.

doug-martin added a commit that referenced this issue Jul 26, 2019
+* [ADDED] `goqu.V` so values can be used on the LHS of expressions #104
@doug-martin
Copy link
Owner

I added this in v8.0.1 #113. It just an alias for goqu.L("?", val). Ill close this once I release the new version.

@doug-martin doug-martin mentioned this issue Jul 26, 2019
doug-martin added a commit that referenced this issue Jul 26, 2019
+* [ADDED] `goqu.V` so values can be used on the LHS of expressions #104
doug-martin added a commit that referenced this issue Jul 26, 2019
* [ADDED] `goqu.V` so values can be used on the LHS of expressions #104
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants