Skip to content

Commit

Permalink
Merge pull request #32 from LUSHDigital/feature/builtin-names-conflict
Browse files Browse the repository at this point in the history
Feature/builtin names conflict
  • Loading branch information
codingconcepts authored Sep 23, 2018
2 parents 8b2c8e6 + b11de50 commit 66ba117
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
OLD_SHA:=$(shell shasum -a 256 main-packr.go | cut -d' ' -f1)
NEW_SHA= $(shell shasum -a 256 main-packr.go | cut -d' ' -f1)
OLD_SHA:=$(shell shasum -a 256 a_main-packr.go | cut -d' ' -f1)
NEW_SHA= $(shell shasum -a 256 a_main-packr.go | cut -d' ' -f1)

all: test install post
test:
go test -v ./...
go test -v -count 1 ./...
go build
docker-compose --no-ansi -f docker-compose.yml up -d --force-recreate
sleep 5
Expand All @@ -12,7 +12,7 @@ test:
rm -rf modelgen
rm -rf ./generated_models
test-ci:
go test -v ./...
go test -v -count 1 ./...
go build
docker-compose --no-ansi -f docker-compose.yml up -d --force-recreate
sleep 30 # annoying, but for ci.
Expand All @@ -26,6 +26,6 @@ install:
packr && go install
post:
@if [ "$(NEW_SHA)" != "$(OLD_SHA)" ]; then\
echo "sha comparison failed on main-packr.go";\
echo "sha comparison failed on a_main-packr.go";\
exit 1;\
fi
14 changes: 6 additions & 8 deletions main-packr.go → a_main-packr.go

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func getTables() (tables map[string]string) {
return tables
}

// GetOrderFromComment reads the modelgen:1 type comments and returns
// the integer part on the right
func GetOrderFromComment(comment string) (order int) {
if !strings.HasPrefix(comment, "modelgen") {
return
Expand All @@ -120,11 +122,17 @@ func GetOrderFromComment(comment string) (order int) {
return
}

// backtick is needed is the user picked a table name
// which conflicts with a builtin keyword, example "order"
func backtick(s string) string { return "`" + s + "`" }

// ToStructs takes an 'EXPLAIN' statement and transforms it's output
// into structs.
func ToStructs(tables map[string]string) []tmpl.TmplStruct {
var explained = make(map[string][]sqltypes.Explain)
for table := range tables {
var expl []sqltypes.Explain
rows, err := database.Query("EXPLAIN " + table)
rows, err := database.Query("EXPLAIN " + backtick(table))
if err != nil {
log.Fatal(err)
}
Expand Down
9 changes: 9 additions & 0 deletions init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ CREATE DATABASE `modelgen_tests`;

USE `modelgen_tests`;

-- order is a builtin, tests should pass despite this
DROP TABLE IF EXISTS `order`;

-- only one field should not break despite the special cases.
CREATE TABLE `order` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `common_cases`;

CREATE TABLE `common_cases` (
Expand Down
4 changes: 2 additions & 2 deletions tmpl/model.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// Insert a new {{.Model.Name}} row in the {{.Model.TableName}} table
func ({{.Receiver}} *{{.Model.Name}}) Insert(qu Queryer) (lastInsertID int64, err error) {
const stmt = "INSERT INTO {{.Model.TableName}} ({{.Model.Fields | insert_fields}}) VALUES ({{.Model.Fields | insert_values}})"
res, err := qu.Exec(stmt, {{ . | insert_args }})
res, err := qu.Exec(stmt{{ . | insert_args }})
if err != nil {
return 0, err
}
Expand All @@ -39,7 +39,7 @@
// Update an existing {{.Model.Name}} row in the {{.Model.TableName}} table.
func ({{.Receiver}} *{{.Model.Name}}) Update(qu Queryer, id int64) (int64, error) {
const stmt = "UPDATE {{.Model.TableName}} SET {{ . | update_values }} WHERE id = ?"
result, err := qu.Exec(stmt, {{ . | update_args }} , id)
result, err := qu.Exec(stmt, {{ . | update_args }} id)
if err != nil {
return 0, err
}
Expand Down
30 changes: 18 additions & 12 deletions tmpl/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
)

var FuncMap = template.FuncMap{
"insert_fields": GetInsertFields,
"insert_values": GetInsertValues,
"insert_args": GetInsertArgs,
"scan_fields": GetScanFields,
"update_args": GetUpdateArgs,
"update_values": GetUpdateValues,
"upsert_fields": GetUpsertFields,
"upsert_values": GetUpsertValues,
"insert_fields": GetInsertFields,
"insert_values": GetInsertValues,
"insert_args": GetInsertArgs,
"scan_fields": GetScanFields,
"update_args": GetUpdateArgs,
"update_values": GetUpdateValues,
"upsert_fields": GetUpsertFields,
"upsert_values": GetUpsertValues,
"upsert_on_duplicate": GetUpsertOnDuplicate,
"upsert_args": GetUpsertArgs,
"upsert_args": GetUpsertArgs,
}

func GetInsertFields(fields []TmplField) string {
Expand Down Expand Up @@ -55,7 +55,10 @@ func GetInsertArgs(m StructTmplData) string {
}
parts = append(parts, fmt.Sprintf("%s.%s", m.Receiver, fl.Name))
}
return strings.Join(parts, ", ")
if len(parts) > 0 {
return ", " + strings.Join(parts, ", ")
}
return ""
}

func GetScanFields(m StructTmplData) template.HTML {
Expand All @@ -75,7 +78,10 @@ func GetUpdateArgs(m StructTmplData) template.HTML {
}
parts = append(parts, fmt.Sprintf("%s.%s", m.Receiver, fl.Name))
}
return template.HTML(strings.Join(parts, ", "))
if len(parts) > 0 {
return template.HTML(strings.Join(parts, ", ") + ", ")
}
return ""
}

func GetUpdateValues(m StructTmplData) string {
Expand Down Expand Up @@ -142,4 +148,4 @@ func GetUpsertArgs(m StructTmplData) string {
parts = append(parts, fmt.Sprintf("%s.%s", m.Receiver, fl.Name))
}
return strings.Join(parts, ", ")
}
}

0 comments on commit 66ba117

Please sign in to comment.