Added some querymods
* Added finishers that are broken * Added some template stuffs * Added a TestTemplates file output line thingy
This commit is contained in:
parent
d89d23e673
commit
4b12c849fc
8 changed files with 100 additions and 53 deletions
|
@ -24,7 +24,7 @@ var currentDB Executor
|
||||||
func Begin() (Transactor, error) {
|
func Begin() (Transactor, error) {
|
||||||
creator, ok := currentDB.(Creator)
|
creator, ok := currentDB.(Creator)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("Your database handle does not support transactions.")
|
panic("Your database does not support transactions.")
|
||||||
}
|
}
|
||||||
|
|
||||||
return creator.Begin()
|
return creator.Begin()
|
||||||
|
|
|
@ -19,7 +19,15 @@ func (q *Query) buildQuery() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// makes a new empty query ?????
|
// NewQuery initializes a new Query using the passed in QueryMods
|
||||||
func New() {
|
func NewQuery(mods ...QueryMod) *Query {
|
||||||
|
return NewQueryX(currentDB, mods...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewQueryX initializes a new Query using the passed in QueryMods
|
||||||
|
func NewQueryX(executor Executor, mods ...QueryMod) *Query {
|
||||||
|
q := &Query{executor: executor}
|
||||||
|
q.Apply(mods...)
|
||||||
|
|
||||||
|
return q
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,22 @@
|
||||||
package cmds
|
package cmds
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pobri19/sqlboiler/dbdrivers"
|
"github.com/pobri19/sqlboiler/dbdrivers"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmdData *CmdData
|
var cmdData *CmdData
|
||||||
|
var rgxHasSpaces = regexp.MustCompile(`^\s+`)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdData = &CmdData{
|
cmdData = &CmdData{
|
||||||
|
@ -94,15 +100,89 @@ func TestTemplates(t *testing.T) {
|
||||||
t.Errorf("Unable to run SQLBoilerRun: %s", err)
|
t.Errorf("Unable to run SQLBoilerRun: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
buf2 := bytes.Buffer{}
|
|
||||||
|
|
||||||
cmd := exec.Command("go", "test", "-c")
|
cmd := exec.Command("go", "test", "-c")
|
||||||
cmd.Dir = cmdData.OutFolder
|
cmd.Dir = cmdData.OutFolder
|
||||||
cmd.Stderr = &buf
|
cmd.Stderr = buf
|
||||||
cmd.Stdout = &buf2
|
|
||||||
|
|
||||||
if err = cmd.Run(); err != nil {
|
if err = cmd.Run(); err != nil {
|
||||||
t.Errorf("go test cmd execution failed: %s\n\n%s", err, buf.String())
|
t.Errorf("go test cmd execution failed: %s", err)
|
||||||
|
outputCompileErrors(buf, cmdData.OutFolder)
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func outputCompileErrors(buf *bytes.Buffer, outFolder string) {
|
||||||
|
type errObj struct {
|
||||||
|
errMsg string
|
||||||
|
fileName string
|
||||||
|
lineNumber int
|
||||||
|
}
|
||||||
|
|
||||||
|
var errObjects []errObj
|
||||||
|
lineBuf := &bytes.Buffer{}
|
||||||
|
|
||||||
|
bufLines := bytes.Split(buf.Bytes(), []byte{'\n'})
|
||||||
|
for i := 0; i < len(bufLines); i++ {
|
||||||
|
lineBuf.Reset()
|
||||||
|
if !bytes.HasPrefix(bufLines[i], []byte("./")) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(lineBuf, "%s\n", bufLines[i])
|
||||||
|
|
||||||
|
splits := bytes.Split(bufLines[i], []byte{':'})
|
||||||
|
lineNum, err := strconv.Atoi(string(splits[1]))
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("Cant convert line number to int: %s", bufLines[i]))
|
||||||
|
}
|
||||||
|
|
||||||
|
eObj := errObj{
|
||||||
|
fileName: string(splits[0]),
|
||||||
|
lineNumber: lineNum,
|
||||||
|
}
|
||||||
|
|
||||||
|
for y := i; y < len(bufLines); y++ {
|
||||||
|
if !rgxHasSpaces.Match(bufLines[y]) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fmt.Fprintf(lineBuf, "%s\n", bufLines[y])
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
eObj.errMsg = lineBuf.String()
|
||||||
|
|
||||||
|
errObjects = append(errObjects, eObj)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, eObj := range errObjects {
|
||||||
|
fmt.Printf("-----------------\n")
|
||||||
|
fmt.Println(eObj.errMsg)
|
||||||
|
|
||||||
|
filePath := filepath.Join(outFolder, eObj.fileName)
|
||||||
|
fh, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("Cant open the file: %#v", eObj))
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(fh)
|
||||||
|
throwaway := eObj.lineNumber - 2
|
||||||
|
for throwaway > 0 && scanner.Scan() {
|
||||||
|
throwaway--
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
if scanner.Scan() {
|
||||||
|
b := scanner.Bytes()
|
||||||
|
if len(b) != 0 {
|
||||||
|
fmt.Printf("%s\n", b)
|
||||||
|
} else {
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fh.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
|
||||||
// {{$tableNameSingular}}FindSelect retrieves the specified columns for a single record by ID.
|
|
||||||
// Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve.
|
|
||||||
// For example: friendName string `db:"friend_name"`
|
|
||||||
func {{$tableNameSingular}}FindSelect(id int, results interface{}) error {
|
|
||||||
if id == 0 {
|
|
||||||
return errors.New("{{.PkgName}}: no id provided for {{.Table.Name}} select")
|
|
||||||
}
|
|
||||||
|
|
||||||
query := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE id=$1`, boil.SelectNames(results))
|
|
||||||
err := boil.GetDB().Select(results, query, id)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
3
cmds/templates/finishers.tpl
Normal file
3
cmds/templates/finishers.tpl
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
// One
|
||||||
|
// All
|
||||||
|
// Count
|
|
@ -1,14 +0,0 @@
|
||||||
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
|
|
||||||
// {{$tableNamePlural}}Select retrieves the specified columns for all records.
|
|
||||||
// Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve.
|
|
||||||
// For example: friendName string `db:"friend_name"`
|
|
||||||
func {{$tableNamePlural}}Select(results interface{}) error {
|
|
||||||
query := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}}`, boil.SelectNames(results))
|
|
||||||
err := boil.GetDB().Select(results, query)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
|
|
||||||
// {{$tableNamePlural}}SelectWhere retrieves all records with the specified column values.
|
|
||||||
func {{$tableNamePlural}}SelectWhere(results interface{}, columns map[string]interface{}) error {
|
|
||||||
query := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, boil.SelectNames(results), boil.WhereClause(columns))
|
|
||||||
err := boil.GetDB().Select(results, query, boil.WhereParams(columns)...)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
0
cmds/templates_test/finishers.tpl
Normal file
0
cmds/templates_test/finishers.tpl
Normal file
Loading…
Add table
Reference in a new issue