godbc中使用mssql的小实例

前端之家收集整理的这篇文章主要介绍了godbc中使用mssql的小实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<pre name="code" class="plain">package main

import (
	"fmt"
	"github.com/go-xorm/xorm"
	_ "github.com/lunny/godbc"
)

func main() {
	Engin,err := xorm.NewEngine("odbc","driver={sql Server};Server=127.0.0.1;Database=Test; uid=sa; pwd=123456;")
	if err != nil {
		fmt.Println("新建引擎",err)
		return
	}
	fmt.Println(Engin.Ping())
}

 
package xorm

import (
	"database/sql"
	"testing"

	_ "github.com/lunny/godbc"
)

const mssqlConnStr = "driver={sql Server};Server=192.168.20.135;Database=xorm_test; uid=sa; pwd=1234;"

func newMssqlEngine() (*Engine,error) {
	return NewEngine("odbc",mssqlConnStr)
}

func TestMssql(t *testing.T) {
	engine,err := newMssqlEngine()
	defer engine.Close()
	if err != nil {
		t.Error(err)
		return
	}
	engine.Showsql = showTestsql
	engine.ShowErr = showTestsql
	engine.ShowWarn = showTestsql
	engine.ShowDebug = showTestsql

	testAll(engine,t)
	testAll2(engine,t)
}

func TestMssqlWithCache(t *testing.T) {
	engine,err := newMssqlEngine()
	defer engine.Close()
	if err != nil {
		t.Error(err)
		return
	}
	engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(),1000))
	engine.Showsql = showTestsql
	engine.ShowErr = showTestsql
	engine.ShowWarn = showTestsql
	engine.ShowDebug = showTestsql

	testAll(engine,t)
}

func newMssqlDriverDB() (*sql.DB,error) {
	return sql.Open("odbc",mssqlConnStr)
}

const (
	createTableMssql = `IF NOT EXISTS (SELECT [name] FROM sys.tables WHERE [name] = 'big_struct' ) CREATE TABLE
		"big_struct" ("id" BIGINT PRIMARY KEY IDENTITY NOT NULL,"name" VARCHAR(255) NULL,"title" VARCHAR(255) NULL,"age" VARCHAR(255) NULL,"alias" VARCHAR(255) NULL,"nick_name" VARCHAR(255) NULL);
		`

	dropTableMssql = "IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'big_struct') and OBJECTPROPERTY(id,N'IsUserTable') = 1) DROP TABLE IF EXISTS `big_struct`;"
)

func BenchmarkMssqlDriverInsert(t *testing.B) {
	doBenchDriver(newMssqlDriverDB,createTableMssql,dropTableMssql,doBenchDriverInsert,t)
}

func BenchmarkMssqlDriverFind(t *testing.B) {
	doBenchDriver(newMssqlDriverDB,doBenchDriverFind,t)
}

func BenchmarkMssqlNoCacheInsert(t *testing.B) {
	engine,err := newMssqlEngine()
	defer engine.Close()
	if err != nil {
		t.Error(err)
		return
	}
	//engine.Showsql = true
	doBenchInsert(engine,t)
}

func BenchmarkMssqlNoCacheFind(t *testing.B) {
	engine,err := newMssqlEngine()
	defer engine.Close()
	if err != nil {
		t.Error(err)
		return
	}
	//engine.Showsql = true
	doBenchFind(engine,t)
}

func BenchmarkMssqlNoCacheFindPtr(t *testing.B) {
	engine,err := newMssqlEngine()
	defer engine.Close()
	if err != nil {
		t.Error(err)
		return
	}
	//engine.Showsql = true
	doBenchFindPtr(engine,t)
}

func BenchmarkMssqlCacheInsert(t *testing.B) {
	engine,1000))

	doBenchInsert(engine,t)
}

func BenchmarkMssqlCacheFind(t *testing.B) {
	engine,1000))

	doBenchFind(engine,t)
}

func BenchmarkMssqlCacheFindPtr(t *testing.B) {
	engine,1000))

	doBenchFindPtr(engine,t)
}
原文链接:https://www.f2er.com/go/190212.html

猜你在找的Go相关文章