-- name: InsertIntoDomain64 :one
INSERT INTO domain64 
  (rfqdn, d64) 
  VALUES 
  (?, ?)
  RETURNING d64
;

-- If you forget a semicolon, the previous
-- query gets flagged as a duplicate.
-- https://github.com/sqlc-dev/sqlc/issues/3851

-- name: GetTLDId :one
SELECT tld_id FROM tlds
  WHERE tld = ?
;

-- name: CountTLDs :one
SELECT COUNT(*) FROM tlds;

-- name: InsertTLD :exec
INSERT INTO tlds
  (tld_id, tld)
  VALUES
  (?, ?)
;

-- name: GetDomainId :one
SELECT domain_id FROM domains
  WHERE 
    tld_id = ? 
    AND 
    domain = ?
;

-- name: CountDomains :one
SELECT COUNT(DISTINCT domain_id) 
  FROM domains
  WHERE
    tld_id = ?
;

-- name: InsertDomain :exec
INSERT INTO domains 
  (tld_id, domain_id, domain)
  VALUES
  (?, ?, ?)
;

-- name: GetSubdomainId :one
SELECT subdomain_id FROM subdomains
  WHERE 
    tld_id = ?
    AND
    domain_id = ?
    AND
    subdomain = ?
;

-- name: CountSubdomains :one
SELECT COUNT(DISTINCT subdomain_id) 
  FROM subdomains
  WHERE
    tld_id = ?
    AND
    domain_id = ?
;

-- name: InsertSubdomain :exec
INSERT INTO subdomains
  (tld_id, domain_id, subdomain_id, subdomain)
  VALUES
  (?, ?, ?, ?)
;

-- name: GetPathId :one
SELECT path_id FROM paths
  WHERE 
    tld_id = ?
    AND
    domain_id = ?
    AND
    subdomain_id = ?
    AND
    path = ?
;

-- name: CountPaths :one
SELECT COUNT(DISTINCT path_id) 
  FROM paths
  WHERE
    tld_id = ?
    AND
    domain_id = ?
    AND
    subdomain_id = ?
;

-- name: InsertPath :exec
INSERT INTO paths
  (tld_id, domain_id, subdomain_id, path_id, path)
  VALUES
  (?, ?, ?, ?, ?)
;

-- -- name: CountTLD :one
-- SELECT COUNT(DISTINCT tld_id) FROM domain64 WHERE tld_id = ?;