optional logging of SQL statments

This commit is contained in:
Сахнов Глеб Андреевич
2021-12-07 16:53:44 +03:00
committed by glebarez
parent 7b21af2c7f
commit fbd12bd006

View File

@@ -12,6 +12,7 @@ import (
"database/sql/driver"
"fmt"
"io"
"log"
"math"
"net/url"
"reflect"
@@ -46,6 +47,8 @@ var (
_ error = (*Error)(nil)
)
var LogSqlStatements bool
const (
driverName = "sqlite"
ptrSize = unsafe.Sizeof(uintptr(0))
@@ -781,9 +784,27 @@ func newConn(dsn string) (*conn, error) {
return nil, err
}
// register statement logger
if LogSqlStatements {
if sqlite3.Xsqlite3_trace_v2(c.tls, db, sqlite3.SQLITE_TRACE_STMT, *(*uintptr)(unsafe.Pointer(&struct {
f func(*libc.TLS, uint32, uintptr, uintptr, uintptr) int32
}{stmtLog})), 0) != 0 {
log.Fatal("failed to register tracing handler")
}
}
return c, nil
}
func stmtLog(tls *libc.TLS, type1 uint32, cd uintptr, pd uintptr, xd uintptr) int32 { /* tclsqlite.c:661:12: */
if type1 == uint32(sqlite3.SQLITE_TRACE_STMT) {
// get SQL string
stmtEx := libc.GoString(sqlite3.Xsqlite3_expanded_sql(tls, pd))
log.Println(strings.Trim(stmtEx, "\r\n\t "))
}
return sqlite3.SQLITE_OK
}
func applyQueryParams(c *conn, query string) error {
q, err := url.ParseQuery(query)
if err != nil {
@@ -1387,5 +1408,8 @@ func newDriver() *Driver { return &Driver{} }
//
// The returned connection is only used by one goroutine at a time.
func (d *Driver) Open(name string) (driver.Conn, error) {
if LogSqlStatements {
log.Println("new connection")
}
return newConn(name)
}