本文整理汇总了Golang中upper/io/db.Database类的典型用法代码示例。如果您正苦于以下问题:Golang Database类的具体用法?Golang Database怎么用?Golang Database使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Database类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BenchmarkAppendUpper
// Benchmarking Append().
//
// Contributed by wei2912
// See: https://github.com/gosexy/db/issues/20#issuecomment-20097801
func BenchmarkAppendUpper(b *testing.B) {
var sess db.Database
var artist db.Collection
var err error
if sess, err = db.Open(Adapter, settings); err != nil {
b.Fatal(err)
}
defer sess.Close()
if artist, err = sess.Collection("artist"); err != nil {
b.Fatal(err)
}
artist.Truncate()
item := struct {
Name string `db:"name"`
}{"Hayao Miyazaki"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err = artist.Append(item); err != nil {
b.Fatal(err)
}
}
}
示例2: 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
}
示例3: 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)
}
}
示例4: TestResultCount
// Attempts to count all rows in our newly defined set.
func TestResultCount(t *testing.T) {
var err error
var res db.Result
var sess db.Database
var artist db.Collection
var total uint64
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
// We should close the database when it's no longer in use.
if artist, err = sess.Collection("artist"); err != nil {
t.Fatal(err)
}
// Defining a set with no conditions.
res = artist.Find()
// Counting all the matching rows.
if total, err = res.Count(); err != nil {
t.Fatal(err)
}
if total == 0 {
t.Fatalf("Counter should not be zero, we've just added some rows!")
}
}
示例5: 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)
}
}
}
}
示例6: 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)
}
}
}
示例7: TestSetterAndConstrainer
// MongoDB: Does not support schemas so it can't has composite keys. We're
// testing db.Constrainer and db.IDSetter interface.
func TestSetterAndConstrainer(t *testing.T) {
var err error
var id interface{}
var sess db.Database
var compositeKeys db.Collection
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
if compositeKeys, err = sess.Collection("composite_keys"); err != nil {
if err != db.ErrCollectionDoesNotExist {
t.Fatal(err)
}
}
//n := rand.Intn(100000)
item := itemWithKey{
// "ABCDEF",
// strconv.Itoa(n),
SomeVal: "Some value",
}
if id, err = compositeKeys.Append(&item); err != nil {
t.Fatal(err)
}
// ids := id.([]interface{})
// if ids[0].(string) != item.Code {
// t.Fatal(`Keys must match.`)
// }
//
// if ids[1].(string) != item.UserID {
// t.Fatal(`Keys must match.`)
// }
// Using constraint interface.
res := compositeKeys.Find(itemWithKey{ID: id.(bson.ObjectId)})
var item2 itemWithKey
if item2.SomeVal == item.SomeVal {
t.Fatal(`Values must be different before query.`)
}
if err := res.One(&item2); err != nil {
t.Fatal(err)
}
if item2.SomeVal != item.SomeVal {
t.Fatal(`Values must be equal after query.`)
}
}
示例8: TestGroup
func TestGroup(t *testing.T) {
var err error
var sess db.Database
var stats db.Collection
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
type statsType struct {
Numeric int `db:"numeric"`
Value int `db:"value"`
}
defer sess.Close()
if stats, err = sess.Collection("stats_test"); err != nil {
t.Fatal(err)
}
// Truncating table.
if err = stats.Truncate(); err != nil {
t.Fatal(err)
}
// Adding row append.
for i := 0; i < 1000; i++ {
numeric, value := rand.Intn(10), rand.Intn(100)
if _, err = stats.Append(statsType{numeric, value}); err != nil {
t.Fatal(err)
}
}
// db.Func{"COUNT", 1},
// db.Func{"SUM", `value`},
// Testing GROUP BY
res := stats.Find().Select(
`numeric`,
db.Raw{`COUNT(1) AS counter`},
db.Raw{`SUM(value) AS total`},
).Group(`numeric`)
var results []map[string]interface{}
if err = res.All(&results); err != nil {
t.Fatal(err)
}
if len(results) != 10 {
t.Fatalf(`Expecting exactly 10 results, this could fail, but it's very unlikely to happen.`)
}
}
示例9: TestFunction
// Attempts to use functions within database queries.
func TestFunction(t *testing.T) {
var err error
var res db.Result
var sess db.Database
var artist db.Collection
var total uint64
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
if artist, err = sess.Collection("artist"); err != nil {
t.Fatal(err)
}
row_s := struct {
Id uint64
Name string
}{}
res = artist.Find(db.Cond{"id NOT IN": []int{0, -1}})
if err = res.One(&row_s); err != nil {
t.Fatal(err)
}
if total, err = res.Count(); err != nil {
t.Fatal(err)
}
if total != 3 {
t.Fatalf("Expecting 3 items.")
}
res = artist.Find(db.Cond{"id": db.Func{"NOT IN", []int{0, -1}}})
if err = res.One(&row_s); err != nil {
t.Fatal(err)
}
if total, err = res.Count(); err != nil {
t.Fatal(err)
}
if total != 3 {
t.Fatalf("Expecting 3 items.")
}
res.Close()
}
示例10: TestDataTypes
// Attempts to add many different datatypes to a single row in a collection,
// then it tries to get the stored datatypes and check if the stored and the
// original values match.
func TestDataTypes(t *testing.T) {
var res db.Result
var sess db.Database
var dataTypes db.Collection
var err error
var id interface{}
var exists uint64
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
// Getting a pointer to the "data_types" collection.
if dataTypes, err = sess.Collection("data_types"); err != nil {
t.Fatal(err)
}
// Removing all data.
if err = dataTypes.Truncate(); err != nil {
t.Fatal(err)
}
// Appending our test subject.
if id, err = dataTypes.Append(testValues); err != nil {
t.Fatal(err)
}
// Defining our set.
res = dataTypes.Find(db.Cond{"id": id})
if exists, err = res.Count(); err != nil {
t.Fatal(err)
}
if exists == 0 {
t.Fatalf("Expecting an item.")
}
// Trying to dump the subject into an empty structure of the same type.
var item testValuesStruct
res.One(&item)
// The original value and the test subject must match.
if reflect.DeepEqual(item, testValues) == false {
t.Fatalf("Struct is different.")
}
}
示例11: TestOldSettings
// Old settings must be compatible.
func TestOldSettings(t *testing.T) {
var err error
var sess db.Database
oldSettings := db.Settings{
Database: database,
}
// Opening database.
if sess, err = db.Open(Adapter, oldSettings); err != nil {
t.Fatal(err)
}
// Closing database.
sess.Close()
}
示例12: TestCompositeKeys
// Attempts to test composite keys.
func TestCompositeKeys(t *testing.T) {
var err error
var sess db.Database
var compositeKeys db.Collection
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
if compositeKeys, err = sess.Collection("composite_keys"); err != nil {
t.Fatal(err)
}
n := rand.Intn(100000)
item := itemWithKey{
"ABCDEF",
strconv.Itoa(n),
"Some value",
}
if _, err = compositeKeys.Append(&item); err != nil {
t.Fatal(err)
}
// Using constraint interface.
var item2 itemWithKey
if item2.SomeVal == item.SomeVal {
t.Fatal(`Values must be different before query.`)
}
res := compositeKeys.Find(item)
if err := res.One(&item2); err != nil {
t.Fatal(err)
}
if item2.SomeVal != item.SomeVal {
t.Fatal(`Values must be equal after query.`)
}
}
示例13: 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.")
}
}
示例14: TestUse
// Test USE
func TestUse(t *testing.T) {
var err error
var sess db.Database
// Opening database, no error expected.
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
// Connecting to another database, error expected.
if err = sess.Use("Another database"); err == nil {
t.Fatal("This database does not exists!")
}
// Closing connection.
sess.Close()
}
示例15: TestOpen
func TestOpen(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)
}
err = sess.Close()
if err != nil {
t.Fatalf(`Test for wrapper %s failed: %q`, wrapper, err)
}
}
}
}