2023-01-20 10:34:30 +00:00
|
|
|
package ens
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Database struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2023-04-26 15:37:18 +00:00
|
|
|
type UsernameDetail struct {
|
2023-01-20 10:34:30 +00:00
|
|
|
Username string `json:"username"`
|
|
|
|
ChainID uint64 `json:"chainId"`
|
2023-04-26 15:37:18 +00:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2023-04-26 15:37:18 +00:00
|
|
|
func (db *Database) GetEnsUsernames(removed *bool) (result []*UsernameDetail, err error) {
|
2023-01-20 10:34:30 +00:00
|
|
|
|
2023-04-26 15:37:18 +00:00
|
|
|
var sqlQuery = `SELECT username, chain_id, clock, removed
|
2023-01-20 10:34:30 +00:00
|
|
|
FROM ens_usernames`
|
|
|
|
|
2023-04-26 15:37:18 +00:00
|
|
|
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() {
|
2023-04-26 15:37:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-26 15:37:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-26 15:37:18 +00:00
|
|
|
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 = ?`
|
2023-04-26 15:37:18 +00:00
|
|
|
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
|
|
|
|
}
|