Skip to content

Commit

Permalink
[MySQL] Add NEW alias for the rows to be inserted.
Browse files Browse the repository at this point in the history
  • Loading branch information
go-jet committed Aug 23, 2022
1 parent c264529 commit 4e1ff65
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 107 deletions.
78 changes: 2 additions & 76 deletions generator/template/file_templates.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 19 additions & 15 deletions generator/template/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,29 @@ func processTableSQLBuilder(fileTypes, dirPath string,

for _, tableMetaData := range tablesMetaData {

var tableSQLBuilderTemplate TableSQLBuilder
var tableSQLBuilder TableSQLBuilder

if fileTypes == "view" {
tableSQLBuilderTemplate = sqlBuilderTemplate.View(tableMetaData)
tableSQLBuilder = sqlBuilderTemplate.View(tableMetaData)
} else {
tableSQLBuilderTemplate = sqlBuilderTemplate.Table(tableMetaData)
tableSQLBuilder = sqlBuilderTemplate.Table(tableMetaData)
}

if tableSQLBuilderTemplate.Skip {
if tableSQLBuilder.Skip {
continue
}

tableSQLBuilderPath := path.Join(dirPath, tableSQLBuilderTemplate.Path)
tableSQLBuilderPath := path.Join(dirPath, tableSQLBuilder.Path)

err := utils.EnsureDirPath(tableSQLBuilderPath)
throw.OnError(err)

text, err := generateTemplate(
autoGenWarningTemplate+getTableSQLBuilderTemplate(dialect),
autoGenWarningTemplate+tableSQLBuilderTemplate,
tableMetaData,
template.FuncMap{
"package": func() string {
return tableSQLBuilderTemplate.PackageName()
return tableSQLBuilder.PackageName()
},
"dialect": func() jet.Dialect {
return dialect
Expand All @@ -151,29 +151,33 @@ func processTableSQLBuilder(fileTypes, dirPath string,
return schemaMetaData.Name
},
"tableTemplate": func() TableSQLBuilder {
return tableSQLBuilderTemplate
return tableSQLBuilder
},
"structImplName": func() string { // postgres only
structName := tableSQLBuilderTemplate.TypeName
structName := tableSQLBuilder.TypeName
return string(strings.ToLower(structName)[0]) + structName[1:]
},
"columnField": func(columnMetaData metadata.Column) TableSQLBuilderColumn {
return tableSQLBuilderTemplate.Column(columnMetaData)
return tableSQLBuilder.Column(columnMetaData)
},
"toUpper": strings.ToUpper,
"insertedRowAlias": func() string {
return insertedRowAlias(dialect)
},
})
throw.OnError(err)

err = utils.SaveGoFile(tableSQLBuilderPath, tableSQLBuilderTemplate.FileName, text)
err = utils.SaveGoFile(tableSQLBuilderPath, tableSQLBuilder.FileName, text)
throw.OnError(err)
}
}

func getTableSQLBuilderTemplate(dialect jet.Dialect) string {
if dialect.Name() == "PostgreSQL" || dialect.Name() == "SQLite" {
return tableSQLBuilderTemplateWithEXCLUDED
func insertedRowAlias(dialect jet.Dialect) string {
if dialect.Name() == "MySQL" {
return "new"
}

return tableSQLBuilderTemplate
return "excluded"
}

func processTableModels(fileTypes, modelDirPath string, tablesMetaData []metadata.Table, modelTemplate Model) {
Expand Down
7 changes: 7 additions & 0 deletions internal/jet/clause.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ func (v *ClauseValuesQuery) Serialize(statementType StatementType, out *SQLBuild
// ClauseValues struct
type ClauseValues struct {
Rows [][]Serializer
As string
}

// Serialize serializes clause into SQLBuilder
Expand All @@ -417,6 +418,12 @@ func (v *ClauseValues) Serialize(statementType StatementType, out *SQLBuilder, o

out.WriteByte(')')
}

if len(v.As) > 0 {
out.WriteString("AS")
out.WriteIdentifier(v.As)
}

out.DecreaseIdent(7)
}

Expand Down
8 changes: 7 additions & 1 deletion mysql/insert_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type InsertStatement interface {
// If data is not struct or there is no field for every column selected, this method will panic.
MODEL(data interface{}) InsertStatement
MODELS(data interface{}) InsertStatement
AS_NEW() InsertStatement

ON_DUPLICATE_KEY_UPDATE(assigments ...ColumnAssigment) InsertStatement

Expand Down Expand Up @@ -52,6 +53,11 @@ func (is *insertStatementImpl) MODELS(data interface{}) InsertStatement {
return is
}

func (is *insertStatementImpl) AS_NEW() InsertStatement {
is.ValuesQuery.As = "new"
return is
}

func (is *insertStatementImpl) ON_DUPLICATE_KEY_UPDATE(assigments ...ColumnAssigment) InsertStatement {
is.OnDuplicateKey = assigments
return is
Expand Down Expand Up @@ -79,7 +85,7 @@ func (s onDuplicateKeyUpdateClause) Serialize(statementType jet.StatementType, o
out.NewLine()
}

jet.Serialize(assigment, statementType, out, jet.ShortName.WithFallTrough(options)...)
jet.Serialize(assigment, statementType, out, jet.FallTrough(options)...)
}

out.DecreaseIdent(24)
Expand Down
54 changes: 40 additions & 14 deletions tests/mysql/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ import (
var Actor = newActorTable("dvds", "actor", "")
type ActorTable struct {
type actorTable struct {
mysql.Table
//Columns
Expand All @@ -251,27 +251,40 @@ type ActorTable struct {
MutableColumns mysql.ColumnList
}
type ActorTable struct {
actorTable
NEW actorTable
}
// AS creates new ActorTable with assigned alias
func (a ActorTable) AS(alias string) ActorTable {
func (a ActorTable) AS(alias string) *ActorTable {
return newActorTable(a.SchemaName(), a.TableName(), alias)
}
// Schema creates new ActorTable with assigned schema name
func (a ActorTable) FromSchema(schemaName string) ActorTable {
func (a ActorTable) FromSchema(schemaName string) *ActorTable {
return newActorTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ActorTable with assigned table prefix
func (a ActorTable) WithPrefix(prefix string) ActorTable {
func (a ActorTable) WithPrefix(prefix string) *ActorTable {
return newActorTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ActorTable with assigned table suffix
func (a ActorTable) WithSuffix(suffix string) ActorTable {
func (a ActorTable) WithSuffix(suffix string) *ActorTable {
return newActorTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newActorTable(schemaName, tableName, alias string) ActorTable {
func newActorTable(schemaName, tableName, alias string) *ActorTable {
return &ActorTable{
actorTable: newActorTableImpl(schemaName, tableName, alias),
NEW: newActorTableImpl("", "new", ""),
}
}
func newActorTableImpl(schemaName, tableName, alias string) actorTable {
var (
ActorIDColumn = mysql.IntegerColumn("actor_id")
FirstNameColumn = mysql.StringColumn("first_name")
Expand All @@ -281,7 +294,7 @@ func newActorTable(schemaName, tableName, alias string) ActorTable {
mutableColumns = mysql.ColumnList{FirstNameColumn, LastNameColumn, LastUpdateColumn}
)
return ActorTable{
return actorTable{
Table: mysql.NewTable(schemaName, tableName, alias, allColumns...),
//Columns
Expand Down Expand Up @@ -334,7 +347,7 @@ import (
var ActorInfo = newActorInfoTable("dvds", "actor_info", "")
type ActorInfoTable struct {
type actorInfoTable struct {
mysql.Table
//Columns
Expand All @@ -347,27 +360,40 @@ type ActorInfoTable struct {
MutableColumns mysql.ColumnList
}
type ActorInfoTable struct {
actorInfoTable
NEW actorInfoTable
}
// AS creates new ActorInfoTable with assigned alias
func (a ActorInfoTable) AS(alias string) ActorInfoTable {
func (a ActorInfoTable) AS(alias string) *ActorInfoTable {
return newActorInfoTable(a.SchemaName(), a.TableName(), alias)
}
// Schema creates new ActorInfoTable with assigned schema name
func (a ActorInfoTable) FromSchema(schemaName string) ActorInfoTable {
func (a ActorInfoTable) FromSchema(schemaName string) *ActorInfoTable {
return newActorInfoTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ActorInfoTable with assigned table prefix
func (a ActorInfoTable) WithPrefix(prefix string) ActorInfoTable {
func (a ActorInfoTable) WithPrefix(prefix string) *ActorInfoTable {
return newActorInfoTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ActorInfoTable with assigned table suffix
func (a ActorInfoTable) WithSuffix(suffix string) ActorInfoTable {
func (a ActorInfoTable) WithSuffix(suffix string) *ActorInfoTable {
return newActorInfoTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newActorInfoTable(schemaName, tableName, alias string) ActorInfoTable {
func newActorInfoTable(schemaName, tableName, alias string) *ActorInfoTable {
return &ActorInfoTable{
actorInfoTable: newActorInfoTableImpl(schemaName, tableName, alias),
NEW: newActorInfoTableImpl("", "new", ""),
}
}
func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable {
var (
ActorIDColumn = mysql.IntegerColumn("actor_id")
FirstNameColumn = mysql.StringColumn("first_name")
Expand All @@ -377,7 +403,7 @@ func newActorInfoTable(schemaName, tableName, alias string) ActorInfoTable {
mutableColumns = mysql.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn}
)
return ActorInfoTable{
return actorInfoTable{
Table: mysql.NewTable(schemaName, tableName, alias, allColumns...),
//Columns
Expand Down
Loading

0 comments on commit 4e1ff65

Please sign in to comment.