3
0

Add support for RDBMS index creation

This commit is contained in:
Denis Arh
2022-09-06 19:51:29 +02:00
parent 5992471337
commit 4e96804d46
27 changed files with 732 additions and 107 deletions

View File

@@ -55,6 +55,31 @@ var (
},
{{ end -}}
},
Indexes: dal.IndexSet{
{{- range .indexes }}
&dal.Index{
Ident: {{ printf "%q" .ident }},
Type: {{ printf "%q" .type }},
{{ if .unique }}Unique: true,{{ end }}
{{ if .predicate }}Predicate: {{ printf "%q" .predicate }},{{ end }}
Fields: []*dal.IndexField{
{{- range .fields }}
{
AttributeIdent: {{ printf "%q" .attribute }},
{{- if .modifiers }}
Modifiers: []dal.IndexFieldModifier{ {{- range .modifiers }}{{ printf "%q" . }},{{- end }} },
{{- end }}
{{- if eq .sort "ASC" }}Sort: dal.IndexFieldSortAsc, {{ end }}
{{- if eq .sort "DESC" }}Sort: dal.IndexFieldSortDesc, {{ end }}
{{- if eq .nulls "FIRST" }}Nulls: dal.IndexFieldNullsFirst,{{ end }}
{{- if eq .nulls "LAST" }}Nulls: dal.IndexFieldNullsLast, {{ end }}
},
{{ end -}}
},
},
{{ end -}}
},
}
{{ end }}
)

View File

@@ -9,6 +9,10 @@ import (
attributes: {
[name=_]: {"name": name} & #ModelAttribute
}
indexes?: {
[name=_]: {"name": name} & #ModelIndex
}
}
#ModelAttributeDalType:
@@ -206,3 +210,49 @@ SortableTimestampNilField: {
jsonTag: "json:\"\(json.field)\(_omitEmpty)\(_string)\""
}
}
#ModelIndex: {
name: #ident
_attributes: { [_]: #ModelAttribute }
_words: strings.Replace(strings.Replace(name, "_", " ", -1), ".", " ", -1)
_ident: strings.ToCamel(strings.Replace(strings.ToTitle(_words), " ", "", -1))
// lowercase (unexported, golang) identifier
ident: #ident | *_ident
primary: bool | *(strings.ToLower(name) == "primary")
unique: bool | *(strings.Contains(name, "unique") || primary)
type: "BTREE" | *"BTREE"
attribute?: string
attributes?: [string, ...]
if attribute != _|_ {
attributes: [attribute]
}
fields: [#ModelIndexField, ...] | *([
if attributes != _|_ {
for a in attributes {
"attribute": a
#ModelIndexField
}
}
])
predicate?: string
}
#IndexFieldModifier: "LOWERCASE"
#ModelIndexField: {
attribute: string
modifiers?: [#IndexFieldModifier, ...]
length?: number
sort?: "DESC" | "ASC"
nulls?: "FIRST" | "LAST"
}

View File

@@ -21,11 +21,12 @@ import (
cmpIdent: cmp.ident
// Operation/resource validators, grouped by resource
models: [
for res in cmp.resources if res.model.attributes != _|_ {
for res in cmp.resources if (res.model.attributes != _|_) {
var: "\(res.expIdent)"
resType: "types.\(res.expIdent)ResourceType"
ident: res.model.ident
ident: res.model.ident
attributes: [
for attr in res.model.attributes {
attr
@@ -39,6 +40,39 @@ import (
}
}
]
if res.model.indexes != _|_ {
indexes: [
for index in res.model.indexes {
if index.primary {
ident: "PRIMARY",
}
if !index.primary {
ident: index.ident,
unique: index.unique,
}
type: index.type,
predicate?: index.predicate,
fields: [
for field in index.fields {
// craft a handy string that will yield a descriptive error
// when referencing an unexisting attribute
"model (\(res.model.ident)) index (\(index.ident)) field attribute reference (\(field.attribute)) validation":
res.model.attributes[field.attribute].ident
"attribute": res.model.attributes[field.attribute].expIdent
"modifiers"?: field.modifiers
"sort"?: field.sort
"nulls"?: field.nulls
},
]
}
]
}
},
]
}