2024-07-18 15:30:10 +00:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const upsertDAppQuery = "INSERT INTO connector_dapps (url, name, icon_url, shared_account, chain_id) VALUES (?, ?, ?, ?, ?) ON CONFLICT(url) DO UPDATE SET name = excluded.name, icon_url = excluded.icon_url, shared_account = excluded.shared_account, chain_id = excluded.chain_id"
|
|
|
|
const selectDAppByUrlQuery = "SELECT name, icon_url, shared_account, chain_id FROM connector_dapps WHERE url = ?"
|
2024-08-07 07:32:40 +00:00
|
|
|
const selectDAppsQuery = "SELECT url, name, icon_url, shared_account, chain_id FROM connector_dapps"
|
2024-07-18 15:30:10 +00:00
|
|
|
const deleteDAppQuery = "DELETE FROM connector_dapps WHERE url = ?"
|
|
|
|
|
|
|
|
type DApp struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
IconURL string `json:"iconUrl"`
|
|
|
|
SharedAccount types.Address `json:"sharedAccount"`
|
|
|
|
ChainID uint64 `json:"chainId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpsertDApp(db *sql.DB, dApp *DApp) error {
|
|
|
|
_, err := db.Exec(upsertDAppQuery, dApp.URL, dApp.Name, dApp.IconURL, dApp.SharedAccount, dApp.ChainID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func SelectDAppByUrl(db *sql.DB, url string) (*DApp, error) {
|
|
|
|
dApp := &DApp{
|
|
|
|
URL: url,
|
|
|
|
}
|
|
|
|
err := db.QueryRow(selectDAppByUrlQuery, url).Scan(&dApp.Name, &dApp.IconURL, &dApp.SharedAccount, &dApp.ChainID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return dApp, err
|
|
|
|
}
|
|
|
|
|
2024-08-07 07:32:40 +00:00
|
|
|
func SelectAllDApps(db *sql.DB) ([]DApp, error) {
|
|
|
|
rows, err := db.Query(selectDAppsQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var dApps []DApp
|
|
|
|
for rows.Next() {
|
|
|
|
dApp := DApp{}
|
|
|
|
err = rows.Scan(&dApp.URL, &dApp.Name, &dApp.IconURL, &dApp.SharedAccount, &dApp.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dApps = append(dApps, dApp)
|
|
|
|
}
|
|
|
|
return dApps, nil
|
|
|
|
}
|
|
|
|
|
2024-07-18 15:30:10 +00:00
|
|
|
func DeleteDApp(db *sql.DB, url string) error {
|
|
|
|
_, err := db.Exec(deleteDAppQuery, url)
|
|
|
|
return err
|
|
|
|
}
|