doug-martin / goqu

SQL builder and query library for golang
http://doug-martin.github.io/goqu/
MIT License
2.37k stars 207 forks source link

Using prepared statment results in error #385

Open pdeva opened 1 year ago

pdeva commented 1 year ago

Describe the bug This code works perfectly fine:

       var role string

    found, err := db.From(TableAccountUsers).Select("role").Where(goqu.Ex{
        "user_id":    userId,
        "account_id": accountId,
    }).ScanValContext(ctx, &role)

However, if i add .Prepared(true) to it, it results in an error:

       var role string

    found, err := db.From(TableAccountUsers)..Prepared(true).Select("role").Where(goqu.Ex{
        "user_id":    userId,
        "account_id": accountId,
    }).ScanValContext(ctx, &role)

error:

{"level":"error","module":"pgx","err":"ERROR: syntax error at or near \")\" (SQLSTATE 42601)","name":"stmtcache_16","pid":56,"sql":"SELECT \"role\" FROM \"account_users\" WHERE ((\"account_id\" = ?) AND (\"user_id\" = ?)) LIMIT ?","time":0.522084,"time":"2023-07-19T12:00:44-07:00","message":"Prepare"}
{"level":"error","module":"pgx","args":[{"1082":1,"1114":1,"1184":1,"16":1,"17":1,"20":1,"21":1,"23":1,"26":1,"28":1,"29":1,"700":1,"701":1},"a59a0a44-49d3-4f77-ad48-45d48afa4138","77fdd86f-ff4f-4ca7-a40e-9a51ad490167",1],"err":"ERROR: syntax error at or near \")\" (SQLSTATE 42601)","pid":56,"sql":"SELECT \"role\" FROM \"account_users\" WHERE ((\"account_id\" = ?) AND (\"user_id\" = ?)) LIMIT ?","time":0.531333,"time":"2023-07-19T12:00:44-07:00","message":"Query"}

To Reproduce

        var role string

    found, err := db.From(TableAccountUsers)..Prepared(true).Select("role").Where(goqu.Ex{
        "user_id":    userId,
        "account_id": accountId,
    }).ScanValContext(ctx, &role)

Expected behavior Should work without errors

Dialect:

Additional context Add any other context about the problem here.

ankitLiv commented 8 months ago

I am also facing this issue, for postgres placeholder should be like '$1' in prepared statement. but it is looking like it is generating '?'

ankitLiv commented 8 months ago

@doug-martin

ankitLiv commented 8 months ago

I am able to solve this problem. I haven't import _ "github.com/doug-martin/goqu/v9/dialect/postgres" this. which internally registers Postgres Dialect