// Wrapper for inserting through a Database struct func BulkInsert(db *sql.DB, analytics []database.Analytic) error { // Base query insertQuery := sq. Insert("visits"). Columns("time", "event", "path", "ip", "platform", "refererDomain", "countryCode") // Add values for each analytic object for _, analytic := range analytics { insertQuery = insertQuery.Values( analytic.Time.Unix(), analytic.Event, analytic.Path, analytic.Ip, analytic.Platform, analytic.RefererDomain, analytic.CountryCode) } // Add database insertQuery = insertQuery.RunWith(db) _, err := insertQuery.Exec() if err != nil { return err } return nil }
// CreateUser converts an incoming JSON body into a User struct func registerUser(usr *gardenswap.User) error { err := usr.SetPassword(usr.Password) if err != nil { return err } err = usr.RotateJWT() if err != nil { return err } query, _, err := sq.Insert(UserType). Columns("id", "name", "email", "jwt", "password"). Values(usr.ID, usr.Name, usr.Email, usr.JWT, usr.PasswordHash). ToSql() if err != nil { return fmt.Errorf("Error creating SQL: %s", err.Error()) } _, err = db.Get().Exec(query) if err != nil { return fmt.Errorf("Error inserting new user: %s", err.Error()) } return nil }
func organizationPost(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() errors.CheckHttpErr(err != nil, "Internal Error", 500) form := r.PostForm // TODO ensure all params are there // TODO confirm admin permissions name := form["name"][0] city := form["city"][0] latitude := form["latitude"][0] longitude := form["longitude"][0] orgStr := []string{name, city, latitude, longitude} orgVals := make([]interface{}, len(orgStr)) for i, s := range orgStr { orgVals[i] = s } // TODO auto assign org_id orgQuery, orgArgs, err := sq.Insert("organization"). Columns([]string{"name", "city", "latitude", "longitude"}...). Values(orgVals...). ToSql() errors.CheckHttpErr(err != nil, "Internal Error", 500) _, err = db.DB.Exec(orgQuery, orgArgs...) if mysqlError, ok := err.(*mysql.MySQLError); ok { errors.CheckHttpErr(mysqlError.Number == 1062, "Organization Already Exists", 409) } errors.CheckHttpErr(err != nil, "Internal Error", 500) w.Write([]byte("Organization created.\n")) }
// insert処理、last inserted idを返却する func (self *Handle) InsertX(obj Model, values map[string]interface{}) int { //values["created_at"] = sq.Expr("NOW()") //values["updated_at"] = sq.Expr("NOW()") b := sq.Insert(obj.GetTable()).SetMap(values) s, args, err := b.ToSql() if err != nil { log.WithFields(log.Fields{ "table": obj.GetTable(), "values": values, "err": err, }).Panic("Fail To Build Insert SQL") } res, err := self.Conn.Exec(s, args...) if err != nil { log.WithFields(log.Fields{ "table": obj.GetTable(), "values": values, "err": err, }).Panic("Fail To Execute Insert SQL") } lastID, err := res.LastInsertId() if err != nil { log.WithFields(log.Fields{ "table": obj.GetTable(), "values": values, "err": err, }).Panic("Fail To get Last Insert ID") } return int(lastID) }
func userPost(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() errors.CheckHttpErr(err != nil, "Internal Error", 500) // TODO generate ids manually instead of sql auto_increment // TODO role assigned by server, not request form := r.PostForm // TODO ensure all params are there // TODO validation of email, password, role // TODO confirm admin permissions // TODO better error handling hashedPassword, err := bcrypt.GenerateFromPassword([]byte(form["password"][0]), bcrypt.DefaultCost) errors.CheckHttpErr(err != nil, "Internal Error", 500) name := form["name"][0] email := form["email"][0] password := string(hashedPassword) usrStr := []string{name, email, password} usrVals := make([]interface{}, len(usrStr)) for i, s := range usrStr { usrVals[i] = s } query, args, err := sq.Insert("user"). Columns([]string{"name", "email", "password"}...). Values(usrVals...). ToSql() errors.CheckHttpErr(err != nil, "Internal Error", 500) _, err = db.DB.Exec(query, args...) if mysqlError, ok := err.(*mysql.MySQLError); ok { errors.CheckHttpErr(mysqlError.Number == 1062, "User Already Exists", 409) } errors.CheckHttpErr(err != nil, "Internal Error", 500) w.Write([]byte("User created.\n")) }