12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package database
- import (
- "database/sql"
- "fmt"
- _ "github.com/go-sql-driver/mysql" // This package allow me to use exec command
- "rate-it-api/utils"
- )
- //"github.com/go-yaml/yaml"
- // MysqlExec Allow to execute a new query on mysql database
- func mysqlGetConnection() (*sql.DB, error) {
- config := utils.GetConfig()
- credentials := config.Mysql
- con, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", credentials.User, credentials.Password, credentials.Hostname, credentials.Port, credentials.Database))
- if err != nil {
- return con, err
- }
- return con, nil
- }
- // MysqlExecInsert Allow to execute a new insert query on mysql database
- func MysqlExecInsert(query string) (sql.Result, error) {
- con, err := mysqlGetConnection()
- var result sql.Result
- if err != nil {
- return result, err
- }
- result, err = con.Exec(query)
- if err != nil {
- return result, err
- }
- defer con.Close()
- return result, nil
- }
- // MysqlExecSelect Allow to execute a new insert query on mysql database
- func MysqlExecSelect(query string) (*sql.Rows, error) {
- con, err := mysqlGetConnection()
- var result *sql.Rows
- if err != nil {
- return result, err
- }
- result, err = con.Query(query)
- if err != nil {
- return result, err
- }
- defer con.Close()
- return result, nil
- }
|