Beispiel #1
0
func (ls *Languages) Delete(optsx ...go2sql.DeleteOption) (err error) {
	opts := go2sql.DeleteOptions(optsx)
	db, ok := opts.GetDB()
	if !ok {
		db = go2sql.DefaultDB.DB
	}
	if db == nil {
		err = errors.New("should specify *sql.DB by go2sql.DB or init go2sql.DefaultDB")
		return
	}

	var ids []interface{}
	var idsCons []string
	for _, l := range *ls {
		ids = append(ids, l.ID)
		idsCons = append(idsCons, "id = ?")
	}
	_, err = db.Exec(`DELETE FROM languages WHERE `+strings.Join(idsCons, " or "), ids...)
	if err != nil {
		return
	}
	tables, _ := opts.GetTables()
	for _, table := range tables {
		switch table.Name {
		case LanguageColumnAuthor:
			var people People
			for _, l := range *ls {
				people = append(people, l.Author)
			}
			err = people.Delete(go2sql.DB(db))
		case LanguageColumnKeywords:
			var keywords Keywords
			for _, l := range *ls {
				keywords = append(keywords, l.Keywords...)
			}
			err = keywords.Delete(go2sql.DB(db))
		case LanguageColumnTeachers:
			var teachers Teachers
			for _, l := range *ls {
				teachers = append(teachers, l.Teachers...)
			}
			err = teachers.Delete(go2sql.DB(db))
		default:
			err = fmt.Errorf("go2sql: unknown column %s", table)
		}
		if err != nil {
			return
		}
	}

	return
}
Beispiel #2
0
func (l *Language) Delete(optsx ...go2sql.DeleteOption) (err error) {
	if l == nil || l.IsEmptyRow() {
		return
	}

	opts := go2sql.DeleteOptions(optsx)
	db, ok := opts.GetDB()
	if !ok {
		db = go2sql.DefaultDB.DB
	}
	if db == nil {
		err = errors.New("should specify *sql.DB by go2sql.DB or init go2sql.DefaultDB")
		return
	}

	_, err = db.Exec(`DELETE FROM languages WHERE id = ?`, l.ID)
	if err != nil {
		return
	}
	tables, _ := opts.GetTables()
	for _, table := range tables {
		switch table.Name {
		case LanguageColumnAuthor:
			err = l.Author.Delete(go2sql.DB(db), table.Tables)
		case LanguageColumnKeywords:
			keywords := Keywords(l.Keywords)
			err = keywords.Delete(go2sql.DB(db), table.Tables)
		case LanguageColumnTeachers:
			teachers := Teachers(l.Teachers)
			err = teachers.Delete(go2sql.DB(db), table.Tables)
		default:
			err = fmt.Errorf("go2sql: unknown column %s", table)
		}
		if err != nil {
			return
		}
	}

	return
}