From 04c1a51ba7d709eb6ec2ef833a3667f509375011 Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 4 Dec 2022 20:33:55 +0530 Subject: [PATCH] Changed SetSchema to UseSchema --- generator/template/file_templates.go | 5 ++++- generator/template/process.go | 10 +++++----- tests/mysql/generator_test.go | 10 ++++++++-- tests/postgres/generator_test.go | 10 ++++++++-- tests/sqlite/generator_test.go | 10 ++++++++-- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/generator/template/file_templates.go b/generator/template/file_templates.go index 209113e..3a4eae9 100644 --- a/generator/template/file_templates.go +++ b/generator/template/file_templates.go @@ -99,7 +99,10 @@ func new{{tableTemplate.TypeName}}Impl(schemaName, tableName, alias string) {{st var tableSqlBuilderSetSchemaTemplate = `package {{package}} -func {{setSchemaMethodName}}(schema string) { +// {{schemaMethodName}} changes all global tables/views with the value returned +// returned by calling FromSchema on them. Passing an empty string to this function +// will cause queries to be generated without any table/view alias. +func {{schemaMethodName}}(schema string) { {{- range .}} {{ .InstanceName }} = {{ .InstanceName }}.FromSchema(schema) {{- end}} diff --git a/generator/template/process.go b/generator/template/process.go index a37b374..c661ebd 100644 --- a/generator/template/process.go +++ b/generator/template/process.go @@ -177,16 +177,16 @@ func processTableSQLBuilder(fileTypes, dirPath string, } if len(generatedBuilders) > 0 { - generateSetSchema(dirPath, fileTypes, schemaMetaData, generatedBuilders) + generateUseSchemaFunc(dirPath, fileTypes, schemaMetaData, generatedBuilders) } } -func generateSetSchema(dirPath, fileTypes string, schemaMetadata metadata.Schema, builders []TableSQLBuilder) { +func generateUseSchemaFunc(dirPath, fileTypes string, schemaMetadata metadata.Schema, builders []TableSQLBuilder) { basePath := path.Join(dirPath, builders[0].Path) schemaIdentifier := utils.ToGoIdentifier(schemaMetadata.Name) - methodName := fmt.Sprintf("Set%sSchema", schemaIdentifier) + methodName := fmt.Sprintf("Use%sSchema", schemaIdentifier) fmt.Printf("Generating global `%s` method for %s\n", methodName, fileTypes) @@ -194,8 +194,8 @@ func generateSetSchema(dirPath, fileTypes string, schemaMetadata metadata.Schema autoGenWarningTemplate+tableSqlBuilderSetSchemaTemplate, builders, template.FuncMap{ - "package": func() string { return builders[0].PackageName() }, - "setSchemaMethodName": func() string { return methodName }, + "package": func() string { return builders[0].PackageName() }, + "schemaMethodName": func() string { return methodName }, }, ) throw.OnError(err) diff --git a/tests/mysql/generator_test.go b/tests/mysql/generator_test.go index 8f2c3b7..ebf795c 100644 --- a/tests/mysql/generator_test.go +++ b/tests/mysql/generator_test.go @@ -322,7 +322,10 @@ var tableSetSchemaFile = ` package table -func SetDvdsSchema(schema string) { +// UseDvdsSchema changes all global tables/views with the value returned +// returned by calling FromSchema on them. Passing an empty string to this function +// will cause queries to be generated without any table/view alias. +func UseDvdsSchema(schema string) { Actor = Actor.FromSchema(schema) Address = Address.FromSchema(schema) Category = Category.FromSchema(schema) @@ -460,7 +463,10 @@ var viewSetSchemaFile = ` package view -func SetDvdsSchema(schema string) { +// UseDvdsSchema changes all global tables/views with the value returned +// returned by calling FromSchema on them. Passing an empty string to this function +// will cause queries to be generated without any table/view alias. +func UseDvdsSchema(schema string) { ActorInfo = ActorInfo.FromSchema(schema) CustomerList = CustomerList.FromSchema(schema) FilmList = FilmList.FromSchema(schema) diff --git a/tests/postgres/generator_test.go b/tests/postgres/generator_test.go index 7222b81..0a4f92a 100644 --- a/tests/postgres/generator_test.go +++ b/tests/postgres/generator_test.go @@ -400,7 +400,10 @@ var actorSQLBuilderTableFile = ` package table -func SetDvdsSchema(schema string) { +// UseDvdsSchema changes all global tables/views with the value returned +// returned by calling FromSchema on them. Passing an empty string to this function +// will cause queries to be generated without any table/view alias. +func UseDvdsSchema(schema string) { Film = Film.FromSchema(schema) Address = Address.FromSchema(schema) Actor = Actor.FromSchema(schema) @@ -538,7 +541,10 @@ var actorInfoSQLBuilderViewFile = ` package view -func SetDvdsSchema(schema string) { +// UseDvdsSchema changes all global tables/views with the value returned +// returned by calling FromSchema on them. Passing an empty string to this function +// will cause queries to be generated without any table/view alias. +func UseDvdsSchema(schema string) { ActorInfo = ActorInfo.FromSchema(schema) CustomerList = CustomerList.FromSchema(schema) FilmList = FilmList.FromSchema(schema) diff --git a/tests/sqlite/generator_test.go b/tests/sqlite/generator_test.go index 46ec50b..c987fc1 100644 --- a/tests/sqlite/generator_test.go +++ b/tests/sqlite/generator_test.go @@ -238,7 +238,10 @@ const actorSQLBuilderTableFile = ` package table -func SetSchema(schema string) { +// UseSchema changes all global tables/views with the value returned +// returned by calling FromSchema on them. Passing an empty string to this function +// will cause queries to be generated without any table/view alias. +func UseSchema(schema string) { Actor = Actor.FromSchema(schema) Address = Address.FromSchema(schema) Category = Category.FromSchema(schema) @@ -367,7 +370,10 @@ const filmListSQLBuilderViewFile = ` package view -func SetSchema(schema string) { +// UseSchema changes all global tables/views with the value returned +// returned by calling FromSchema on them. Passing an empty string to this function +// will cause queries to be generated without any table/view alias. +func UseSchema(schema string) { CustomerList = CustomerList.FromSchema(schema) FilmList = FilmList.FromSchema(schema) SalesByFilmCategory = SalesByFilmCategory.FromSchema(schema)