本文整理汇总了Golang中upper/io/db.Database.Driver方法的典型用法代码示例。如果您正苦于以下问题:Golang Database.Driver方法的具体用法?Golang Database.Driver怎么用?Golang Database.Driver使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类upper/io/db.Database
的用法示例。
在下文中一共展示了Database.Driver方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BenchmarkAppendTxRawSQL
// Benchmarking raw database/sql.
func BenchmarkAppendTxRawSQL(b *testing.B) {
var err error
var sess db.Database
var tx *sql.Tx
if sess, err = db.Open(Adapter, settings); err != nil {
b.Fatal(err)
}
defer sess.Close()
driver := sess.Driver().(*sql.DB)
if tx, err = driver.Begin(); err != nil {
b.Fatal(err)
}
if _, err = tx.Exec("TRUNCATE TABLE `artist`"); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err = tx.Exec("INSERT INTO `artist` (`name`) VALUES('Hayao Miyazaki')"); err != nil {
b.Fatal(err)
}
}
if err = tx.Commit(); err != nil {
b.Fatal(err)
}
}
示例2: TestSetup
func TestSetup(t *testing.T) {
var err error
for _, wrapper := range wrappers {
if settings[wrapper] == nil {
t.Fatalf(`No such settings entry for wrapper %s.`, wrapper)
} else {
var sess db.Database
sess, err = db.Open(wrapper, *settings[wrapper])
if err != nil {
t.Fatalf(`Test for wrapper %s failed: %q`, wrapper, err)
}
if setupFn[wrapper] == nil {
t.Fatalf(`Missing setup function for wrapper %s.`, wrapper)
} else {
if err = setupFn[wrapper](sess.Driver()); err != nil {
t.Fatalf(`Failed to setup wrapper %s: %q`, wrapper, err)
}
}
err = sess.Close()
if err != nil {
t.Fatalf(`Could not close %s: %q`, wrapper, err)
}
}
}
}
示例3: SetupCollection
func SetupCollection() (col db.Collection) {
var (
err error
sess db.Database
settings db.Settings
)
settings = db.Settings{
Database: `:memory:`,
}
if sess, err = db.Open("sqlite", settings); err != nil {
panic(err)
}
if _, err = sess.Driver().(*sql.DB).Exec(`CREATE TABLE users (
id INTEGER,
first_name VARCHAR(80),
last_name VARCHAR(80),
age INTEGER
);
`); err != nil {
panic(err)
}
if col, err = sess.Collection("users"); err != nil {
panic(err)
}
return col
}
示例4: BenchmarkAppendRawSQL
// Benchmarking raw database/sql.
func BenchmarkAppendRawSQL(b *testing.B) {
var err error
var sess db.Database
if sess, err = db.Open(Adapter, settings); err != nil {
b.Fatal(err)
}
defer sess.Close()
driver := sess.Driver().(*sql.DB)
if _, err = driver.Exec(`DELETE FROM "artist"`); err != nil {
b.Fatal(err)
}
b.ResetTimer()
stmt, err := driver.Prepare(
`INSERT INTO "artist" ("name") VALUES('Hayao Miyazaki')`)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
if _, err = stmt.Exec(); err != nil {
b.Fatal(err)
}
}
}
示例5: TestRawQuery
func TestRawQuery(t *testing.T) {
var sess db.Database
var rows *sql.Rows
var err error
var drv *sql.DB
type publicationType struct {
ID int64 `db:"id,omitempty"`
Title string `db:"title"`
AuthorID int64 `db:"author_id"`
}
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
drv = sess.Driver().(*sql.DB)
rows, err = drv.Query(`
SELECT
p.id,
p.title AS publication_title,
a.name AS artist_name
FROM
artist AS a,
publication AS p
WHERE
a.id = p.author_id
`)
if err != nil {
t.Fatal(err)
}
var all []publicationType
if err = sqlutil.FetchRows(rows, &all); err != nil {
t.Fatal(err)
}
if len(all) != 9 {
t.Fatalf("Expecting some rows.")
}
}