From cdf85b6e18ab45516c8189773af625cf8e70a25d Mon Sep 17 00:00:00 2001 From: Jim Smart Date: Mon, 19 Apr 2021 14:09:59 +0100 Subject: [PATCH] Escape table and column names in DDL/SQL --- sqlite_writer.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sqlite_writer.go b/sqlite_writer.go index 9be839f..7ddc29c 100644 --- a/sqlite_writer.go +++ b/sqlite_writer.go @@ -140,7 +140,7 @@ var kindToDBType = map[reflect.Kind]string{ func (w *SQLiteWriter) createDDL(t reflect.Type) string { // Create table using type name. - ddl := "CREATE TABLE " + t.Name() + " (\n" + ddl := "CREATE TABLE \"" + t.Name() + "\" (\n" // List of DDL statements to build the table definition. var ddlLines []string @@ -154,9 +154,8 @@ func (w *SQLiteWriter) createDDL(t reflect.Type) string { for i := 0; i < len(typs); i++ { // TODO(js) We should quote this appropriately, to handle reserved words. // Column name. - col := "\t" + hdrs[i] + " " + col := "\t\"" + hdrs[i] + "\" " - // TODO(js) Refactor reflect-type->db-type out, to reduce cyclomatic complexity. // Column datatype. t, ok := kindToDBType[typs[i].Kind()] if !ok { @@ -198,7 +197,7 @@ func (w *SQLiteWriter) createDDL(t reflect.Type) string { func (w *SQLiteWriter) createInsert(t reflect.Type) string { // TODO Add an option to allow different insert modes (default/ignore/update). - s := "INSERT OR IGNORE INTO " + t.Name() + " (" + s := "INSERT OR IGNORE INTO \"" + t.Name() + "\" (" hdrs := w.headersByType[t] s += strings.Join(hdrs, ",") s += ") VALUES ("