本文整理汇总了Golang中upper/io/db.Collection.Truncate方法的典型用法代码示例。如果您正苦于以下问题:Golang Collection.Truncate方法的具体用法?Golang Collection.Truncate怎么用?Golang Collection.Truncate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类upper/io/db.Collection
的用法示例。
在下文中一共展示了Collection.Truncate方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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.`)
}
}
示例3: 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.")
}
}
示例4: TestTruncate
// Attempts to get all collections and truncate each one of them.
func TestTruncate(t *testing.T) {
var err error
var sess db.Database
var collections []string
var col db.Collection
// Opening database.
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
// We should close the database when it's no longer in use.
defer sess.Close()
// Getting a list of all collections in this database.
if collections, err = sess.Collections(); err != nil {
t.Fatal(err)
}
if len(collections) == 0 {
t.Fatalf("Expecting some collections.")
}
// Walking over collections.
for _, name := range collections {
// Getting a collection.
if col, err = sess.Collection(name); err != nil {
t.Fatal(err)
}
// Table must exists before we can use it.
if col.Exists() == true {
// Truncating the table.
if err = col.Truncate(); err != nil {
t.Fatal(err)
}
}
}
}
示例5: BenchmarkAppendTxUpperMap
// Benchmarking Append() with map.
func BenchmarkAppendTxUpperMap(b *testing.B) {
var sess db.Database
var err error
if sess, err = db.Open(Adapter, settings); err != nil {
b.Fatal(err)
}
defer sess.Close()
var tx db.Tx
if tx, err = sess.Transaction(); err != nil {
b.Fatal(err)
}
var artist db.Collection
if artist, err = tx.Collection("artist"); err != nil {
b.Fatal(err)
}
if err = artist.Truncate(); err != nil {
b.Fatal(err)
}
item := map[string]string{"name": "Hayao Miyazaki"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err = artist.Append(item); err != nil {
b.Fatal(err)
}
}
if err = tx.Commit(); err != nil {
b.Fatal(err)
}
}
示例6: TestRawRelations
// Attempts to use SQL raw statements.
func TestRawRelations(t *testing.T) {
var sess db.Database
var err error
var artist db.Collection
var publication db.Collection
var review db.Collection
type artistType struct {
ID int64 `db:"id,omitempty"`
Name string `db:"name"`
}
type publicationType struct {
ID int64 `db:"id,omitempty"`
Title string `db:"title"`
AuthorID int64 `db:"author_id"`
}
type reviewType struct {
ID int64 `db:"id,omitempty"`
PublicationID int64 `db:"publication_id"`
Name string `db:"name"`
Comments string `db:"comments"`
Created time.Time `db:"created"`
}
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
// Artist collection.
if artist, err = sess.Collection("artist"); err != nil {
t.Fatal(err)
}
if err = artist.Truncate(); err != nil {
t.Fatal(err)
}
// Publication collection.
if publication, err = sess.Collection("publication"); err != nil {
t.Fatal(err)
}
if err = publication.Truncate(); err != nil {
t.Fatal(err)
}
// Review collection.
if review, err = sess.Collection("review"); err != nil {
t.Fatal(err)
}
if err = review.Truncate(); err != nil {
t.Fatal(err)
}
// Adding some artists.
var miyazakiID interface{}
miyazaki := artistType{Name: `Hayao Miyazaki`}
if miyazakiID, err = artist.Append(miyazaki); err != nil {
t.Fatal(err)
}
miyazaki.ID = miyazakiID.(int64)
var asimovID interface{}
asimov := artistType{Name: `Isaac Asimov`}
if asimovID, err = artist.Append(asimov); err != nil {
t.Fatal(err)
}
var marquezID interface{}
marquez := artistType{Name: `Gabriel García Márquez`}
if marquezID, err = artist.Append(marquez); err != nil {
t.Fatal(err)
}
// Adding some publications.
publication.Append(publicationType{
Title: `Tonari no Totoro`,
AuthorID: miyazakiID.(int64),
})
publication.Append(publicationType{
Title: `Howl's Moving Castle`,
AuthorID: miyazakiID.(int64),
})
publication.Append(publicationType{
Title: `Ponyo`,
AuthorID: miyazakiID.(int64),
})
publication.Append(publicationType{
Title: `Memoria de mis Putas Tristes`,
AuthorID: marquezID.(int64),
//.........这里部分代码省略.........
示例7: TestNullableFields
// Attempts to test nullable fields.
func TestNullableFields(t *testing.T) {
var err error
var sess db.Database
var col db.Collection
var id interface{}
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
type testType struct {
ID int64 `db:"id,omitempty"`
NullStringTest sql.NullString `db:"_string"`
NullInt64Test sql.NullInt64 `db:"_int64"`
NullFloat64Test sql.NullFloat64 `db:"_float64"`
NullBoolTest sql.NullBool `db:"_bool"`
}
var test testType
if col, err = sess.Collection(`data_types`); err != nil {
t.Fatal(err)
}
if err = col.Truncate(); err != nil {
t.Fatal(err)
}
// Testing insertion of invalid nulls.
test = testType{
NullStringTest: sql.NullString{"", false},
NullInt64Test: sql.NullInt64{0, false},
NullFloat64Test: sql.NullFloat64{0.0, false},
NullBoolTest: sql.NullBool{false, false},
}
if id, err = col.Append(testType{}); err != nil {
t.Fatal(err)
}
// Testing fetching of invalid nulls.
if err = col.Find(db.Cond{"id": id}).One(&test); err != nil {
t.Fatal(err)
}
if test.NullInt64Test.Valid {
t.Fatalf(`Expecting invalid null.`)
}
if test.NullFloat64Test.Valid {
t.Fatalf(`Expecting invalid null.`)
}
if test.NullBoolTest.Valid {
t.Fatalf(`Expecting invalid null.`)
}
if test.NullStringTest.Valid {
t.Fatalf(`Expecting invalid null.`)
}
// Testing insertion of valid nulls.
test = testType{
NullStringTest: sql.NullString{"", true},
NullInt64Test: sql.NullInt64{0, true},
NullFloat64Test: sql.NullFloat64{0.0, true},
NullBoolTest: sql.NullBool{false, true},
}
if id, err = col.Append(test); err != nil {
t.Fatal(err)
}
// Testing fetching of valid nulls.
if err = col.Find(db.Cond{"id": id}).One(&test); err != nil {
t.Fatal(err)
}
if test.NullInt64Test.Valid == false {
t.Fatalf(`Expecting valid value.`)
}
if test.NullFloat64Test.Valid == false {
t.Fatalf(`Expecting valid value.`)
}
if test.NullBoolTest.Valid == false {
t.Fatalf(`Expecting valid value.`)
}
if test.NullStringTest.Valid == false {
t.Fatalf(`Expecting valid value.`)
}
}
示例8: TestTransactionsAndRollback
// Attempts to test database transactions.
func TestTransactionsAndRollback(t *testing.T) {
var sess db.Database
var err error
type artistType struct {
ID int64 `db:"id,omitempty"`
Name string `db:"name"`
}
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
// Simple transaction that should not fail.
var tx db.Tx
if tx, err = sess.Transaction(); err != nil {
t.Fatal(err)
}
var artist db.Collection
if artist, err = tx.Collection("artist"); err != nil {
t.Fatal(err)
}
if err = artist.Truncate(); err != nil {
t.Fatal(err)
}
// Simple transaction
if _, err = artist.Append(artistType{1, "First"}); err != nil {
t.Fatal(err)
}
if err = tx.Commit(); err != nil {
t.Fatal(err)
}
// Attempt to use the same transaction should fail.
if _, err = tx.Collection("artist"); err == nil {
t.Fatalf("Illegal, transaction has already been commited.")
}
// Use another transaction.
if tx, err = sess.Transaction(); err != nil {
t.Fatal(err)
}
if artist, err = tx.Collection("artist"); err != nil {
t.Fatal(err)
}
// Won't fail.
if _, err = artist.Append(artistType{2, "Second"}); err != nil {
t.Fatal(err)
}
// Won't fail.
if _, err = artist.Append(artistType{3, "Third"}); err != nil {
t.Fatal(err)
}
// Will fail.
if _, err = artist.Append(artistType{1, "Duplicated"}); err == nil {
t.Fatal("Should have failed, as we have already inserted ID 1.")
}
if err = tx.Rollback(); err != nil {
t.Fatal(err)
}
if err = tx.Commit(); err == nil {
t.Fatalf("Should have failed, as we've already rolled back.")
}
// Let's verify we still have one element.
if artist, err = sess.Collection("artist"); err != nil {
t.Fatal(err)
}
var count uint64
if count, err = artist.Find().Count(); err != nil {
t.Fatal(err)
}
if count != 1 {
t.Fatalf("Expecting only one element.")
}
// Attempt to add some rows.
if tx, err = sess.Transaction(); err != nil {
t.Fatal(err)
}
if artist, err = tx.Collection("artist"); err != nil {
t.Fatal(err)
}
//.........这里部分代码省略.........
示例9: TestExplicitAndDefaultMapping
func TestExplicitAndDefaultMapping(t *testing.T) {
var err error
var col db.Collection
var sess db.Database
var res db.Result
var testE mapE
var testN mapN
for _, wrapper := range wrappers {
if settings[wrapper] == nil {
t.Fatalf(`No such settings entry for wrapper %s.`, wrapper)
} else {
if sess, err = db.Open(wrapper, *settings[wrapper]); err != nil {
t.Fatalf(`Test for wrapper %s failed: %q`, wrapper, err)
}
defer sess.Close()
col, err = sess.Collection("Case_Test")
if col, err = sess.Collection("CaSe_TesT"); err != nil {
if wrapper == `mongo` && err == db.ErrCollectionDoesNotExist {
// Nothing, it's expected.
} else {
t.Fatal(err)
}
}
if err = col.Truncate(); err != nil {
if wrapper == `mongo` {
// Nothing, it's expected.
} else {
t.Fatal(err)
}
}
// Testing explicit mapping.
testE = mapE{
CaseTest: "Hello!",
}
if _, err = col.Append(testE); err != nil {
t.Fatal(err)
}
res = col.Find(db.Cond{"Case_Test": "Hello!"})
if wrapper == `ql` {
res = res.Select(`id() as ID`, `Case_Test`)
}
if err = res.One(&testE); err != nil {
t.Fatal(err)
}
if wrapper == `mongo` {
if testE.MongoID.Valid() == false {
t.Fatalf("Expecting an ID.")
}
} else {
if testE.ID == 0 {
t.Fatalf("Expecting an ID.")
}
}
// Testing default mapping.
testN = mapN{
Casetest: "World!",
}
if _, err = col.Append(testN); err != nil {
t.Fatal(err)
}
if wrapper == `mongo` {
// We don't have this kind of control with mongodb.
res = col.Find(db.Cond{"casetest": "World!"})
} else {
res = col.Find(db.Cond{"Case_Test": "World!"})
}
if wrapper == `ql` {
res = res.Select(`id() as ID`, `Case_Test`)
}
if err = res.One(&testN); err != nil {
t.Fatal(err)
}
if wrapper == `mongo` {
if testN.MongoID.Valid() == false {
t.Fatalf("Expecting an ID.")
}
} else {
if testN.ID == 0 {
t.Fatalf("Expecting an ID.")
}
//.........这里部分代码省略.........