diff --git a/tools/goctl/api/parser/g4/gen/api/apiparser_parser.go b/tools/goctl/api/parser/g4/gen/api/apiparser_parser.go index 8599337c..82cd78d2 100755 --- a/tools/goctl/api/parser/g4/gen/api/apiparser_parser.go +++ b/tools/goctl/api/parser/g4/gen/api/apiparser_parser.go @@ -634,3 +634,4 @@ func NewSyntaxLitContext(parser antlr.Parser, parent antlr.ParserRuleContext, in return p } + diff --git a/tools/goctl/model/sql/template/delete.go b/tools/goctl/model/sql/template/delete.go index b6dac7d8..373e6225 100644 --- a/tools/goctl/model/sql/template/delete.go +++ b/tools/goctl/model/sql/template/delete.go @@ -2,21 +2,21 @@ package template // Delete defines a delete template var Delete = ` -func (m *default{{.upperStartCamelObject}}Model) Delete({{.lowerStartCamelPrimaryKey}} {{.dataType}}) error { - {{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne({{.lowerStartCamelPrimaryKey}}) +func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error { + {{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOneCtx(ctx, {{.lowerStartCamelPrimaryKey}}) if err!=nil{ return err - }{{end}} + } - {{.keys}} - _, err {{if .containsIndexCache}}={{else}}:={{end}} m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { +{{end}} {{.keys}} + _, err {{if .containsIndexCache}}={{else}}:={{end}} m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("delete from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table) - return conn.Exec(query, {{.lowerStartCamelPrimaryKey}}) + return conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}}) }, {{.keyValues}}){{else}}query := fmt.Sprintf("delete from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table) - _,err:=m.conn.Exec(query, {{.lowerStartCamelPrimaryKey}}){{end}} + _,err:=m.conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}}){{end}} return err } ` // DeleteMethod defines a delete template for interface method -var DeleteMethod = `Delete({{.lowerStartCamelPrimaryKey}} {{.dataType}}) error` +var DeleteMethod = `Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error` diff --git a/tools/goctl/model/sql/template/find.go b/tools/goctl/model/sql/template/find.go index c1962d65..b358d518 100644 --- a/tools/goctl/model/sql/template/find.go +++ b/tools/goctl/model/sql/template/find.go @@ -2,12 +2,12 @@ package template // FindOne defines find row by id. var FindOne = ` -func (m *default{{.upperStartCamelObject}}Model) FindOne({{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) { +func (m *default{{.upperStartCamelObject}}Model) FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) { {{if .withCache}}{{.cacheKey}} var resp {{.upperStartCamelObject}} - err := m.QueryRow(&resp, {{.cacheKeyVariable}}, func(conn sqlx.SqlConn, v interface{}) error { + err := m.QueryRowCtx(ctx, &resp, {{.cacheKeyVariable}}, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error { query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) - return conn.QueryRow(v, query, {{.lowerStartCamelPrimaryKey}}) + return conn.QueryRowCtx(ctx, v, query, {{.lowerStartCamelPrimaryKey}}) }) switch err { case nil: @@ -18,7 +18,7 @@ func (m *default{{.upperStartCamelObject}}Model) FindOne({{.lowerStartCamelPrima return nil, err }{{else}}query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) var resp {{.upperStartCamelObject}} - err := m.conn.QueryRow(&resp, query, {{.lowerStartCamelPrimaryKey}}) + err := m.conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelPrimaryKey}}) switch err { case nil: return &resp, nil @@ -35,9 +35,9 @@ var FindOneByField = ` func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) { {{if .withCache}}{{.cacheKey}} var resp {{.upperStartCamelObject}} - err := m.QueryRowIndex(&resp, {{.cacheKeyVariable}}, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) { + err := m.QueryRowIndexCtx(ctx, &resp, {{.cacheKeyVariable}}, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) { query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) - if err := conn.QueryRow(&resp, query, {{.lowerStartCamelField}}); err != nil { + if err := conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelField}}); err != nil { return nil, err } return resp.{{.upperStartCamelPrimaryKey}}, nil @@ -52,7 +52,7 @@ func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}({{.in} } }{{else}}var resp {{.upperStartCamelObject}} query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table ) - err := m.conn.QueryRow(&resp, query, {{.lowerStartCamelField}}) + err := m.conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelField}}) switch err { case nil: return &resp, nil @@ -70,14 +70,14 @@ func (m *default{{.upperStartCamelObject}}Model) formatPrimary(primary interface return fmt.Sprintf("%s%v", {{.primaryKeyLeft}}, primary) } -func (m *default{{.upperStartCamelObject}}Model) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error { +func (m *default{{.upperStartCamelObject}}Model) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error { query := fmt.Sprintf("select %s from %s where {{.originalPrimaryField}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table ) - return conn.QueryRow(v, query, primary) + return conn.QueryRowCtx(ctx, v, query, primary) } ` // FindOneMethod defines find row method. -var FindOneMethod = `FindOne({{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)` +var FindOneMethod = `FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)` // FindOneByFieldMethod defines find row by field method. -var FindOneByFieldMethod = `FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) ` +var FindOneByFieldMethod = `FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error) ` diff --git a/tools/goctl/model/sql/template/import.go b/tools/goctl/model/sql/template/import.go index 8d90c1f2..a52e3be1 100644 --- a/tools/goctl/model/sql/template/import.go +++ b/tools/goctl/model/sql/template/import.go @@ -3,6 +3,7 @@ package template var ( // Imports defines a import template for model in cache case Imports = `import ( + "context" "database/sql" "fmt" "strings" @@ -17,6 +18,7 @@ var ( ` // ImportsNoCache defines a import template for model in normal case ImportsNoCache = `import ( + "context" "database/sql" "fmt" "strings" diff --git a/tools/goctl/model/sql/template/insert.go b/tools/goctl/model/sql/template/insert.go index 62a48c49..115ea7ff 100644 --- a/tools/goctl/model/sql/template/insert.go +++ b/tools/goctl/model/sql/template/insert.go @@ -2,18 +2,18 @@ package template // Insert defines a template for insert code in model var Insert = ` -func (m *default{{.upperStartCamelObject}}Model) Insert(data *{{.upperStartCamelObject}}) (sql.Result,error) { +func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error) { {{if .withCache}}{{if .containsIndexCache}}{{.keys}} - ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { + ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet) - return conn.Exec(query, {{.expressionValues}}) + return conn.ExecCtx(ctx, query, {{.expressionValues}}) }, {{.keyValues}}){{else}}query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet) - ret,err:=m.ExecNoCache(query, {{.expressionValues}}) + ret,err:=m.ExecNoCacheCtx(ctx, query, {{.expressionValues}}) {{end}}{{else}}query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet) - ret,err:=m.conn.Exec(query, {{.expressionValues}}){{end}} + ret,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}} return ret,err } ` // InsertMethod defines an interface method template for insert code in model -var InsertMethod = `Insert(data *{{.upperStartCamelObject}}) (sql.Result,error)` +var InsertMethod = `Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error)` diff --git a/tools/goctl/model/sql/template/update.go b/tools/goctl/model/sql/template/update.go index b4792d56..62edaee9 100644 --- a/tools/goctl/model/sql/template/update.go +++ b/tools/goctl/model/sql/template/update.go @@ -2,16 +2,16 @@ package template // Update defines a template for generating update codes var Update = ` -func (m *default{{.upperStartCamelObject}}Model) Update(data *{{.upperStartCamelObject}}) error { +func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, data *{{.upperStartCamelObject}}) error { {{if .withCache}}{{.keys}} - _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { + _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder) - return conn.Exec(query, {{.expressionValues}}) + return conn.ExecCtx(ctx, query, {{.expressionValues}}) }, {{.keyValues}}){{else}}query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder) - _,err:=m.conn.Exec(query, {{.expressionValues}}){{end}} + _,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}} return err } ` // UpdateMethod defines an interface method template for generating update codes -var UpdateMethod = `Update(data *{{.upperStartCamelObject}}) error` +var UpdateMethod = `Update(ctx context.Context, data *{{.upperStartCamelObject}}) error`