Guest User

Untitled

a guest
Aug 21st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. //OpenConnection opens a connection to a MySQL database by `connStr`
  2. // or returns error. If `connStr` is empty, error is returned.
  3. //
  4. // Parameters:
  5. // - `connStr`: the URL of the database to connect to
  6. // - `interpolateParams` : should we interpolate parameters?
  7. //
  8. // Returns:
  9. // - pointer to the database connection
  10. // - any errors that happened during this process
  11. func OpenConnection(connStr string, interpolateParams bool) (*sql.DB, *errors.ErrorSt) {
  12. if connStr == "" {
  13. return nil, errors.Database().ReplaceMessage("No database connection string.")
  14. }
  15. connStr = connStr + "?parseTime=true&multiStatements=true"
  16. if interpolateParams {
  17. connStr += "&interpolateParams=true"
  18. }
  19.  
  20. db, err := sql.Open("mysql", connStr)
  21. if err != nil {
  22. return nil, errors.Database().AddDetails(err.Error(), connStr)
  23. }
  24. return db, nil
  25. }
Add Comment
Please, Sign In to add comment