本文整理汇总了Golang中menteslibres/net/gosexy/db.Open函数的典型用法代码示例。如果您正苦于以下问题:Golang Open函数的具体用法?Golang Open怎么用?Golang Open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestOpenFailed
// Trying to open an empty datasource, must fail.
func TestOpenFailed(t *testing.T) {
_, err := db.Open(wrapperName, db.DataSource{})
if err == nil {
t.Errorf("Could not open database.")
}
}
示例2: BenchmarkAppendDbItem_Transaction
// Contributed by wei2912
// See: https://github.com/gosexy/db/issues/20#issuecomment-20167939
// Applying the BEGIN and END transaction optimizations.
func BenchmarkAppendDbItem_Transaction(b *testing.B) {
sess, err := db.Open(wrapperName, settings)
if err != nil {
b.Fatalf(err.Error())
}
defer sess.Close()
people := sess.ExistentCollection("people")
people.Truncate()
err = sess.Begin()
if err != nil {
b.Fatalf(err.Error())
}
for i := 0; i < b.N; i++ {
_, err = people.Append(db.Item{"name": "john"})
if err != nil {
b.Fatalf(err.Error())
}
}
err = sess.End()
if err != nil {
b.Fatalf(err.Error())
}
}
示例3: TestUpdate
// Tries to update rows.
func TestUpdate(t *testing.T) {
var found int
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Fatalf(err.Error())
}
defer sess.Close()
people := sess.ExistentCollection("people")
// Update with map.
people.Update(db.Cond{"name": "José"}, db.Set{"name": "Joseph"})
found, _ = people.Count(db.Cond{"name": "Joseph"})
if found != 1 {
t.Fatalf("Could not update a recently appended item.")
}
// Update with struct.
people.Update(db.Cond{"name": "Joseph"}, struct{ Name string }{"José"})
found, _ = people.Count(db.Cond{"name": "José"})
if found != 1 {
t.Fatalf("Could not update a recently appended item.")
}
}
示例4: TestTruncate
// Truncates all collections/tables, one by one.
func TestTruncate(t *testing.T) {
var err error
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Fatalf(err.Error())
}
defer sess.Close()
collections := sess.Collections()
for _, name := range collections {
col := sess.ExistentCollection(name)
col.Truncate()
total, err := col.Count()
if err != nil {
t.Fatalf(err.Error())
}
if total != 0 {
t.Errorf("Could not truncate.")
}
}
}
示例5: TestPopulate
// Tries to add test data and relations.
func TestPopulate(t *testing.T) {
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Errorf(err.Error())
}
defer sess.Close()
people := sess.ExistentCollection("people")
places := sess.ExistentCollection("places")
children := sess.ExistentCollection("children")
visits := sess.ExistentCollection("visits")
values := []string{"Alaska", "Nebraska", "Alaska", "Acapulco", "Rome", "Singapore", "Alabama", "Cancún"}
for i, value := range values {
places.Append(db.Item{
"code_id": i,
"name": value,
})
}
results, _ := people.FindAll(
db.Fields{"id", "name"},
db.Sort{"name": "ASC", "id": -1},
)
for _, person := range results {
// Has 5 children.
for j := 0; j < 5; j++ {
children.Append(db.Item{
"name": fmt.Sprintf("%s's child %d", person["name"], j+1),
"parent_id": person["id"],
})
}
// Lives in
people.Update(
db.Cond{"id": person["id"]},
db.Set{"place_code_id": int(rand.Float32() * float32(len(results)))},
)
// Has visited
for j := 0; j < 3; j++ {
place, _ := places.Find(db.Cond{
"code_id": int(rand.Float32() * float32(len(results))),
})
visits.Append(db.Item{
"place_id": place["id"],
"person_id": person["id"],
})
}
}
}
示例6: main
func main() {
sess, err := db.Open("postgresql", settings)
if err != nil {
panic(err)
}
defer sess.Close()
animals, err := sess.Collection("animals")
if err != nil {
fmt.Printf("Please create the `animals` table: %s", err.Error())
return
}
animals.Truncate()
animals.Append(db.Item{
"animal": "Bird",
"young": "Chick",
"female": "Hen",
"male": "Cock",
"group": "flock",
})
animals.Append(db.Item{
"animal": "Bovidae",
"young": "Calf",
"female": "Cow",
"male": "Bull",
"group": "Herd",
})
animals.Append(db.Item{
"animal": "Canidae",
"young": "Puppy, Pup",
"female": "Bitch",
"male": "Dog",
"group": "Pack",
})
items, err := animals.FindAll()
if err != nil {
panic(err.Error())
}
for _, item := range items {
fmt.Printf("animal: %s, young: %s\n", item["animal"], item["young"])
}
}
示例7: TestRelation
// Tests relations between collections.
func TestRelation(t *testing.T) {
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Errorf(err.Error())
}
defer sess.Close()
people, _ := sess.Collection("people")
results, _ := people.FindAll(
db.Relate{
"lives_in": db.On{
sess.ExistentCollection("places"),
db.Cond{"code_id": "{place_code_id}"},
},
},
db.RelateAll{
"has_children": db.On{
sess.ExistentCollection("children"),
db.Cond{"parent_id": "{id}"},
},
"has_visited": db.On{
sess.ExistentCollection("visits"),
db.Cond{"person_id": "{id}"},
db.Relate{
"place": db.On{
sess.ExistentCollection("places"),
db.Cond{"id": "{place_id}"},
},
},
},
},
)
fmt.Printf("relations (1) %# v\n", pretty.Formatter(results))
var testv string
testv = dig.String(&results, 0, "lives_in", "name")
if testv == "" {
t.Fatalf("Test failed, expected some value.")
}
testv = dig.String(&results, 1, "has_children", 2, "name")
if testv == "" {
t.Fatalf("Test failed, expected some value.")
}
}
示例8: BenchmarkAppendStruct
func BenchmarkAppendStruct(b *testing.B) {
sess, err := db.Open(wrapperName, settings)
if err != nil {
b.Fatalf(err.Error())
}
defer sess.Close()
people := sess.ExistentCollection("people")
people.Truncate()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = people.Append(struct{ Name string }{"john"})
if err != nil {
b.Fatalf(err.Error())
}
}
}
示例9: TestLimitOffset
// Tests limit and offset.
func TestLimitOffset(t *testing.T) {
var err error
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Fatalf(err.Error())
}
defer sess.Close()
people, _ := sess.Collection("people")
items, _ := people.FindAll(db.Limit(2), db.Offset(1))
if len(items) != 2 {
t.Fatalf("Test failed")
}
}
示例10: TestDelete
// Tries to delete rows.
func TestDelete(t *testing.T) {
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Fatalf(err.Error())
}
defer sess.Close()
people := sess.ExistentCollection("people")
people.Remove(db.Cond{"name": "Juan"})
result, _ := people.Find(db.Cond{"name": "Juan"})
if len(result) > 0 {
t.Fatalf("Could not remove a recently appended item.")
}
}
示例11: BenchmarkAppendRaw
func BenchmarkAppendRaw(b *testing.B) {
sess, err := db.Open(wrapperName, settings)
if err != nil {
b.Fatalf(err.Error())
}
defer sess.Close()
people := sess.ExistentCollection("people")
people.Truncate()
driver := sess.Driver().(*sql.DB)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := driver.Exec(`INSERT INTO people (name) VALUES("john")`)
if err != nil {
b.Fatalf(err.Error())
}
}
}
示例12: TestDataTypes
// Tests datatype conversions.
func TestDataTypes(t *testing.T) {
var res db.Result
var items []db.Item
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Fatalf(err.Error())
}
defer sess.Close()
dataTypes := sess.ExistentCollection("data_types")
dataTypes.Truncate()
ids, err := dataTypes.Append(testValues)
if err != nil {
t.Fatalf(err.Error())
}
found, err := dataTypes.Count(db.Cond{"id": db.Id(ids[0])})
if err != nil {
t.Fatalf(err.Error())
}
if found == 0 {
t.Errorf("Expecting an item.")
}
// Getting and reinserting (a db.Item).
item, _ := dataTypes.Find()
_, err = dataTypes.Append(item)
if err == nil {
t.Fatalf("Expecting duplicated-key error.")
}
delete(item, "id")
_, err = dataTypes.Append(item)
if err != nil {
t.Fatalf(err.Error())
}
// Testing date ranges
items, err = dataTypes.FindAll(db.Cond{
"_date": time.Now(),
})
if err != nil {
t.Fatalf(err.Error())
}
if len(items) > 0 {
t.Fatalf("Expecting no results.")
}
items, err = dataTypes.FindAll(db.Cond{
"_date <=": time.Now(),
})
if err != nil {
t.Fatalf(err.Error())
}
if len(items) != 2 {
t.Fatalf("Expecting some results.")
}
// Testing struct
sresults := []testValuesStruct{}
res, err = dataTypes.Query()
if err != nil {
t.Fatalf(err.Error())
}
err = res.All(&sresults)
if err != nil {
t.Fatalf(err.Error())
}
// Testing struct equality
for _, item := range sresults {
if reflect.DeepEqual(item, testValues) == false {
t.Errorf("Struct is different.")
}
}
// Testing maps
results, _ := dataTypes.FindAll()
//.........这里部分代码省略.........
示例13: TestRelationStruct
// Tests relations between collections using structs.
func TestRelationStruct(t *testing.T) {
var err error
var res db.Result
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Errorf(err.Error())
}
defer sess.Close()
people := sess.ExistentCollection("people")
results := []struct {
Id int
Name string
PlaceCodeId int
LivesIn struct {
Name string
}
HasChildren []struct {
Name string
}
HasVisited []struct {
PlaceId int
Place struct {
Name string
}
}
}{}
res, err = people.Query(
db.Relate{
"LivesIn": db.On{
sess.ExistentCollection("places"),
db.Cond{"code_id": "{PlaceCodeId}"},
},
},
db.RelateAll{
"HasChildren": db.On{
sess.ExistentCollection("children"),
db.Cond{"parent_id": "{Id}"},
},
"HasVisited": db.On{
sess.ExistentCollection("visits"),
db.Cond{"person_id": "{Id}"},
db.Relate{
"Place": db.On{
sess.ExistentCollection("places"),
db.Cond{"id": "{PlaceId}"},
},
},
},
},
)
if err != nil {
t.Fatalf(err.Error())
}
err = res.All(&results)
if err != nil {
t.Fatalf(err.Error())
}
fmt.Printf("relations (2) %# v\n", pretty.Formatter(results))
}
示例14: TestAppend
// Appends maps and structs.
func TestAppend(t *testing.T) {
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Fatalf(err.Error())
}
defer sess.Close()
_, err = sess.Collection("doesnotexists")
if err == nil {
t.Fatalf("Collection should not exists.")
}
people := sess.ExistentCollection("people")
// To be inserted
names := []string{
"Juan",
"José",
"Pedro",
"María",
"Roberto",
"Manuel",
"Miguel",
}
var total int
// Append db.Item
people.Truncate()
for _, name := range names {
people.Append(db.Item{"name": name})
}
total, _ = people.Count()
if total != len(names) {
t.Fatalf("Could not append all items.")
}
// Append map[string]string
people.Truncate()
for _, name := range names {
people.Append(map[string]string{"name": name})
}
total, _ = people.Count()
if total != len(names) {
t.Fatalf("Could not append all items.")
}
// Append map[string]interface{}
people.Truncate()
for _, name := range names {
people.Append(map[string]interface{}{"name": name})
}
total, _ = people.Count()
if total != len(names) {
t.Fatalf("Could not append all items.")
}
// Append struct
people.Truncate()
for _, name := range names {
people.Append(struct {
ignoreMe string
LastName string `ignorenil:"true"`
// Must ignore OtherName and use "name" as column.
OtherName string `field:"name",ignorenil:"true"`
// Should not get inserted.
nothing string
}{"nuff said", "", name, "nothing"})
}
total, _ = people.Count()
if total != len(names) {
t.Fatalf("Could not append all items.")
}
}
示例15: TestFind
// Tries to find and fetch rows.
func TestFind(t *testing.T) {
var err error
var res db.Result
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Fatalf(err.Error())
}
defer sess.Close()
/*
// Testing simultaneous connection.
sess2, err := db.Open(wrapperName, db.DataSource {
Database: "mysql",
Socket: socket,
User: "root",
})
if err != nil {
t.Fatalf(err.Error())
}
defer sess2.Close()
*/
people, _ := sess.Collection("people")
// Testing Find()
item, _ := people.Find(db.Cond{"name": "José"})
if item["name"] != "José" {
t.Fatalf("Could not find a recently appended item.")
}
// Fetch into map slice.
dst := []map[string]string{}
res, err = people.Query(db.Cond{"name": "José"})
if err != nil {
t.Fatalf(err.Error())
}
err = res.All(&dst)
if err != nil {
t.Fatalf(err.Error())
}
if len(dst) != 1 {
t.Fatalf("Could not find a recently appended item.")
}
if dst[0]["name"] != "José" {
t.Fatalf("Could not find a recently appended item.")
}
// Fetch into struct slice.
dst2 := []struct {
foo string
PersonName string `field:"name"`
none string
}{}
res, err = people.Query(db.Cond{"name": "José"})
if err != nil {
t.Fatalf(err.Error())
}
err = res.All(&dst2)
if err != nil {
t.Fatalf(err.Error())
}
if len(dst2) != 1 {
t.Fatalf("Could not find a recently appended item.")
}
if dst2[0].PersonName != "José" {
t.Fatalf("Could not find a recently appended item.")
}
// Fetch into map.
dst3 := map[string]interface{}{}
res, err = people.Query(db.Cond{"name": "José"})
if err != nil {
t.Fatalf(err.Error())
}
err = res.One(&dst3)
if err != nil {
//.........这里部分代码省略.........