@@ -12,66 +12,99 @@ import (
1212)
1313
1414func main () {
15- format := flag .String ("format" , "txt " , "Output format (txt, csv, json, jsonl)" )
16- output := flag .String ("output" , "" , "Output file (if not specified, prints to stdout )" )
15+ format := flag .String ("format" , "" , "Output format (txt, csv, json, jsonl)" )
16+ output := flag .String ("output" , "" , "Output file (for single table export )" )
1717 workers := flag .Int ("workers" , getWorkerCount (), "Number of worker threads" )
18+ exportAll := flag .Bool ("all" , false , "Export all tables (creates a directory named after the input file)" )
1819 flag .Parse ()
1920
2021 args := flag .Args ()
2122 if len (args ) < 1 {
22- fmt .Printf ("Usage: sqlparser [-format=txt|csv|json] [-output=filename] [-workers=N] <sqlfile>\n " )
23- fmt .Printf (" -format: Output format (default: txt) \n " )
24- fmt .Printf (" -output: Output file (default: stdout )\n " )
23+ fmt .Printf ("Usage: sqlparser [-format=txt|csv|json] [-output=filename] [-workers=N] [-all] <sqlfile>\n " )
24+ fmt .Printf (" -format: Output format (txt, csv, json, jsonl). If specified without -output, creates files in a directory \n " )
25+ fmt .Printf (" -output: Output file (optional, defaults to directory output if format is specified )\n " )
2526 fmt .Printf (" -workers: Number of worker threads (default: %d)\n " , getWorkerCount ())
27+ fmt .Printf (" -all: Export all tables into separate files (default: false)\n " )
2628 os .Exit (1 )
2729 }
2830
2931 filename := args [0 ]
3032
31- // Scan for tables and prompt for selection
33+ // Scan for tables
3234 tables , err := parser .ScanTables (filename )
3335 if err != nil {
3436 fmt .Printf ("Error scanning tables: %v\n " , err )
3537 os .Exit (1 )
3638 }
3739
38- selectedTable , err := parser .PromptTableSelection (tables )
39- if err != nil {
40- fmt .Printf ("Error selecting table: %v\n " , err )
41- os .Exit (1 )
40+ var selectedTables []* parser.TableInfo
41+ if * exportAll {
42+ selectedTables = make ([]* parser.TableInfo , len (tables ))
43+ for i := range tables {
44+ selectedTables [i ] = & tables [i ]
45+ }
46+ fmt .Printf ("\n Exporting all %d tables\n " , len (tables ))
47+ } else {
48+ selectedTable , err := parser .PromptTableSelection (tables )
49+ if err != nil {
50+ // Check if all tables were selected from the menu
51+ if allSelected , ok := err .(* parser.AllTablesSelected ); ok {
52+ selectedTables = make ([]* parser.TableInfo , len (allSelected .Tables ))
53+ for i := range allSelected .Tables {
54+ selectedTables [i ] = & allSelected .Tables [i ]
55+ }
56+ fmt .Printf ("\n Exporting all %d tables\n " , len (tables ))
57+ } else {
58+ fmt .Printf ("Error selecting table: %v\n " , err )
59+ os .Exit (1 )
60+ }
61+ } else {
62+ selectedTables = []* parser.TableInfo {selectedTable }
63+ fmt .Printf ("\n Selected table: %s\n " , selectedTable .Name )
64+ }
4265 }
4366
44- fmt . Printf ( " \n Selected table: %s \n " , selectedTable . Name )
45-
67+ // If format is specified but no output, use directory output by default
68+ useDirectoryOutput := * exportAll || len ( selectedTables ) > 1 || ( * format != "" && * output == "" )
4669 outputFormat := models .OutputFormat (* format )
70+ if * format == "" {
71+ outputFormat = models .FormatText // default to text if no format specified
72+ }
4773
48- // Create output file or use stdout
49- var out * os.File
50- if * output != "" {
51- out , err = os .Create (* output )
52- if err != nil {
53- fmt .Printf ("Error creating output file: %v\n " , err )
54- os .Exit (1 )
55- }
56- defer out .Close ()
74+ var w writer.Writer
75+ if useDirectoryOutput {
76+ w , err = writer .CreateMultiWriter (outputFormat , filename )
5777 } else {
58- out = os .Stdout
78+ if * output == "" {
79+ w , err = writer .CreateWriter (outputFormat , os .Stdout )
80+ } else {
81+ out , err := os .Create (* output )
82+ if err != nil {
83+ fmt .Printf ("Error creating output file: %v\n " , err )
84+ os .Exit (1 )
85+ }
86+ defer out .Close ()
87+ w , err = writer .CreateWriter (outputFormat , out )
88+ if err != nil {
89+ fmt .Printf ("Error creating writer: %v\n " , err )
90+ os .Exit (1 )
91+ }
92+ }
5993 }
6094
61- // Initialize the output writer based on format
62- w , err := writer .CreateWriter (outputFormat , out )
6395 if err != nil {
6496 fmt .Printf ("Error creating writer: %v\n " , err )
6597 os .Exit (1 )
6698 }
6799 defer w .Close ()
68100
69- // Process the file
101+ // Process each selected table
70102 fmt .Printf ("Processing with %d workers...\n " , * workers )
71- err = parser .ProcessSQLFileInBatches (filename , w , * workers , selectedTable )
72- if err != nil {
73- fmt .Printf ("Error processing SQL file: %v\n " , err )
74- os .Exit (1 )
103+ for _ , table := range selectedTables {
104+ if err := parser .ProcessSQLFileInBatches (filename , w , * workers , table ); err != nil {
105+ fmt .Printf ("Error processing table %s: %v\n " , table .Name , err )
106+ os .Exit (1 )
107+ }
75108 }
76109}
77110
0 commit comments