github.com/kunlun-qilian/sqlx/v3@v3.0.0/helpers.go (about)

     1  package sqlx
     2  
     3  import (
     4  	"github.com/kunlun-qilian/sqlx/v3/builder"
     5  )
     6  
     7  func InsertToDB(db DBExecutor, model builder.Model, zeroFields []string, additions ...builder.Addition) builder.SqlExpr {
     8  	table := db.T(model)
     9  	cols, vals := table.ColumnsAndValuesByFieldValues(FieldValuesFromModel(table, model, zeroFields...))
    10  	return builder.Insert().Into(table, additions...).Values(cols, vals...)
    11  }
    12  
    13  func AsAssignments(db DBExecutor, model builder.Model, zeroFields ...string) builder.Assignments {
    14  	table := db.T(model)
    15  	return table.AssignmentsByFieldValues(FieldValuesFromModel(table, model, zeroFields...))
    16  }
    17  
    18  func FieldValuesFromModel(table *builder.Table, model builder.Model, zeroFields ...string) builder.FieldValues {
    19  	fieldValues := builder.FieldValuesFromStructByNonZero(model, zeroFields...)
    20  	if autoIncrementCol := table.AutoIncrement(); autoIncrementCol != nil {
    21  		delete(fieldValues, autoIncrementCol.FieldName)
    22  	}
    23  	return fieldValues
    24  }