github.com/kunlun-qilian/sqlx/v2@v2.24.0/builder/assignment.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  	"math"
     6  )
     7  
     8  type SqlAssignment interface {
     9  	SqlExpr
    10  	SqlAssignmentMarker
    11  }
    12  
    13  type SqlAssignmentMarker interface {
    14  	asCondition()
    15  }
    16  
    17  func WriteAssignments(e *Ex, assignments ...*Assignment) {
    18  	count := 0
    19  
    20  	for i := range assignments {
    21  		a := assignments[i]
    22  
    23  		if IsNilExpr(a) {
    24  			continue
    25  		}
    26  
    27  		if count > 0 {
    28  			e.WriteQuery(", ")
    29  		}
    30  
    31  		e.WriteExpr(a)
    32  		count++
    33  	}
    34  }
    35  
    36  func ColumnsAndValues(columnOrColumns SqlExpr, values ...interface{}) *Assignment {
    37  	lenOfColumn := 1
    38  	if canLen, ok := columnOrColumns.(interface{ Len() int }); ok {
    39  		lenOfColumn = canLen.Len()
    40  	}
    41  	return &Assignment{columnOrColumns: columnOrColumns, lenOfColumn: lenOfColumn, values: values}
    42  }
    43  
    44  type Assignments []*Assignment
    45  
    46  type Assignment struct {
    47  	SqlAssignmentMarker
    48  
    49  	columnOrColumns SqlExpr
    50  	lenOfColumn     int
    51  
    52  	values []interface{}
    53  }
    54  
    55  func (a *Assignment) IsNil() bool {
    56  	return a == nil || IsNilExpr(a.columnOrColumns) || len(a.values) == 0
    57  }
    58  
    59  func (a *Assignment) Ex(ctx context.Context) *Ex {
    60  	e := Expr("")
    61  	e.Grow(len(a.values))
    62  
    63  	useValues := TogglesFromContext(ctx).Is(ToggleUseValues)
    64  
    65  	if useValues {
    66  		e.WriteGroup(func(e *Ex) {
    67  			e.WriteExpr(ExprBy(func(ctx context.Context) *Ex {
    68  				return a.columnOrColumns.Ex(ContextWithToggles(ctx, Toggles{
    69  					ToggleMultiTable: false,
    70  				}))
    71  			}))
    72  		})
    73  
    74  		if len(a.values) == 1 {
    75  			if s, ok := a.values[0].(SelectStatement); ok {
    76  				e.WriteQueryByte(' ')
    77  				e.WriteExpr(s)
    78  				return e.Ex(ctx)
    79  			}
    80  		}
    81  
    82  		e.WriteQuery(" VALUES ")
    83  
    84  		groupCount := int(math.Round(float64(len(a.values)) / float64(a.lenOfColumn)))
    85  
    86  		for i := 0; i < groupCount; i++ {
    87  			if i > 0 {
    88  				e.WriteQueryByte(',')
    89  			}
    90  
    91  			e.WriteGroup(func(e *Ex) {
    92  				for j := 0; j < a.lenOfColumn; j++ {
    93  					e.WriteHolder(j)
    94  				}
    95  			})
    96  		}
    97  
    98  		e.AppendArgs(a.values...)
    99  
   100  		return e.Ex(ctx)
   101  	}
   102  
   103  	e.WriteExpr(ExprBy(func(ctx context.Context) *Ex {
   104  		return a.columnOrColumns.Ex(ContextWithToggles(ctx, Toggles{
   105  			ToggleMultiTable: false,
   106  		}))
   107  	}))
   108  
   109  	e.WriteQuery(" = ?")
   110  	e.AppendArgs(a.values[0])
   111  
   112  	return e.Ex(ctx)
   113  }