Skip to content

Commit

Permalink
Merge pull request #13 from RyougiNevermore/master
Browse files Browse the repository at this point in the history
fix generate bug at no go type expr
  • Loading branch information
RyougiNevermore committed Jul 22, 2020
2 parents 739964f + 1927661 commit 74f8f3e
Show file tree
Hide file tree
Showing 26 changed files with 666 additions and 645 deletions.
2 changes: 1 addition & 1 deletion cmd/dalc/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/pharosnet/dalc/cmd/dalc
module github.com/pharosnet/dalc/cmd/dalc/v2

go 1.14

Expand Down
27 changes: 0 additions & 27 deletions cmd/dalc/internal/entry/column.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
package entry

import (
"fmt"
"strings"
)

type ColumnType string

func (ct ColumnType) GoType() (goType *GoType, err error) {

switch ct {
case TINYINT, SMALLINT, MEDIUMINT, INT, INTEGER:
goType = NewGoType("sql.NullInt32")
case BIGINT:
goType = NewGoType("sql.NullInt64")
case FLOAT, DOUBLE, DECIMAL:
goType = NewGoType("sql.NullFloat64")
case BOOLEAN:
goType = NewGoType("sql.NullBool")
case DATE, TIME, YEAR, DATETIME, TIMESTAMP:
goType = NewGoType("sql.NullTime")
case CHAR, NCHAR, VARCHAR, NVARCHAR:
goType = NewGoType("sql.NullString")
case TINYBLOB, TINYTEXT, BLOB, TEXT, MEDIUMBLOB, MEDIUMTEXT, LONGBLOB, LONGTEXT:
goType = NewGoType("[]byte")
case JSON:
goType = NewGoType("json.RawMessage")
default:
err = fmt.Errorf("column type mapped no go type, %v", ct)
}

return
}

type Column struct {
Name string
Type ColumnType
Expand Down
19 changes: 19 additions & 0 deletions cmd/dalc/internal/entry/query_fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@ func QueryFill(tables []*Table, queries0 []*Query) (queries []*Query, err error)
if expr.GoType != nil {
continue
}
got := false
for _, table := range tables {
if got {
break
}
if expr.Table.Schema == table.Schema && expr.Table.Table == table.Name {
for _, column := range table.Columns {
if column.Name == expr.ColumnName {
expr.GoType = column.GoType
got = true
break
}
}
}
}
if !got {
err = fmt.Errorf("fill %s failed, select expr %s does not find type", query.Name, expr.Name)
}
}

for _, expr := range query.CondExprList.ExprList {
expr.BuildName()
if expr.Name == "" {
Expand All @@ -41,15 +51,24 @@ func QueryFill(tables []*Table, queries0 []*Query) (queries []*Query, err error)
if expr.GoType != nil {
continue
}
got := false
for _, table := range tables {
if got {
break
}
if expr.Table.Schema == table.Schema && expr.Table.Table == table.Name {
for _, column := range table.Columns {
if column.Name == expr.ColumnName {
expr.GoType = column.GoType
got = true
break
}
}
}
}
if !got {
err = fmt.Errorf("fill %s failed, where cond expr %s does not find type", query.Name, expr.Name)
}
}
}
queries = queries0
Expand Down
4 changes: 2 additions & 2 deletions cmd/dalc/internal/generates/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ package {{ .Package }}
import (
"context"
"database/sql"
"github.com/pharosnet/dalc"
"github.com/pharosnet/dalc/v2"
{{ range $key, $value := .Imports }}
"{{ $key }}"
{{ end }}
Expand Down Expand Up @@ -216,7 +216,7 @@ var templateExec = `// DO NOT EDIT THIS FILE, IT IS GENERATED BY DALC
package {{ .Package }}
import (
"github.com/pharosnet/dalc"
"github.com/pharosnet/dalc/v2"
{{ range $key, $value := .Imports }}
"{{ $key }}"
{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dalc/internal/generates/query_exec.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package {{ .Package }}

import (
"github.com/pharosnet/dalc"
"github.com/pharosnet/dalc/v2"
{{ range $key, $value := .Imports }}
"{{ $key }}"
{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dalc/internal/generates/query_select.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package {{ .Package }}
import (
"context"
"database/sql"
"github.com/pharosnet/dalc"
"github.com/pharosnet/dalc/v2"
{{ range $key, $value := .Imports }}
"{{ $key }}"
{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dalc/internal/generates/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ package {{ .Package }}
import (
"context"
"database/sql"
"github.com/pharosnet/dalc"
"github.com/pharosnet/dalc/v2"
{{ range $key, $value := .Imports }}"{{ $key }}"{{ end }}
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/dalc/internal/generates/table.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package {{ .Package }}
import (
"context"
"database/sql"
"github.com/pharosnet/dalc"
"github.com/pharosnet/dalc/v2"
{{ range $key, $value := .Imports }}"{{ $key }}"{{ end }}
)

Expand Down
146 changes: 146 additions & 0 deletions example/generated/dal/model.applications.business_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// DO NOT EDIT THIS FILE, IT IS GENERATED BY DALC
package dal

import (
"context"
"database/sql"
"github.com/pharosnet/dalc/v2"
"time"
)

const (
businessCodeRowGetByPkSQL = "SELECT `ID`, `CREATE_BY`, `CREATE_AT`, `MODIFY_BY`, `MODIFY_AT`, `DELETE_BY`, `DELETE_AT`, `VERSION`, `CODE`, `GROUP`, `DESCRIPTION`, `TEXT_CODE`FROM `APPLICATIONS`.`BUSINESS_CODE` WHERE `ID` = ?"
businessCodeRowInsertSQL = "INSERT INTO `APPLICATIONS`.`BUSINESS_CODE` ( `ID`, `CREATE_BY`, `CREATE_AT`, `MODIFY_BY`, `MODIFY_AT`, `DELETE_BY`, `DELETE_AT`, `VERSION`, `CODE`, `GROUP`, `DESCRIPTION`, `TEXT_CODE`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
businessCodeRowUpdateSQL = "UPDATE `APPLICATIONS`.`BUSINESS_CODE` SET `CREATE_BY` = ?, `CREATE_AT` = ?, `MODIFY_BY` = ?, `MODIFY_AT` = ?, `DELETE_BY` = ?, `DELETE_AT` = ?, `VERSION` = ?, `CODE` = ?, `GROUP` = ?, `DESCRIPTION` = ?, `TEXT_CODE` = ? WHERE `ID` = ?"
businessCodeRowDeleteSQL = "DELETE FROM `APPLICATIONS`.`BUSINESS_CODE` WHERE `ID` = ?"
)

type BusinessCodeRow struct {
Id string `json:"id"`
CreateBy string `json:"create_by"`
CreateAt time.Time `json:"create_at"`
ModifyBy string `json:"modify_by"`
ModifyAt time.Time `json:"modify_at"`
DeleteBy string `json:"delete_by"`
DeleteAt time.Time `json:"delete_at"`
Version int64 `json:"version"`
Code string `json:"code"`
Group string `json:"group"`
Description string `json:"description"`
TextCode string `json:"text_code"`
}

func (row *BusinessCodeRow) scanSQLRow(rows *sql.Rows) (err error) {
err = rows.Scan(
&row.Id,
&row.CreateBy,
&row.CreateAt,
&row.ModifyBy,
&row.ModifyAt,
&row.DeleteBy,
&row.DeleteAt,
&row.Version,
&row.Code,
&row.Group,
&row.Description,
&row.TextCode,
)
return
}

func (row *BusinessCodeRow) conventToGetArgs() (args *dalc.Args) {

args = dalc.NewArgs()
args.Arg(row.Id)

return
}

func (row *BusinessCodeRow) Get(ctx dalc.PreparedContext) (err error) {
err = dalc.Query(ctx, businessCodeRowGetByPkSQL, row.conventToGetArgs(), func(ctx context.Context, rows *sql.Rows, rowErr error) (err error) {
if rowErr != nil {
err = rowErr
return
}
err = row.scanSQLRow(rows)
return
})
return
}

func (row *BusinessCodeRow) conventToInsertArgs() (args *dalc.Args) {

args = dalc.NewArgs()
args.Arg(row.Id)
args.Arg(row.CreateBy)
args.Arg(row.CreateAt)
args.Arg(row.ModifyBy)
args.Arg(row.ModifyAt)
args.Arg(row.DeleteBy)
args.Arg(row.DeleteAt)
args.Arg(row.Version)
args.Arg(row.Code)
args.Arg(row.Group)
args.Arg(row.Description)
args.Arg(row.TextCode)

return
}

func (row *BusinessCodeRow) Insert(ctx dalc.PreparedContext) (err error) {

_, execErr := dalc.Execute(ctx, businessCodeRowInsertSQL, row.conventToInsertArgs())
if execErr != nil {
err = execErr
return
}

return
}

func (row *BusinessCodeRow) conventToUpdateArgs() (args *dalc.Args) {

args = dalc.NewArgs()

args.Arg(row.CreateBy)
args.Arg(row.CreateAt)
args.Arg(row.ModifyBy)
args.Arg(row.ModifyAt)
args.Arg(row.DeleteBy)
args.Arg(row.DeleteAt)
args.Arg(row.Version)
args.Arg(row.Code)
args.Arg(row.Group)
args.Arg(row.Description)
args.Arg(row.TextCode)

args.Arg(row.Id)

return
}

func (row *BusinessCodeRow) Update(ctx dalc.PreparedContext) (err error) {
_, execErr := dalc.Execute(ctx, businessCodeRowUpdateSQL, row.conventToUpdateArgs())
if execErr != nil {
err = execErr
return
}
return
}

func (row *BusinessCodeRow) conventToDeleteArgs() (args *dalc.Args) {

args = dalc.NewArgs()
args.Arg(row.Id)

return
}

func (row *BusinessCodeRow) Delete(ctx dalc.PreparedContext) (err error) {
_, execErr := dalc.Execute(ctx, businessCodeRowDeleteSQL, row.conventToDeleteArgs())
if execErr != nil {
err = execErr
return
}
return
}
Loading

0 comments on commit 74f8f3e

Please sign in to comment.