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

There is an error when implementing the type DBTX interface (SQLC code generator) with the pgsql driver pinpoint plugin #11

Closed
zikki0190 opened this issue Jan 7, 2022 · 6 comments
Labels
bug Something isn't working
Milestone

Comments

@zikki0190
Copy link

Code overview :
// load DB
import (
"database/sql"
"fmt"
"net/url"
"time"
_ "github.com/pinpoint-apm/pinpoint-go-agent/plugin/pgsql" // use wrapped postgres driver
)
func (conf DBConf) loadDB() *sql.DB {
newDB, err := sql.Open("pq-pinpoint", connectionString)
return newDB
}

`// Code generated by sqlc. DO NOT EDIT.

package postgres

import (
"context"
"database/sql"
)

type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

func New(db DBTX) *Queries {
return &Queries{db: db}
}

type Queries struct {
db DBTX
}

func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

func (q *Queries) QueryGetTbGuestById(ctx context.Context, id string) (TbTransactionDelivery, error) {
row := q.db.QueryRowContext(ctx, queryGetTbGuestById, id)
var i TbTransactionDelivery
err := row.Scan(
&i.Id,
&i.Name,
)
return i, err
}
`

Error :
interface conversion: *pq.conn is not driver.NamedValueChecker: missing method CheckNamedValue"

@baskoroaks
Copy link

i got same issue
the point is, if query using parameter arguments got failed and return error missing method CheckNamedValue
but, if using query plain without parameter is succeed

dwkang added a commit that referenced this issue Aug 19, 2022
dwkang added a commit that referenced this issue Aug 19, 2022
dwkang added a commit that referenced this issue Aug 19, 2022
dwkang added a commit that referenced this issue Aug 19, 2022
dwkang added a commit that referenced this issue Aug 22, 2022
dwkang added a commit that referenced this issue Aug 22, 2022
@baskoroaks
Copy link

how to query using func QueryContext with argument?
i used db.QueryContext(ctx, myQuery, args1, args2)
i still get error *pq.conn is not driver.NamedValueChecker: missing method CheckNamedValue

@dwkang
Copy link
Contributor

dwkang commented Aug 23, 2022

The code that solved the problem was committed to the main branch, and the problem you mentioned do not occur when I test. I think you tested it with the previously released version. Check if you tested it with the main branch.

Or maybe it's because of the go runtime version, but I tested it at 1.18.4. What version did you use?

@baskoroaks
Copy link

baskoroaks commented Aug 23, 2022

The code that solved the problem was committed to the main branch, and the problem you mentioned do not occur when I test. I think you tested it with the previously released version. Check if you tested it with the main branch.

Or maybe it's because of the go runtime version, but I tested it at 1.18.4. What version did you use?

I used go 19 version, and i'm still strugling with this errors :D

it's my code, if you can correct my code

`
package main

import (
"context"
"database/sql"
"fmt"
"log"
"net/http"

pinpoint "github.com/pinpoint-apm/pinpoint-go-agent"
phttp "github.com/pinpoint-apm/pinpoint-go-agent/plugin/http"
"github.com/pkg/errors"

_ "github.com/pinpoint-apm/pinpoint-go-agent/plugin/pgsql" // wrap driver psql.

)

const queryG = "select
smc_member,
smc_no_juklak,
sum(smc_star) as star
from star_member_cancel smc
join star_master_expired sme on smc.smc_no_juklak = sme.sme_no_juklak and (now() at time zone 'Asia/Jakarta')::timestamp <= (sme.sme_expired_date at time zone 'Asia/Jakarta')::timestamp
where smc_no_juklak = $1
group by smc_member, smc_no_juklak"

func main() {
opts := []pinpoint.ConfigOption{
pinpoint.WithAppName("test-pinpoint"),
pinpoint.WithAgentId("test-pinpoint-agent"),
pinpoint.WithCollectorHost("pinpoint-host"),
}

cfg, _ := pinpoint.NewConfig(opts...)
agent, err := pinpoint.NewAgent(cfg)
if err != nil {
	log.Fatalf("pinpoint agent start fail=%v", err)
}

http.HandleFunc(phttp.WrapHandleFunc(agent, "query", "/", query))

http.ListenAndServe(":9000", nil)
agent.Shutdown()

}

func query(w http.ResponseWriter, r *http.Request) {
db, err := sql.Open("pq-pinpoint", "postgresql://user:password@host:port/db?sslmode=disable")
if err != nil {
log.Fatalf("error connecting DB =%v", err)
}

defer db.Close()

tracer := pinpoint.TracerFromRequestContext(r)
ctx := pinpoint.NewContext(context.Background(), tracer)

_, err = db.Prepare("select * from star_member_collect where smc_no_juklak=$1")
if err != nil {
	log.Fatalf("err stmt: %v", err)
}

rows, err := db.QueryContext(ctx, queryG, "asdasds")
if err != nil {
	log.Fatalf("sql error: %v", err)
}

var count int

for rows.Next() {
	err = rows.Scan(&count)
	if err != nil && !errors.Is(err, sql.ErrNoRows) {
		log.Fatalf("sql error: %v", err)
	}
}

defer rows.Close()

fmt.Println("number of entries in pg_catalog.pg_tables", count)

}
`

and my pinpoint-go-agent version is v0.5.1

@dwkang
Copy link
Contributor

dwkang commented Aug 23, 2022

There is no problem when I test the go 19 version. Please refer to the link below and test it with the main branch.

https://stackoverflow.com/questions/53682247/how-to-point-go-module-dependency-in-go-mod-to-a-latest-commit-in-a-repo

@baskoroaks
Copy link

There is no problem when I test the go 19 version. Please refer to the link below and test it with the main branch.

https://stackoverflow.com/questions/53682247/how-to-point-go-module-dependency-in-go-mod-to-a-latest-commit-in-a-repo

it's solved, thank you for yout advice

@dwkang dwkang closed this as completed Oct 14, 2022
@dwkang dwkang added this to the v1.0.0 milestone Dec 26, 2022
@dwkang dwkang added the bug Something isn't working label Dec 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants