mirror of
https://github.com/glebarez/go-sqlite.git
synced 2025-10-05 15:56:52 +08:00
optional logging of SQL statments
This commit is contained in:

committed by
glebarez

parent
7b21af2c7f
commit
fbd12bd006
24
sqlite.go
24
sqlite.go
@@ -12,6 +12,7 @@ import (
|
|||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -46,6 +47,8 @@ var (
|
|||||||
_ error = (*Error)(nil)
|
_ error = (*Error)(nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var LogSqlStatements bool
|
||||||
|
|
||||||
const (
|
const (
|
||||||
driverName = "sqlite"
|
driverName = "sqlite"
|
||||||
ptrSize = unsafe.Sizeof(uintptr(0))
|
ptrSize = unsafe.Sizeof(uintptr(0))
|
||||||
@@ -781,9 +784,27 @@ func newConn(dsn string) (*conn, error) {
|
|||||||
return nil, err
|
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
|
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 {
|
func applyQueryParams(c *conn, query string) error {
|
||||||
q, err := url.ParseQuery(query)
|
q, err := url.ParseQuery(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1387,5 +1408,8 @@ func newDriver() *Driver { return &Driver{} }
|
|||||||
//
|
//
|
||||||
// The returned connection is only used by one goroutine at a time.
|
// The returned connection is only used by one goroutine at a time.
|
||||||
func (d *Driver) Open(name string) (driver.Conn, error) {
|
func (d *Driver) Open(name string) (driver.Conn, error) {
|
||||||
|
if LogSqlStatements {
|
||||||
|
log.Println("new connection")
|
||||||
|
}
|
||||||
return newConn(name)
|
return newConn(name)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user