Masterminds / squirrel

Fluent SQL generation for golang
Other
6.88k stars 465 forks source link

Select FROM stored procedure #306

Open lgopalab opened 2 years ago

lgopalab commented 2 years ago

Currently there is no way of executing queries like SELECT * FROM my_stored_proc(a,b,c) as FROM clause only accepts a single string as argument. https://github.com/Masterminds/squirrel/blob/75b018d6ca72f526bf83d5ae500841097c458bea/select.go#L274 Simply by making the function to accept variadic arguments, we can achieve a basic solution.

func (b SelectBuilder) FromProc(from string, args ...interface{}) SelectBuilder {
    from = from+"("+Placeholders(len(args))+")"
    return builder.Set(b, "From", newPart(from, args...)).(SelectBuilder)
}

This can be invoked as follows

Select("*").FromProc("my_stored_proc",args[:]...)

I'm not sure if this is an acceptable solution. But It does simplify some workflows.

lgopalab commented 2 years ago

Just saw this. https://github.com/Masterminds/squirrel/issues/294 These are similar.

dvd0101 commented 2 years ago

Yes! This is the only case, in our codebase, where we need to construct a piece of sql (the store procedure call) using fmt.Sprintf