[MySQL] Add NEW alias for the rows to be inserted.
This commit is contained in:
parent
c264529e95
commit
4e1ff65023
6 changed files with 135 additions and 107 deletions
|
|
@ -238,7 +238,7 @@ import (
|
|||
|
||||
var Actor = newActorTable("dvds", "actor", "")
|
||||
|
||||
type ActorTable struct {
|
||||
type actorTable struct {
|
||||
mysql.Table
|
||||
|
||||
//Columns
|
||||
|
|
@ -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")
|
||||
|
|
@ -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
|
||||
|
|
@ -334,7 +347,7 @@ import (
|
|||
|
||||
var ActorInfo = newActorInfoTable("dvds", "actor_info", "")
|
||||
|
||||
type ActorInfoTable struct {
|
||||
type actorInfoTable struct {
|
||||
mysql.Table
|
||||
|
||||
//Columns
|
||||
|
|
@ -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")
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -255,7 +255,7 @@ func TestInsertOnDuplicateKey(t *testing.T) {
|
|||
INSERT INTO test_sample.link
|
||||
VALUES (?, ?, ?, DEFAULT),
|
||||
(?, ?, ?, DEFAULT)
|
||||
ON DUPLICATE KEY UPDATE id = (id + ?),
|
||||
ON DUPLICATE KEY UPDATE id = (link.id + ?),
|
||||
name = ?;
|
||||
`, randId, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
|
||||
randId, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
|
||||
|
|
@ -283,6 +283,65 @@ ON DUPLICATE KEY UPDATE id = (id + ?),
|
|||
})
|
||||
}
|
||||
|
||||
func TestInsertOnDuplicateKeyUpdateNEW(t *testing.T) {
|
||||
skipForMariaDB(t)
|
||||
|
||||
randId := rand.Int31()
|
||||
|
||||
stmt := Link.INSERT().
|
||||
MODELS([]model.Link{
|
||||
{
|
||||
ID: randId,
|
||||
URL: "https://www.postgresqltutorial.com",
|
||||
Name: "PostgreSQL Tutorial",
|
||||
Description: nil,
|
||||
},
|
||||
{
|
||||
ID: randId,
|
||||
URL: "https://www.yahoo.com",
|
||||
Name: "Yahoo",
|
||||
Description: testutils.StringPtr("web portal and search engine"),
|
||||
},
|
||||
}).AS_NEW().
|
||||
ON_DUPLICATE_KEY_UPDATE(
|
||||
Link.ID.SET(Link.ID.ADD(Int(11))),
|
||||
Link.URL.SET(Link.NEW.URL),
|
||||
Link.Name.SET(Link.NEW.Name),
|
||||
Link.Description.SET(Link.NEW.Description),
|
||||
)
|
||||
|
||||
testutils.AssertStatementSql(t, stmt, `
|
||||
INSERT INTO test_sample.link
|
||||
VALUES (?, ?, ?, ?),
|
||||
(?, ?, ?, ?) AS new
|
||||
ON DUPLICATE KEY UPDATE id = (link.id + ?),
|
||||
url = new.url,
|
||||
name = new.name,
|
||||
description = new.description;
|
||||
`)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
_, err := stmt.Exec(tx)
|
||||
require.NoError(t, err)
|
||||
|
||||
stmt := SELECT(Link.AllColumns).
|
||||
FROM(Link).
|
||||
WHERE(Link.ID.EQ(Int32(randId + 11)))
|
||||
|
||||
var dest model.Link
|
||||
|
||||
err = stmt.Query(tx, &dest)
|
||||
require.NoError(t, err)
|
||||
|
||||
testutils.AssertDeepEqual(t, dest, model.Link{
|
||||
ID: randId + 11,
|
||||
URL: "https://www.yahoo.com",
|
||||
Name: "Yahoo",
|
||||
Description: testutils.StringPtr("web portal and search engine"),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestInsertWithQueryContext(t *testing.T) {
|
||||
stmt := Link.INSERT().
|
||||
VALUES(1100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue