package squirrel import ( "bytes" "database/sql" "fmt" "github.com/lann/builder" "strings" ) type insertData struct { PlaceholderFormat PlaceholderFormat RunWith BaseRunner Prefixes exprs Options []string Into string Columns []string Values [][]interface{} Suffixes exprs } func (d *insertData) Exec() (sql.Result, error) { if d.RunWith == nil { return nil, RunnerNotSet } return ExecWith(d.RunWith, d) } func (d *insertData) Query() (*sql.Rows, error) { if d.RunWith == nil { return nil, RunnerNotSet } return QueryWith(d.RunWith, d) } func (d *insertData) QueryRow() RowScanner { if d.RunWith == nil { return &Row{err: RunnerNotSet} } queryRower, ok := d.RunWith.(QueryRower) if !ok { return &Row{err: RunnerNotQueryRunner} } return QueryRowWith(queryRower, d) } func (d *insertData) ToSql() (sqlStr string, args []interface{}, err error) { if len(d.Into) == 0 { err = fmt.Errorf("insert statements must specify a table") return } if len(d.Values) == 0 { err = fmt.Errorf("insert statements must have at least one set of values") return } sql := &bytes.Buffer{} if len(d.Prefixes) > 0 { args, _ = d.Prefixes.AppendToSql(sql, " ", args) sql.WriteString(" ") } sql.WriteString("INSERT ") if len(d.Options) > 0 { sql.WriteString(strings.Join(d.Options, " ")) sql.WriteString(" ") } sql.WriteString("INTO ") sql.WriteString(d.Into) sql.WriteString(" ") if len(d.Columns) > 0 { sql.WriteString("(") sql.WriteString(strings.Join(d.Columns, ",")) sql.WriteString(") ") } sql.WriteString("VALUES ") valuesStrings := make([]string, len(d.Values)) for r, row := range d.Values { valueStrings := make([]string, len(row)) for v, val := range row { e, isExpr := val.(expr) if isExpr { valueStrings[v] = e.sql args = append(args, e.args...) } else { valueStrings[v] = "?" args = append(args, val) } } valuesStrings[r] = fmt.Sprintf("(%s)", strings.Join(valueStrings, ",")) } sql.WriteString(strings.Join(valuesStrings, ",")) if len(d.Suffixes) > 0 { sql.WriteString(" ") args, _ = d.Suffixes.AppendToSql(sql, " ", args) } sqlStr, err = d.PlaceholderFormat.ReplacePlaceholders(sql.String()) return } // Builder // InsertBuilder builds SQL INSERT statements. type InsertBuilder builder.Builder func init() { builder.Register(InsertBuilder{}, insertData{}) } // Format methods // PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the // query. func (b InsertBuilder) PlaceholderFormat(f PlaceholderFormat) InsertBuilder { return builder.Set(b, "PlaceholderFormat", f).(InsertBuilder) } // Runner methods // RunWith sets a Runner (like database/sql.DB) to be used with e.g. Exec. func (b InsertBuilder) RunWith(runner BaseRunner) InsertBuilder { return setRunWith(b, runner).(InsertBuilder) } // Exec builds and Execs the query with the Runner set by RunWith. func (b InsertBuilder) Exec() (sql.Result, error) { data := builder.GetStruct(b).(insertData) return data.Exec() } // Query builds and Querys the query with the Runner set by RunWith. func (b InsertBuilder) Query() (*sql.Rows, error) { data := builder.GetStruct(b).(insertData) return data.Query() } // QueryRow builds and QueryRows the query with the Runner set by RunWith. func (b InsertBuilder) QueryRow() RowScanner { data := builder.GetStruct(b).(insertData) return data.QueryRow() } // Scan is a shortcut for QueryRow().Scan. func (b InsertBuilder) Scan(dest ...interface{}) error { return b.QueryRow().Scan(dest...) } // SQL methods // ToSql builds the query into a SQL string and bound args. func (b InsertBuilder) ToSql() (string, []interface{}, error) { data := builder.GetStruct(b).(insertData) return data.ToSql() } // Prefix adds an expression to the beginning of the query func (b InsertBuilder) Prefix(sql string, args ...interface{}) InsertBuilder { return builder.Append(b, "Prefixes", Expr(sql, args...)).(InsertBuilder) } // Options adds keyword options before the INTO clause of the query. func (b InsertBuilder) Options(options ...string) InsertBuilder { return builder.Extend(b, "Options", options).(InsertBuilder) } // Into sets the INTO clause of the query. func (b InsertBuilder) Into(from string) InsertBuilder { return builder.Set(b, "Into", from).(InsertBuilder) } // Columns adds insert columns to the query. func (b InsertBuilder) Columns(columns ...string) InsertBuilder { return builder.Extend(b, "Columns", columns).(InsertBuilder) } // Values adds a single row's values to the query. func (b InsertBuilder) Values(values ...interface{}) InsertBuilder { return builder.Append(b, "Values", values).(InsertBuilder) } // Suffix adds an expression to the end of the query func (b InsertBuilder) Suffix(sql string, args ...interface{}) InsertBuilder { return builder.Append(b, "Suffixes", Expr(sql, args...)).(InsertBuilder) } // SetMap set columns and values for insert builder from a map of column name and value // note that it will reset all previous columns and values was set if any func (b InsertBuilder) SetMap(clauses map[string]interface{}) InsertBuilder { cols := make([]string, 0, len(clauses)) vals := make([]interface{}, 0, len(clauses)) for col, val := range clauses { cols = append(cols, col) vals = append(vals, val) } b = builder.Set(b, "Columns", cols).(InsertBuilder) b = builder.Set(b, "Values", [][]interface{}{vals}).(InsertBuilder) return b }