daily pastebin goal
6%
SHARE
TWEET

Untitled

a guest Mar 20th, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // BulkInsertRecords performs a postgres copy of bulk data
  2. func BulkInsertRecords(records chan []string) {
  3.     dbb, _ := sql.Open("postgres", "postgres://postgres:postgres@localhost:5432/records?sslmode=disable")
  4.     dbb.SetMaxIdleConns(5)
  5.     dbb.SetMaxOpenConns(5)
  6.     dbb.SetConnMaxLifetime(0)
  7.  
  8.     txn, err := dbb.Begin()
  9.  
  10.     if err != nil {
  11.         log.Fatal(err)
  12.     }
  13.  
  14.     stmt, _ := txn.Prepare(pq.CopyIn("records", "source", "origin", "destination", "date", "time", "details", "duration"))
  15. //  fmt.Println(stmt)
  16.  
  17.     counter := 0
  18.     for record := range records {
  19.         _, err := stmt.Exec(record[0], record[1], record[2], record[3], record[4], record[5], record[6])
  20.         if err != nil {
  21.             log.Fatal(err)
  22.         }
  23.         counter = counter + 1
  24.     }
  25.     //Flush buffer
  26.     _, err = stmt.Exec()
  27.     if err != nil {
  28.         log.Fatal(err)
  29.     }
  30.  
  31.     err = stmt.Close()
  32.     if err != nil {
  33.         log.Fatal(err)
  34.     }
  35.  
  36.     err = txn.Commit()
  37.     if err != nil {
  38.         log.Fatal(err)
  39.     }
  40. }
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