Renamed generated schema func to UseSchema.
This commit is contained in:
parent
04c1a51ba7
commit
1bf48d640a
6 changed files with 46 additions and 41 deletions
|
|
@ -99,10 +99,10 @@ func new{{tableTemplate.TypeName}}Impl(schemaName, tableName, alias string) {{st
|
||||||
|
|
||||||
var tableSqlBuilderSetSchemaTemplate = `package {{package}}
|
var tableSqlBuilderSetSchemaTemplate = `package {{package}}
|
||||||
|
|
||||||
// {{schemaMethodName}} changes all global tables/views with the value returned
|
// UseSchema changes all global tables/views with the value returned
|
||||||
// returned by calling FromSchema on them. Passing an empty string to this function
|
// returned by calling FromSchema on them. Passing an empty string to this function
|
||||||
// will cause queries to be generated without any table/view alias.
|
// will cause queries to be generated without any table/view alias.
|
||||||
func {{schemaMethodName}}(schema string) {
|
func UseSchema(schema string) {
|
||||||
{{- range .}}
|
{{- range .}}
|
||||||
{{ .InstanceName }} = {{ .InstanceName }}.FromSchema(schema)
|
{{ .InstanceName }} = {{ .InstanceName }}.FromSchema(schema)
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
|
|
||||||
|
|
@ -177,25 +177,20 @@ func processTableSQLBuilder(fileTypes, dirPath string,
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(generatedBuilders) > 0 {
|
if len(generatedBuilders) > 0 {
|
||||||
generateUseSchemaFunc(dirPath, fileTypes, schemaMetaData, generatedBuilders)
|
generateUseSchemaFunc(dirPath, fileTypes, generatedBuilders)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateUseSchemaFunc(dirPath, fileTypes string, schemaMetadata metadata.Schema, builders []TableSQLBuilder) {
|
func generateUseSchemaFunc(dirPath, fileTypes string, builders []TableSQLBuilder) {
|
||||||
|
|
||||||
basePath := path.Join(dirPath, builders[0].Path)
|
basePath := path.Join(dirPath, builders[0].Path)
|
||||||
|
|
||||||
schemaIdentifier := utils.ToGoIdentifier(schemaMetadata.Name)
|
fmt.Printf("Generating global `UseSchema` method for %s\n", fileTypes)
|
||||||
methodName := fmt.Sprintf("Use%sSchema", schemaIdentifier)
|
|
||||||
|
|
||||||
fmt.Printf("Generating global `%s` method for %s\n", methodName, fileTypes)
|
|
||||||
|
|
||||||
text, err := generateTemplate(
|
text, err := generateTemplate(
|
||||||
autoGenWarningTemplate+tableSqlBuilderSetSchemaTemplate,
|
autoGenWarningTemplate+tableSqlBuilderSetSchemaTemplate,
|
||||||
builders,
|
builders,
|
||||||
template.FuncMap{
|
template.FuncMap{
|
||||||
"package": func() string { return builders[0].PackageName() },
|
"package": func() string { return builders[0].PackageName() },
|
||||||
"schemaMethodName": func() string { return methodName },
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
throw.OnError(err)
|
throw.OnError(err)
|
||||||
|
|
|
||||||
|
|
@ -322,10 +322,10 @@ var tableSetSchemaFile = `
|
||||||
|
|
||||||
package table
|
package table
|
||||||
|
|
||||||
// UseDvdsSchema changes all global tables/views with the value returned
|
// UseSchema changes all global tables/views with the value returned
|
||||||
// returned by calling FromSchema on them. Passing an empty string to this function
|
// returned by calling FromSchema on them. Passing an empty string to this function
|
||||||
// will cause queries to be generated without any table/view alias.
|
// will cause queries to be generated without any table/view alias.
|
||||||
func UseDvdsSchema(schema string) {
|
func UseSchema(schema string) {
|
||||||
Actor = Actor.FromSchema(schema)
|
Actor = Actor.FromSchema(schema)
|
||||||
Address = Address.FromSchema(schema)
|
Address = Address.FromSchema(schema)
|
||||||
Category = Category.FromSchema(schema)
|
Category = Category.FromSchema(schema)
|
||||||
|
|
@ -463,10 +463,10 @@ var viewSetSchemaFile = `
|
||||||
|
|
||||||
package view
|
package view
|
||||||
|
|
||||||
// UseDvdsSchema changes all global tables/views with the value returned
|
// UseSchema changes all global tables/views with the value returned
|
||||||
// returned by calling FromSchema on them. Passing an empty string to this function
|
// returned by calling FromSchema on them. Passing an empty string to this function
|
||||||
// will cause queries to be generated without any table/view alias.
|
// will cause queries to be generated without any table/view alias.
|
||||||
func UseDvdsSchema(schema string) {
|
func UseSchema(schema string) {
|
||||||
ActorInfo = ActorInfo.FromSchema(schema)
|
ActorInfo = ActorInfo.FromSchema(schema)
|
||||||
CustomerList = CustomerList.FromSchema(schema)
|
CustomerList = CustomerList.FromSchema(schema)
|
||||||
FilmList = FilmList.FromSchema(schema)
|
FilmList = FilmList.FromSchema(schema)
|
||||||
|
|
|
||||||
25
tests/mysql/update_dvds_test.go
Normal file
25
tests/mysql/update_dvds_test.go
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/go-jet/jet/v2/internal/testutils"
|
||||||
|
. "github.com/go-jet/jet/v2/mysql"
|
||||||
|
. "github.com/go-jet/jet/v2/tests/.gentestdata/mysql/dvds/table"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUpdateWithJoin(t *testing.T) {
|
||||||
|
statement := Staff.INNER_JOIN(Address, Address.AddressID.EQ(Staff.AddressID)).
|
||||||
|
UPDATE(Staff.LastName).
|
||||||
|
SET(String("New staff name")).
|
||||||
|
WHERE(Staff.StaffID.EQ(Int(1)))
|
||||||
|
|
||||||
|
testutils.AssertStatementSql(t, statement, `
|
||||||
|
UPDATE dvds.staff
|
||||||
|
INNER JOIN dvds.address ON (address.address_id = staff.address_id)
|
||||||
|
SET last_name = ?
|
||||||
|
WHERE staff.staff_id = ?;
|
||||||
|
`, "New staff name", int64(1))
|
||||||
|
|
||||||
|
testutils.AssertExecAndRollback(t, statement, db)
|
||||||
|
}
|
||||||
|
|
@ -3,14 +3,15 @@ package mysql
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"github.com/go-jet/jet/v2/internal/testutils"
|
|
||||||
. "github.com/go-jet/jet/v2/mysql"
|
|
||||||
. "github.com/go-jet/jet/v2/tests/.gentestdata/mysql/dvds/table"
|
|
||||||
"github.com/go-jet/jet/v2/tests/.gentestdata/mysql/test_sample/model"
|
|
||||||
. "github.com/go-jet/jet/v2/tests/.gentestdata/mysql/test_sample/table"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/go-jet/jet/v2/internal/testutils"
|
||||||
|
. "github.com/go-jet/jet/v2/mysql"
|
||||||
|
"github.com/go-jet/jet/v2/tests/.gentestdata/mysql/test_sample/model"
|
||||||
|
. "github.com/go-jet/jet/v2/tests/.gentestdata/mysql/test_sample/table"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUpdateValues(t *testing.T) {
|
func TestUpdateValues(t *testing.T) {
|
||||||
|
|
@ -259,22 +260,6 @@ func TestUpdateExecContext(t *testing.T) {
|
||||||
require.Error(t, err, "context deadline exceeded")
|
require.Error(t, err, "context deadline exceeded")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateWithJoin(t *testing.T) {
|
|
||||||
statement := Staff.INNER_JOIN(Address, Address.AddressID.EQ(Staff.AddressID)).
|
|
||||||
UPDATE(Staff.LastName).
|
|
||||||
SET(String("New staff name")).
|
|
||||||
WHERE(Staff.StaffID.EQ(Int(1)))
|
|
||||||
|
|
||||||
testutils.AssertStatementSql(t, statement, `
|
|
||||||
UPDATE dvds.staff
|
|
||||||
INNER JOIN dvds.address ON (address.address_id = staff.address_id)
|
|
||||||
SET last_name = ?
|
|
||||||
WHERE staff.staff_id = ?;
|
|
||||||
`, "New staff name", int64(1))
|
|
||||||
|
|
||||||
testutils.AssertExecAndRollback(t, statement, db)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUpdateOptimizerHints(t *testing.T) {
|
func TestUpdateOptimizerHints(t *testing.T) {
|
||||||
|
|
||||||
stmt := Link.UPDATE(Link.AllColumns).
|
stmt := Link.UPDATE(Link.AllColumns).
|
||||||
|
|
|
||||||
|
|
@ -400,10 +400,10 @@ var actorSQLBuilderTableFile = `
|
||||||
|
|
||||||
package table
|
package table
|
||||||
|
|
||||||
// UseDvdsSchema changes all global tables/views with the value returned
|
// UseSchema changes all global tables/views with the value returned
|
||||||
// returned by calling FromSchema on them. Passing an empty string to this function
|
// returned by calling FromSchema on them. Passing an empty string to this function
|
||||||
// will cause queries to be generated without any table/view alias.
|
// will cause queries to be generated without any table/view alias.
|
||||||
func UseDvdsSchema(schema string) {
|
func UseSchema(schema string) {
|
||||||
Film = Film.FromSchema(schema)
|
Film = Film.FromSchema(schema)
|
||||||
Address = Address.FromSchema(schema)
|
Address = Address.FromSchema(schema)
|
||||||
Actor = Actor.FromSchema(schema)
|
Actor = Actor.FromSchema(schema)
|
||||||
|
|
@ -541,10 +541,10 @@ var actorInfoSQLBuilderViewFile = `
|
||||||
|
|
||||||
package view
|
package view
|
||||||
|
|
||||||
// UseDvdsSchema changes all global tables/views with the value returned
|
// UseSchema changes all global tables/views with the value returned
|
||||||
// returned by calling FromSchema on them. Passing an empty string to this function
|
// returned by calling FromSchema on them. Passing an empty string to this function
|
||||||
// will cause queries to be generated without any table/view alias.
|
// will cause queries to be generated without any table/view alias.
|
||||||
func UseDvdsSchema(schema string) {
|
func UseSchema(schema string) {
|
||||||
ActorInfo = ActorInfo.FromSchema(schema)
|
ActorInfo = ActorInfo.FromSchema(schema)
|
||||||
CustomerList = CustomerList.FromSchema(schema)
|
CustomerList = CustomerList.FromSchema(schema)
|
||||||
FilmList = FilmList.FromSchema(schema)
|
FilmList = FilmList.FromSchema(schema)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue