| 1 |
//go:build cgo && sqlite_trace |
| 2 |
|
| 3 |
package litestream |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/mattn/go-sqlite3" |
| 10 |
) |
| 11 |
|
| 12 |
func traceCallback(info sqlite3.TraceInfo) int { |
| 13 |
switch info.EventCode { |
| 14 |
case sqlite3.TraceStmt: |
| 15 |
fmt.Println(info.ExpandedSQL) |
| 16 |
fmt.Println("-------------------------------------------------------------") |
| 17 |
default: |
| 18 |
fmt.Print("exectime: ", time.Duration(info.RunTimeNanosec).String()) |
| 19 |
if info.AutoCommit { |
| 20 |
fmt.Print(" - autocommit") |
| 21 |
} else { |
| 22 |
fmt.Print(" - transaction") |
| 23 |
} |
| 24 |
if info.DBError.Code != 0 || info.DBError.ExtendedCode != 0 { |
| 25 |
fmt.Printf(" - error %#v", info.DBError) |
| 26 |
} |
| 27 |
fmt.Println() |
| 28 |
} |
| 29 |
return 0 |
| 30 |
} |
| 31 |
|
| 32 |
func DoSetTrace(conn *sqlite3.SQLiteConn) error { |
| 33 |
return conn.SetTrace(&sqlite3.TraceConfig{ |
| 34 |
Callback: traceCallback, |
| 35 |
EventMask: sqlite3.TraceStmt | sqlite3.TraceProfile | sqlite3.TraceRow | sqlite3.TraceClose, |
| 36 |
WantExpandedSQL: true, |
| 37 |
}) |
| 38 |
} |