CATCHV Blog

golang sqlc query log print 본문

Dev/Go

golang sqlc query log print

catchv 2024. 3. 8. 11:21
반응형

golang에서 sqlc를 사용하는데 쿼리를 log에 출력하는 옵션이 없어서 찾아 보니

 

driver의 hook을 설정해서 쿼리의 실행전과 실행 후에 쿼리를 찍는 소소가 있었다.

 

그런데 sqlc는 쿼리를 prepared 형식으로 던지기 때문에 

 

parameter와 쿼리가 분리되어서 출력 되는 문제가 있었다. 

 

그래서 gorm의 쿼리를 출력하는 소스를 찾아 보니 logger 폴더에 parameter를 찾아서 찍어 주는 소스가 존재 했다.

 

두개를 합치면 다음과 같은 소스로 원하는 쿼리를 출력해 볼 수 있다.

 

1. database driver hook

https://github.com/qustavo/sqlhooks

 

GitHub - qustavo/sqlhooks: Attach hooks to any database/sql driver

Attach hooks to any database/sql driver. Contribute to qustavo/sqlhooks development by creating an account on GitHub.

github.com

 

2. gorm logger 

https://github.com/go-gorm/gorm/blob/master/logger/logger.go

 

 

package main

import (
	"context"
	"fmt"
	"time"

	"gorm.io/gorm/logger"
	_ "github.com/qustavo/sqlhooks/v2"
	_ "github.com/go-sql-driver/mysql"	
)

// Hooks satisfies the sqlhook.Hooks interface
type Hooks struct{}

// Before hook will print the query with it's args and return the context with the timestamp
func (h *Hooks) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {

	return context.WithValue(ctx, "begin", time.Now()), nil
}

// After hook will get the timestamp registered on the Before hook and print the elapsed time
func (h *Hooks) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	fmt.Printf("[QUERY] [%s] %s\n",
		time.Since(ctx.Value("begin").(time.Time)),
		logger.ExplainSQL(query, nil, `'`, args...),
	)
	return ctx, nil
}

func main() {
	// DB를 접속하는 경우
	// ctx := context.Background()

	// // First, register the wrapper
	// sql.Register("mysqlWithHooks", sqlhooks.Wrap(&mysql.MySQLDriver{}, &Hooks{}))
	// db, err := sql.Open("mysqlWithHooks", dsn)

	sql := "SELECT * FROM person WHERE id = ?"
	vars := []interface{}{1}

	explain := logger.ExplainSQL(sql, nil, `'`, vars...)

	fmt.Println(explain)
}

 

결과

SELECT * FROM person WHERE id = ? [1]
SELECT * FROM person WHERE id = 1

 

 

 

반응형

'Dev > Go' 카테고리의 다른 글

google Project IDX  (0) 2024.04.02
go 1.22 for loop 변경  (0) 2024.02.23
dotnet으로 Golang gin 로컬 개발(localhost) 인증서 만들기  (0) 2024.01.11
Golang cross-compile windows, mac(darwin), linux  (0) 2023.12.18
go - package  (1) 2023.11.24
Comments