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

How to call a function with array params #118

Closed
kokteyldev opened this issue Mar 14, 2016 · 6 comments
Closed

How to call a function with array params #118

kokteyldev opened this issue Mar 14, 2016 · 6 comments
Assignees

Comments

@kokteyldev
Copy link

I have a function for batch insert, which has array parameters like so:

insert_batch(_dates date[], _values integer[])

I am trying to call it like the following:

SELECT * FROM insert_batch($1, $2)

I read through the souce code however I couldn't find a way I can do this. Is it possible to call raw select sql with bind parameters.

@vmihailenco
Copy link
Member

With pg.v4 you can try something like:

ints := []int{1, 2, 3}
db.Exec(`SELECT * FROM insert_batch(?)`, types.Array(ints))

But array of time types.Array([]time.Time{...}) is not supported...

@vmihailenco
Copy link
Member

Overall array support is very limited right now - https://github.com/go-pg/pg/blob/v4/types/array.go#L16-L28

@drt24
Copy link

drt24 commented Mar 21, 2016

Actually the example needs curly brackets rather than round brackets:

ints := []int{1, 2, 3}
db.Exec("SELECT * FROM insert_batch(?)", types.Array{ints})

Otherwise you get:

cannot convert ints (type []int) to type types.Array

@vmihailenco
Copy link
Member

Eventually I will make it work correctly, but first I want to stabilize v4 branch and release ORM to public...

@vmihailenco
Copy link
Member

FYI #138 added support for []time.Time array, but API changed slightly:

ints := []int{1, 2, 3}
times := []time.TIme{time.Now()}
db.Exec(`SELECT * FROM insert_batch(?, ?)`, pg.Array(&ints), pg.Array(&times))

There is still some work to be done, but it should be usable.

@vmihailenco
Copy link
Member

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

3 participants