good morning!!!!

Skip to content
Snippets Groups Projects
main.go 1.18 KiB
Newer Older
package sqlgen

import (
	"bytes"
	"text/template"
)

type Type uint

const (
	SqlTruncate = iota
	SqlDropTable
	SqlDropDatabase
	SqlSelectCount
José Carlos Nieto's avatar
José Carlos Nieto committed
	SqlInsert
	SqlSelect
José Carlos Nieto's avatar
José Carlos Nieto committed
	SqlUpdate
	SqlDelete
	Limit  int
	Offset int
)

func mustParse(text string, data interface{}) string {
	var b bytes.Buffer

	t := template.Must(template.New("").Parse(text))

	if err := t.Execute(&b, data); err != nil {
		panic("t.Execute: " + err.Error())
	}

	return b.String()
}

type Statement struct {
	Limit
	Offset
	Columns
José Carlos Nieto's avatar
José Carlos Nieto committed
	Values
	ColumnValues
	OrderBy
func (self *Statement) Compile() string {
	switch self.Type {
	case SqlTruncate:
		return mustParse(Layout.TruncateLayout, self)
		return mustParse(Layout.DropTableLayout, self)
		return mustParse(Layout.DropDatabaseLayout, self)
		return mustParse(Layout.SelectCountLayout, self)
	case SqlSelect:
		return mustParse(Layout.SelectLayout, self)
José Carlos Nieto's avatar
José Carlos Nieto committed
	case SqlDelete:
		return mustParse(Layout.DeleteLayout, self)
José Carlos Nieto's avatar
José Carlos Nieto committed
	case SqlUpdate:
		return mustParse(Layout.UpdateLayout, self)
José Carlos Nieto's avatar
José Carlos Nieto committed
	case SqlInsert:
		return mustParse(Layout.InsertLayout, self)