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

Selector SetColumns kept private #454

Open
pxue opened this issue Jun 21, 2018 · 9 comments
Open

Selector SetColumns kept private #454

pxue opened this issue Jun 21, 2018 · 9 comments

Comments

@pxue
Copy link
Contributor

pxue commented Jun 21, 2018

Hey @xiam hope everything's well!

I've a quick questions regarding setColumns. Was there a design reason in keeping this method private? I would like to have it exposed on the interface so I can manually do something similar to the paginator, ie someSelector.setColumns("count(1) _t")

Thanks!

@xiam
Copy link
Member

xiam commented Jul 7, 2018

Hellooo @pxue :)!

Is this because, in this case, you'd like the query to forget all the previously set columns and use new ones?

@pxue
Copy link
Contributor Author

pxue commented Jul 10, 2018

hey @xiam

yes that's exactly it.

for example i would like to modify some original query and replace the select with a count total.

   query := builder.Select("products.*").From("products").Where(...)
   ...

   countTotal := query.SetColumns(db.Raw("count(1) as _t"))...

Would be more than happy to submit a PR :)

@VojtechVitek
Copy link
Member

Can't you do

query := builder.
    Select("products.*", db.Raw("count(1) as _t")).
    From("products").
    Where(...)`

?

@pxue
Copy link
Contributor Author

pxue commented Oct 16, 2018

No because this still "selecting" the row plus some.

I'd like to be able to filter previously projected columns down to ones I want in this instanc
e, ie: the count.

@pkieltyka
Copy link
Member

pkieltyka commented Oct 16, 2018

in the past when we had to do queries outside of the builder, we just did it a raw query .. like when we did the recursive posts query in pressly api. The builder's construct won't be able to support every kind of query, its more of a helper for ~80% of queries. It should have facilitates around it though for interpolation and struct mapping (which it does)

btw, hi Pressly alum :)

@VojtechVitek
Copy link
Member

@pxue would this work for you?

   cond := db.Cond{} // common condition

   countTotal := builder.Select(db.Raw("count(1) as _t")).From("products").Where(cond)

   query := builder.Select("products.*").From("products").Where(cond)

Pressly alumni PR party :D

148660-cd1eeda32b0e1f48ca2dec60009b37fa-medium_jpg

@pxue
Copy link
Contributor Author

pxue commented Oct 17, 2018

hey @pkieltyka! :)

@VojtechVitek yes i think for now that'll be the way, we have some complex queries and it's a bit cumbersome to repeat the entire query for a count. but it's the easiest way forward without introducing changes to upper.

feel free to close up the issue. thanks guys for the recommendations!

@VojtechVitek
Copy link
Member

@pxue btw, I think you could use Paginator too.

https://godoc.org/upper.io/db.v3/lib/sqlbuilder#Paginator

  q := builder.Select("*").From("products").Where(db.Cond{"price >=", 100}).Paginate(50)

  count, err := q.TotalEntries()

@VojtechVitek
Copy link
Member

@xiam Maybe sqlbuilder#Selector should have .Count() method, similarly to upper/db#Result? So you could get the count even without the Paginator?

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

No branches or pull requests

4 participants