Skip to content

Commit 30c9070

Browse files
authored
refactor: move quoteIdent to utils and used in priority queue (#3)
1 parent 36e4b8a commit 30c9070

3 files changed

Lines changed: 18 additions & 16 deletions

File tree

priority_queue.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ func newPriorityQueue(db *sql.DB, tableName string, opts ...Option) (*PriorityQu
3636
func (pq *PriorityQueue) initPriorityColumn() error {
3737
// Check if priority column exists
3838
var name string
39-
err := pq.client.QueryRow(fmt.Sprintf("PRAGMA table_info(%s)", pq.tableName)).Scan(nil, &name, nil, nil, nil, nil)
39+
err := pq.client.QueryRow(fmt.Sprintf("PRAGMA table_info(%s)", quoteIdent(pq.tableName))).Scan(nil, &name, nil, nil, nil, nil)
4040

4141
if err != nil || name != "priority" {
4242
// Add priority column with default value 0
43-
_, err := pq.client.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN priority INTEGER NOT NULL DEFAULT 0", pq.tableName))
43+
_, err := pq.client.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN priority INTEGER NOT NULL DEFAULT 0", quoteIdent(pq.tableName)))
4444
if err != nil {
4545
return err
4646
}
4747

4848
// Create index on priority (ASC for lower numbers = higher priority)
49-
_, err = pq.client.Exec(fmt.Sprintf("CREATE INDEX IF NOT EXISTS %s_priority_idx ON %s (priority ASC, created_at ASC)", pq.tableName, pq.tableName))
49+
_, err = pq.client.Exec(fmt.Sprintf("CREATE INDEX IF NOT EXISTS %s ON %s (priority ASC, created_at ASC)", quoteIdent(pq.tableName+"_priority_idx"), quoteIdent(pq.tableName)))
5050
if err != nil {
5151
return err
5252
}
@@ -75,7 +75,7 @@ func (pq *PriorityQueue) Enqueue(item any, priority int) bool {
7575
}()
7676

7777
_, err = tx.Exec(
78-
fmt.Sprintf("INSERT INTO %s (data, status, created_at, updated_at, priority) VALUES (?, ?, ?, ?, ?)", pq.tableName),
78+
fmt.Sprintf("INSERT INTO %s (data, status, created_at, updated_at, priority) VALUES (?, ?, ?, ?, ?)", quoteIdent(pq.tableName)),
7979
item, "pending", now, now, priority,
8080
)
8181
if err != nil {
@@ -108,7 +108,7 @@ func (pq *PriorityQueue) dequeueInternal(withAckId bool) (any, bool, string) {
108108
var data []byte
109109
row := tx.QueryRow(fmt.Sprintf(
110110
"SELECT id, data FROM %s WHERE status = 'pending' ORDER BY priority ASC, created_at ASC LIMIT 1",
111-
pq.tableName,
111+
quoteIdent(pq.tableName),
112112
))
113113
err = row.Scan(&id, &data)
114114
if err != nil {
@@ -128,13 +128,13 @@ func (pq *PriorityQueue) dequeueInternal(withAckId bool) (any, bool, string) {
128128
ackID = cuid.New()
129129

130130
_, err = tx.Exec(
131-
fmt.Sprintf("UPDATE %s SET status = 'processing', ack_id = ?, updated_at = ? WHERE id = ?", pq.tableName),
131+
fmt.Sprintf("UPDATE %s SET status = 'processing', ack_id = ?, updated_at = ? WHERE id = ?", quoteIdent(pq.tableName)),
132132
ackID, now, id,
133133
)
134134
} else {
135135
// remove the row if there is no ack
136136
_, err = tx.Exec(
137-
fmt.Sprintf("DELETE FROM %s WHERE id = ?", pq.tableName),
137+
fmt.Sprintf("DELETE FROM %s WHERE id = ?", quoteIdent(pq.tableName)),
138138
id,
139139
)
140140
}

queue.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sqliteq
33
import (
44
"database/sql"
55
"fmt"
6-
"strings"
76
"sync/atomic"
87
"time"
98

@@ -317,11 +316,3 @@ func (q *Queue) Close() error {
317316

318317
return nil
319318
}
320-
321-
// Applies quotes to an identifier escaping any internal quotes.
322-
// See: https://www.sqlite.org/lang_keywords.html
323-
func quoteIdent(name string) string {
324-
// Replace quotes with dobule quotes
325-
escaped := strings.ReplaceAll(name, `"`, `""`)
326-
return `"` + escaped + `"`
327-
}

utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package sqliteq
2+
3+
import "strings"
4+
5+
// Applies quotes to an identifier escaping any internal quotes.
6+
// See: https://www.sqlite.org/lang_keywords.html
7+
func quoteIdent(name string) string {
8+
// Replace quotes with dobule quotes
9+
escaped := strings.ReplaceAll(name, `"`, `""`)
10+
return `"` + escaped + `"`
11+
}

0 commit comments

Comments
 (0)