...

Source file src/github.com/doug-martin/goqu/v9/exp/conflict.go

Documentation: github.com/doug-martin/goqu/v9/exp

     1  package exp
     2  
     3  type (
     4  	doNothingConflict struct{}
     5  	// ConflictUpdate is the struct that represents the UPDATE fragment of an
     6  	// INSERT ... ON CONFLICT/ON DUPLICATE KEY DO UPDATE statement
     7  	conflictUpdate struct {
     8  		target      string
     9  		update      interface{}
    10  		whereClause ExpressionList
    11  	}
    12  )
    13  
    14  // Creates a conflict struct to be passed to InsertConflict to ignore constraint errors
    15  //  InsertConflict(DoNothing(),...) -> INSERT INTO ... ON CONFLICT DO NOTHING
    16  func NewDoNothingConflictExpression() ConflictExpression {
    17  	return &doNothingConflict{}
    18  }
    19  
    20  func (c doNothingConflict) Expression() Expression {
    21  	return c
    22  }
    23  
    24  func (c doNothingConflict) Clone() Expression {
    25  	return c
    26  }
    27  
    28  func (c doNothingConflict) Action() ConflictAction {
    29  	return DoNothingConflictAction
    30  }
    31  
    32  // Creates a ConflictUpdate struct to be passed to InsertConflict
    33  // Represents a ON CONFLICT DO UPDATE portion of an INSERT statement (ON DUPLICATE KEY UPDATE for mysql)
    34  //
    35  //  InsertConflict(DoUpdate("target_column", update),...) ->
    36  //  	INSERT INTO ... ON CONFLICT DO UPDATE SET a=b
    37  //  InsertConflict(DoUpdate("target_column", update).Where(Ex{"a": 1},...) ->
    38  //  	INSERT INTO ... ON CONFLICT DO UPDATE SET a=b WHERE a=1
    39  func NewDoUpdateConflictExpression(target string, update interface{}) ConflictUpdateExpression {
    40  	return &conflictUpdate{target: target, update: update}
    41  }
    42  
    43  func (c conflictUpdate) Expression() Expression {
    44  	return c
    45  }
    46  
    47  func (c conflictUpdate) Clone() Expression {
    48  	return &conflictUpdate{
    49  		target:      c.target,
    50  		update:      c.update,
    51  		whereClause: c.whereClause.Clone().(ExpressionList),
    52  	}
    53  }
    54  
    55  func (c conflictUpdate) Action() ConflictAction {
    56  	return DoUpdateConflictAction
    57  }
    58  
    59  // Returns the target conflict column. Only necessary for Postgres.
    60  // Will return an error for mysql/sqlite. Will also return an error if missing from a postgres ConflictUpdate.
    61  func (c conflictUpdate) TargetColumn() string {
    62  	return c.target
    63  }
    64  
    65  // Returns the Updates which represent the ON CONFLICT DO UPDATE portion of an insert statement. If nil,
    66  // there are no updates.
    67  func (c conflictUpdate) Update() interface{} {
    68  	return c.update
    69  }
    70  
    71  // Append to the existing Where clause for an ON CONFLICT DO UPDATE ... WHERE ...
    72  //  InsertConflict(DoNothing(),...) -> INSERT INTO ... ON CONFLICT DO NOTHING
    73  func (c *conflictUpdate) Where(expressions ...Expression) ConflictUpdateExpression {
    74  	if c.whereClause == nil {
    75  		c.whereClause = NewExpressionList(AndType, expressions...)
    76  	} else {
    77  		c.whereClause = c.whereClause.Append(expressions...)
    78  	}
    79  	return c
    80  }
    81  
    82  // Append to the existing Where clause for an ON CONFLICT DO UPDATE ... WHERE ...
    83  //  InsertConflict(DoNothing(),...) -> INSERT INTO ... ON CONFLICT DO NOTHING
    84  func (c *conflictUpdate) WhereClause() ExpressionList {
    85  	return c.whereClause
    86  }
    87  

View as plain text