Custom query with go-pg into []String slice - postgresql

I'm using https://godoc.org/github.com/go-pg/pg a bunch of other places in the code so I'm hoping I don't have to switch to another client.
I can't get the ORM to write this query (below) correctly, so I just want to pass it thru as a custom string. But I can't figure out how to get the results into my []string slice.
tokens := []string{}
qry := `SELECT p.token
FROM pntokens p
join
(VALUES ('123'), ('456'), ('789')) AS t (userid)
on p.userid = t.userid ;`
I've tried:
err := db.Model(&Pntoken{}, qry).Select(&tokens)
err := db.Query([]string{}, qry, nil).Select(&tokens)
_, err := db.Exec(qry)
res, err := db.Model((*Pntoken)(nil)).Exec(qry)
But cannot get the tool out of my way enough to just get some simple results into my slice.
All tips appreciated!

Use like this for lib https://github.com/go-pg/pg:
tokens := []string
qry := `SELECT p.token FROM pntokens p
join (VALUES ('123'), ('456'), ('789')) AS t (userid)
on p.userid = t.userid ;`
_, err := pc.DB.Query(&tokens,"qry",nil)
if err != nil {
return tokens, err
}

Related

How can count number of rows in query?

I have a question how can i count number of rows in query. as below example
rows, err := repo.DBConn.Query("SELECT init_id, email, address, phone, name, zipcode , about,backgroundimg_url,icon_url FROM public.initiator where init_id in (select init_id from public.events where request_id=$1)",request_id)
If you don't care about the actual contents of the rows use a select-count query.
var count int
row := repo.DBConn.QueryRow("SELECT COUNT(*) FROM public.initiator where init_id in (select init_id from public.events where request_id=$1)",request_id)
if err := row.Scan(&count); err != nil {
return err
}
If you care about the contents then scan the rows into a slice and then get its length using the len function.
rows, err := repo.DBConn.Query(`SELECT init_id, email, address, phone, name, zipcode , about,backgroundimg_url,icon_url FROM public.initiator where init_id in (select init_id from public.events where request_id=$1)",request_id`)
if err != nil {
return err
}
defer rows.Close()
var inits []*Initiator
for rows.Next() {
ini := new(Initiator)
if err := rows.Scan(&ini.InitID, &ini.Email, ...); err != nil {
return err
}
inits = append(inits, ini)
}
if err := rows.Err(); err != nil {
return err
}
count := len(inits)
If you're doing pagination and all you want is just, say a 20 rows per request, but you also want the total number of rows that satisfies the WHERE clause, then you need to execute both queries, the one to retrieve the 20 rows and the one to count the total number of rows. You can combine the above examples to do that.

I'm looking for a way to pass in either a slice of int or a comma delimited string into a database/sql 'in' query [duplicate]

I am trying to execute the following query against the PostgreSQL database in Go using pq driver:
SELECT COUNT(id)
FROM tags
WHERE id IN (1, 2, 3)
where 1, 2, 3 is passed at a slice tags := []string{"1", "2", "3"}.
I have tried many different things like:
s := "(" + strings.Join(tags, ",") + ")"
if err := Db.QueryRow(`
SELECT COUNT(id)
FROM tags
WHERE id IN $1`, s,
).Scan(&num); err != nil {
log.Println(err)
}
which results in pq: syntax error at or near "$1". I also tried
if err := Db.QueryRow(`
SELECT COUNT(id)
FROM tags
WHERE id IN ($1)`, strings.Join(stringTagIds, ","),
).Scan(&num); err != nil {
log.Println(err)
}
which also fails with pq: invalid input syntax for integer: "1,2,3"
I also tried passing a slice of integers/strings directly and got sql: converting Exec argument #0's type: unsupported type []string, a slice.
So how can I execute this query in Go?
Pre-building the SQL query (preventing SQL injection)
If you're generating an SQL string with a param placeholder for each of the values, it's easier to just generate the final SQL right away.
Note that since values are strings, there's place for SQL injection attack, so we first test if all the string values are indeed numbers, and we only proceed if so:
tags := []string{"1", "2", "3"}
buf := bytes.NewBufferString("SELECT COUNT(id) FROM tags WHERE id IN(")
for i, v := range tags {
if i > 0 {
buf.WriteString(",")
}
if _, err := strconv.Atoi(v); err != nil {
panic("Not number!")
}
buf.WriteString(v)
}
buf.WriteString(")")
Executing it:
num := 0
if err := Db.QueryRow(buf.String()).Scan(&num); err != nil {
log.Println(err)
}
Using ANY
You can also use Postgresql's ANY, whose syntax is as follows:
expression operator ANY (array expression)
Using that, our query may look like this:
SELECT COUNT(id) FROM tags WHERE id = ANY('{1,2,3}'::int[])
In this case you can declare the text form of the array as a parameter:
SELECT COUNT(id) FROM tags WHERE id = ANY($1::int[])
Which can simply be built like this:
tags := []string{"1", "2", "3"}
param := "{" + strings.Join(tags, ",") + "}"
Note that no check is required in this case as the array expression will not allow SQL injection (but rather will result in a query execution error).
So the full code:
tags := []string{"1", "2", "3"}
q := "SELECT COUNT(id) FROM tags WHERE id = ANY($1::int[])"
param := "{" + strings.Join(tags, ",") + "}"
num := 0
if err := Db.QueryRow(q, param).Scan(&num); err != nil {
log.Println(err)
}
This is not really a Golang issue, you are using a string to compare to integer (id) in your SQL request. That means, SQL receive:
SELECT COUNT(id)
FROM tags
WHERE id IN ("1, 2, 3")
instead of what you want to give it. You just need to convert your tags into integer and passe it to the query.
EDIT:
Since you are trying to pass multiple value to the query, then you should tell it:
params := make([]string, 0, len(tags))
for i := range tags {
params = append(params, fmt.Sprintf("$%d", i+1))
}
query := fmt.Sprintf("SELECT COUNT(id) FROM tags WHERE id IN (%s)", strings.Join(params, ", "))
This will end the query with a "($1, $2, $3...", then convert your tags as int:
values := make([]int, 0, len(tags))
for _, s := range tags {
val, err := strconv.Atoi(s)
if err != nil {
// Do whatever is required with the error
fmt.Println("Err : ", err)
} else {
values = append(values, val)
}
}
And finally, you can use it in the query:
Db.QueryRow(query, values...)
This should do it.
Extending #icza solution, you can use pq.Array instead of building the params yourself.
So using his example, the code can look like this:
tags := []string{"1", "2", "3"}
q := "SELECT COUNT(id) FROM tags WHERE id = ANY($1::int[])"
num := 0
if err := Db.QueryRow(q, pq.Array(tags)).Scan(&num); err != nil {
log.Println(err)
}

IN pq.Array fails to pass values

I have the following code:
package main
import (
"database/sql"
"fmt"
"github.com/lib/pq"
)
const (
DB_USER = "<username>"
DB_PASSWORD = "<password>"
DB_NAME = "<db>"
)
func main() {
dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
DB_USER, DB_PASSWORD, DB_NAME)
db, err := sql.Open("postgres", dbinfo)
checkErr(err)
defer db.Close()
fmt.Println("# Querying with blacklist in SQL")
rows, err := db.Query(`
SELECT * FROM(
SELECT x from (values ('A'), ('B'), ('C') ) s(x)
) As Res1 WHERE x NOT IN ('A');
`)
checkErr(err)
for rows.Next() {
var str string
err = rows.Scan(&str)
fmt.Println(str) // Prints B,C
}
fmt.Println("Querying with blacklist in Golang")
blacklist := []string{"A"}
q := `
SELECT * FROM(
SELECT x from (values ('A'), ('B'), ('C') ) s(x)
) As Res1 WHERE x NOT IN ($1);
`
rows, err = db.Query(q, pq.Array(blacklist))
checkErr(err)
for rows.Next() {
var str string
err = rows.Scan(&str)
fmt.Println(str) // Prints A, B, C
}
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
where I pass a pq.Array as a parameter to a Golang Postgres format string $1. However the parameter fails to get passed. When I expect an output of B,C, the program is printing A,B,C.
# Querying with blacklist in SQL
B
C
Querying with blacklist in Golang
A
B
C
Postgres's IN does not take an array like for example ANY or ALL which is where you can use pq.Array.
Instead of using x NOT IN($1) you can use x <> ALL($1).
From ALL's docs.
The left-hand expression is evaluated and compared to each element of
the array using the given operator, which must yield a Boolean result.
The result of ALL is "true" if all comparisons yield true (including
the case where the array has zero elements). The result is "false" if
any false result is found.

Go and IN clause in Postgres

I am trying to execute the following query against the PostgreSQL database in Go using pq driver:
SELECT COUNT(id)
FROM tags
WHERE id IN (1, 2, 3)
where 1, 2, 3 is passed at a slice tags := []string{"1", "2", "3"}.
I have tried many different things like:
s := "(" + strings.Join(tags, ",") + ")"
if err := Db.QueryRow(`
SELECT COUNT(id)
FROM tags
WHERE id IN $1`, s,
).Scan(&num); err != nil {
log.Println(err)
}
which results in pq: syntax error at or near "$1". I also tried
if err := Db.QueryRow(`
SELECT COUNT(id)
FROM tags
WHERE id IN ($1)`, strings.Join(stringTagIds, ","),
).Scan(&num); err != nil {
log.Println(err)
}
which also fails with pq: invalid input syntax for integer: "1,2,3"
I also tried passing a slice of integers/strings directly and got sql: converting Exec argument #0's type: unsupported type []string, a slice.
So how can I execute this query in Go?
Pre-building the SQL query (preventing SQL injection)
If you're generating an SQL string with a param placeholder for each of the values, it's easier to just generate the final SQL right away.
Note that since values are strings, there's place for SQL injection attack, so we first test if all the string values are indeed numbers, and we only proceed if so:
tags := []string{"1", "2", "3"}
buf := bytes.NewBufferString("SELECT COUNT(id) FROM tags WHERE id IN(")
for i, v := range tags {
if i > 0 {
buf.WriteString(",")
}
if _, err := strconv.Atoi(v); err != nil {
panic("Not number!")
}
buf.WriteString(v)
}
buf.WriteString(")")
Executing it:
num := 0
if err := Db.QueryRow(buf.String()).Scan(&num); err != nil {
log.Println(err)
}
Using ANY
You can also use Postgresql's ANY, whose syntax is as follows:
expression operator ANY (array expression)
Using that, our query may look like this:
SELECT COUNT(id) FROM tags WHERE id = ANY('{1,2,3}'::int[])
In this case you can declare the text form of the array as a parameter:
SELECT COUNT(id) FROM tags WHERE id = ANY($1::int[])
Which can simply be built like this:
tags := []string{"1", "2", "3"}
param := "{" + strings.Join(tags, ",") + "}"
Note that no check is required in this case as the array expression will not allow SQL injection (but rather will result in a query execution error).
So the full code:
tags := []string{"1", "2", "3"}
q := "SELECT COUNT(id) FROM tags WHERE id = ANY($1::int[])"
param := "{" + strings.Join(tags, ",") + "}"
num := 0
if err := Db.QueryRow(q, param).Scan(&num); err != nil {
log.Println(err)
}
This is not really a Golang issue, you are using a string to compare to integer (id) in your SQL request. That means, SQL receive:
SELECT COUNT(id)
FROM tags
WHERE id IN ("1, 2, 3")
instead of what you want to give it. You just need to convert your tags into integer and passe it to the query.
EDIT:
Since you are trying to pass multiple value to the query, then you should tell it:
params := make([]string, 0, len(tags))
for i := range tags {
params = append(params, fmt.Sprintf("$%d", i+1))
}
query := fmt.Sprintf("SELECT COUNT(id) FROM tags WHERE id IN (%s)", strings.Join(params, ", "))
This will end the query with a "($1, $2, $3...", then convert your tags as int:
values := make([]int, 0, len(tags))
for _, s := range tags {
val, err := strconv.Atoi(s)
if err != nil {
// Do whatever is required with the error
fmt.Println("Err : ", err)
} else {
values = append(values, val)
}
}
And finally, you can use it in the query:
Db.QueryRow(query, values...)
This should do it.
Extending #icza solution, you can use pq.Array instead of building the params yourself.
So using his example, the code can look like this:
tags := []string{"1", "2", "3"}
q := "SELECT COUNT(id) FROM tags WHERE id = ANY($1::int[])"
num := 0
if err := Db.QueryRow(q, pq.Array(tags)).Scan(&num); err != nil {
log.Println(err)
}

Golang postgres Commit unknown command error?

Using postgres 9.3, go 1.6
I've been trying to use transactions with the go pq library.
// Good
txn, _ := db.Begin()
txn.Query("UPDATE t_name SET a = 1")
err := txn.Commit() // err is nil
// Bad
txn, _ := db.Begin()
txn.Query("UPDATE t_name SET a = $1", 1)
err := txn.Commit() // Gives me a "unexpected command tag Q" error
// although the data is committed
For some reason, when I execute a Query with parameters, I always get an unexpected command tag Q error from the Commit(). What is this error (what is Q?) and why am I getting it?
I believe this is where the error is created.
To start of i agree whit Dmitri from the comments, in this case you should probably use Exec.
However after receiving this same issue I started digging:
Query returns 2 arguments a Rows pointer and an error. What you always have to do with a Rows object is to close it when you are don with it:
// Fixed
txn, _ := db.Begin()
rows, _ := txn.Query("UPDATE t_name SET a = $1", 1)
//Read out rows
rows.Close() //<- This will solve the error
err := txn.Commit()
I was however unable to see any difference in the traffic to the database when using rows.Close() witch indicates to me that this might be a bug in pq.