├── LICENSE ├── README.md ├── go.mod └── gotsql.go /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 MigOps Inc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GoTSQL - A Better Way to Organize SQL codebase using Templates in Golang 2 | ​ 3 | 4 | 5 | 1. Installation through Go Get command 6 | 7 | ```bash 8 | $ go get github.com/migopsrepos/gotsql 9 | ``` 10 | 11 | 2. Initialize the GoTSQL object 12 | 13 | ```go 14 | import "github.com/migopsrepos/gotsql" 15 | ... 16 | g := gotsql.GoTSQL{} 17 | ``` 18 | 19 | 3. Load the template file or directory with template files. (Templates are discussed in detail in next section) 20 | 21 | ```go 22 | g.Load("library/") 23 | ... 24 | g.Load("library_dev/") 25 | ``` 26 | 27 | 4. Get the query using namespace and use it in Golang 28 | 29 | ```go 30 | query, err := g.Get("library/books/crud/getAllBooks", nil) 31 | ... 32 | rows, err := db.Query(query, ...) 33 | 34 | 35 | > You can find the usage and more examples in this [article](https://www.migops.com/blog/2021/10/22/organizing-postgresql-codebase-using-templates-in-golang/) 36 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/MigOpsRepos/gotsql 2 | 3 | go 1.17 4 | -------------------------------------------------------------------------------- /gotsql.go: -------------------------------------------------------------------------------- 1 | package gotsql 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "os" 7 | "path/filepath" 8 | "text/template" 9 | ) 10 | 11 | type GoTSQL struct { 12 | Templates map[string]*template.Template 13 | } 14 | 15 | func (g *GoTSQL) Load(filedir string) { 16 | if g.Templates == nil { 17 | g.Templates = make(map[string]*template.Template) 18 | } 19 | 20 | err := filepath.Walk(filedir, func(path string, info os.FileInfo, err error) error { 21 | if info.IsDir() { 22 | return nil 23 | } 24 | if filepath.Ext(path) != ".gotsql" { 25 | return nil 26 | } 27 | t, er := template.ParseFiles(path) 28 | if er != nil { 29 | return fmt.Errorf("error parsing template %s", path, er) 30 | } 31 | g.Templates[path[:len(path)-len(filepath.Ext(path))]] = t 32 | return nil 33 | }) 34 | 35 | if err != nil { 36 | panic(err) 37 | } 38 | } 39 | 40 | func (g *GoTSQL) Get(commandName string, data interface{}) (string, error) { 41 | namespace := filepath.Dir(commandName) 42 | 43 | if t, ok := g.Templates[namespace]; ok { 44 | var b bytes.Buffer 45 | 46 | templateName := filepath.Base(commandName) 47 | tmpl_err := t.ExecuteTemplate(&b, templateName, data) 48 | if tmpl_err != nil { 49 | return "", tmpl_err 50 | } 51 | 52 | return b.String(), nil 53 | } else { 54 | return "", fmt.Errorf("template %s not found or loaded", namespace) 55 | } 56 | 57 | } 58 | --------------------------------------------------------------------------------