11package main
22
33import (
4+ "C"
45 "flag"
56 "fmt"
67 "io"
78 "log"
89 "os"
910 "path/filepath"
1011 "time"
12+ "unsafe"
1113
1214 // Modules
1315 importer "github.com/djthorpe/go-sqlite/pkg/importer"
@@ -16,6 +18,10 @@ import (
1618 // Namespace Imports
1719 . "github.com/djthorpe/go-sqlite"
1820)
21+ import (
22+ "errors"
23+ "math"
24+ )
1925
2026var (
2127 flagOverwrite = flag .Bool ("overwrite" , false , "Overwrite existing tables" )
@@ -49,6 +55,8 @@ func main() {
4955 }
5056 defer db .Close ()
5157
58+ db .SetTraceHook (trace , sqlite3 .SQLITE_TRACE_PROFILE )
59+
5260 // Report on the database
5361 log .Println ("database:" , db .Filename (sqlite3 .DefaultSchema ))
5462
@@ -75,52 +83,44 @@ func main() {
7583 // Read files
7684 for _ , url := range flag .Args ()[1 :] {
7785 // Create an importer
78- importer , err := importer .NewImporter (config , url )
86+ importer , err := importer .NewImporter (config , url , writer )
7987 if err != nil {
8088 fmt .Fprintln (os .Stderr , importer .URL (), ": " , err )
8189 continue
8290 }
8391
84- // Create the decoder
92+ // Create the decoder, guess mimetype instead of supplying it
8593 decoder , err := importer .Decoder ("" )
8694 if err != nil {
8795 fmt .Fprintln (os .Stderr , importer .URL (), ": " , err )
8896 continue
8997 }
90- defer decoder .Close ()
9198
9299 // Reset the counter
93100 log .Println (" import:" , importer .URL ())
94101 log .Println (" ...decoder" , decoder )
95102
96- // Call Begin for writer to get writing function
97- fn , err := writer .Begin (importer .Name (), sqlite3 .DefaultSchema , []string {"continent" })
98- if err != nil {
99- fmt .Fprintln (os .Stderr , importer .URL (), ": " , err )
100- continue
101- }
102-
103103 // Read and write rows
104104 start , mark := time .Now (), time .Now ()
105105 for {
106- if err := importer .ReadWrite (decoder , fn ); err == io .EOF {
107- writer .End (true ) // commit
106+ if err := importer .ReadWrite (decoder ); err == io .EOF {
107+ break
108+ } else if errors .Is (err , io .EOF ) {
108109 break
109110 } else if err != nil {
110- writer .End (false ) // rollback
111111 fmt .Fprintln (os .Stderr , importer .URL (), ": " , err )
112112 break
113113 }
114114 if time .Since (mark ) > 5 * time .Second {
115- log .Printf (" ...written %d rows" , 0 )
115+ log .Printf (" ...written %d rows" , writer . Count () )
116116 mark = time .Now ()
117117 }
118118 }
119119
120120 // Report
121121 since := time .Since (start )
122- // ops_per_sec := math.Round(float64(writer.Count()) * 1000 / float64(since.Milliseconds()))
123- log .Printf (" ...written %d rows in %v (%.0f ops/s)" , 0 , since .Truncate (time .Millisecond ), 0 )
122+ ops_per_sec := math .Round (float64 (writer .Count ()) * 1000 / float64 (since .Milliseconds ()))
123+ log .Printf (" ...written %d rows in %v (%.0f ops/s)" , writer . Count () , since .Truncate (time .Millisecond ), ops_per_sec )
124124 }
125125}
126126
@@ -131,3 +131,17 @@ func logger(name string) *log.Logger {
131131 return log .New (os .Stderr , name , 0 )
132132 }
133133}
134+ func trace (t sqlite3.TraceType , a , b unsafe.Pointer ) int {
135+ switch t {
136+ case sqlite3 .SQLITE_TRACE_STMT :
137+ fmt .Println ("STMT => " , (* sqlite3 .Statement )(a ), C .GoString ((* C .char )(b )))
138+ case sqlite3 .SQLITE_TRACE_PROFILE :
139+ ms := time .Duration (time .Duration (* (* int64 )(b )) * time .Nanosecond )
140+ fmt .Println ("PROF => " , (* sqlite3 .Statement )(a ), ms )
141+ case sqlite3 .SQLITE_TRACE_ROW :
142+ fmt .Println ("ROW => " , (* sqlite3 .Statement )(a ))
143+ case sqlite3 .SQLITE_TRACE_CLOSE :
144+ fmt .Println ("CLSE => " , (* sqlite3 .Conn )(a ))
145+ }
146+ return 0
147+ }
0 commit comments