fix: escape 'key' as reserved word in all MySQL queries with backticks
This commit is contained in:
+12
-12
@@ -41,10 +41,10 @@ func (db *DB) Conn() *sql.DB {
|
||||
|
||||
func (db *DB) migrate() error {
|
||||
stmts := []string{
|
||||
`CREATE TABLE IF NOT EXISTS settings (
|
||||
key VARCHAR(255) PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
||||
"CREATE TABLE IF NOT EXISTS settings (" +
|
||||
"`key` VARCHAR(255) PRIMARY KEY," +
|
||||
"value TEXT NOT NULL" +
|
||||
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
||||
`CREATE TABLE IF NOT EXISTS users (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
email VARCHAR(255) NOT NULL UNIQUE,
|
||||
@@ -52,13 +52,13 @@ func (db *DB) migrate() error {
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
||||
`CREATE TABLE IF NOT EXISTS user_settings (
|
||||
user_id BIGINT NOT NULL,
|
||||
key VARCHAR(255) NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
PRIMARY KEY (user_id, key),
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
||||
"CREATE TABLE IF NOT EXISTS user_settings (" +
|
||||
"user_id BIGINT NOT NULL," +
|
||||
"`key` VARCHAR(255) NOT NULL," +
|
||||
"value TEXT NOT NULL," +
|
||||
"PRIMARY KEY (user_id, `key`)," +
|
||||
"FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE" +
|
||||
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
|
||||
`CREATE TABLE IF NOT EXISTS repositories (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id BIGINT,
|
||||
@@ -105,7 +105,7 @@ func (db *DB) migrate() error {
|
||||
func (db *DB) seedDefaults() error {
|
||||
for key, val := range models.DefaultSettings {
|
||||
_, err := db.conn.Exec(
|
||||
`INSERT IGNORE INTO settings (key, value) VALUES (?, ?)`, key, val,
|
||||
"INSERT IGNORE INTO settings (`key`, value) VALUES (?, ?)", key, val,
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("warning: failed to seed setting %s: %v", key, err)
|
||||
|
||||
+1
-1
@@ -126,7 +126,7 @@ func (h *AuthHandler) HandleRegister(c *gin.Context) {
|
||||
|
||||
// Seed default settings for the user
|
||||
for key, val := range defaultUserSettings {
|
||||
h.db.Exec(`INSERT IGNORE INTO user_settings (user_id, key, value) VALUES (?, ?, ?)`, userID, key, val)
|
||||
h.db.Exec("INSERT IGNORE INTO user_settings (user_id, `key`, value) VALUES (?, ?, ?)", userID, key, val)
|
||||
}
|
||||
|
||||
// Auto-login: set session
|
||||
|
||||
+3
-3
@@ -55,8 +55,8 @@ func (h *PageHandler) Review(c *gin.Context) {
|
||||
topN := "20"
|
||||
concurrency := "5"
|
||||
if user != nil {
|
||||
h.db.QueryRow(`SELECT value FROM user_settings WHERE user_id = ? AND key = 'review.top_n'`, user.ID).Scan(&topN)
|
||||
h.db.QueryRow(`SELECT value FROM user_settings WHERE user_id = ? AND key = 'review.concurrency'`, user.ID).Scan(&concurrency)
|
||||
h.db.QueryRow("SELECT value FROM user_settings WHERE user_id = ? AND `key` = 'review.top_n'", user.ID).Scan(&topN)
|
||||
h.db.QueryRow("SELECT value FROM user_settings WHERE user_id = ? AND `key` = 'review.concurrency'", user.ID).Scan(&concurrency)
|
||||
}
|
||||
if topN == "" {
|
||||
topN = "20"
|
||||
@@ -78,7 +78,7 @@ func (h *PageHandler) Settings(c *gin.Context) {
|
||||
settings[key] = ""
|
||||
}
|
||||
if user != nil {
|
||||
rows, err := h.db.Query(`SELECT key, value FROM user_settings WHERE user_id = ?`, user.ID)
|
||||
rows, err := h.db.Query("SELECT `key`, value FROM user_settings WHERE user_id = ?", user.ID)
|
||||
if err == nil {
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
|
||||
+1
-1
@@ -100,7 +100,7 @@ func (h *ReposHandler) CleanupRepos(c *gin.Context) {
|
||||
}
|
||||
|
||||
var maxAgeDays string
|
||||
h.db.QueryRow(`SELECT value FROM user_settings WHERE user_id = ? AND key = 'cache.max_age_days'`, user.ID).Scan(&maxAgeDays)
|
||||
h.db.QueryRow("SELECT value FROM user_settings WHERE user_id = ? AND `key` = 'cache.max_age_days'", user.ID).Scan(&maxAgeDays)
|
||||
if maxAgeDays == "" {
|
||||
maxAgeDays = "7"
|
||||
}
|
||||
|
||||
+2
-2
@@ -60,7 +60,7 @@ func (h *ReviewHandler) Review(c *gin.Context) {
|
||||
topN = *req.TopN
|
||||
} else {
|
||||
var topNStr string
|
||||
h.db.QueryRow(`SELECT value FROM user_settings WHERE user_id = ? AND key = 'review.top_n'`, user.ID).Scan(&topNStr)
|
||||
h.db.QueryRow("SELECT value FROM user_settings WHERE user_id = ? AND `key` = 'review.top_n'", user.ID).Scan(&topNStr)
|
||||
if topNStr != "" {
|
||||
if n, err := strconv.Atoi(topNStr); err == nil && n > 0 {
|
||||
topN = n
|
||||
@@ -74,7 +74,7 @@ func (h *ReviewHandler) Review(c *gin.Context) {
|
||||
concurrency = *req.Concurrency
|
||||
} else {
|
||||
var concStr string
|
||||
h.db.QueryRow(`SELECT value FROM user_settings WHERE user_id = ? AND key = 'review.concurrency'`, user.ID).Scan(&concStr)
|
||||
h.db.QueryRow("SELECT value FROM user_settings WHERE user_id = ? AND `key` = 'review.concurrency'", user.ID).Scan(&concStr)
|
||||
if concStr != "" {
|
||||
if n, err := strconv.Atoi(concStr); err == nil && n > 0 {
|
||||
concurrency = n
|
||||
|
||||
@@ -23,7 +23,7 @@ func (h *SettingsHandler) GetSettings(c *gin.Context) {
|
||||
}
|
||||
|
||||
settings := make(map[string]string)
|
||||
rows, err := h.db.Query(`SELECT key, value FROM user_settings WHERE user_id = ?`, user.ID)
|
||||
rows, err := h.db.Query("SELECT `key`, value FROM user_settings WHERE user_id = ?", user.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -56,7 +56,7 @@ func (h *SettingsHandler) UpdateSettings(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
for key, val := range body {
|
||||
_, err := tx.Exec(`INSERT INTO user_settings (user_id, key, value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE value = VALUES(value)`, user.ID, key, val)
|
||||
_, err := tx.Exec("INSERT INTO user_settings (user_id, `key`, value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE value = VALUES(value)", user.ID, key, val)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ type LLMConfig struct {
|
||||
func GetLLMConfig(db *sql.DB, userID int64) (LLMConfig, error) {
|
||||
config := LLMConfig{}
|
||||
|
||||
rows, err := db.Query(`SELECT key, value FROM user_settings WHERE user_id = ? AND key IN ('llm.endpoint', 'llm.api_key', 'llm.model')`, userID)
|
||||
rows, err := db.Query("SELECT `key`, value FROM user_settings WHERE user_id = ? AND `key` IN ('llm.endpoint', 'llm.api_key', 'llm.model')", userID)
|
||||
if err != nil {
|
||||
return config, fmt.Errorf("read settings: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user