package models

import (
	"github.com/go-xorm/core"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"fmt"
)

var x *xorm.Engine

func getEngine() (*xorm.Engine, error){
	connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true",
		"root", "jup2000", "127.0.0.1", "library2")
	return xorm.NewEngine("mysql", connStr)
}


// SetEngine sets the xorm.Engine
func SetEngine() (err error) {
	x, err = getEngine()
	if err != nil {

		return fmt.Errorf("Failed to connect to database: %v", err)
	}

	x.SetMapper(core.GonicMapper{})

	// Sync dat shit
	x.Sync(&Book{})
	x.Sync(&User{})
	x.Sync(&Publisher{})
	x.Sync(&Author{})
	x.Sync(&AuthorBook{})

	x.ShowSQL(true)
	return nil
}
