lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.01k stars 910 forks source link

pq: cannot insert multiple commands into a prepared statement #928

Closed BrendanMartin closed 4 years ago

BrendanMartin commented 4 years ago

I'm attempting to run two SELECT statements in one Query with arguments but I'm getting the error pq: cannot insert multiple commands into a prepared statement.

A very contrived example of what I'm trying to do is this:

stmt := `
    SELECT id, name 
    FROM user u
    WHERE u.email = $1
    ;

    SELECT id, employee_number
    FROM employee e
    WHERE e.email = $1
        ;
`

rows, err := db.Query(stmt, "bob@example.com")

This creates the error above.

An example of how this should work is in the MultipleResultSets example under Query: https://golang.org/pkg/database/sql/#DB.Query

vaibhav2ghadge commented 4 years ago

but how pq handle this case it hadled by sql package it self

maddyblue commented 4 years ago

This is a limitation of postgres, not pq. You can only have a single query if you are using parameters.

agorina commented 3 years ago

@mjibson : Just curious to revisit this. I see that this issue was solved in the JDBC driver: https://knowledgebase.progress.com/articles/Article/PostgreSQL-JDBC-driver-throws-cannot-insert-multiple-commands-into-a-prepared-statement-while-executing-multiple-prepared-statements

Is it possible to implement it in this module?

JonCognioDigital commented 3 years ago

I have just run into this issue. Apparently this has been possible in postgres for quite a while but a simple query like this fails with the error above.

const sql: SQLStatement = SQL`

    SELECT COUNT(*) FROM users;

    SELECT * FROM users
        LIMIT ${resultsPerPage}
        OFFSET ${resultsPerPage * (page-1)}

`
JonCognioDigital commented 3 years ago

Actually, I've just realised that if you omit the pagination from my example then it works fine. Returns an array of result sets so it can definitely handle multiple queries and multiple result sets, just not when using parameters. Weird.