Beispiel #1
0
func (l *Language) Insert(optsx ...go2sql.InsertOption) (err error) {
	if !l.IsNewRow() {
		return
	}

	opts := go2sql.InsertOptions(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
	}

	tables, _ := opts.GetTables()

	for _, table := range tables {
		switch table.Name {
		case LanguageColumnAuthor:
			if l.Author.IsEmptyRow() {
				continue
			}
			if err = l.Author.Insert(go2sql.DB(db), table.Tables); err != nil {
				return
			}
			l.AuthorID = l.Author.ID
		default:
			err = fmt.Errorf("go2sql: unknown column %s", table)
			return
		}
	}

	// TODO: support selects
	r, err := db.Exec(`INSERT INTO languages (name, words_stat) VALUES(?, ?)`, l.Name, l.WordsCount)
	if err != nil {
		return
	}
	id, err := r.LastInsertId()
	if err != nil {
		return
	}
	l.ID = uint(id)

	for _, table := range tables {
		switch table.Name {
		case LanguageColumnKeywords:
			for index := range l.Keywords {
				l.Keywords[index].LanguageID = l.ID
				// if err := l.Keywords[index].Update(db); err != nil {
				// 	return
				// }
			}
			keywords := Keywords(l.Keywords)
			if err = keywords.Update(go2sql.DB(db), table.Tables); err != nil {
				return
			}
		case LanguageColumnTeachers:
			teachers := Teachers(l.Teachers)
			if err = teachers.Update(go2sql.DB(db), table.Tables); err != nil {
				return
			}
			for index := range l.Teachers {
				// if l.Teachers[index].ID <= 0 {
				// 	if _, err = l.Teachers[index].Insert(db); err != nil {
				// 		return
				// 	}
				// }
				if _, err = db.Exec("INSERT INTO languages_teachers_xref (language_id, teacher_id) VALUES (?, ?)", l.ID, l.Teachers[index].ID); err != nil {
					return
				}
			}
		default:
			err = fmt.Errorf("go2sql: unknown column %s", table)
			return
		}
	}

	return
}
Beispiel #2
0
func (ls *Languages) Insert(optsx ...go2sql.InsertOption) (err error) {
	if len(*ls) == 0 {
		return
	}

	opts := go2sql.InsertOptions(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
	}

	tables, _ := opts.GetTables()

	for _, table := range tables {
		switch table.Name {
		case LanguageColumnAuthor:
			var authors People
			for _, l := range *ls {
				if l.Author != nil {
					authors = append(authors, l.Author)
				}
			}
			if err = authors.Insert(); err != nil {
				return
			}
		default:
			err = fmt.Errorf("go2sql: unknown table %s", table)
			return
		}
	}

	// TODO: support selects
	stmt, err := db.Prepare(`INSERT INTO languages (name, words_stat) VALUES(?, ?)`)
	if err != nil {
		return
	}
	defer func() {
		er := stmt.Close()
		if err == nil {
			err = er
		} else {
			log.Println(er)
		}
	}()
	for _, l := range *ls {
		var r sql.Result
		r, err = stmt.Exec(l.Name, l.WordsCount)
		if err != nil {
			return
		}
		var id int64
		id, err = r.LastInsertId()
		if err != nil {
			return
		}
		l.ID = uint(id)
	}

	for _, table := range tables {
		switch table.Name {
		case LanguageColumnKeywords:
			var keywords Keywords
			for _, l := range *ls {
				for _, keyword := range l.Keywords {
					keyword.LanguageID = l.ID
					keywords = append(keywords, keyword)
				}
			}
			if err = keywords.Update(go2sql.DB(db), table.Tables); err != nil {
				return
			}
		case LanguageColumnTeachers:
			var teachers Teachers
			for _, l := range *ls {
				for _, teacher := range l.Teachers {
					teacher.LanguageID = l.ID
					teachers = append(teachers, teacher)
				}
			}
			if err = teachers.Update(go2sql.DB(db), table.Tables); err != nil {
				return
			}
			for _, teacher := range teachers {
				if _, err = db.Exec("INSERT INTO languages_teachers_xref (language_id, teacher_id) VALUES (?, ?)", teacher.LanguageID, teacher.ID); err != nil {
					return
				}
			}
			// teachers := Teachers(l.Teachers)
			// if err = teachers.Update(go2sql.DB(db), table.Tables); err != nil {
			// 	return
			// }
			// for index := range l.Teachers {
			// 	if _, err = db.Exec("INSERT INTO languages_teachers_xref (language_id, teacher_id) VALUES (?, ?)", l.ID, l.Teachers[index].ID); err != nil {
			// 		return
			// 	}
			// }
		default:
			err = fmt.Errorf("go2sql: unknown column %s", table)
			return
		}
	}

	return
}