本文整理汇总了Golang中github.com/lunny/xorm.NewEngine函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEngine函数的具体用法?Golang NewEngine怎么用?Golang NewEngine使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewEngine函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SetEngine
func SetEngine() (err error) {
switch DbCfg.Type {
case "mysql":
orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%[email protected](%s)/%s?charset=utf8",
DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
case "postgres":
orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
case "sqlite3":
os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
default:
return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
}
if err != nil {
return fmt.Errorf("models.init(fail to conntect database): %v", err)
}
// WARNNING: for serv command, MUST remove the output to os.stdout,
// so use log file to instead print to stdout.
execDir, _ := base.ExecDir()
logPath := execDir + "/log/xorm.log"
os.MkdirAll(path.Dir(logPath), os.ModePerm)
f, err := os.Create(logPath)
if err != nil {
return fmt.Errorf("models.init(fail to create xorm.log): %v", err)
}
orm.Logger = f
orm.ShowSQL = true
orm.ShowDebug = true
orm.ShowErr = true
return nil
}
示例2: setEngine
func setEngine() {
var err error
switch DbCfg.Type {
case "mysql":
orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%[email protected]%s/%s?charset=utf8",
DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
case "postgres":
orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
case "sqlite3":
os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
default:
fmt.Printf("Unknown database type: %s\n", DbCfg.Type)
os.Exit(2)
}
if err != nil {
fmt.Printf("models.init(fail to conntect database): %v\n", err)
os.Exit(2)
}
// WARNNING: for serv command, MUST remove the output to os.stdout,
// so use log file to instead print to stdout.
//x.ShowDebug = true
//orm.ShowErr = true
f, err := os.Create("xorm.log")
if err != nil {
fmt.Printf("models.init(fail to create xorm.log): %v\n", err)
os.Exit(2)
}
orm.Logger = f
orm.ShowSQL = true
}
示例3: XConDb
func XConDb() (*xorm.Engine, error) {
switch {
case dbtype == "sqlite":
return xorm.NewEngine("sqlite3", DbName)
case dbtype == "mysql":
return xorm.NewEngine("mysql", "user=mysql [email protected]#9^&* dbname=mysql")
case dbtype == "pgsql":
return xorm.NewEngine("postgres", "user=postgres [email protected]#$%^&* dbname=pgsql sslmode=disable")
}
return nil, errors.New("尚未设定数据库连接")
}
示例4: ConDb
func ConDb() (*xorm.Engine, error) {
switch {
case dbtype == "sqlite":
return xorm.NewEngine("sqlite3", "./data/sqlite.db")
case dbtype == "mysql":
return xorm.NewEngine("mysql", "user=mysql [email protected]#9^&* dbname=mysql")
case dbtype == "pgsql":
// "user=postgres [email protected]#$%^&* dbname=pgsql sslmode=disable maxcons=10 persist=true"
//return xorm.NewEngine("postgres", "host=110.76.39.205 user=postgres password=LhS88root dbname=pgsql sslmode=disable")
return xorm.NewEngine("postgres", "user=postgres password=LhS88root dbname=mzr sslmode=disable")
}
return nil, errors.New("尚未设定数据库连接")
}
示例5: SetDB
//设置数据库
func SetDB() {
path, _ := filepath.Abs("")
c, _ := config.ReadDefault(fmt.Sprintf("%s/admin/conf/databases.conf", path))
driver, _ := c.String("database", "db.driver")
dbname, _ := c.String("database", "db.dbname")
user, _ := c.String("database", "db.user")
password, _ := c.String("database", "db.password")
host, _ := c.String("database", "db.host")
//prefix, _ := c.String("database", "db.prefix")
//数据库链接
var err error
Engine, err = xorm.NewEngine(driver, fmt.Sprintf("%s:%[email protected](%s)/%s?charset=utf8", user, password, host, dbname))
if err != nil {
revel.WARN.Printf("错误: %v", err)
}
//缓存方式是存放到内存中,缓存struct的记录数为1000条
//cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
//Engine.SetDefaultCacher(cacher)
//控制台打印SQL语句
//Engine.ShowSQL = true
//控制台打印调试信息
//Engine.ShowDebug = true
//控制台打印错误信息
//Engine.ShowErr = true
//控制台打印警告信息
//Engine.ShowWarn = true
}
示例6: ConnectDb
func ConnectDb() (*xorm.Engine, error) {
fmt.Println("database type: " + dbtype)
switch {
case dbtype == "sqlite":
return xorm.NewEngine("sqlite3", dbname)
case dbtype == "mysql":
return xorm.NewEngine("mysql", fmt.Sprintf("%v:%[email protected](%v:%v)/%v?charset=%v",
dbuser, dbpassword, dbhost, dbport, dbname, dbcharset))
case dbtype == "pgsql":
return xorm.NewEngine("postgres", fmt.Sprintf("%v:%[email protected](%v:%v)/%v?charset=%v",
dbuser, dbpassword, dbhost, dbport, dbname, dbcharset))
}
return nil, errors.New("No database found!")
}
示例7: setEngine
func setEngine() {
dbName := utils.Cfg.MustValue("db", "name")
dbPwd := utils.Cfg.MustValue("db", "pwd_"+runtime.GOOS)
if runtime.GOOS == "darwin" {
u, err := user.Current()
if err != nil {
beego.Critical("models.init -> fail to get user:", err.Error())
os.Exit(2)
}
dbPwd = utils.Cfg.MustValue("db", "pwd_"+runtime.GOOS+"_"+u.Username)
}
var err error
x, err = xorm.NewEngine("mysql", fmt.Sprintf("%v:%[email protected]%v/%v?charset=utf8",
utils.Cfg.MustValue("db", "user"), dbPwd,
utils.Cfg.MustValue("db", "host"), dbName))
if err != nil {
beego.Critical("models.init -> fail to conntect database:", err.Error())
os.Exit(2)
}
if beego.RunMode != "pro" {
x.ShowDebug = true
x.ShowErr = true
//x.ShowSQL = true
}
beego.Trace("Initialized database ->", dbName)
}
示例8: ConDb
func ConDb() *xorm.Engine {
/*
engine, _ = xorm.NewEngine("sqlite3", "./test.db")
*/
engine, _ = xorm.NewEngine("postgres", "user=postgres [email protected]#$%^&* dbname=pgsql sslmode=disable")
return engine
}
示例9: InitDB
// should be call before pacakge called
func InitDB(driver, dataSource string) (err error) {
x, err = xorm.NewEngine(driver, dataSource)
if err != nil {
return
}
// create tables
return x.Sync(new(Project), new(File))
}
示例10: InitOrm
func InitOrm() *xorm.Engine {
LoadDBConfig()
orm, err := xorm.NewEngine("postgres", fmt.Sprintf("user=%s dbname=%s sslmode=%s",
DBConfig.User, DBConfig.Name, DBConfig.SslMode))
if err != nil {
panic(err)
}
return orm
}
示例11: NewTestEngine
func NewTestEngine(x *xorm.Engine) (err error) {
switch DbCfg.Type {
case "mysql":
x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%[email protected](%s)/%s?charset=utf8",
DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
case "postgres":
x, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
// case "sqlite3":
// os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
// x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
default:
return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
}
if err != nil {
return fmt.Errorf("models.init(fail to conntect database): %v", err)
}
return x.Sync(new(User), new(PublicKey), new(Repository), new(Watch),
new(Action), new(Access), new(Issue), new(Comment))
}
示例12: SetupDb
func SetupDb() (engine *xorm.Engine) {
var (
err error
)
if engine, err = xorm.NewEngine("sqlite3", "/tmp/tmp.db"); err != nil {
panic(err)
}
if err = engine.Sync(new(User)); err != nil {
panic(err)
}
return engine
}
示例13: init
func (s *ServerType) init() {
s.DB, err = xorm.NewEngine("mysql", s.MYSQL_DSN)
if err != nil {
panic(err)
}
s.DB.ShowSQL = s.MYSQL_DEBUG
if s.PHP_CLI == "" {
s.PHP_CLI = DEFAULT_PHP_CLI
}
if s.LogFile != "" {
logFile, err := os.OpenFile(s.LogFile, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
log.SetOutput(logFile)
}
//worker_num为0表示不启用PHP引擎
if s.PHP_WORKER_NUM != 0 {
if s.PHP_TPL_DIR == "" {
s.PHP_TPL_DIR = s.Root + "/static/template/"
}
//php模板引擎
s.PHP = php.NewEngine(s.PHP_WORKER_NUM, s.PHP_CLI, s.PHP_TPL_DIR)
//设置PHP的运行路径
s.PHP.RunDir = s.Root
//初始化
s.PHP.Init()
go s.PHP.EngineLoop()
}
go Session_CheckExpire()
//默认
if s.DefaultAction == "" {
s.DefaultAction = "index"
}
if s.DefaultController == "" {
s.DefaultController = "Base"
}
s.Charset = "utf-8"
}
示例14: init
func init() {
st := NewSuite("xorm")
st.InitF = func() {
st.AddBenchmark("Insert", 2000*ORM_MULTI, XormInsert)
st.AddBenchmark("MultiInsert 100 row", 500*ORM_MULTI, XormInsertMulti)
st.AddBenchmark("Update", 2000*ORM_MULTI, XormUpdate)
st.AddBenchmark("Read", 4000*ORM_MULTI, XormRead)
st.AddBenchmark("MultiRead limit 100", 2000*ORM_MULTI, XormReadSlice)
engine, _ := xorm.NewEngine("mysql", ORM_SOURCE)
engine.SetMaxIdleConns(ORM_MAX_IDLE)
engine.SetMaxConns(ORM_MAX_CONN)
xo = engine.NewSession()
}
}
示例15: init
func init() {
var err error
orm, err = xorm.NewEngine("sqlite3", "./test.db")
if err != nil {
fmt.Println(err)
}
orm.ShowSQL = true
orm.ShowDebug = true
err = orm.Sync(&User{}, &Repository{})
if err != nil {
fmt.Println(err)
}
base.RepoRootPath = "test"
}