Files
coredns-web-ui/records_store.go
T
bryanpedini 7089a6f408 build: add makefile, dockerfile, ci and development stack
- build: add makefile, dockerfile, ci and development stack
- feat(config): add environment configuration and logger
- feat(server): add server lifecycle with health and version routes
- feat(db): add connection, contract check, migrations and stores
- feat(cli): add command dispatch and user management
- test(db): add integration tests against mysql
2026-11-19 15:46:12 +01:00

170 lines
5.4 KiB
Go

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
package main
import (
"context"
"database/sql"
"errors"
"fmt"
)
type zoneSummary struct {
Zone string
RecordCount int64
SOAContent sql.NullString
}
type recordStore struct {
q dbtx
t tableNames
}
func newRecordStore(q dbtx, t tableNames) recordStore {
return recordStore{q: q, t: t}
}
const recordColumns = "`id`, `zone`, `name`, `ttl`, `content`, `record_type`"
const zoneSummarySelect = "SELECT r.`zone`, COUNT(*), " +
"(SELECT s.`content` FROM {records} s WHERE s.`zone` = r.`zone` AND CAST(s.`record_type` AS BINARY) = 'SOA' " +
"ORDER BY s.`name` <> '', s.`id` LIMIT 1) FROM {records} r "
func (s recordStore) ListZones(ctx context.Context) ([]zoneSummary, error) {
return s.queryZones(ctx, s.t.sql(zoneSummarySelect+"GROUP BY r.`zone` ORDER BY r.`zone`"))
}
func (s recordStore) ListZonesOfUser(ctx context.Context, userID int64) ([]zoneSummary, error) {
return s.queryZones(ctx, s.t.sql(zoneSummarySelect+
"WHERE r.`zone` IN (SELECT d.`zone` FROM {delegations} d WHERE d.`user_id` = ?) GROUP BY r.`zone` ORDER BY r.`zone`"), userID)
}
func (s recordStore) queryZones(ctx context.Context, query string, args ...any) ([]zoneSummary, error) {
rows, err := s.q.QueryContext(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("list zones: %w", err)
}
defer rows.Close()
var zones []zoneSummary
for rows.Next() {
var z zoneSummary
if err := rows.Scan(&z.Zone, &z.RecordCount, &z.SOAContent); err != nil {
return nil, fmt.Errorf("list zones: %w", err)
}
zones = append(zones, z)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("list zones: %w", err)
}
return zones, nil
}
func (s recordStore) ZoneExists(ctx context.Context, zone string) (bool, error) {
var one int
err := s.q.QueryRowContext(ctx, s.t.sql("SELECT 1 FROM {records} WHERE `zone` = ? LIMIT 1"), zone).Scan(&one)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("zone exists: %w", err)
}
return true, nil
}
func (s recordStore) ZoneRows(ctx context.Context, zone string) ([]recordRow, error) {
return s.queryRecords(ctx, s.t.sql("SELECT "+recordColumns+" FROM {records} WHERE `zone` = ? ORDER BY `id`"), zone)
}
func (s recordStore) ZoneRowsForUpdate(ctx context.Context, zone string) ([]recordRow, error) {
return s.queryRecords(ctx, s.t.sql("SELECT "+recordColumns+" FROM {records} WHERE `zone` = ? ORDER BY `id` FOR UPDATE"), zone)
}
func (s recordStore) queryRecords(ctx context.Context, query string, args ...any) ([]recordRow, error) {
rows, err := s.q.QueryContext(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("read records: %w", err)
}
defer rows.Close()
var records []recordRow
for rows.Next() {
var r recordRow
if err := rows.Scan(&r.ID, &r.Zone, &r.Name, &r.TTL, &r.Content, &r.RecordType); err != nil {
return nil, fmt.Errorf("read records: %w", err)
}
records = append(records, r)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("read records: %w", err)
}
return records, nil
}
func (s recordStore) RecordByID(ctx context.Context, id int64) (recordRow, error) {
return s.queryRecord(ctx, s.t.sql("SELECT "+recordColumns+" FROM {records} WHERE `id` = ?"), id)
}
func (s recordStore) RecordByIDForUpdate(ctx context.Context, id int64) (recordRow, error) {
return s.queryRecord(ctx, s.t.sql("SELECT "+recordColumns+" FROM {records} WHERE `id` = ? FOR UPDATE"), id)
}
func (s recordStore) queryRecord(ctx context.Context, query string, id int64) (recordRow, error) {
var r recordRow
err := s.q.QueryRowContext(ctx, query, id).Scan(&r.ID, &r.Zone, &r.Name, &r.TTL, &r.Content, &r.RecordType)
if errors.Is(err, sql.ErrNoRows) {
return recordRow{}, errRowNotFound
}
if err != nil {
return recordRow{}, fmt.Errorf("read record: %w", err)
}
return r, nil
}
func (s recordStore) InsertRecord(ctx context.Context, r recordRow) (int64, error) {
res, err := s.q.ExecContext(ctx,
s.t.sql("INSERT INTO {records} (`zone`, `name`, `ttl`, `content`, `record_type`) VALUES (?, ?, ?, ?, ?)"),
r.Zone, r.Name, r.TTL, r.Content, r.RecordType)
if err != nil {
return 0, fmt.Errorf("insert record: %w", err)
}
id, err := res.LastInsertId()
if err != nil {
return 0, fmt.Errorf("insert record: %w", err)
}
return id, nil
}
func (s recordStore) UpdateRecord(ctx context.Context, r recordRow) error {
_, err := s.q.ExecContext(ctx,
s.t.sql("UPDATE {records} SET `zone` = ?, `name` = ?, `ttl` = ?, `content` = ?, `record_type` = ? WHERE `id` = ?"),
r.Zone, r.Name, r.TTL, r.Content, r.RecordType, r.ID)
if err != nil {
return fmt.Errorf("update record: %w", err)
}
return nil
}
func (s recordStore) DeleteRecord(ctx context.Context, id int64) (bool, error) {
res, err := s.q.ExecContext(ctx, s.t.sql("DELETE FROM {records} WHERE `id` = ?"), id)
if err != nil {
return false, fmt.Errorf("delete record: %w", err)
}
n, err := res.RowsAffected()
if err != nil {
return false, fmt.Errorf("delete record: %w", err)
}
return n > 0, nil
}
func (s recordStore) DeleteZoneRecords(ctx context.Context, zone string) (int64, error) {
res, err := s.q.ExecContext(ctx, s.t.sql("DELETE FROM {records} WHERE `zone` = ?"), zone)
if err != nil {
return 0, fmt.Errorf("delete zone records: %w", err)
}
n, err := res.RowsAffected()
if err != nil {
return 0, fmt.Errorf("delete zone records: %w", err)
}
return n, nil
}