SHARE
TWEET

Untitled

qazedcwsx4 Jun 2nd, 2020 763 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func insertAnime() error {
  2.     tx, err := db.Beginx()
  3.     if err != nil {
  4.         return err
  5.     }
  6.     if err := execAndHandle(tx, "INSERT INTO entry (name, image, description) VALUES ('testName', 'testImage', 'testDescription');");err != nil{
  7.         return err
  8.     }
  9.     if err := execAndHandle(tx, "SELECT @entry_id := LAST_INSERT_ID();");err != nil{
  10.         return err
  11.     }
  12.     if err := execAndHandle(tx, "INSERT INTO series (entry_id) VALUES (@entry_id);");err != nil{
  13.         return err
  14.     }
  15.     if err := execAndHandle(tx, "INSERT INTO anime (series_id, mal_reference) VALUES (LAST_INSERT_ID(), 'testMalReference');");err != nil{
  16.         return err
  17.     }
  18.     if err := execAndHandle(tx, "INSERT INTO episode (entry_id) VALUES (@entry_id);");err != nil{
  19.         return err
  20.     }
  21.     if err := execAndHandle(tx, "SELECT @episode_id := LAST_INSERT_ID();");err != nil{
  22.         return err
  23.     }
  24.     if err := execAndHandle(tx, "INSERT INTO subtitle (episode_id, language, status, subtitle_path) VALUES (@episode_id, 'testLanguage', 'testStatus', 'testSubtitlePath');");err != nil{
  25.         return err
  26.     }
  27.     if err := execAndHandle(tx, "INSERT INTO video (episode_id, video_path) VALUES (@episode_id, 'testVideoPath');");err != nil{
  28.         return err
  29.     }
  30.     err = tx.Commit()
  31.     if err != nil {
  32.         return err
  33.     }
  34.     return nil
  35. }
  36.  
  37. func execAndHandle(tx *sqlx.Tx, query string) error {
  38.     if _, err := tx.Exec(query); err != nil {
  39.         if err := tx.Rollback(); err != nil {
  40.             return err
  41.         }
  42.         return err
  43.     }
  44.     return nil
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top