Skip to content

Commit 839421e

Browse files
committed
Leverage quoteIdentifier from pgx
1 parent 9fe7383 commit 839421e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

database/clickhouse/clickhouse.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (ch *ClickHouse) ensureVersionTable() (err error) {
220220

221221
var (
222222
table string
223-
query = "SHOW TABLES FROM \"" + ch.config.DatabaseName + "\" LIKE '" + ch.config.MigrationsTable + "'"
223+
query = "SHOW TABLES FROM " + quoteIdentifier(ch.config.DatabaseName) + " LIKE '" + ch.config.MigrationsTable + "'"
224224
)
225225
// check if migration table exists
226226
if err := ch.conn.QueryRow(query).Scan(&table); err != nil {
@@ -259,7 +259,7 @@ func (ch *ClickHouse) ensureVersionTable() (err error) {
259259
}
260260

261261
func (ch *ClickHouse) Drop() (err error) {
262-
query := "SHOW TABLES FROM \"" + ch.config.DatabaseName + "\""
262+
query := "SHOW TABLES FROM " + quoteIdentifier(ch.config.DatabaseName)
263263
tables, err := ch.conn.Query(query)
264264

265265
if err != nil {
@@ -277,7 +277,7 @@ func (ch *ClickHouse) Drop() (err error) {
277277
return err
278278
}
279279

280-
query = "DROP TABLE IF EXISTS \"" + ch.config.DatabaseName + "\"." + table
280+
query = "DROP TABLE IF EXISTS " + quoteIdentifier(ch.config.DatabaseName) + "." + quoteIdentifier(table)
281281

282282
if _, err := ch.conn.Exec(query); err != nil {
283283
return &database.Error{OrigErr: err, Query: []byte(query)}
@@ -305,3 +305,12 @@ func (ch *ClickHouse) Unlock() error {
305305
return nil
306306
}
307307
func (ch *ClickHouse) Close() error { return ch.conn.Close() }
308+
309+
// Copied from lib/pq implementation: https://github.com/lib/pq/blob/v1.9.0/conn.go#L1611
310+
func quoteIdentifier(name string) string {
311+
end := strings.IndexRune(name, 0)
312+
if end > -1 {
313+
name = name[:end]
314+
}
315+
return `"` + strings.Replace(name, `"`, `""`, -1) + `"`
316+
}

0 commit comments

Comments
 (0)