remove old database templates

This commit is contained in:
paul 2020-07-21 22:55:17 +02:00
parent 6d25a0928f
commit 9528767a5d
9 changed files with 0 additions and 510 deletions

View file

@ -1,62 +0,0 @@
{{- $type := .Name -}}
{{- $short := (shortname $type "enumVal" "text" "buf" "ok" "src") -}}
{{- $reverseNames := .ReverseConstNames -}}
// {{ $type }} is the '{{ .Enum.EnumName }}' enum type from schema '{{ .Schema }}'.
type {{ $type }} uint16
const (
{{- range .Values }}
// {{ if $reverseNames }}{{ .Name }}{{ $type }}{{ else }}{{ $type }}{{ .Name }}{{ end }} is the '{{ .Val.EnumValue }}' {{ $type }}.
{{ if $reverseNames }}{{ .Name }}{{ $type }}{{ else }}{{ $type }}{{ .Name }}{{ end }} = {{ $type }}({{ .Val.ConstValue }})
{{ end -}}
)
// String returns the string value of the {{ $type }}.
func ({{ $short }} {{ $type }}) String() string {
var enumVal string
switch {{ $short }} {
{{- range .Values }}
case {{ if $reverseNames }}{{ .Name }}{{ $type }}{{ else }}{{ $type }}{{ .Name }}{{ end }}:
enumVal = "{{ .Val.EnumValue }}"
{{ end -}}
}
return enumVal
}
// MarshalText marshals {{ $type }} into text.
func ({{ $short }} {{ $type }}) MarshalText() ([]byte, error) {
return []byte({{ $short }}.String()), nil
}
// UnmarshalText unmarshals {{ $type }} from text.
func ({{ $short }} *{{ $type }}) UnmarshalText(text []byte) error {
switch string(text) {
{{- range .Values }}
case "{{ .Val.EnumValue }}":
*{{ $short }} = {{ if $reverseNames }}{{ .Name }}{{ $type }}{{ else }}{{ $type }}{{ .Name }}{{ end }}
{{ end }}
default:
return errors.New("invalid {{ $type }}")
}
return nil
}
// Value satisfies the sql/driver.Valuer interface for {{ $type }}.
func ({{ $short }} {{ $type }}) Value() (driver.Value, error) {
return {{ $short }}.String(), nil
}
// Scan satisfies the database/sql.Scanner interface for {{ $type }}.
func ({{ $short }} *{{ $type }}) Scan(src interface{}) error {
buf, ok := src.([]byte)
if !ok {
return errors.New("invalid {{ $type }}")
}
return {{ $short }}.UnmarshalText(buf)
}

View file

@ -1,8 +0,0 @@
{{- $short := (shortname .Type.Name) -}}
// {{ .Name }} returns the {{ .RefType.Name }} associated with the {{ .Type.Name }}'s {{ .Field.Name }} ({{ .Field.Col.ColumnName }}).
//
// Generated from foreign key '{{ .ForeignKey.ForeignKeyName }}'.
func ({{ $short }} *{{ .Type.Name }}) {{ .Name }}(db XODB) (*{{ .RefType.Name }}, error) {
return {{ .RefType.Name }}By{{ .RefField.Name }}(db, {{ convext $short .Field .RefField }})
}

View file

@ -1,58 +0,0 @@
{{- $short := (shortname .Type.Name "err" "sqlstr" "db" "q" "res" "XOLog" .Fields) -}}
{{- $table := (schema .Schema .Type.Table.TableName) -}}
// {{ .FuncName }} retrieves a row from '{{ $table }}' as a {{ .Type.Name }}.
//
// Generated from index '{{ .Index.IndexName }}'.
func {{ .FuncName }}(db XODB{{ goparamlist .Fields true true }}) ({{ if not .Index.IsUnique }}[]{{ end }}*{{ .Type.Name }}, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`{{ colnames .Type.Fields }} ` +
`FROM {{ $table }} ` +
`WHERE {{ colnamesquery .Fields " AND " }}`
// run query
XOLog(sqlstr{{ goparamlist .Fields true false }})
{{- if .Index.IsUnique }}
{{ $short }} := {{ .Type.Name }}{
{{- if .Type.PrimaryKey }}
_exists: true,
{{ end -}}
}
err = db.QueryRow(sqlstr{{ goparamlist .Fields true false }}).Scan({{ fieldnames .Type.Fields (print "&" $short) }})
if err != nil {
return nil, err
}
return &{{ $short }}, nil
{{- else }}
q, err := db.Query(sqlstr{{ goparamlist .Fields true false }})
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*{{ .Type.Name }}{}
for q.Next() {
{{ $short }} := {{ .Type.Name }}{
{{- if .Type.PrimaryKey }}
_exists: true,
{{ end -}}
}
// scan
err = q.Scan({{ fieldnames .Type.Fields (print "&" $short) }})
if err != nil {
return nil, err
}
res = append(res, &{{ $short }})
}
return res, nil
{{- end }}
}

View file

@ -1,28 +0,0 @@
{{- $notVoid := (ne .Proc.ReturnType "void") -}}
{{- $proc := (schema .Schema .Proc.ProcName) -}}
{{- if ne .Proc.ReturnType "trigger" -}}
// {{ .Name }} calls the stored procedure '{{ $proc }}({{ .ProcParams }}) {{ .Proc.ReturnType }}' on db.
func {{ .Name }}(db XODB{{ goparamlist .Params true true }}) ({{ if $notVoid }}{{ retype .Return.Type }}, {{ end }}error) {
var err error
// sql query
const sqlstr = `SELECT {{ $proc }}({{ colvals .Params }})`
// run query
{{- if $notVoid }}
var ret {{ retype .Return.Type }}
XOLog(sqlstr{{ goparamlist .Params true false }})
err = db.QueryRow(sqlstr{{ goparamlist .Params true false }}).Scan(&ret)
if err != nil {
return {{ reniltype .Return.NilType }}, err
}
return ret, nil
{{- else }}
XOLog(sqlstr)
_, err = db.Exec(sqlstr)
return err
{{- end }}
}
{{- end }}

View file

@ -1,49 +0,0 @@
{{- $short := (shortname .Type.Name "err" "sqlstr" "db" "q" "res" "XOLog" .QueryParams) -}}
{{- $queryComments := .QueryComments -}}
{{- if .Comment -}}
// {{ .Comment }}
{{- else -}}
// {{ .Name }} runs a custom query, returning results as {{ .Type.Name }}.
{{- end }}
func {{ .Name }} (db XODB{{ range .QueryParams }}, {{ .Name }} {{ .Type }}{{ end }}) ({{ if not .OnlyOne }}[]{{ end }}*{{ .Type.Name }}, error) {
var err error
// sql query
{{ if .Interpolate }}var{{ else }}const{{ end }} sqlstr = {{ range $i, $l := .Query }}{{ if $i }} +{{ end }}{{ if (index $queryComments $i) }} // {{ index $queryComments $i }}{{ end }}{{ if $i }}
{{end -}}`{{ $l }}`{{ end }}
// run query
XOLog(sqlstr{{ range .QueryParams }}{{ if not .Interpolate }}, {{ .Name }}{{ end }}{{ end }})
{{- if .OnlyOne }}
var {{ $short }} {{ .Type.Name }}
err = db.QueryRow(sqlstr{{ range .QueryParams }}, {{ .Name }}{{ end }}).Scan({{ fieldnames .Type.Fields (print "&" $short) }})
if err != nil {
return nil, err
}
return &{{ $short }}, nil
{{- else }}
q, err := db.Query(sqlstr{{ range .QueryParams }}, {{ .Name }}{{ end }})
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*{{ .Type.Name }}{}
for q.Next() {
{{ $short }} := {{ .Type.Name }}{}
// scan
err = q.Scan({{ fieldnames .Type.Fields (print "&" $short) }})
if err != nil {
return nil, err
}
res = append(res, &{{ $short }})
}
return res, nil
{{- end }}
}

View file

@ -1,12 +0,0 @@
{{- $table := (schema .Schema .Table.TableName) -}}
{{- if .Comment -}}
// {{ .Comment }}
{{- else -}}
// {{ .Name }} represents a row from '{{ $table }}'.
{{- end }}
type {{ .Name }} struct {
{{- range .Fields }}
{{ .Name }} {{ retype .Type }} // {{ .Col.ColumnName }}
{{- end }}
}

View file

@ -1,206 +0,0 @@
{{- $short := (shortname .Name "err" "res" "sqlstr" "db" "XOLog") -}}
{{- $table := (schema .Schema .Table.TableName) -}}
{{- if .Comment -}}
// {{ .Comment }}
{{- else -}}
// {{ .Name }} represents a row from '{{ $table }}'.
{{- end }}
type {{ .Name }} struct {
{{- range .Fields }}
{{ .Name }} {{ retype .Type }} `db:"{{ .Col.ColumnName }}"` // {{ .Col.ColumnName }}
{{- end }}
{{- if .PrimaryKey }}
// xo fields
_exists, _deleted bool
{{ end }}
}
{{ if .PrimaryKey }}
// Exists determines if the {{ .Name }} exists in the database.
func ({{ $short }} *{{ .Name }}) Exists() bool {
return {{ $short }}._exists
}
// Deleted provides information if the {{ .Name }} has been deleted from the database.
func ({{ $short }} *{{ .Name }}) Deleted() bool {
return {{ $short }}._deleted
}
// Insert inserts the {{ .Name }} to the database.
func ({{ $short }} *{{ .Name }}) Insert(db XODB) error {
var err error
// if already exist, bail
if {{ $short }}._exists {
return errors.New("insert failed: already exists")
}
{{ if .Table.ManualPk }}
// sql insert query, primary key must be provided
const sqlstr = `INSERT INTO {{ $table }} (` +
`{{ colnames .Fields }}` +
`) VALUES (` +
`{{ colvals .Fields }}` +
`)`
// run query
XOLog(sqlstr, {{ fieldnames .Fields $short }})
_, err = db.Exec(sqlstr, {{ fieldnames .Fields $short }})
if err != nil {
return err
}
{{ else }}
// sql insert query, primary key provided by sequence
const sqlstr = `INSERT INTO {{ $table }} (` +
`{{ colnames .Fields .PrimaryKey.Name }}` +
`) VALUES (` +
`{{ colvals .Fields .PrimaryKey.Name }}` +
`) RETURNING {{ colname .PrimaryKey.Col }}`
// run query
XOLog(sqlstr, {{ fieldnames .Fields $short .PrimaryKey.Name }})
err = db.QueryRow(sqlstr, {{ fieldnames .Fields $short .PrimaryKey.Name }}).Scan(&{{ $short }}.{{ .PrimaryKey.Name }})
if err != nil {
return err
}
{{ end }}
// set existence
{{ $short }}._exists = true
return nil
}
{{ if ne (fieldnamesmulti .Fields $short .PrimaryKeyFields) "" }}
// Update updates the {{ .Name }} in the database.
func ({{ $short }} *{{ .Name }}) Update(db XODB) error {
var err error
// if doesn't exist, bail
if !{{ $short }}._exists {
return errors.New("update failed: does not exist")
}
// if deleted, bail
if {{ $short }}._deleted {
return errors.New("update failed: marked for deletion")
}
{{ if gt ( len .PrimaryKeyFields ) 1 }}
// sql query with composite primary key
const sqlstr = `UPDATE {{ $table }} SET (` +
`{{ colnamesmulti .Fields .PrimaryKeyFields }}` +
`) = ( ` +
`{{ colvalsmulti .Fields .PrimaryKeyFields }}` +
`) WHERE {{ colnamesquerymulti .PrimaryKeyFields " AND " (getstartcount .Fields .PrimaryKeyFields) nil }}`
// run query
XOLog(sqlstr, {{ fieldnamesmulti .Fields $short .PrimaryKeyFields }}, {{ fieldnames .PrimaryKeyFields $short}})
_, err = db.Exec(sqlstr, {{ fieldnamesmulti .Fields $short .PrimaryKeyFields }}, {{ fieldnames .PrimaryKeyFields $short}})
return err
{{- else }}
// sql query
const sqlstr = `UPDATE {{ $table }} SET (` +
`{{ colnames .Fields .PrimaryKey.Name }}` +
`) = ( ` +
`{{ colvals .Fields .PrimaryKey.Name }}` +
`) WHERE {{ colname .PrimaryKey.Col }} = ${{ colcount .Fields .PrimaryKey.Name }}`
// run query
XOLog(sqlstr, {{ fieldnames .Fields $short .PrimaryKey.Name }}, {{ $short }}.{{ .PrimaryKey.Name }})
_, err = db.Exec(sqlstr, {{ fieldnames .Fields $short .PrimaryKey.Name }}, {{ $short }}.{{ .PrimaryKey.Name }})
return err
{{- end }}
}
// Save saves the {{ .Name }} to the database.
func ({{ $short }} *{{ .Name }}) Save(db XODB) error {
if {{ $short }}.Exists() {
return {{ $short }}.Update(db)
}
return {{ $short }}.Insert(db)
}
// Upsert performs an upsert for {{ .Name }}.
//
// NOTE: PostgreSQL 9.5+ only
func ({{ $short }} *{{ .Name }}) Upsert(db XODB) error {
var err error
// if already exist, bail
if {{ $short }}._exists {
return errors.New("insert failed: already exists")
}
// sql query
const sqlstr = `INSERT INTO {{ $table }} (` +
`{{ colnames .Fields }}` +
`) VALUES (` +
`{{ colvals .Fields }}` +
`) ON CONFLICT ({{ colnames .PrimaryKeyFields }}) DO UPDATE SET (` +
`{{ colnames .Fields }}` +
`) = (` +
`{{ colprefixnames .Fields "EXCLUDED" }}` +
`)`
// run query
XOLog(sqlstr, {{ fieldnames .Fields $short }})
_, err = db.Exec(sqlstr, {{ fieldnames .Fields $short }})
if err != nil {
return err
}
// set existence
{{ $short }}._exists = true
return nil
}
{{ else }}
// Update statements omitted due to lack of fields other than primary key
{{ end }}
// Delete deletes the {{ .Name }} from the database.
func ({{ $short }} *{{ .Name }}) Delete(db XODB) error {
var err error
// if doesn't exist, bail
if !{{ $short }}._exists {
return nil
}
// if deleted, bail
if {{ $short }}._deleted {
return nil
}
{{ if gt ( len .PrimaryKeyFields ) 1 }}
// sql query with composite primary key
const sqlstr = `DELETE FROM {{ $table }} WHERE {{ colnamesquery .PrimaryKeyFields " AND " }}`
// run query
XOLog(sqlstr, {{ fieldnames .PrimaryKeyFields $short }})
_, err = db.Exec(sqlstr, {{ fieldnames .PrimaryKeyFields $short }})
if err != nil {
return err
}
{{- else }}
// sql query
const sqlstr = `DELETE FROM {{ $table }} WHERE {{ colname .PrimaryKey.Col }} = $1`
// run query
XOLog(sqlstr, {{ $short }}.{{ .PrimaryKey.Name }})
_, err = db.Exec(sqlstr, {{ $short }}.{{ .PrimaryKey.Name }})
if err != nil {
return err
}
{{- end }}
// set deleted
{{ $short }}._deleted = true
return nil
}
{{- end }}

View file

@ -1,71 +0,0 @@
// XODB is the common interface for database operations that can be used with
// types from schema '{{ schema .Schema }}'.
//
// This should work with database/sql.DB and database/sql.Tx.
type XODB interface {
Exec(string, ...interface{}) (sql.Result, error)
Query(string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
}
// XOLog provides the log func used by generated queries.
var XOLog = func(string, ...interface{}) { }
// ScannerValuer is the common interface for types that implement both the
// database/sql.Scanner and sql/driver.Valuer interfaces.
type ScannerValuer interface {
sql.Scanner
driver.Valuer
}
// StringSlice is a slice of strings.
type StringSlice []string
// quoteEscapeRegex is the regex to match escaped characters in a string.
var quoteEscapeRegex = regexp.MustCompile(`([^\\]([\\]{2})*)\\"`)
// Scan satisfies the sql.Scanner interface for StringSlice.
func (ss *StringSlice) Scan(src interface{}) error {
buf, ok := src.([]byte)
if !ok {
return errors.New("invalid StringSlice")
}
// change quote escapes for csv parser
str := quoteEscapeRegex.ReplaceAllString(string(buf), `$1""`)
str = strings.Replace(str, `\\`, `\`, -1)
// remove braces
str = str[1:len(str)-1]
// bail if only one
if len(str) == 0 {
*ss = StringSlice([]string{})
return nil
}
// parse with csv reader
cr := csv.NewReader(strings.NewReader(str))
slice, err := cr.Read()
if err != nil {
fmt.Printf("exiting!: %v\n", err)
return err
}
*ss = StringSlice(slice)
return nil
}
// Value satisfies the driver.Valuer interface for StringSlice.
func (ss StringSlice) Value() (driver.Value, error) {
v := make([]string, len(ss))
for i, s := range ss {
v[i] = `"` + strings.Replace(strings.Replace(s, `\`, `\\\`, -1), `"`, `\"`, -1) + `"`
}
return "{" + strings.Join(v, ",") + "}", nil
}
// Slice is a slice of ScannerValuers.
type Slice []ScannerValuer

View file

@ -1,16 +0,0 @@
// Package {{ .Package }} contains the types for schema '{{ schema .Schema }}'.
package {{ .Package }}
// Code generated by xo. DO NOT EDIT.
import (
"database/sql"
"database/sql/driver"
"encoding/csv"
"errors"
"fmt"
"regexp"
"strings"
"time"
)