basic queries
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
{{/*
|
||||
This is the list of all the fields in *entity.Entity, in a way that is
|
||||
suitable to be imported by other templates that need to select these fields
|
||||
from either the "entity" or the "entity_history" tables.
|
||||
|
||||
Example usage:
|
||||
|
||||
SELECT {{ template "common_entity_select_into" . }}
|
||||
FROM {{ .Ident "entity" }} AS e
|
||||
|
||||
*/}}
|
||||
{{ define "common_entity_select_into" }}
|
||||
|
||||
e.{{ .Ident "guid" | .Into .Entity.Guid }},
|
||||
e.{{ .Ident "resource_version" | .Into .Entity.ResourceVersion }},
|
||||
|
||||
e.{{ .Ident "key" | .Into .Entity.Key }},
|
||||
|
||||
e.{{ .Ident "group" | .Into .Entity.Group }},
|
||||
e.{{ .Ident "group_version" | .Into .Entity.GroupVersion }},
|
||||
e.{{ .Ident "resource" | .Into .Entity.Resource }},
|
||||
e.{{ .Ident "namespace" | .Into .Entity.Namespace }},
|
||||
e.{{ .Ident "name" | .Into .Entity.Name }},
|
||||
|
||||
e.{{ .Ident "folder" | .Into .Entity.Folder }},
|
||||
|
||||
e.{{ .Ident "meta" | .Into .Entity.Meta }},
|
||||
e.{{ .Ident "body" | .Into .Entity.Body }},
|
||||
e.{{ .Ident "status" | .Into .Entity.Status }},
|
||||
|
||||
e.{{ .Ident "size" | .Into .Entity.Size }},
|
||||
e.{{ .Ident "etag" | .Into .Entity.ETag }},
|
||||
|
||||
e.{{ .Ident "created_at" | .Into .Entity.CreatedAt }},
|
||||
e.{{ .Ident "created_by" | .Into .Entity.CreatedBy }},
|
||||
e.{{ .Ident "updated_at" | .Into .Entity.UpdatedAt }},
|
||||
e.{{ .Ident "updated_by" | .Into .Entity.UpdatedBy }},
|
||||
|
||||
e.{{ .Ident "origin" | .Into .Entity.Origin.Source }},
|
||||
e.{{ .Ident "origin_key" | .Into .Entity.Origin.Key }},
|
||||
e.{{ .Ident "origin_ts" | .Into .Entity.Origin.Time }},
|
||||
|
||||
e.{{ .Ident "title" | .Into .Entity.Title }},
|
||||
e.{{ .Ident "slug" | .Into .Entity.Slug }},
|
||||
e.{{ .Ident "description" | .Into .Entity.Description }},
|
||||
|
||||
e.{{ .Ident "message" | .Into .Entity.Message }},
|
||||
e.{{ .Ident "labels" | .Into .Entity.Labels }},
|
||||
e.{{ .Ident "fields" | .Into .Entity.Fields }},
|
||||
e.{{ .Ident "errors" | .Into .Entity.Errors }},
|
||||
|
||||
e.{{ .Ident "action" | .Into .Entity.Action }}
|
||||
{{ end }}
|
||||
|
||||
{{/* Build an ORDER BY clause from a []SortBy contained in a .Sort field */}}
|
||||
{{ define "common_order_by" }}
|
||||
{{ $comma := listSep ", " }}
|
||||
{{ range .Sort }}
|
||||
{{- call $comma -}} {{ $.Ident .Field }} {{ .Direction.String }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
@@ -0,0 +1,37 @@
|
||||
SELECT
|
||||
{{ .Ident "rv" | .Into .Resource.Version }},
|
||||
{{ .Ident "value" | .Into .Resource.Value }},
|
||||
{{ .Ident "blob" | .Into .Resource.Blob }},
|
||||
|
||||
FROM "resource"
|
||||
|
||||
WHERE 1 = 1
|
||||
AND {{ .Ident "namespace" }} = {{ .Arg .Key.Namespace }}
|
||||
AND {{ .Ident "group" }} = {{ .Arg .Key.Group }}
|
||||
AND {{ .Ident "resource" }} = {{ .Arg .Key.Resource }}
|
||||
AND {{ .Ident "name" }} = {{ .Arg .Key.Name }}
|
||||
|
||||
{{/*
|
||||
Resource versions work like snapshots at the kind level. Thus, a request
|
||||
to retrieve a specific resource version should be interpreted as asking
|
||||
for a resource as of how it existed at that point in time. This is why we
|
||||
request matching entities with at most the provided resource version, and
|
||||
return only the one with the highest resource version. In the case of not
|
||||
specifying a resource version (i.e. resource version zero), it is
|
||||
interpreted as the latest version of the given entity, thus we instead
|
||||
query the "entity" table (which holds only the latest version of
|
||||
non-deleted entities) and we don't need to specify anything else. The
|
||||
"entity" table has a unique constraint on (namespace, group, resource,
|
||||
name), so we're guaranteed to have at most one matching row.
|
||||
*/}}
|
||||
{{ if gt .ResourceVersion 0 }}
|
||||
AND {{ .Ident "rv" }} <= {{ .Arg .ResourceVersion }}
|
||||
|
||||
ORDER BY {{ .Ident "rv" }} DESC
|
||||
LIMIT 1
|
||||
{{ end }}
|
||||
|
||||
{{ if .SelectForUpdate }}
|
||||
{{ .SelectFor "UPDATE NOWAIT" }}
|
||||
{{ end }}
|
||||
;
|
||||
@@ -0,0 +1,31 @@
|
||||
INSERT INTO "resource"
|
||||
{{/* Explicitly specify fields that will be set */}}
|
||||
(
|
||||
{{ .Ident "event" }},
|
||||
{{ .Ident "group" }},
|
||||
{{ .Ident "api_version" }},
|
||||
{{ .Ident "namespace" }},
|
||||
{{ .Ident "resource" }},
|
||||
{{ .Ident "name" }},
|
||||
{{ .Ident "operation" }},
|
||||
{{ .Ident "message" }},
|
||||
{{ .Ident "value" }},
|
||||
{{ .Ident "hash" }},
|
||||
{{ .Ident "blob" }},
|
||||
)
|
||||
|
||||
{{/* Provide the values */}}
|
||||
VALUES (
|
||||
{{ .Arg .Event.ID }},
|
||||
{{ .Arg .Event.Group }},
|
||||
{{ .Arg .Event.ApiVersion }},
|
||||
{{ .Arg .Event.Namespace }},
|
||||
{{ .Arg .Event.Resource }},
|
||||
{{ .Arg .Event.Name }},
|
||||
{{ .Arg .Event.Operation }},
|
||||
{{ .Arg .Event.Message }},
|
||||
{{ .Arg .Event.Value }},
|
||||
{{ .Arg .Event.Hash }},
|
||||
{{ .Arg .Event.Blob }},
|
||||
)
|
||||
;
|
||||
@@ -0,0 +1,8 @@
|
||||
SELECT
|
||||
{{ .Ident "rv" | .Into .ResourceVersion }}
|
||||
|
||||
FROM {{ .Ident "resource_version" }}
|
||||
WHERE 1 = 1
|
||||
AND {{ .Ident "group" }} = {{ .Arg .Group }}
|
||||
AND {{ .Ident "resource" }} = {{ .Arg .Resource }}
|
||||
;
|
||||
@@ -0,0 +1,9 @@
|
||||
UPDATE {{ .Ident "resource_version" }}
|
||||
SET
|
||||
{{ .Ident "rv" }} = {{ .Arg .ResourceVersion }} + 1,
|
||||
|
||||
WHERE 1 = 1
|
||||
AND {{ .Ident "group" }} = {{ .Arg .Group }}
|
||||
AND {{ .Ident "resource" }} = {{ .Arg .Resource }}
|
||||
AND {{ .Ident "rv" }} = {{ .Arg .ResourceVersion }}
|
||||
;
|
||||
@@ -0,0 +1,13 @@
|
||||
INSERT INTO {{ .Ident "resource_version" }}
|
||||
(
|
||||
{{ .Ident "group" }},
|
||||
{{ .Ident "resource" }},
|
||||
{{ .Ident "rv" }},
|
||||
)
|
||||
|
||||
VALUES (
|
||||
{{ .Arg .Group }},
|
||||
{{ .Arg .Resource }},
|
||||
1,
|
||||
)
|
||||
;
|
||||
@@ -0,0 +1,7 @@
|
||||
SELECT {{ .Ident "rv" | .Into .ResourceVersion }}
|
||||
FROM {{ .Ident "resource_version" }}
|
||||
WHERE 1 = 1
|
||||
AND {{ .Ident "group" }} = {{ .Arg .Group }}
|
||||
AND {{ .Ident "resource" }} = {{ .Arg .Resource }}
|
||||
{{ .SelectFor "UPDATE" }}
|
||||
;
|
||||
Reference in New Issue
Block a user