// Update updates a entry with ID id in the associated table. func (self *Mod) Update(id int, item interface{}) error { entries, err := encoding.Encode(item) if err != nil { return err } vals := make([]interface{}, len(entries)) sql := "UPDATE " + self.table + " SET " // prepare arguments for i := 0; i < len(entries); i++ { sql += entries[i].Column + " = ?," vals[i] = entries[i].Value } // remove last comma sql = sql[:len(sql)-1] sql += " WHERE ID = ?" vals = append(vals, id) _, err = self.db.Execute(sql, vals...) if err != nil { return err } return nil }
// Insert adds a new row the associated table from the given struct. If ignore // is true it ignores duplication conflicts. func (self *Mod) insert(item interface{}, ignore bool) (sql.Result, error) { entries, err := encoding.Encode(item) if err != nil { return nil, err } var sql, cols, qmarks string vals := make([]interface{}, len(entries)) // prepare arguments for i := 0; i < len(entries); i++ { cols += entries[i].Column + "," vals[i] = entries[i].Value qmarks += "?," } // remove last comma cols = cols[:len(cols)-1] qmarks = qmarks[:len(qmarks)-1] if ignore { sql = "INSERT OR IGNORE INTO " + self.table + "(" + cols + ") VALUES(" + qmarks + ")" } else { sql = "INSERT INTO " + self.table + "(" + cols + ") VALUES(" + qmarks + ")" } res, err := self.db.Execute(sql, vals...) if err != nil { return nil, err } return res, nil }