forked from nao1215/filesql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
704 lines (613 loc) · 20.3 KB
/
builder.go
File metadata and controls
704 lines (613 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
package filesql
import (
"bytes"
"context"
"database/sql"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/xuri/excelize/v2"
"modernc.org/sqlite" // Direct SQLite driver usage
)
// DBBuilder configures and creates database connections from various data sources.
//
// Basic usage:
//
// builder := filesql.NewBuilder().
// AddPath("data.csv").
// AddPath("users.tsv")
//
// validatedBuilder, err := builder.Build(ctx)
// if err != nil {
// return err
// }
//
// db, err := validatedBuilder.Open(ctx)
// defer db.Close()
//
// Supports:
// - File paths (AddPath)
// - Embedded filesystems (AddFS)
// - io.Reader streams (AddReader)
// - Auto-save functionality (EnableAutoSave)
type DBBuilder struct {
// paths contains regular file paths
paths []string
// filesystems contains fs.FS instances
filesystems []fs.FS
// readers contains reader configurations
readers []readerInput
// collectedPaths contains all paths after Build validation
collectedPaths []string
// parsedTables contains tables parsed from streaming readers
parsedTables []*table
// autoSaveConfig contains auto-save settings
autoSaveConfig *autoSaveConfig
// defaultChunkSize is the default chunk size for reading large files (10MB)
defaultChunkSize int
// Internal processors for handling different responsibilities
validator *validator
fileProcessor *fileProcessor
streamProcessor *streamProcessor
}
// readerInput represents configuration for reading from io.Reader
type readerInput struct {
// reader is the data source
reader io.Reader
// tableName is the name of the table to create
tableName string
// fileType specifies the file format using domain/model types
fileType FileType
}
// NewBuilder creates a new database builder.
//
// Start here when you need:
// - Multiple data sources (files, streams, embedded FS)
// - Auto-save functionality
// - Custom chunk sizes for large files
// - More control than the simple Open() function
//
// Example:
//
// builder := filesql.NewBuilder().
// AddPath("data.csv").
// EnableAutoSave("./backup")
func NewBuilder() *DBBuilder {
chunkSize := DefaultChunkSize
return &DBBuilder{
paths: make([]string, 0),
filesystems: make([]fs.FS, 0),
readers: make([]readerInput, 0),
collectedPaths: make([]string, 0),
parsedTables: make([]*table, 0),
autoSaveConfig: nil, // Default: no auto-save
defaultChunkSize: chunkSize,
// Initialize internal processors
validator: newValidator(),
fileProcessor: newFileProcessor(chunkSize),
streamProcessor: newStreamProcessor(chunkSize),
}
}
// AddPath adds a file or directory to load.
//
// Examples:
// - Single file: AddPath("users.csv")
// - Compressed: AddPath("data.tsv.gz")
// - Directory: AddPath("/data/") // loads all CSV/TSV/LTSV files
//
// Returns self for chaining.
func (b *DBBuilder) AddPath(path string) *DBBuilder {
b.paths = append(b.paths, path)
return b
}
// AddPaths adds multiple files or directories at once.
//
// Example:
//
// builder.AddPaths("users.csv", "products.tsv", "/data/logs/")
//
// Returns self for chaining.
func (b *DBBuilder) AddPaths(paths ...string) *DBBuilder {
b.paths = append(b.paths, paths...)
return b
}
// AddReader adds data from an io.Reader (file, network stream, etc.).
//
// Parameters:
// - reader: Any io.Reader (file, bytes.Buffer, http.Response.Body, etc.)
// - tableName: Name for the SQL table (e.g., "users")
// - fileType: Data format (FileTypeCSV, FileTypeTSV, FileTypeLTSV, etc.)
//
// Example:
//
// resp, _ := http.Get("https://example.com/data.csv")
// builder.AddReader(resp.Body, "remote_data", FileTypeCSV)
//
// Returns self for chaining.
func (b *DBBuilder) AddReader(reader io.Reader, tableName string, fileType FileType) *DBBuilder {
b.readers = append(b.readers, readerInput{
reader: reader,
tableName: tableName,
fileType: fileType,
})
return b
}
// SetDefaultChunkSize sets chunk size (number of rows) for large file processing.
//
// Default: 1000 rows. Adjust based on available memory and processing needs.
//
// Example:
//
// builder.SetDefaultChunkSize(5000) // 5000 rows per chunk
//
// Returns self for chaining.
func (b *DBBuilder) SetDefaultChunkSize(size int) *DBBuilder {
if size > 0 {
b.defaultChunkSize = size
}
return b
}
// AddFS adds files from an embedded filesystem (go:embed).
//
// Automatically finds all CSV, TSV, and LTSV files in the filesystem.
//
// Example:
//
// //go:embed data/*.csv data/*.tsv
// var dataFS embed.FS
//
// builder.AddFS(dataFS)
//
// Returns self for chaining.
func (b *DBBuilder) AddFS(filesystem fs.FS) *DBBuilder {
b.filesystems = append(b.filesystems, filesystem)
return b
}
// EnableAutoSave automatically saves changes when the database is closed.
//
// Parameters:
// - outputDir: Where to save files
// - "" (empty): Overwrite original files
// - "./backup": Save to backup directory
//
// Example:
//
// builder.AddPath("data.csv").
// EnableAutoSave("") // Auto-save to original file on db.Close()
//
// Returns self for chaining.
func (b *DBBuilder) EnableAutoSave(outputDir string, options ...DumpOptions) *DBBuilder {
opts := NewDumpOptions()
if len(options) > 0 {
opts = options[0]
}
b.autoSaveConfig = &autoSaveConfig{
enabled: true,
timing: autoSaveOnClose, // Default to close-time saving
outputDir: outputDir,
options: opts,
}
return b
}
// EnableAutoSaveOnCommit automatically saves changes after each transaction commit.
//
// Use this for real-time persistence. Note: May impact performance.
//
// Example:
//
// builder.AddPath("data.csv").
// EnableAutoSaveOnCommit("./output") // Save after each commit
//
// Returns self for chaining.
func (b *DBBuilder) EnableAutoSaveOnCommit(outputDir string, options ...DumpOptions) *DBBuilder {
opts := NewDumpOptions()
if len(options) > 0 {
opts = options[0]
}
b.autoSaveConfig = &autoSaveConfig{
enabled: true,
timing: autoSaveOnCommit,
outputDir: outputDir,
options: opts,
}
return b
}
// DisableAutoSave disables automatic saving (default behavior).
// Returns the builder for method chaining.
func (b *DBBuilder) DisableAutoSave() *DBBuilder {
b.autoSaveConfig = nil
return b
}
// Build validates all configured inputs and prepares the builder for opening a database.
// This method must be called before Open(). It performs the following operations:
//
// 1. Validates that at least one input source is configured
// 2. Checks existence and format of all file paths
// 3. Processes embedded filesystems by converting files to streaming readers
// 4. Validates that all files have supported extensions
//
// After successful validation, the builder is ready to create database connections
// with Open(). The context is used for file operations and can be used for cancellation.
//
// Returns the same builder instance for method chaining, or an error if validation fails.
func (b *DBBuilder) Build(ctx context.Context) (*DBBuilder, error) {
// Validate that we have at least one input
if len(b.paths) == 0 && len(b.filesystems) == 0 && len(b.readers) == 0 {
return nil, errors.New("at least one path must be provided")
}
// Use validator to validate auto-save config
if err := b.validator.validateAutoSaveConfig(b.autoSaveConfig); err != nil {
return nil, err
}
// Use file processor to collect paths
collectedPaths, err := b.fileProcessor.collectFilesFromPaths(b.paths)
if err != nil {
return nil, err
}
b.collectedPaths = collectedPaths
// Use file processor to handle filesystems
fsReaders, err := b.fileProcessor.processFilesystemsToReaders(ctx, b.filesystems)
if err != nil {
return nil, err
}
b.readers = append(b.readers, fsReaders...)
// Use validator to validate reader inputs
for _, readerInput := range b.readers {
if err := b.validator.validateReader(readerInput.reader, readerInput.tableName, readerInput.fileType); err != nil {
return nil, err
}
}
// Use validator to validate final state
if err := b.validator.validateFinalState(b.collectedPaths, b.readers, b.paths); err != nil {
return nil, err
}
return b, nil
}
// Open creates and returns a database connection using the configured and validated inputs.
// This method can only be called after Build() has been successfully executed.
// It creates an in-memory SQLite database and loads all configured files as tables using streaming.
//
// Table names are derived from file names without extensions:
// - "users.csv" becomes table "users"
// - "data.tsv.gz" becomes table "data"
// - "user-data.csv" becomes table "user_data" (hyphens become underscores)
// - "my file.csv" becomes table "my_file" (spaces become underscores)
//
// Special characters in file names are automatically sanitized for SQL safety.
//
// The returned database connection supports the full SQLite3 SQL syntax.
// Auto-save functionality is supported for both file paths and reader inputs.
// The caller is responsible for closing the connection when done.
//
// Returns a *sql.DB connection or an error if the database cannot be created.
func (b *DBBuilder) Open(ctx context.Context) (*sql.DB, error) {
// Use validator to validate inputs availability
if err := b.validator.validateInputsAvailable(b.collectedPaths, b.readers); err != nil {
return nil, err
}
// Use file processor to deduplicate compressed files
b.collectedPaths = b.fileProcessor.deduplicateCompressedFiles(b.collectedPaths)
db, err := b.createInMemoryDatabase()
if err != nil {
return nil, err
}
// Use stream processor for all streaming operations (now includes XLSX support)
if err := b.streamProcessor.streamAllFilesToDatabase(ctx, db, b.collectedPaths); err != nil {
_ = db.Close() // Ignore close error during error handling
return nil, err
}
if err := b.streamProcessor.streamAllReadersToDatabase(ctx, db, b.readers); err != nil {
_ = db.Close() // Ignore close error during error handling
return nil, err
}
if err := b.validateDatabaseConnection(ctx, db); err != nil {
_ = db.Close() // Ignore close error during error handling
return nil, err
}
db, err = b.setupAutoSaveIfNeeded(ctx, db)
if err != nil {
return nil, err
}
return db, nil
}
// deduplicateCompressedFiles removes compressed duplicates when uncompressed versions exist.
// DEPRECATED: This method has been moved to fileProcessor.deduplicateCompressedFiles()
func (b *DBBuilder) deduplicateCompressedFiles(files []string) []string {
return b.fileProcessor.deduplicateCompressedFiles(files)
}
// createInMemoryDatabase creates a new in-memory SQLite database connection.
func (b *DBBuilder) createInMemoryDatabase() (*sql.DB, error) {
sqliteDriver := &sqlite.Driver{}
conn, err := sqliteDriver.Open(":memory:")
if err != nil {
return nil, fmt.Errorf("failed to create in-memory database: %w", err)
}
return sql.OpenDB(&directConnector{conn: conn}), nil
}
// validateDatabaseConnection validates the database connection is working.
func (b *DBBuilder) validateDatabaseConnection(ctx context.Context, db *sql.DB) error {
if err := db.PingContext(ctx); err != nil {
closeErr := db.Close()
var allErrors []error
allErrors = append(allErrors, err)
if closeErr != nil {
allErrors = append(allErrors, fmt.Errorf("failed to close database: %w", closeErr))
}
return errors.Join(allErrors...)
}
return nil
}
// setupAutoSaveIfNeeded sets up auto-save functionality if enabled.
func (b *DBBuilder) setupAutoSaveIfNeeded(ctx context.Context, db *sql.DB) (*sql.DB, error) {
if b.autoSaveConfig == nil || !b.autoSaveConfig.enabled {
return db, nil
}
if err := db.Close(); err != nil {
return nil, fmt.Errorf("failed to close intermediate database: %w", err)
}
sqliteDriver := &sqlite.Driver{}
freshConn, err := sqliteDriver.Open(":memory:")
if err != nil {
return nil, fmt.Errorf("failed to create fresh SQLite connection for auto-save: %w", err)
}
connector := &autoSaveConnector{
sqliteConn: freshConn,
autoSaveConfig: b.autoSaveConfig,
originalPaths: b.collectOriginalPaths(),
}
db = sql.OpenDB(connector)
// Use stream processor for all streaming operations (now includes XLSX support)
if err := b.streamProcessor.streamAllFilesToDatabase(ctx, db, b.collectedPaths); err != nil {
_ = db.Close() // Ignore close error during error handling
return nil, err
}
if err := b.streamProcessor.streamAllReadersToDatabase(ctx, db, b.readers); err != nil {
_ = db.Close() // Ignore close error during error handling
return nil, err
}
return db, nil
}
// processFSToReaders processes all supported files from an fs.FS and creates ReaderInput
func (b *DBBuilder) processFSToReaders(_ context.Context, filesystem fs.FS) ([]readerInput, error) {
readers := make([]readerInput, 0)
// Search for all supported file patterns
supportedPatterns := supportedFileExtPatterns()
// Collect all matching files
allMatches := make([]string, 0)
for _, pattern := range supportedPatterns {
matches, err := fs.Glob(filesystem, pattern)
if err != nil {
return nil, fmt.Errorf("failed to search pattern %s: %w", pattern, err)
}
allMatches = append(allMatches, matches...)
}
// Also search recursively in subdirectories
// Check if "." exists in the filesystem before walking
if _, err := fs.Stat(filesystem, "."); err == nil {
// "." exists, we can safely walk the filesystem
walkErr := fs.WalkDir(filesystem, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if isSupportedFile(path) {
// Check if already found by glob patterns
// Use path.Clean to normalize paths for comparison (fs.FS uses forward slashes)
normalizedPath := filepath.ToSlash(path)
found := false
for _, existing := range allMatches {
normalizedExisting := filepath.ToSlash(existing)
if normalizedExisting == normalizedPath {
found = true
break
}
}
if !found {
allMatches = append(allMatches, path)
}
}
return nil
})
if walkErr != nil {
return nil, fmt.Errorf("failed to walk filesystem: %w", walkErr)
}
}
// If "." doesn't exist, we'll just use what we found with glob patterns
if len(allMatches) == 0 {
return nil, errors.New("no supported files found in filesystem")
}
// Remove compressed duplicates when uncompressed versions exist
allMatches = b.deduplicateCompressedFiles(allMatches)
// Create ReaderInput for each matched file
for _, match := range allMatches {
// Open the file from FS
file, err := filesystem.Open(match)
if err != nil {
return nil, fmt.Errorf("failed to open FS file %s: %w", match, err)
}
// Determine file type from extension using NewFile
fileInfo := newFile(match)
fileType := fileInfo.getFileType()
// Generate table name from file path (remove extension and clean up)
tableName := sanitizeTableName(tableFromFilePath(match))
// Create ReaderInput
readerInput := readerInput{
reader: file,
tableName: tableName,
fileType: fileType,
}
readers = append(readers, readerInput)
}
return readers, nil
}
// streamXLSXFileToSQLite handles XLSX files by creating separate tables for each sheet
func (b *DBBuilder) streamXLSXFileToSQLite(ctx context.Context, db *sql.DB, reader io.Reader, filePath string) error {
// Read all data into memory (XLSX requires random access)
data, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("failed to read XLSX data: %w", err)
}
if len(data) == 0 {
return errors.New("empty XLSX file")
}
// Open XLSX file from bytes
xlsxFile, err := excelize.OpenReader(bytes.NewReader(data))
if err != nil {
return fmt.Errorf("failed to open XLSX file: %w", err)
}
defer func() {
_ = xlsxFile.Close() // Ignore close error
}()
// Get all sheet names
sheetNames := xlsxFile.GetSheetList()
if len(sheetNames) == 0 {
return errors.New("no sheets found in XLSX file")
}
// Base table name from file path (sanitize to ensure a valid identifier)
baseTableName := sanitizeTableName(tableFromFilePath(filePath))
// Process each sheet as a separate table
for _, sheetName := range sheetNames {
rows, err := xlsxFile.GetRows(sheetName)
if err != nil {
return fmt.Errorf("failed to read sheet %s: %w", sheetName, err)
}
// Skip empty sheets
if len(rows) == 0 {
continue
}
// Create table name: filename_sheetname
tableName := fmt.Sprintf("%s_%s", baseTableName, sanitizeTableName(sheetName))
// Check if table already exists
var tableExists int
err = db.QueryRowContext(ctx,
`SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?`,
tableName,
).Scan(&tableExists)
if err != nil {
return fmt.Errorf("failed to check table existence: %w", err)
}
if tableExists > 0 {
return fmt.Errorf("table '%s' already exists, duplicate table names are not allowed", tableName)
}
// Process sheet data
if err := b.createTableFromXLSXSheet(ctx, db, tableName, rows); err != nil {
return fmt.Errorf("failed to create table from sheet %s: %w", sheetName, err)
}
}
return nil
}
// createTableFromXLSXSheet creates a SQLite table from XLSX sheet data
func (b *DBBuilder) createTableFromXLSXSheet(ctx context.Context, db *sql.DB, tableName string, rows [][]string) error {
if len(rows) == 0 {
return errors.New("no rows in sheet")
}
// First row is header
headers := rows[0]
if len(headers) == 0 {
return errors.New("no columns in sheet header")
}
// Check for duplicate column names
columnsSeen := make(map[string]bool)
for _, col := range headers {
if columnsSeen[col] {
return fmt.Errorf("%w: %s", errDuplicateColumnName, col)
}
columnsSeen[col] = true
}
// Collect data rows for type inference
dataRows := make([][]string, 0, len(rows)-1)
for i := 1; i < len(rows); i++ {
dataRows = append(dataRows, rows[i])
}
// Create records for type inference
records := make([]Record, len(dataRows))
for i, row := range dataRows {
// Pad row with empty strings if necessary
paddedRow := make(Record, len(headers))
for j := range headers {
if j < len(row) {
paddedRow[j] = row[j]
} else {
paddedRow[j] = ""
}
}
records[i] = paddedRow
}
// Infer column types
headerObj := header(headers)
columnInfo := inferColumnsInfo(headerObj, records)
// Create table
if err := b.createSQLiteTable(ctx, db, tableName, columnInfo); err != nil {
return fmt.Errorf("failed to create SQLite table: %w", err)
}
// Insert data
if len(records) > 0 {
if err := b.insertDataIntoTable(ctx, db, tableName, headers, records); err != nil {
return fmt.Errorf("failed to insert data: %w", err)
}
}
return nil
}
// createSQLiteTable creates a SQLite table with the given columns
func (b *DBBuilder) createSQLiteTable(ctx context.Context, db *sql.DB, tableName string, columnInfo []columnInfo) error {
columns := make([]string, 0, len(columnInfo))
for _, col := range columnInfo {
columns = append(columns, fmt.Sprintf(`"%s" %s`, col.Name, col.Type.string()))
}
query := fmt.Sprintf(
`CREATE TABLE IF NOT EXISTS "%s" (%s)`,
tableName,
strings.Join(columns, ", "),
)
_, err := db.ExecContext(ctx, query)
return err
}
// insertDataIntoTable inserts records into the specified table
func (b *DBBuilder) insertDataIntoTable(ctx context.Context, db *sql.DB, tableName string, headers []string, records []Record) error {
placeholders := make([]string, len(headers))
for i := range placeholders {
placeholders[i] = "?"
}
query := fmt.Sprintf( //nolint:gosec // SQL table name is validated, placeholders are safe
`INSERT INTO "%s" VALUES (%s)`,
tableName,
strings.Join(placeholders, ", "),
)
stmt, err := db.PrepareContext(ctx, query)
if err != nil {
return fmt.Errorf("failed to prepare insert statement: %w", err)
}
defer stmt.Close()
for _, record := range records {
values := make([]any, len(record))
for i, value := range record {
values[i] = value
}
if _, err := stmt.ExecContext(ctx, values...); err != nil {
return fmt.Errorf("failed to insert record: %w", err)
}
}
return nil
}
// createDecompressedReader creates a decompressed reader based on file extension
func (b *DBBuilder) createDecompressedReader(file *os.File, filePath string) (io.Reader, error) {
factory := NewCompressionFactory()
handler := factory.CreateHandlerForFile(filePath)
reader, _, err := handler.CreateReader(file)
if err != nil {
return nil, err
}
return reader, nil
}
// collectOriginalPaths collects original file paths for overwrite mode
func (b *DBBuilder) collectOriginalPaths() []string {
var paths []string
paths = append(paths, b.collectedPaths...)
return paths
}