...

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

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

     1  package exp
     2  
     3  type (
     4  	joinExpression struct {
     5  		isConditioned bool
     6  		// The JoinType
     7  		joinType JoinType
     8  		// The table expressions (e.g. LEFT JOIN "my_table", ON (....))
     9  		table Expression
    10  	}
    11  	// Container for all joins within a dataset
    12  	conditionedJoin struct {
    13  		joinExpression
    14  		// The condition to join (e.g. USING("a", "b"), ON("my_table"."fkey" = "other_table"."id")
    15  		condition JoinCondition
    16  	}
    17  	JoinExpressions []JoinExpression
    18  )
    19  
    20  func NewUnConditionedJoinExpression(joinType JoinType, table Expression) JoinExpression {
    21  	return joinExpression{
    22  		joinType:      joinType,
    23  		table:         table,
    24  		isConditioned: false,
    25  	}
    26  }
    27  
    28  func (je joinExpression) Clone() Expression {
    29  	return je
    30  }
    31  
    32  func (je joinExpression) Expression() Expression {
    33  	return je
    34  }
    35  
    36  func (je joinExpression) IsConditioned() bool {
    37  	return je.isConditioned
    38  }
    39  
    40  func (je joinExpression) JoinType() JoinType {
    41  	return je.joinType
    42  }
    43  
    44  func (je joinExpression) Table() Expression {
    45  	return je.table
    46  }
    47  
    48  func NewConditionedJoinExpression(joinType JoinType, table Expression, condition JoinCondition) ConditionedJoinExpression {
    49  	return conditionedJoin{
    50  		joinExpression: joinExpression{
    51  			joinType:      joinType,
    52  			table:         table,
    53  			isConditioned: true,
    54  		},
    55  		condition: condition,
    56  	}
    57  }
    58  
    59  func (je conditionedJoin) Clone() Expression {
    60  	return je
    61  }
    62  
    63  func (je conditionedJoin) Expression() Expression {
    64  	return je
    65  }
    66  
    67  func (je conditionedJoin) Condition() JoinCondition {
    68  	return je.condition
    69  }
    70  
    71  func (je conditionedJoin) IsConditionEmpty() bool {
    72  	return je.condition == nil || je.condition.IsEmpty()
    73  }
    74  
    75  func (jes JoinExpressions) Clone() JoinExpressions {
    76  	ret := make(JoinExpressions, 0, len(jes))
    77  	for _, jc := range jes {
    78  		ret = append(ret, jc.Clone().(JoinExpression))
    79  	}
    80  	return ret
    81  }
    82  
    83  type (
    84  	JoinConditionType int
    85  	JoinCondition     interface {
    86  		Type() JoinConditionType
    87  		IsEmpty() bool
    88  	}
    89  	JoinOnCondition interface {
    90  		JoinCondition
    91  		On() ExpressionList
    92  	}
    93  	JoinUsingCondition interface {
    94  		JoinCondition
    95  		Using() ColumnListExpression
    96  	}
    97  	joinOnCondition struct {
    98  		on ExpressionList
    99  	}
   100  
   101  	joinUsingCondition struct {
   102  		using ColumnListExpression
   103  	}
   104  )
   105  
   106  // Creates a new ON clause to be used within a join
   107  //    ds.Join(I("my_table"), On(I("my_table.fkey").Eq(I("other_table.id")))
   108  func NewJoinOnCondition(expressions ...Expression) JoinCondition {
   109  	return joinOnCondition{on: NewExpressionList(AndType, expressions...)}
   110  }
   111  
   112  func (joc joinOnCondition) Type() JoinConditionType {
   113  	return OnJoinCondType
   114  }
   115  
   116  func (joc joinOnCondition) On() ExpressionList {
   117  	return joc.on
   118  }
   119  
   120  func (joc joinOnCondition) IsEmpty() bool {
   121  	return len(joc.on.Expressions()) == 0
   122  }
   123  
   124  // Creates a new USING clause to be used within a join
   125  func NewJoinUsingCondition(expressions ...interface{}) JoinCondition {
   126  	return joinUsingCondition{using: NewColumnListExpression(expressions...)}
   127  }
   128  
   129  func (juc joinUsingCondition) Type() JoinConditionType {
   130  	return UsingJoinCondType
   131  }
   132  
   133  func (juc joinUsingCondition) Using() ColumnListExpression {
   134  	return juc.using
   135  }
   136  
   137  func (juc joinUsingCondition) IsEmpty() bool {
   138  	return len(juc.using.Columns()) == 0
   139  }
   140  

View as plain text