Skip to content

Commit

Permalink
Merge pull request smallnest#40 from alexj212/master
Browse files Browse the repository at this point in the history
Added Functionaility
  • Loading branch information
smallnest authored Apr 26, 2020
2 parents 4647d80 + a8e0bf4 commit 4ae51ad
Show file tree
Hide file tree
Showing 35 changed files with 1,596 additions and 1,328 deletions.
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

.idea/
packrd/
main-packr.go
gen
#
# only store sample db in git
#
example/.gitignore
example/README.md
example/api/
example/app/
example/dao/
example/go.mod
example/model/
example/Makefile
example/docs/
example/go.sum
54 changes: 52 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
WORKDIR=`pwd`
export PACKR2_EXECUTABLE := $(shell command -v packr2 2> /dev/null)


default: build


check_prereq:
ifndef PACKR2_EXECUTABLE
go get -u github.com/gobuffalo/packr/v2/packr2
endif
$(warning "found packr2")


install:
go get github.com/smallnest/gen

Expand All @@ -15,6 +25,7 @@ tools:
go get -u github.com/gordonklaus/ineffassign
go get -u github.com/fzipp/gocyclo
go get -u github.com/golang/lint/golint
go get -u github.com/gobuffalo/packr/v2/packr2

lint:
golint ./...
Expand Down Expand Up @@ -43,8 +54,47 @@ deps:
fmt:
go fmt .

build:
go build .
build: check_prereq
packr2 build .

test:
go test -v .


generate_example: clean_example
ls -latr ./example
go run . \
--sqltype=sqlite3 \
--connstr "./example/sample.db" \
--database main \
--json \
--gorm \
--guregu \
--rest \
--out ./example \
--module github.com/alexj212/generated \
--mod \
--server \
--makefile \
--overwrite

cd ./example && $(MAKE) example

run_example: generate_example
ls -latr ./example/bin
./example/bin/example


clean_example:
rm -rf ./example/Makefile \
./example/README.md \
./example/api \
./example/app \
./example/bin \
./example/dao \
./example/docs \
./example/go.mod \
./example/go.sum \
./example/model \
./example/.gitignore

93 changes: 82 additions & 11 deletions dbmeta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/iancoleman/strcase"
"github.com/jimsmart/schema"
)

Expand All @@ -14,6 +15,7 @@ type ModelInfo struct {
ShortStructName string
TableName string
Fields []string
DBCols []*sql.ColumnType
}

// commonInitialisms is a set of common initialisms.
Expand Down Expand Up @@ -83,30 +85,46 @@ const (
gureguNullTime = "null.Time"
golangTime = "time.Time"
golangBool = "bool"

)


// GenerateStruct generates a struct for the given table.
func GenerateStruct(db *sql.DB, tableName string, structName string, pkgName string, jsonAnnotation bool, gormAnnotation bool, gureguTypes bool) *ModelInfo {
cols, _ := schema.Table(db, tableName)
fields := generateFieldsTypes(db, cols, 0, jsonAnnotation, gormAnnotation, gureguTypes)
func GenerateStruct(db *sql.DB,
sqlDatabase,
tableName string,
structName string,
pkgName string,
jsonAnnotation bool,
gormAnnotation bool,
gureguTypes bool,
jsonNameFormat string,
verbose bool) *ModelInfo {

//fields := generateMysqlTypes(db, columnTypes, 0, jsonAnnotation, gormAnnotation, gureguTypes)
cols, _ := schema.Table(db, tableName)
fields := generateFieldsTypes(db, tableName, cols, 0, jsonAnnotation, gormAnnotation, gureguTypes, jsonNameFormat, verbose)

var modelInfo = &ModelInfo{
PackageName: pkgName,
StructName: structName,
TableName: tableName,
ShortStructName: strings.ToLower(string(structName[0])),
Fields: fields,
DBCols: cols,
}

return modelInfo
}

// Generate fields string
func generateFieldsTypes(db *sql.DB, columns []*sql.ColumnType, depth int, jsonAnnotation bool, gormAnnotation bool, gureguTypes bool) []string {
func generateFieldsTypes(
db *sql.DB,
tableName string,
columns []*sql.ColumnType,
depth int,
jsonAnnotation bool,
gormAnnotation bool,
gureguTypes bool,
jsonNameFormat string,
verbose bool) []string {

//sort.Strings(keys)

Expand All @@ -115,8 +133,15 @@ func generateFieldsTypes(db *sql.DB, columns []*sql.ColumnType, depth int, jsonA
for i, c := range columns {
nullable, _ := c.Nullable()
key := c.Name()
if verbose {
fmt.Printf(" [%s] [%d] field: %s type: %s\n", tableName, i, key, c.DatabaseTypeName())
}

valueType := sqlTypeToGoType(strings.ToLower(c.DatabaseTypeName()), nullable, gureguTypes)
if valueType == "" { // unknown type
if verbose {
fmt.Printf("table: %s unable to generate struct field: %s type: %s\n", tableName, key, c.DatabaseTypeName())
}
continue
}
fieldName := FmtFieldName(stringifyFirstChar(key))
Expand All @@ -131,7 +156,21 @@ func generateFieldsTypes(db *sql.DB, columns []*sql.ColumnType, depth int, jsonA

}
if jsonAnnotation == true {
annotations = append(annotations, fmt.Sprintf("json:\"%s\"", key))
var jsonName string
switch jsonNameFormat {
case "snake":
jsonName = strcase.ToSnake(key)
case "camel":
jsonName = strcase.ToCamel(key)
case "lower_camel":
jsonName = strcase.ToLowerCamel(key)
case "none":
jsonName = key
default:
jsonName = key
}

annotations = append(annotations, fmt.Sprintf("json:\"%s\"", jsonName))
}
if len(annotations) > 0 {
field = fmt.Sprintf("%s %s `%s`",
Expand Down Expand Up @@ -195,24 +234,27 @@ func generateMapTypes(db *sql.DB, columns []*sql.ColumnType, depth int, jsonAnno
}

func sqlTypeToGoType(mysqlType string, nullable bool, gureguTypes bool) string {
mysqlType = strings.Trim(mysqlType, " \t")
mysqlType = strings.ToLower(mysqlType)

switch mysqlType {
case "tinyint", "int", "smallint", "mediumint", "int4", "int2":
case "tinyint", "int", "smallint", "mediumint", "int4", "int2", "integer":
if nullable {
if gureguTypes {
return gureguNullInt
}
return sqlNullInt
}
return golangInt
case "bigint","int8":
case "bigint", "int8":
if nullable {
if gureguTypes {
return gureguNullInt
}
return sqlNullInt
}
return golangInt64
case "char", "enum", "varchar", "longtext", "mediumtext", "text", "tinytext","varchar2","json","jsonb", "nvarchar":
case "char", "enum", "varchar", "longtext", "mediumtext", "text", "tinytext", "varchar2", "json", "jsonb", "nvarchar":
if nullable {
if gureguTypes {
return gureguNullString
Expand Down Expand Up @@ -247,6 +289,35 @@ func sqlTypeToGoType(mysqlType string, nullable bool, gureguTypes bool) string {
return golangBool
}

if strings.HasPrefix(mysqlType, "nvarchar") || strings.HasPrefix(mysqlType, "varchar") {
if nullable {
if gureguTypes {
return gureguNullString
}
return sqlNullString
}
return "string"
}

if strings.HasPrefix(mysqlType, "numeric") {
if nullable {
if gureguTypes {
return gureguNullFloat
}
return sqlNullFloat
}
return golangFloat64
}

return ""
}

func IsNullable(colType *sql.ColumnType) bool {
nullable, _ := colType.Nullable()
return nullable
}

func ColumnLength(colType *sql.ColumnType) int64 {
len, _ := colType.Length()
return len
}
Loading

0 comments on commit 4ae51ad

Please sign in to comment.