This commit is contained in:
40
db/query/entities.sql
Normal file
40
db/query/entities.sql
Normal file
@@ -0,0 +1,40 @@
|
||||
-- name: CreateEntity :one
|
||||
INSERT INTO entities (
|
||||
name, description, thumbnail_url
|
||||
) VALUES (
|
||||
$1, $2, $3
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
|
||||
-- name: GetEntityById :one
|
||||
SELECT *
|
||||
FROM entities
|
||||
WHERE id = $1 AND is_deleted = false;
|
||||
|
||||
|
||||
-- name: UpdateEntity :one
|
||||
UPDATE entities
|
||||
SET
|
||||
name = COALESCE(sqlc.narg('name'), name),
|
||||
description = COALESCE(sqlc.narg('description'), description),
|
||||
thumbnail_url = COALESCE(sqlc.narg('thumbnail_url'), thumbnail_url)
|
||||
WHERE id = sqlc.arg('id') AND is_deleted = false
|
||||
RETURNING *;
|
||||
|
||||
|
||||
-- name: DeleteEntity :exec
|
||||
UPDATE entities
|
||||
SET
|
||||
is_deleted = true
|
||||
WHERE id = $1;
|
||||
|
||||
|
||||
-- name: SearchEntities :many
|
||||
SELECT *
|
||||
FROM entities
|
||||
WHERE is_deleted = false
|
||||
AND name ILIKE '%' || sqlc.arg('name')::text || '%'
|
||||
AND (sqlc.narg('cursor_id')::uuid IS NULL OR id < sqlc.narg('cursor_id')::uuid)
|
||||
ORDER BY id DESC
|
||||
LIMIT sqlc.arg('limit_count');
|
||||
90
db/query/geometries.sql
Normal file
90
db/query/geometries.sql
Normal file
@@ -0,0 +1,90 @@
|
||||
-- name: CreateGeometry :one
|
||||
INSERT INTO geometries (
|
||||
geo_type, draw_geometry, binding, time_start, time_end, bbox
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, ST_MakeEnvelope(sqlc.arg('min_lng')::float8, sqlc.arg('min_lat')::float8, sqlc.arg('max_lng')::float8, sqlc.arg('max_lat')::float8, 4326)
|
||||
)
|
||||
RETURNING id, geo_type, draw_geometry, binding, time_start, time_end,
|
||||
ST_XMin(bbox)::float8 as min_lng, ST_YMin(bbox)::float8 as min_lat, ST_XMax(bbox)::float8 as max_lng, ST_YMax(bbox)::float8 as max_lat,
|
||||
is_deleted, created_at, updated_at;
|
||||
|
||||
-- name: GetGeometryById :one
|
||||
SELECT id, geo_type, draw_geometry, binding, time_start, time_end,
|
||||
ST_XMin(bbox)::float8 as min_lng, ST_YMin(bbox)::float8 as min_lat, ST_XMax(bbox)::float8 as max_lng, ST_YMax(bbox)::float8 as max_lat,
|
||||
is_deleted, created_at, updated_at
|
||||
FROM geometries
|
||||
WHERE id = $1 AND is_deleted = false;
|
||||
|
||||
-- name: UpdateGeometry :one
|
||||
UPDATE geometries
|
||||
SET
|
||||
geo_type = COALESCE(sqlc.narg('geo_type'), geo_type),
|
||||
draw_geometry = COALESCE(sqlc.narg('draw_geometry'), draw_geometry),
|
||||
binding = COALESCE(sqlc.narg('binding'), binding),
|
||||
time_start = COALESCE(sqlc.narg('time_start'), time_start),
|
||||
time_end = COALESCE(sqlc.narg('time_end'), time_end),
|
||||
bbox = CASE
|
||||
WHEN sqlc.narg('update_bbox')::boolean = true THEN
|
||||
ST_MakeEnvelope(sqlc.narg('min_lng')::float8, sqlc.narg('min_lat')::float8, sqlc.narg('max_lng')::float8, sqlc.narg('max_lat')::float8, 4326)
|
||||
ELSE bbox
|
||||
END,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg('id') AND is_deleted = false
|
||||
RETURNING id, geo_type, draw_geometry, binding, time_start, time_end,
|
||||
ST_XMin(bbox)::float8 as min_lng, ST_YMin(bbox)::float8 as min_lat, ST_XMax(bbox)::float8 as max_lng, ST_YMax(bbox)::float8 as max_lat,
|
||||
is_deleted, created_at, updated_at;
|
||||
|
||||
-- name: DeleteGeometry :exec
|
||||
UPDATE geometries
|
||||
SET
|
||||
is_deleted = true
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: SearchGeometries :many
|
||||
SELECT
|
||||
g.id, g.geo_type, g.draw_geometry, g.binding, g.time_start, g.time_end,
|
||||
ST_XMin(g.bbox)::float8 as min_lng,
|
||||
ST_YMin(g.bbox)::float8 as min_lat,
|
||||
ST_XMax(g.bbox)::float8 as max_lng,
|
||||
ST_YMax(g.bbox)::float8 as max_lat,
|
||||
g.is_deleted, g.created_at, g.updated_at
|
||||
FROM geometries g
|
||||
WHERE g.is_deleted = false
|
||||
AND (
|
||||
sqlc.narg('search_min_lng')::float8 IS NULL OR
|
||||
sqlc.narg('search_min_lat')::float8 IS NULL OR
|
||||
sqlc.narg('search_max_lng')::float8 IS NULL OR
|
||||
sqlc.narg('search_max_lat')::float8 IS NULL OR
|
||||
g.bbox && ST_MakeEnvelope(
|
||||
sqlc.narg('search_min_lng')::float8,
|
||||
sqlc.narg('search_min_lat')::float8,
|
||||
sqlc.narg('search_max_lng')::float8,
|
||||
sqlc.narg('search_max_lat')::float8,
|
||||
4326
|
||||
)
|
||||
)
|
||||
AND (
|
||||
sqlc.narg('time_point')::int IS NULL OR
|
||||
(g.time_start <= sqlc.narg('time_point')::int AND g.time_end >= sqlc.narg('time_point')::int)
|
||||
)
|
||||
AND (
|
||||
sqlc.narg('entity_id')::uuid IS NULL OR
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM entity_geometries eg
|
||||
WHERE eg.geometry_id = g.id
|
||||
AND eg.entity_id = sqlc.narg('entity_id')::uuid
|
||||
)
|
||||
)
|
||||
ORDER BY g.id DESC;
|
||||
|
||||
-- name: BulkDeleteEntityGeometriesByEntityId :many
|
||||
DELETE FROM entity_geometries
|
||||
WHERE entity_id = $1
|
||||
RETURNING geometry_id;
|
||||
|
||||
-- name: CreateEntityGeometries :exec
|
||||
INSERT INTO entity_geometries (
|
||||
entity_id, geometry_id
|
||||
)
|
||||
SELECT $1, unnest(@geometry_ids::uuid[]);
|
||||
56
db/query/wiki.sql
Normal file
56
db/query/wiki.sql
Normal file
@@ -0,0 +1,56 @@
|
||||
-- name: CreateWiki :one
|
||||
INSERT INTO wikis (
|
||||
title, content
|
||||
) VALUES (
|
||||
$1, $2
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetWikiById :one
|
||||
SELECT *
|
||||
FROM wikis
|
||||
WHERE id = $1 AND is_deleted = false;
|
||||
|
||||
-- name: UpdateWiki :one
|
||||
UPDATE wikis
|
||||
SET
|
||||
title = COALESCE(sqlc.narg('title'), title),
|
||||
content = COALESCE(sqlc.narg('content'), content)
|
||||
WHERE id = sqlc.arg('id') AND is_deleted = false
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteWiki :exec
|
||||
UPDATE wikis
|
||||
SET
|
||||
is_deleted = true
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: SearchWikis :many
|
||||
SELECT w.*
|
||||
FROM wikis w
|
||||
WHERE w.is_deleted = false
|
||||
AND w.title ILIKE '%' || sqlc.arg('title')::text || '%'
|
||||
AND (
|
||||
sqlc.narg('entity_id')::uuid IS NULL OR
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM entity_wikis ew
|
||||
WHERE ew.wiki_id = w.id
|
||||
AND ew.entity_id = sqlc.narg('entity_id')::uuid
|
||||
)
|
||||
)
|
||||
AND (sqlc.narg('cursor_id')::uuid IS NULL OR w.id < sqlc.narg('cursor_id')::uuid)
|
||||
|
||||
ORDER BY w.id DESC
|
||||
LIMIT sqlc.arg('limit_count');
|
||||
|
||||
-- name: BulkDeleteEntityWikisByEntityId :many
|
||||
DELETE FROM entity_wikis
|
||||
WHERE entity_id = $1
|
||||
RETURNING wiki_id;
|
||||
|
||||
-- name: CreateEntityWikis :exec
|
||||
INSERT INTO entity_wikis (
|
||||
entity_id, wiki_id
|
||||
)
|
||||
SELECT $1, unnest(@wiki_ids::uuid[]);
|
||||
Reference in New Issue
Block a user