package back

import (
	"database/sql"
	"fmt"

	"github.com/lib/pq"
)

// тупо чтобы избавиться от тупого предупреждения подключения pq пакета вне main пакете
var o pq.BoolArray

var db *sql.DB

var queries map[string]*sql.Stmt

func initPostgreSQL() {
	var e error

	db, e = sql.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", Setting.PgHost, Setting.PgPort, Setting.PgUser, Setting.PgPass, Setting.PgBase))
	if e != nil {
		panic(e)
	}

	queries = make(map[string]*sql.Stmt)

	prerareQueries()
}

func prerareQueries() {
	var ebox []string

	ebox = append(ebox, prepareQueriesMenu()...)
	ebox = append(ebox, prepareQueriesManager()...)
	ebox = append(ebox, prepareQueriesMaterial()...)
	ebox = append(ebox, prepareQueriesDocument()...)
	ebox = append(ebox, prepareQueriesSession()...)
	ebox = append(ebox, prepareInfa()...)

	if len(ebox) > 0 {
		logger.Println("ERROR PREPARATION QUERIES")
		logger.Println("-------------------------")

		for i, s := range ebox {
			logger.Println(fmt.Sprintf("%d: %s", i+1, s))
		}

		logger.Println("-------------------------")
	}
}
