115 lines
4.3 KiB
Go
115 lines
4.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
var ErrCityNameAmbiguous = errors.New("City name is ambiguous; qualify the Street by its Quartier or use uuid:")
|
|
|
|
type CityNodeAncestor struct {
|
|
UUID, Name, Title, PathPrefix string
|
|
}
|
|
|
|
// CityNode keeps the trusted Store API. HTTP callers propagate cancellation
|
|
// through CityNodeContext. A bare UUID-looking string remains a name; UUIDs
|
|
// require the explicit uuid: selector, so identity does not depend on spelling.
|
|
func (s *Store) CityNode(domainUUID, kind, id string) (CityNodeDetail, error) {
|
|
return s.CityNodeContext(context.Background(), domainUUID, kind, id)
|
|
}
|
|
|
|
// A Street reference can be a unique legacy name, uuid:<id>, or
|
|
// <Quartier-name>/<Street-name>. Qualified lookup and parent checks happen in
|
|
// the same SQL snapshot, without enumerating the City or opening nested cursors.
|
|
func (s *Store) CityNodeContext(parent context.Context, domainUUID, kind, id string) (detail CityNodeDetail, lookupErr error) {
|
|
defer func() { lookupErr = s.sqlError("city node", lookupErr) }()
|
|
id = strings.ToLower(strings.TrimSpace(id))
|
|
if kind != "quartier" && kind != "street" && kind != "spot" {
|
|
return detail, sql.ErrNoRows
|
|
}
|
|
if id == "" || len(id) > 256 || strings.ContainsAny(id, "\\\x00") {
|
|
return detail, sql.ErrNoRows
|
|
}
|
|
parts := strings.Split(id, "/")
|
|
if len(parts) > 1 && (kind != "street" || len(parts) != 2) {
|
|
return detail, sql.ErrNoRows
|
|
}
|
|
for _, part := range parts {
|
|
if part == "" || part == "." || part == ".." {
|
|
return detail, sql.ErrNoRows
|
|
}
|
|
}
|
|
ctx, cancel := context.WithTimeout(parent, cityReadTimeout)
|
|
defer cancel()
|
|
defer func() {
|
|
if lookupErr != nil && ctx.Err() != nil {
|
|
lookupErr = ctx.Err()
|
|
}
|
|
}()
|
|
from := "zones n"
|
|
columns := `n.uuid,n.domain_uuid,n.kind,COALESCE(n.parent_uuid,''),n.name,n.title,n.description,n.state,n.path_prefix,n.sort_key,n.version`
|
|
ancestors := `,'','','','','','','',''`
|
|
where := `n.domain_uuid=` + s.sqlParam(1) + ` AND n.state!='archived' AND n.kind=` + s.sqlParam(2)
|
|
args := []any{domainUUID, kind}
|
|
if kind == "street" {
|
|
from += ` JOIN zones q ON q.uuid=n.parent_uuid AND q.domain_uuid=n.domain_uuid AND q.kind='quartier' AND q.state!='archived'`
|
|
ancestors = `,q.uuid,q.name,q.title,q.path_prefix,'','','',''`
|
|
} else if kind == "spot" {
|
|
from = `spots n JOIN street_spots ss ON ss.spot_uuid=n.uuid
|
|
JOIN zones st ON st.uuid=ss.street_uuid AND st.domain_uuid=n.domain_uuid AND st.kind='street' AND st.state!='archived'
|
|
JOIN zones q ON q.uuid=st.parent_uuid AND q.domain_uuid=st.domain_uuid AND q.kind='quartier' AND q.state!='archived'`
|
|
columns = `n.uuid,n.domain_uuid,'spot',st.uuid,n.name,n.title,n.summary,n.state,n.path_prefix,ss.sort_key,n.version`
|
|
ancestors = `,q.uuid,q.name,q.title,q.path_prefix,st.uuid,st.name,st.title,st.path_prefix`
|
|
where = `n.domain_uuid=` + s.sqlParam(1) + ` AND n.state!='archived'`
|
|
args = []any{domainUUID}
|
|
}
|
|
if len(parts) == 2 {
|
|
// A qualified URL is name based. Explicit UUID selectors use the short
|
|
// /s/uuid:... route instead of mixing two different identity models.
|
|
where += " AND q.name=" + s.sqlParam(len(args)+1) + " AND n.name=" + s.sqlParam(len(args)+2)
|
|
args = append(args, parts[0], parts[1])
|
|
} else if strings.HasPrefix(id, "uuid:") {
|
|
where += " AND n.uuid=" + s.sqlParam(len(args)+1)
|
|
args = append(args, strings.TrimPrefix(id, "uuid:"))
|
|
} else {
|
|
where += " AND n.name=" + s.sqlParam(len(args)+1)
|
|
args = append(args, id)
|
|
}
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT `+columns+ancestors+` FROM `+from+` WHERE `+where+` LIMIT 2`, args...)
|
|
if err != nil {
|
|
return detail, err
|
|
}
|
|
defer rows.Close()
|
|
count := 0
|
|
for rows.Next() {
|
|
var node CityNodeDetail
|
|
var q, st CityNodeAncestor
|
|
if err := rows.Scan(&node.UUID, &node.DomainUUID, &node.Kind, &node.ParentUUID,
|
|
&node.Name, &node.Title, &node.Description, &node.State, &node.PathPrefix,
|
|
&node.SortKey, &node.Version, &q.UUID, &q.Name, &q.Title, &q.PathPrefix,
|
|
&st.UUID, &st.Name, &st.Title, &st.PathPrefix); err != nil {
|
|
return CityNodeDetail{}, err
|
|
}
|
|
count++
|
|
if count > 1 {
|
|
return CityNodeDetail{}, ErrCityNameAmbiguous
|
|
}
|
|
if q.UUID != "" {
|
|
node.Quartier = &q
|
|
}
|
|
if st.UUID != "" {
|
|
node.Street = &st
|
|
}
|
|
detail = node
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return CityNodeDetail{}, err
|
|
}
|
|
if count == 0 {
|
|
return CityNodeDetail{}, sql.ErrNoRows
|
|
}
|
|
return detail, nil
|
|
}
|