V
V
Vladimir Grabko2016-06-02 15:12:14
MySQL
Vladimir Grabko, 2016-06-02 15:12:14

Should I prepare all requests in init?

Reading the doc, as far as I understand, the prepared query then simply sends the data to the server, which increases the performance of mysql. Reading the muscle driver dock, I found out that it automatically makes a pool of connections. And it occurred to me to make a global map (for the package) and in init define all requests in this map (Prepare), and then receive the request object through the helper and just send data (Exec).
Is it worth it to do this at all, or should I shove Prepare throughout the code?
Since they cannot understand me, this is what I described

package main

import (
  "database/sql"
  "log"

  _ "github.com/go-sql-driver/mysql"
  "github.com/v-grabko/Core/Net/RPC/TCP/Server"
)

var DB *sql.DB
var P map[string]*sql.Stmt

func init() {
  db, err := sql.Open("mysql", "root:[email protected](127.0.0.1:3306)/AuthService")
  if err != nil {
    log.Fatal(err)
  }
  db.SetMaxIdleConns(50)
  P = make(map[string]*sql.Stmt)
  P["users"], err = db.Prepare("INSERT INTO users(login, email, pass) VALUES( ?, ?, ?)")
  if err != nil {
    log.Fatalf(err.Error())
  }

  DB = db

}

func main() {
  Server.Add("Save", Save)
  Server.Run("", "5000", "password")
}

func Save(ps map[string]string) map[string]string {
  var id int
  err := DB.QueryRow("SELECT id FROM users WHERE login=? AND email=?", ps["login"], ps["email"]).Scan(&id)
  switch {
  case err == sql.ErrNoRows:

    _, err = P["users"].Exec(ps["login"], ps["email"], ps["pass"])
    if err != nil {
      panic(err.Error())
    }
    ps["code"] = "1"
    ps["message"] = "Ok"
  case err != nil:
    log.Println(err)
    ps["code"] = "500"
    ps["message"] = "Ошибка сервера"
  default:
    ps["code"] = "2"
    ps["message"] = "Такой пользователь уже есть"
  }

  return ps
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
index0h, 2016-06-02
@VGrabko

It's not worth it with all your might. The basic initialization of the application should (for good) have nothing to do with your business logic (BL). In your case, this is a gross violation of SOLID (SRP).
An example from the "something went wrong" series: your prepared query has changed during execution, the code using it will expect the old behavior, but will fall off (this is at best) only after the query is executed, and there can be anything. As a result: the behavior of the methods using these queries changes without changing their code. Such errors are searched for a long time.
-- upd --
Global public variables are usually a bad idea)) What will you do if you get another connection to the database? Rewrite all code?
Your Save function does select, why? Well, this is a clear contradiction to the name.
As for connecting to the database, I recommend putting it in the settings file, or the launch flag.

S
sim3x, 2016-06-02
@sim3x

Depends on how many of the prepared queries are executed
If more - use View DBMS
but better use lazy technique - the fastest query, the one that does not need to be executed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question