Repository navigation
Expand file tree
/
Copy pathsqlgen.go
More file actions
146 lines (120 loc) · 3.49 KB
/
Copy pathsqlgen.go
File metadata and controls
146 lines (120 loc) · 3.49 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
package gosqlgen
import (
"fmt"
"os"
"path/filepath"
"github.com/jalphad/gosqlgen/parser"
)
// SQLGen is the main interface for the SQL DTO generator
type SQLGen struct {
parser *parser.Parser
generator *Generator
packageName string
outputPath string
packageRootPath string
}
// New creates a new SQLGen instance
func New() *SQLGen {
p := parser.NewParser()
generator := NewGenerator(p)
return &SQLGen{
parser: p,
generator: generator,
packageName: "models",
outputPath: ".",
packageRootPath: "example.local/example",
}
}
// WithPackageName sets the name of the generated package.
func (s *SQLGen) WithPackageName(name string) *SQLGen {
s.packageName = name
s.generator.SetPackageName(name)
return s
}
// WithOutputPath sets the directory that will contain the generated package.
func (s *SQLGen) WithOutputPath(path string) *SQLGen {
s.outputPath = path
return s
}
// WithPackagePath sets the import path root containing the generated package.
func (s *SQLGen) WithPackagePath(path string) *SQLGen {
s.packageRootPath = path
s.generator.SetPackagePath(path)
return s
}
// WithSchemas limits generation to the provided PostgreSQL schemas.
// With no schemas configured, all parsed tables are generated.
func (s *SQLGen) WithSchemas(schemas ...string) *SQLGen {
s.generator.SetSchemas(schemas)
return s
}
// ParseFile parses SQL from a file
func (s *SQLGen) ParseFile(filename string) error {
content, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("failed to read file %s: %w", filename, err)
}
return s.Parse(string(content))
}
// Parse parses SQL statements
func (s *SQLGen) Parse(sql string) error {
return s.parser.Parse(sql)
}
// GenerateFiles generates Go code from parsed SQL as separate files
func (s *SQLGen) GenerateFiles() error {
code, err := s.generator.GenerateFiles()
if err != nil {
return fmt.Errorf("failed to generate code: %w", err)
}
packageOutputPath := filepath.Join(s.outputPath, s.packageName)
for filename, content := range code {
// Create output directory if it doesn't exist
outputFile := filepath.Join(packageOutputPath, filename)
dir := filepath.Dir(outputFile)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory %s: %w", dir, err)
}
// Write generated code to file
if err := os.WriteFile(outputFile, []byte(content), 0644); err != nil {
return fmt.Errorf("failed to write file %s: %w", filename, err)
}
}
return nil
}
// GetTables returns all parsed tables
func (s *SQLGen) GetTables() map[string]*parser.Table {
return s.parser.GetTables()
}
// GetTable returns a specific parsed table
func (s *SQLGen) GetTable(name string) (*parser.Table, bool) {
return s.parser.GetTable(name)
}
// Config holds configuration for the generator
type Config struct {
PackageName string
PackagePath string
OutputPath string
Schemas []string
UseNullTypes bool
UseGORM bool
UseSquirrel bool
DatabaseDriver string // postgres, mysql, sqlite
}
// NewWithConfig creates a new SQLGen with configuration
func NewWithConfig(config Config) *SQLGen {
gen := New()
if config.PackageName != "" {
gen.WithPackageName(config.PackageName)
}
if config.PackagePath != "" {
gen.WithPackagePath(config.PackagePath)
}
if config.OutputPath != "" {
gen.WithOutputPath(config.OutputPath)
}
if len(config.Schemas) > 0 {
gen.WithSchemas(config.Schemas...)
}
// Additional configuration can be applied here
return gen
}