func InsertAnswer(db *sqlx.DB, userId int, questionId int, message string) (*Answer, error) { a := Answer{} a.AuthorId = userId a.QuestionId = questionId a.Message = message var err error if err = ValidateAnswer(a); err != nil { return nil, err } qry := sq.Insert("answers"). Columns("author_id", "question_id", "message"). Values(a.AuthorId, a.QuestionId, a.Message). Suffix("RETURNING *"). PlaceholderFormat(sq.Dollar) sql, params, err := qry.ToSql() if err != nil { return nil, err } err = db.Get(&a, sql, params...) dbErr := dbError(err) if dbErr != nil { return nil, dbErr } else { return &a, nil } }
//Create create resource in the db func (tx *Transaction) Create(resource *schema.Resource) error { var cols []string var values []interface{} db := tx.db s := resource.Schema() data := resource.Data() q := sq.Insert(quote(s.GetDbTableName())) for _, attr := range s.Properties { //TODO(nati) support optional value if _, ok := data[attr.ID]; ok { handler := db.handler(&attr) cols = append(cols, quote(attr.ID)) encoded, err := handler.encode(&attr, data[attr.ID]) if err != nil { return fmt.Errorf("SQL Create encoding error: %s", err) } values = append(values, encoded) } } q = q.Columns(cols...).Values(values...) sql, args, err := q.ToSql() if err != nil { return err } return tx.Exec(sql, args...) }
func (p *Place) Create(tx *sql.Tx) error { result, err := sq.Insert("places"). Columns("name", "address_line_one", "address_line_two", "county", "post_code", "country"). Values(p.Row()[1:]...). RunWith(tx). Exec() if err != nil { return err } p.ID, err = result.LastInsertId() if err != nil { return err } return nil }
func seedUsers(db *sql.DB) { users := []User{ {0, "Brian", 19}, {0, "Thomas", 32}, {0, "Tonny", 99}, } _, err := db.Exec("CREATE TABLE IF NOT EXISTS user (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)") if nil != err { log.Fatal(err) } log.Println("Inserting...") for _, user := range users { _, err = squirrel.Insert("user").Columns("name", "age").Values(user.Name, user.Age).RunWith(db).Exec() if nil != err { log.Fatal(err) } } }
// Put inserts a new object func Put(db DB, p *Proxy) error { set := sq.Eq{} for name, f := range p.schema.fields { if !f.opts.autoincr { set[name] = p.v.FieldByName(f.Name).Interface() } } result, err := exec(sq.Insert(p.schema.table).SetMap(set), db) if err != nil { return err } for _, pk := range p.schema.xinfo.pk { f := p.schema.fields[pk] if f.opts.autoincr { id, _ := result.LastInsertId() p.v.FieldByName(f.Name).SetInt(id) break } } return nil }
//fix: id alanı gönderilince ne oluyor? func MultipleInsert(db *gorm.DB, table string, st interface{}, cs interface{}) error { cats, _ := TakeSliceArg(cs) if len(cats) == 0 { return nil } sq := squirrel. Insert(table). Columns(sqlstruct.Columns(st)) for _, cat := range cats { sq = sq.Values(sqlstruct.Values(cat)...) } sql, args, err := sq.ToSql() if err != nil { return err } return db.Exec(sql, args...).Error }
func UpdateM2M(table, col1, col2 string, id int64, ids []int64, db *gorm.DB) error { if len(ids) == 0 { return nil } sql, args, err := squirrel.Delete(table).Where(squirrel.Eq{col1: id}).ToSql() if err != nil { return err } if err := db.Exec(sql, args...).Error; err != nil { return err } if ids[0] == 0 { return nil } query := squirrel.Insert(table).Columns(col1, col2) for _, id2 := range ids { query = query.Values(id, id2) } sql, args, err = query.ToSql() if err != nil { return err } return db.Exec(sql, args...).Error }
func (ingest *Ingestion) createInsertBuilders() { ingest.ledgers = sq.Insert("history_ledgers").Columns( "importer_version", "id", "sequence", "ledger_hash", "previous_ledger_hash", "total_coins", "fee_pool", "base_fee", "base_reserve", "max_tx_set_size", "closed_at", "created_at", "updated_at", "transaction_count", "operation_count", ) ingest.accounts = sq.Insert("history_accounts").Columns( "id", "address", ) ingest.transactions = sq.Insert("history_transactions").Columns( "id", "transaction_hash", "ledger_sequence", "application_order", "account", "account_sequence", "fee_paid", "operation_count", "tx_envelope", "tx_result", "tx_meta", "tx_fee_meta", "signatures", "time_bounds", "memo_type", "memo", "created_at", "updated_at", ) ingest.transaction_participants = sq.Insert("history_transaction_participants").Columns( "history_transaction_id", "history_account_id", ) ingest.operations = sq.Insert("history_operations").Columns( "id", "transaction_id", "application_order", "source_account", "type", "details", ) ingest.operation_participants = sq.Insert("history_operation_participants").Columns( "history_operation_id", "history_account_id", ) ingest.effects = sq.Insert("history_effects").Columns( "history_account_id", "history_operation_id", "\"order\"", "type", "details", ) }