@@ -12,7 +12,7 @@ import (
1212 "sqlparser/pkg/writer"
1313)
1414
15- func ProcessSQLFileInBatches (filename string , writer writer.Writer , numWorkers int ) error {
15+ func ProcessSQLFileInBatches (filename string , writer writer.Writer , numWorkers int , selectedTable * TableInfo ) error {
1616 startTime := time .Now ()
1717
1818 file , err := os .Open (filename )
@@ -34,6 +34,7 @@ func ProcessSQLFileInBatches(filename string, writer writer.Writer, numWorkers i
3434 }, numWorkers * 2 )
3535
3636 fmt .Printf ("Starting to process file: %s at %s\n " , filename , startTime .Format (time .RFC3339 ))
37+ fmt .Printf ("Processing table: %s (lines %d-%d)\n " , selectedTable .Name , selectedTable .LineFrom , selectedTable .LineTo )
3738
3839 // Start worker pool
3940 var wg sync.WaitGroup
@@ -70,27 +71,9 @@ func ProcessSQLFileInBatches(filename string, writer writer.Writer, numWorkers i
7071 continue
7172 }
7273
73- if result .rows != nil {
74+ if result .rows != nil && result . tableName == selectedTable . Name {
7475 // Handle new table
7576 if result .tableName != currentTableName {
76- if currentTableName != "" {
77- if len (currentBatch ) > 0 {
78- if err := writer .WriteRows (currentBatch ); err != nil {
79- fmt .Printf ("Error writing rows: %v\n " , err )
80- continue
81- }
82- batchCount ++
83- fmt .Printf ("Processed batch %d for table %s (%d rows)\n " , batchCount , currentTableName , len (currentBatch ))
84- currentBatch = nil // Help GC
85- }
86- if err := writer .WriteTableEnd (); err != nil {
87- fmt .Printf ("Error ending table: %v\n " , err )
88- continue
89- }
90- tableDuration := time .Since (tableStartTime )
91- fmt .Printf ("Finished processing table %s (total %d rows in %d batches) in %v\n " ,
92- currentTableName , rowCount , batchCount , tableDuration )
93- }
9477 currentTableName = result .tableName
9578 if err := writer .WriteTableStart (currentTableName ); err != nil {
9679 fmt .Printf ("Error starting table: %v\n " , err )
@@ -100,7 +83,7 @@ func ProcessSQLFileInBatches(filename string, writer writer.Writer, numWorkers i
10083 rowCount = 0
10184 batchCount = 0
10285 tableStartTime = time .Now ()
103- fmt .Printf ("Started processing new table: %s at %s\n " , currentTableName , tableStartTime .Format (time .RFC3339 ))
86+ fmt .Printf ("Started processing table: %s at %s\n " , currentTableName , tableStartTime .Format (time .RFC3339 ))
10487 }
10588
10689 // Process rows immediately
@@ -132,19 +115,37 @@ func ProcessSQLFileInBatches(filename string, writer writer.Writer, numWorkers i
132115
133116 // Read and send statements to workers
134117 var currentStatement strings.Builder
118+ lineNum := 0
119+ inSelectedTable := false
120+
135121 for scanner .Scan () {
122+ lineNum ++
136123 line := scanner .Text ()
137124
125+ // Skip empty lines and comments
138126 if strings .TrimSpace (line ) == "" || strings .HasPrefix (strings .TrimSpace (line ), "--" ) {
139127 continue
140128 }
141129
142- currentStatement .WriteString (line )
143- currentStatement .WriteString (" " )
130+ // Check if we're in the selected table's INSERT statements
131+ if strings .HasPrefix (strings .ToUpper (strings .TrimSpace (line )), "INSERT INTO" ) {
132+ tableParts := strings .Split (line , "`" )
133+ if len (tableParts ) >= 2 && tableParts [1 ] == selectedTable .Name {
134+ inSelectedTable = true
135+ } else {
136+ inSelectedTable = false
137+ }
138+ }
144139
145- if strings .HasSuffix (strings .TrimSpace (line ), ";" ) {
146- statementChan <- currentStatement .String ()
147- currentStatement .Reset ()
140+ // Only process INSERT statements for the selected table
141+ if inSelectedTable {
142+ currentStatement .WriteString (line )
143+ currentStatement .WriteString (" " )
144+
145+ if strings .HasSuffix (strings .TrimSpace (line ), ";" ) {
146+ statementChan <- currentStatement .String ()
147+ currentStatement .Reset ()
148+ }
148149 }
149150 }
150151
@@ -177,6 +178,7 @@ func ProcessSQLFileInBatches(filename string, writer writer.Writer, numWorkers i
177178 totalDuration := time .Since (startTime )
178179 fmt .Printf ("\n Processing Summary:\n " )
179180 fmt .Printf ("File: %s\n " , filename )
181+ fmt .Printf ("Table: %s\n " , selectedTable .Name )
180182 fmt .Printf ("Total Statements: %d\n " , totalStatements )
181183 fmt .Printf ("Total Duration: %v\n " , totalDuration )
182184 fmt .Printf ("Average Time per Statement: %v\n " , totalDuration / time .Duration (totalStatements ))
0 commit comments