status-go/services/ens/database.go

82 lines
2.1 KiB
Go
Raw Permalink Normal View History

2023-01-20 10:34:30 +00:00
package ens
import (
"database/sql"
)
type Database struct {
db *sql.DB
}
type UsernameDetail struct {
2023-01-20 10:34:30 +00:00
Username string `json:"username"`
ChainID uint64 `json:"chainId"`
Clock uint64 `json:"clock"`
Removed bool `json:"removed"`
2023-01-20 10:34:30 +00:00
}
func NewEnsDatabase(db *sql.DB) *Database {
return &Database{db: db}
}
func (db *Database) GetEnsUsernames(removed *bool) (result []*UsernameDetail, err error) {
2023-01-20 10:34:30 +00:00
var sqlQuery = `SELECT username, chain_id, clock, removed
2023-01-20 10:34:30 +00:00
FROM ens_usernames`
var rows *sql.Rows
if removed == nil {
rows, err = db.db.Query(sqlQuery)
} else {
sqlQuery += " WHERE removed = ?"
rows, err = db.db.Query(sqlQuery, removed)
}
2023-01-20 10:34:30 +00:00
if err != nil {
return result, err
}
defer rows.Close()
for rows.Next() {
var ensUsername UsernameDetail
err = rows.Scan(&ensUsername.Username, &ensUsername.ChainID, &ensUsername.Clock, &ensUsername.Removed)
2023-01-20 10:34:30 +00:00
if err != nil {
return nil, err
}
result = append(result, &ensUsername)
}
return result, nil
}
func (db *Database) AddEnsUsername(details *UsernameDetail) error {
const sqlQuery = `INSERT OR REPLACE INTO ens_usernames(username, chain_id, clock, removed)
VALUES (?, ?, ?, ?)`
_, err := db.db.Exec(sqlQuery, details.Username, details.ChainID, details.Clock, details.Removed)
2023-01-20 10:34:30 +00:00
return err
}
func (db *Database) RemoveEnsUsername(details *UsernameDetail) (bool, error) {
const sqlQuery = `UPDATE ens_usernames SET removed = 1, clock = ?
2023-01-20 10:34:30 +00:00
WHERE username = (?) AND chain_id = ?`
result, err := db.db.Exec(sqlQuery, details.Clock, details.Username, details.ChainID)
if err != nil {
return false, err
}
n, err := result.RowsAffected()
if err != nil {
return false, err
}
return n > 0, nil
}
func (db *Database) SaveOrUpdateEnsUsername(details *UsernameDetail) error {
const sqlQuery = `INSERT OR REPLACE INTO ens_usernames (username, chain_id, clock, removed)
SELECT ?, ?, ?, ?
WHERE NOT EXISTS (SELECT 1 FROM ens_usernames WHERE username = ? AND chain_id = ? AND clock >= ?);`
_, err := db.db.Exec(sqlQuery, details.Username, details.ChainID, details.Clock, details.Removed, details.Username, details.ChainID, details.Clock)
2023-01-20 10:34:30 +00:00
return err
}