Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f258f8baeb |
@@ -128,9 +128,19 @@ func (r *Reader) readDirectoryDBML(dirPath string) (*models.Database, error) {
|
|||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// stripQuotes removes surrounding quotes from an identifier
|
// stripQuotes removes surrounding quotes and comments from an identifier
|
||||||
func stripQuotes(s string) string {
|
func stripQuotes(s string) string {
|
||||||
s = strings.TrimSpace(s)
|
s = strings.TrimSpace(s)
|
||||||
|
|
||||||
|
// Remove DBML comments in brackets (e.g., [note: 'description'])
|
||||||
|
// This handles inline comments like: "table_name" [note: 'comment']
|
||||||
|
commentRegex := regexp.MustCompile(`\s*\[.*?\]\s*`)
|
||||||
|
s = commentRegex.ReplaceAllString(s, "")
|
||||||
|
|
||||||
|
// Trim again after removing comments
|
||||||
|
s = strings.TrimSpace(s)
|
||||||
|
|
||||||
|
// Remove surrounding quotes (double or single)
|
||||||
if len(s) >= 2 && ((s[0] == '"' && s[len(s)-1] == '"') || (s[0] == '\'' && s[len(s)-1] == '\'')) {
|
if len(s) >= 2 && ((s[0] == '"' && s[len(s)-1] == '"') || (s[0] == '\'' && s[len(s)-1] == '\'')) {
|
||||||
return s[1 : len(s)-1]
|
return s[1 : len(s)-1]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -207,7 +207,10 @@ func (w *Writer) writeMultiFile(db *models.Database) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate filename: sql_{schema}_{table}.go
|
// Generate filename: sql_{schema}_{table}.go
|
||||||
filename := fmt.Sprintf("sql_%s_%s.go", schema.Name, table.Name)
|
// Sanitize schema and table names to remove quotes, comments, and invalid characters
|
||||||
|
safeSchemaName := writers.SanitizeFilename(schema.Name)
|
||||||
|
safeTableName := writers.SanitizeFilename(table.Name)
|
||||||
|
filename := fmt.Sprintf("sql_%s_%s.go", safeSchemaName, safeTableName)
|
||||||
filepath := filepath.Join(w.options.OutputPath, filename)
|
filepath := filepath.Join(w.options.OutputPath, filename)
|
||||||
|
|
||||||
// Write file
|
// Write file
|
||||||
|
|||||||
@@ -196,7 +196,9 @@ func (w *Writer) writeTableFile(table *models.Table, schema *models.Schema, db *
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate filename: {tableName}.ts
|
// Generate filename: {tableName}.ts
|
||||||
filename := filepath.Join(w.options.OutputPath, table.Name+".ts")
|
// Sanitize table name to remove quotes, comments, and invalid characters
|
||||||
|
safeTableName := writers.SanitizeFilename(table.Name)
|
||||||
|
filename := filepath.Join(w.options.OutputPath, safeTableName+".ts")
|
||||||
return os.WriteFile(filename, []byte(code), 0644)
|
return os.WriteFile(filename, []byte(code), 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -201,7 +201,10 @@ func (w *Writer) writeMultiFile(db *models.Database) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate filename: sql_{schema}_{table}.go
|
// Generate filename: sql_{schema}_{table}.go
|
||||||
filename := fmt.Sprintf("sql_%s_%s.go", schema.Name, table.Name)
|
// Sanitize schema and table names to remove quotes, comments, and invalid characters
|
||||||
|
safeSchemaName := writers.SanitizeFilename(schema.Name)
|
||||||
|
safeTableName := writers.SanitizeFilename(table.Name)
|
||||||
|
filename := fmt.Sprintf("sql_%s_%s.go", safeSchemaName, safeTableName)
|
||||||
filepath := filepath.Join(w.options.OutputPath, filename)
|
filepath := filepath.Join(w.options.OutputPath, filename)
|
||||||
|
|
||||||
// Write file
|
// Write file
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package writers
|
package writers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,3 +31,33 @@ type WriterOptions struct {
|
|||||||
// Additional options can be added here as needed
|
// Additional options can be added here as needed
|
||||||
Metadata map[string]interface{}
|
Metadata map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SanitizeFilename removes quotes, comments, and invalid characters from identifiers
|
||||||
|
// to make them safe for use in filenames. This handles:
|
||||||
|
// - Double and single quotes: "table_name" or 'table_name' -> table_name
|
||||||
|
// - DBML comments: table [note: 'description'] -> table
|
||||||
|
// - Invalid filename characters: replaced with underscores
|
||||||
|
func SanitizeFilename(name string) string {
|
||||||
|
// Remove DBML/DCTX style comments in brackets (e.g., [note: 'description'])
|
||||||
|
commentRegex := regexp.MustCompile(`\s*\[.*?\]\s*`)
|
||||||
|
name = commentRegex.ReplaceAllString(name, "")
|
||||||
|
|
||||||
|
// Remove quotes (both single and double)
|
||||||
|
name = strings.ReplaceAll(name, `"`, "")
|
||||||
|
name = strings.ReplaceAll(name, `'`, "")
|
||||||
|
|
||||||
|
// Remove backticks (MySQL style identifiers)
|
||||||
|
name = strings.ReplaceAll(name, "`", "")
|
||||||
|
|
||||||
|
// Replace invalid filename characters with underscores
|
||||||
|
// Invalid chars: / \ : * ? " < > | and control characters
|
||||||
|
invalidChars := regexp.MustCompile(`[/\\:*?"<>|\x00-\x1f\x7f]`)
|
||||||
|
name = invalidChars.ReplaceAllString(name, "_")
|
||||||
|
|
||||||
|
// Trim whitespace and consecutive underscores
|
||||||
|
name = strings.TrimSpace(name)
|
||||||
|
name = regexp.MustCompile(`_+`).ReplaceAllString(name, "_")
|
||||||
|
name = strings.Trim(name, "_")
|
||||||
|
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user