@@ -110,6 +110,12 @@ func openDatabase(dbPath string) (*sql.DB, error) {
110
110
return db , nil
111
111
}
112
112
113
+ // quoteIdentifier はSQLiteの識別子を正しく引用符で囲みます。
114
+ // 識別子内の二重引用符は二重にエスケープされます。
115
+ func quoteIdentifier (name string ) string {
116
+ return `"` + strings .ReplaceAll (name , `"` , `""` ) + `"`
117
+ }
118
+
113
119
func inferSchema (data []map [string ]interface {}) map [string ]string {
114
120
columnTypes := make (map [string ]string )
115
121
for _ , row := range data {
@@ -157,10 +163,10 @@ func setupTable(db *sql.DB, tableName string, data []map[string]interface{}) ([]
157
163
if ! tableExists {
158
164
var columns []string
159
165
for name , colType := range inferredSchema {
160
- columns = append (columns , fmt .Sprintf ("\" %s \" %s" , name , colType ))
166
+ columns = append (columns , fmt .Sprintf ("%s %s" , quoteIdentifier ( name ) , colType ))
161
167
}
162
168
sort .Strings (columns ) // Ensure consistent order for CREATE TABLE
163
- createQuery := fmt .Sprintf ("CREATE TABLE \" %s \" (%s)" , tableName , strings .Join (columns , ", " ))
169
+ createQuery := fmt .Sprintf ("CREATE TABLE %s (%s)" , quoteIdentifier ( tableName ) , strings .Join (columns , ", " ))
164
170
_ , err := db .Exec (createQuery )
165
171
if err != nil {
166
172
return nil , fmt .Errorf ("failed to create table: %w" , err )
@@ -170,8 +176,7 @@ func setupTable(db *sql.DB, tableName string, data []map[string]interface{}) ([]
170
176
// Use string concatenation for PRAGMA to avoid any Sprintf formatting issues.
171
177
// Table names can't be parameterized in PRAGMA statements.
172
178
pragmaQuery := "PRAGMA table_info(" + `"` + strings .ReplaceAll (tableName , `"` , `""` ) + `"` + ")"
173
-
174
- rows , err := db .Query (pragmaQuery )
179
+ rows , err := db .Query (pragmaQuery )
175
180
if err != nil {
176
181
return nil , fmt .Errorf ("failed to get existing table info: %w" , err )
177
182
}
@@ -193,7 +198,7 @@ rows, err := db.Query(pragmaQuery)
193
198
194
199
for colName , colType := range inferredSchema {
195
200
if ! existingColumns [colName ] {
196
- alterQuery := fmt .Sprintf ("ALTER TABLE \" %s \" ADD COLUMN \" %s \" %s" , tableName , colName , colType )
201
+ alterQuery := fmt .Sprintf ("ALTER TABLE %s ADD COLUMN %s %s" , quoteIdentifier ( tableName ), quoteIdentifier ( colName ) , colType )
197
202
_ , err := db .Exec (alterQuery )
198
203
if err != nil {
199
204
return nil , fmt .Errorf ("failed to add column '%s': %w" , colName , err )
@@ -219,7 +224,13 @@ func insertData(db *sql.DB, tableName string, columns []string, data []map[strin
219
224
placeholders := strings .Repeat ("?," , len (columns ))
220
225
placeholders = placeholders [:len (placeholders )- 1 ]
221
226
222
- query := fmt .Sprintf ("INSERT INTO \" %s\" (%s) VALUES (%s)" , tableName , "\" " + strings .Join (columns , "\" , \" " )+ "\" " , placeholders )
227
+ // Build the quoted column names for the INSERT statement
228
+ quotedColumns := make ([]string , len (columns ))
229
+ for i , col := range columns {
230
+ quotedColumns [i ] = quoteIdentifier (col )
231
+ }
232
+
233
+ query := fmt .Sprintf ("INSERT INTO %s (%s) VALUES (%s)" , quoteIdentifier (tableName ), strings .Join (quotedColumns , ", " ), placeholders )
223
234
224
235
tx , err := db .Begin ()
225
236
if err != nil {
0 commit comments