2016-09-15 05:59:55 +02:00
|
|
|
var dialect = queries.Dialect{
|
2016-09-14 10:08:30 +02:00
|
|
|
LQ: 0x{{printf "%x" .Dialect.LQ}},
|
|
|
|
RQ: 0x{{printf "%x" .Dialect.RQ}},
|
|
|
|
IndexPlaceholders: {{.Dialect.IndexPlaceholders}},
|
2017-03-14 11:59:05 +01:00
|
|
|
UseTopClause: {{.Dialect.UseTopClause}},
|
2016-09-09 19:14:18 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
// NewQueryG initializes a new Query using the passed in QueryMods
|
2016-09-15 05:59:55 +02:00
|
|
|
func NewQueryG(mods ...qm.QueryMod) *queries.Query {
|
2016-09-14 10:08:30 +02:00
|
|
|
return NewQuery(boil.GetDB(), mods...)
|
2016-04-19 04:02:32 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
// NewQuery initializes a new Query using the passed in QueryMods
|
2016-09-15 05:59:55 +02:00
|
|
|
func NewQuery(exec boil.Executor, mods ...qm.QueryMod) *queries.Query {
|
|
|
|
q := &queries.Query{}
|
|
|
|
queries.SetExecutor(q, exec)
|
|
|
|
queries.SetDialect(q, &dialect)
|
2016-09-14 10:08:30 +02:00
|
|
|
qm.Apply(q, mods...)
|
2016-04-19 04:02:32 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return q
|
2016-04-19 04:02:32 +02:00
|
|
|
}
|
2017-05-10 21:29:02 +02:00
|
|
|
|
2017-08-24 17:58:13 +02:00
|
|
|
func mergeModels(tx boil.Executor, primaryID uint64, secondaryID uint64, foreignKeys []foreignKey, conflictingKeys []conflictingUniqueKey) error {
|
2017-05-11 23:42:20 +02:00
|
|
|
if len(foreignKeys) < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for _, conflict := range conflictingKeys {
|
2018-09-03 06:51:55 +02:00
|
|
|
if len(conflict.columns) == 1 && conflict.columns[0] == conflict.objectIdColumn {
|
|
|
|
err = deleteOneToOneConflictsBeforeMerge(tx, conflict, primaryID, secondaryID)
|
|
|
|
} else {
|
|
|
|
err = deleteOneToManyConflictsBeforeMerge(tx, conflict, primaryID, secondaryID)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 23:42:20 +02:00
|
|
|
|
|
|
|
for _, fk := range foreignKeys {
|
|
|
|
// TODO: use NewQuery here, not plain sql
|
|
|
|
query := fmt.Sprintf(
|
|
|
|
"UPDATE %s SET %s = %s WHERE %s = %s",
|
|
|
|
fk.foreignTable, fk.foreignColumn, strmangle.Placeholders(dialect.IndexPlaceholders, 1, 1, 1),
|
|
|
|
fk.foreignColumn, strmangle.Placeholders(dialect.IndexPlaceholders, 1, 2, 1),
|
|
|
|
)
|
|
|
|
_, err = tx.Exec(query, primaryID, secondaryID)
|
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err(err)
|
2017-05-11 23:42:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return checkMerge(tx, foreignKeys)
|
2017-05-10 21:29:02 +02:00
|
|
|
}
|
|
|
|
|
2018-09-03 06:51:55 +02:00
|
|
|
func deleteOneToOneConflictsBeforeMerge(tx boil.Executor, conflict conflictingUniqueKey, primaryID uint64, secondaryID uint64) error {
|
|
|
|
query := fmt.Sprintf(
|
|
|
|
"SELECT COUNT(*) FROM %s WHERE %s IN (%s)",
|
|
|
|
conflict.table, conflict.objectIdColumn,
|
|
|
|
strmangle.Placeholders(dialect.IndexPlaceholders, 2, 1, 1),
|
|
|
|
)
|
2017-05-11 23:42:20 +02:00
|
|
|
|
2018-09-03 06:51:55 +02:00
|
|
|
var count int
|
|
|
|
err := tx.QueryRow(query, primaryID, secondaryID).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if count > 2 {
|
|
|
|
return errors.Err("it should not be possible to have more than two rows here")
|
|
|
|
} else if count != 2 {
|
|
|
|
return nil // no conflicting rows
|
2017-05-11 23:42:20 +02:00
|
|
|
}
|
|
|
|
|
2018-09-03 06:51:55 +02:00
|
|
|
query = fmt.Sprintf(
|
|
|
|
"DELETE FROM %s WHERE %s = %s",
|
|
|
|
conflict.table, conflict.objectIdColumn, strmangle.Placeholders(dialect.IndexPlaceholders, 1, 1, 1),
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err = tx.Exec(query, secondaryID)
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteOneToManyConflictsBeforeMerge(tx boil.Executor, conflict conflictingUniqueKey, primaryID uint64, secondaryID uint64) error {
|
|
|
|
conflictingColumns := strmangle.SetComplement(conflict.columns, []string{conflict.objectIdColumn})
|
|
|
|
|
2017-05-11 23:42:20 +02:00
|
|
|
query := fmt.Sprintf(
|
|
|
|
"SELECT %s FROM %s WHERE %s IN (%s) GROUP BY %s HAVING count(distinct %s) > 1",
|
2018-09-03 06:51:55 +02:00
|
|
|
strings.Join(conflictingColumns, ","), conflict.table, conflict.objectIdColumn,
|
2017-05-11 23:42:20 +02:00
|
|
|
strmangle.Placeholders(dialect.IndexPlaceholders, 2, 1, 1),
|
2018-09-03 06:51:55 +02:00
|
|
|
strings.Join(conflictingColumns, ","), conflict.objectIdColumn,
|
2017-05-11 23:42:20 +02:00
|
|
|
)
|
|
|
|
|
2018-09-03 06:51:55 +02:00
|
|
|
//The selectParams should be the ObjectIDs to search for regarding the conflict.
|
2017-05-11 23:42:20 +02:00
|
|
|
rows, err := tx.Query(query, primaryID, secondaryID)
|
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err(err)
|
2017-05-11 23:42:20 +02:00
|
|
|
}
|
2017-05-10 21:29:02 +02:00
|
|
|
|
2018-09-03 06:51:55 +02:00
|
|
|
//Since we don't don't know if advance how many columns the query returns, we have dynamically assign them to be
|
|
|
|
// used in the delete query.
|
|
|
|
colNames, err := rows.Columns()
|
|
|
|
if err != nil {
|
2018-09-08 02:51:50 +02:00
|
|
|
return errors.Err(err)
|
2018-09-03 06:51:55 +02:00
|
|
|
}
|
|
|
|
//Each row result of the query needs to be removed for being a conflicting row. Store each row's keys in an array.
|
|
|
|
var rowsToRemove = [][]interface{}(nil)
|
2017-05-11 23:42:20 +02:00
|
|
|
for rows.Next() {
|
2018-09-03 06:51:55 +02:00
|
|
|
//Set pointers for dynamic scan
|
|
|
|
iColPtrs := make([]interface{}, len(colNames))
|
|
|
|
for i := 0; i < len(colNames); i++ {
|
|
|
|
s := string("")
|
|
|
|
iColPtrs[i] = &s
|
|
|
|
}
|
|
|
|
//Dynamically scan n columns
|
|
|
|
err = rows.Scan(iColPtrs...)
|
2017-05-11 23:42:20 +02:00
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err(err)
|
2017-05-10 21:29:02 +02:00
|
|
|
}
|
2018-09-03 06:51:55 +02:00
|
|
|
//Grab scanned values for query arguments
|
|
|
|
iCol := make([]interface{}, len(colNames))
|
|
|
|
for i, col := range iColPtrs {
|
|
|
|
x := col.(*string)
|
|
|
|
iCol[i] = *x
|
|
|
|
}
|
|
|
|
rowsToRemove = append(rowsToRemove, iCol)
|
2017-06-01 15:58:50 +02:00
|
|
|
}
|
2018-09-03 06:51:55 +02:00
|
|
|
defer rows.Close()
|
2017-06-01 15:58:50 +02:00
|
|
|
|
2018-09-03 06:51:55 +02:00
|
|
|
//This query will adjust dynamically depending on the number of conflicting keys, adding AND expressions for each
|
|
|
|
// key to ensure the right conflicting rows are deleted.
|
2017-05-11 23:42:20 +02:00
|
|
|
query = fmt.Sprintf(
|
2018-09-03 06:51:55 +02:00
|
|
|
"DELETE FROM %s %s",
|
|
|
|
conflict.table,
|
|
|
|
"WHERE "+strings.Join(conflict.columns, " = ? AND ")+" = ?",
|
2017-05-11 23:42:20 +02:00
|
|
|
)
|
|
|
|
|
2018-09-03 06:51:55 +02:00
|
|
|
//There could be multiple conflicting rows between ObjectIDs. In the SELECT query we grab each row and their column
|
|
|
|
// keys to be deleted here in a loop.
|
|
|
|
for _, rowToDelete := range rowsToRemove {
|
|
|
|
rowToDelete = append(rowToDelete, secondaryID)
|
|
|
|
_, err = tx.Exec(query, rowToDelete...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
2017-05-10 21:29:02 +02:00
|
|
|
}
|
2017-05-11 23:42:20 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-24 17:58:13 +02:00
|
|
|
func checkMerge(tx boil.Executor, foreignKeys []foreignKey) error {
|
2017-05-11 23:42:20 +02:00
|
|
|
uniqueColumns := []interface{}{}
|
|
|
|
uniqueColumnNames := map[string]bool{}
|
|
|
|
handledTablesColumns := map[string]bool{}
|
2017-05-10 21:29:02 +02:00
|
|
|
|
2017-05-11 23:42:20 +02:00
|
|
|
for _, fk := range foreignKeys {
|
|
|
|
handledTablesColumns[fk.foreignTable+"."+fk.foreignColumn] = true
|
|
|
|
if _, ok := uniqueColumnNames[fk.foreignColumn]; !ok {
|
|
|
|
uniqueColumns = append(uniqueColumns, fk.foreignColumn)
|
|
|
|
uniqueColumnNames[fk.foreignColumn] = true
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 21:29:02 +02:00
|
|
|
|
2017-05-11 23:42:20 +02:00
|
|
|
q := fmt.Sprintf(
|
|
|
|
`SELECT table_name, column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND column_name IN (%s)`,
|
|
|
|
strmangle.Placeholders(dialect.IndexPlaceholders, len(uniqueColumns), 1, 1),
|
|
|
|
)
|
|
|
|
rows, err := tx.Query(q, uniqueColumns...)
|
2017-05-10 21:29:02 +02:00
|
|
|
defer rows.Close()
|
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err(err)
|
2017-05-10 21:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var tableName string
|
|
|
|
var columnName string
|
|
|
|
err = rows.Scan(&tableName, &columnName)
|
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err(err)
|
2017-05-10 21:29:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 23:42:20 +02:00
|
|
|
if _, exists := handledTablesColumns[tableName+"."+columnName]; !exists {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err("missing merge for " + tableName + "." + columnName)
|
2017-05-10 21:29:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|