Merge pull request #194 from realbucksavage/master
Added a global `UseSchema` method to table generation
This commit is contained in:
commit
4873e43cc5
10 changed files with 275 additions and 45 deletions
|
|
@ -3,10 +3,11 @@ package mysql
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"github.com/go-jet/jet/v2/generator/metadata"
|
||||
"github.com/go-jet/jet/v2/internal/utils/throw"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// mySqlQuerySet is dialect query set for MySQL
|
||||
|
|
@ -16,7 +17,8 @@ func (m mySqlQuerySet) GetTablesMetaData(db *sql.DB, schemaName string, tableTyp
|
|||
query := `
|
||||
SELECT table_name as "table.name"
|
||||
FROM INFORMATION_SCHEMA.tables
|
||||
WHERE table_schema = ? and table_type = ?;
|
||||
WHERE table_schema = ? and table_type = ?
|
||||
ORDER BY table_name;
|
||||
`
|
||||
var tables []metadata.Table
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package postgres
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/go-jet/jet/v2/generator/metadata"
|
||||
"github.com/go-jet/jet/v2/internal/utils/throw"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
|
|
@ -15,7 +16,8 @@ func (p postgresQuerySet) GetTablesMetaData(db *sql.DB, schemaName string, table
|
|||
query := `
|
||||
SELECT table_name as "table.name"
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = $1 and table_type = $2;
|
||||
WHERE table_schema = $1 and table_type = $2
|
||||
ORDER BY table_name;
|
||||
`
|
||||
var tables []metadata.Table
|
||||
|
||||
|
|
|
|||
|
|
@ -97,6 +97,18 @@ func new{{tableTemplate.TypeName}}Impl(schemaName, tableName, alias string) {{st
|
|||
}
|
||||
`
|
||||
|
||||
var tableSqlBuilderSetSchemaTemplate = `package {{package}}
|
||||
|
||||
// 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) {
|
||||
{{- range .}}
|
||||
{{ .InstanceName }} = {{ .InstanceName }}.FromSchema(schema)
|
||||
{{- end}}
|
||||
}
|
||||
`
|
||||
|
||||
var tableModelFileTemplate = `package {{package}}
|
||||
|
||||
{{ with modelImports }}
|
||||
|
|
|
|||
|
|
@ -3,13 +3,14 @@ package template
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/go-jet/jet/v2/generator/metadata"
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
"github.com/go-jet/jet/v2/internal/utils"
|
||||
"github.com/go-jet/jet/v2/internal/utils/throw"
|
||||
"path"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
// ProcessSchema will process schema metadata and constructs go files using generator Template
|
||||
|
|
@ -118,6 +119,8 @@ func processTableSQLBuilder(fileTypes, dirPath string,
|
|||
|
||||
fmt.Printf("Generating %s sql builder files\n", fileTypes)
|
||||
|
||||
var generatedBuilders []TableSQLBuilder
|
||||
|
||||
for _, tableMetaData := range tablesMetaData {
|
||||
|
||||
var tableSQLBuilder TableSQLBuilder
|
||||
|
|
@ -169,7 +172,31 @@ func processTableSQLBuilder(fileTypes, dirPath string,
|
|||
|
||||
err = utils.SaveGoFile(tableSQLBuilderPath, tableSQLBuilder.FileName, text)
|
||||
throw.OnError(err)
|
||||
|
||||
generatedBuilders = append(generatedBuilders, tableSQLBuilder)
|
||||
}
|
||||
|
||||
if len(generatedBuilders) > 0 {
|
||||
generateUseSchemaFunc(dirPath, fileTypes, generatedBuilders)
|
||||
}
|
||||
}
|
||||
|
||||
func generateUseSchemaFunc(dirPath, fileTypes string, builders []TableSQLBuilder) {
|
||||
|
||||
basePath := path.Join(dirPath, builders[0].Path)
|
||||
|
||||
fmt.Printf("Generating global `UseSchema` method for %s\n", fileTypes)
|
||||
text, err := generateTemplate(
|
||||
autoGenWarningTemplate+tableSqlBuilderSetSchemaTemplate,
|
||||
builders,
|
||||
template.FuncMap{
|
||||
"package": func() string { return builders[0].PackageName() },
|
||||
},
|
||||
)
|
||||
throw.OnError(err)
|
||||
|
||||
err = utils.SaveGoFile(basePath, fileTypes, text)
|
||||
throw.OnError(err)
|
||||
}
|
||||
|
||||
func insertedRowAlias(dialect jet.Dialect) string {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue