mydb/adapter/sqlite/template.go

188 lines
4.5 KiB
Go
Raw Permalink Normal View History

2023-09-18 15:15:42 +08:00
package sqlite
import (
"git.hexq.cn/tiglog/mydb/internal/cache"
"git.hexq.cn/tiglog/mydb/internal/sqladapter/exql"
)
const (
adapterColumnSeparator = `.`
adapterIdentifierSeparator = `, `
adapterIdentifierQuote = `"{{.Value}}"`
adapterValueSeparator = `, `
adapterValueQuote = `'{{.}}'`
adapterAndKeyword = `AND`
adapterOrKeyword = `OR`
adapterDescKeyword = `DESC`
adapterAscKeyword = `ASC`
adapterAssignmentOperator = `=`
adapterClauseGroup = `({{.}})`
adapterClauseOperator = ` {{.}} `
adapterColumnValue = `{{.Column}} {{.Operator}} {{.Value}}`
adapterTableAliasLayout = `{{.Name}}{{if .Alias}} AS {{.Alias}}{{end}}`
adapterColumnAliasLayout = `{{.Name}}{{if .Alias}} AS {{.Alias}}{{end}}`
adapterSortByColumnLayout = `{{.Column}} {{.Order}}`
adapterOrderByLayout = `
{{if .SortColumns}}
ORDER BY {{.SortColumns}}
{{end}}
`
adapterWhereLayout = `
{{if .Conds}}
WHERE {{.Conds}}
{{end}}
`
adapterUsingLayout = `
{{if .Columns}}
USING ({{.Columns}})
{{end}}
`
adapterJoinLayout = `
{{if .Table}}
{{ if .On }}
{{.Type}} JOIN {{.Table}}
{{.On}}
{{ else if .Using }}
{{.Type}} JOIN {{.Table}}
{{.Using}}
{{ else if .Type | eq "CROSS" }}
{{.Type}} JOIN {{.Table}}
{{else}}
NATURAL {{.Type}} JOIN {{.Table}}
{{end}}
{{end}}
`
adapterOnLayout = `
{{if .Conds}}
ON {{.Conds}}
{{end}}
`
adapterSelectLayout = `
SELECT
{{if .Distinct}}
DISTINCT
{{end}}
{{if defined .Columns}}
{{.Columns | compile}}
{{else}}
*
{{end}}
{{if defined .Table}}
FROM {{.Table | compile}}
{{end}}
{{.Joins | compile}}
{{.Where | compile}}
{{if defined .GroupBy}}
{{.GroupBy | compile}}
{{end}}
{{.OrderBy | compile}}
{{if .Limit}}
LIMIT {{.Limit}}
{{end}}
{{if .Offset}}
{{if not .Limit}}
LIMIT -1
{{end}}
OFFSET {{.Offset}}
{{end}}
`
adapterDeleteLayout = `
DELETE
FROM {{.Table | compile}}
{{.Where | compile}}
`
adapterUpdateLayout = `
UPDATE
{{.Table | compile}}
SET {{.ColumnValues | compile}}
{{.Where | compile}}
`
adapterSelectCountLayout = `
SELECT
COUNT(1) AS _t
FROM {{.Table | compile}}
{{.Where | compile}}
`
adapterInsertLayout = `
INSERT INTO {{.Table | compile}}
{{if .Columns }}({{.Columns | compile}}){{end}}
{{if defined .Values}}
VALUES
{{.Values | compile}}
{{else}}
DEFAULT VALUES
{{end}}
{{if defined .Returning}}
RETURNING {{.Returning | compile}}
{{end}}
`
adapterTruncateLayout = `
DELETE FROM {{.Table | compile}}
`
adapterDropDatabaseLayout = `
DROP DATABASE {{.Database | compile}}
`
adapterDropTableLayout = `
DROP TABLE {{.Table | compile}}
`
adapterGroupByLayout = `
{{if .GroupColumns}}
GROUP BY {{.GroupColumns}}
{{end}}
`
)
var template = &exql.Template{
ColumnSeparator: adapterColumnSeparator,
IdentifierSeparator: adapterIdentifierSeparator,
IdentifierQuote: adapterIdentifierQuote,
ValueSeparator: adapterValueSeparator,
ValueQuote: adapterValueQuote,
AndKeyword: adapterAndKeyword,
OrKeyword: adapterOrKeyword,
DescKeyword: adapterDescKeyword,
AscKeyword: adapterAscKeyword,
AssignmentOperator: adapterAssignmentOperator,
ClauseGroup: adapterClauseGroup,
ClauseOperator: adapterClauseOperator,
ColumnValue: adapterColumnValue,
TableAliasLayout: adapterTableAliasLayout,
ColumnAliasLayout: adapterColumnAliasLayout,
SortByColumnLayout: adapterSortByColumnLayout,
WhereLayout: adapterWhereLayout,
JoinLayout: adapterJoinLayout,
OnLayout: adapterOnLayout,
UsingLayout: adapterUsingLayout,
OrderByLayout: adapterOrderByLayout,
InsertLayout: adapterInsertLayout,
SelectLayout: adapterSelectLayout,
UpdateLayout: adapterUpdateLayout,
DeleteLayout: adapterDeleteLayout,
TruncateLayout: adapterTruncateLayout,
DropDatabaseLayout: adapterDropDatabaseLayout,
DropTableLayout: adapterDropTableLayout,
CountLayout: adapterSelectCountLayout,
GroupByLayout: adapterGroupByLayout,
Cache: cache.NewCache(),
}