godbc中使用mssql的小实例

发布时间:2019-10-06 发布网站:大佬教程
大佬教程收集整理的这篇文章主要介绍了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)
}

大佬总结

以上是大佬教程为你收集整理的godbc中使用mssql的小实例全部内容,希望文章能够帮你解决godbc中使用mssql的小实例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ群:277859234,请注明来意。