fork github.com/doug-martin
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
package db_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
dbv2 "git.fsdpf.net/go/db/v2"
|
||||
)
|
||||
|
||||
func ExampleDatabase_Begin() {
|
||||
db := getDB()
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
fmt.Println("Error starting transaction", err.Error())
|
||||
}
|
||||
|
||||
// use tx.From to get a dataset that will execute within this transaction
|
||||
update := tx.Update("test_user").
|
||||
Set(dbv2.Record{"last_name": "Ucon"}).
|
||||
Where(dbv2.Ex{"last_name": "Yukon"}).
|
||||
Returning("id").
|
||||
Executor()
|
||||
|
||||
var ids []int64
|
||||
if err := update.ScanVals(&ids); err != nil {
|
||||
if rErr := tx.Rollback(); rErr != nil {
|
||||
fmt.Println("An error occurred while issuing ROLLBACK\n\t", rErr.Error())
|
||||
} else {
|
||||
fmt.Println("An error occurred while updating users ROLLBACK transaction\n\t", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
fmt.Println("An error occurred while issuing COMMIT\n\t", err.Error())
|
||||
} else {
|
||||
fmt.Printf("Updated users in transaction [ids:=%+v]", ids)
|
||||
}
|
||||
// Output:
|
||||
// Updated users in transaction [ids:=[1 2 3]]
|
||||
}
|
||||
|
||||
func ExampleDatabase_BeginTx() {
|
||||
db := getDB()
|
||||
|
||||
ctx := context.Background()
|
||||
tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
|
||||
if err != nil {
|
||||
fmt.Println("Error starting transaction", err.Error())
|
||||
}
|
||||
|
||||
// use tx.From to get a dataset that will execute within this transaction
|
||||
update := tx.Update("test_user").
|
||||
Set(dbv2.Record{"last_name": "Ucon"}).
|
||||
Where(dbv2.Ex{"last_name": "Yukon"}).
|
||||
Returning("id").
|
||||
Executor()
|
||||
|
||||
var ids []int64
|
||||
if err := update.ScanVals(&ids); err != nil {
|
||||
if rErr := tx.Rollback(); rErr != nil {
|
||||
fmt.Println("An error occurred while issuing ROLLBACK\n\t", rErr.Error())
|
||||
} else {
|
||||
fmt.Println("An error occurred while updating users ROLLBACK transaction\n\t", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
fmt.Println("An error occurred while issuing COMMIT\n\t", err.Error())
|
||||
} else {
|
||||
fmt.Printf("Updated users in transaction [ids:=%+v]", ids)
|
||||
}
|
||||
// Output:
|
||||
// Updated users in transaction [ids:=[1 2 3]]
|
||||
}
|
||||
|
||||
func ExampleDatabase_WithTx() {
|
||||
db := getDB()
|
||||
var ids []int64
|
||||
if err := db.WithTx(func(tx *dbv2.TxDatabase) error {
|
||||
// use tx.From to get a dataset that will execute within this transaction
|
||||
update := tx.Update("test_user").
|
||||
Where(dbv2.Ex{"last_name": "Yukon"}).
|
||||
Returning("id").
|
||||
Set(dbv2.Record{"last_name": "Ucon"}).
|
||||
Executor()
|
||||
|
||||
return update.ScanVals(&ids)
|
||||
}); err != nil {
|
||||
fmt.Println("An error occurred in transaction\n\t", err.Error())
|
||||
} else {
|
||||
fmt.Printf("Updated users in transaction [ids:=%+v]", ids)
|
||||
}
|
||||
// Output:
|
||||
// Updated users in transaction [ids:=[1 2 3]]
|
||||
}
|
||||
|
||||
func ExampleDatabase_Dialect() {
|
||||
db := getDB()
|
||||
|
||||
fmt.Println(db.Dialect())
|
||||
|
||||
// Output:
|
||||
// postgres
|
||||
}
|
||||
|
||||
func ExampleDatabase_Exec() {
|
||||
db := getDB()
|
||||
|
||||
_, err := db.Exec(`DROP TABLE "user_role"; DROP TABLE "test_user"`)
|
||||
if err != nil {
|
||||
fmt.Println("Error occurred while dropping tables", err.Error())
|
||||
}
|
||||
fmt.Println("Dropped tables user_role and test_user")
|
||||
// Output:
|
||||
// Dropped tables user_role and test_user
|
||||
}
|
||||
|
||||
func ExampleDatabase_ExecContext() {
|
||||
db := getDB()
|
||||
d := time.Now().Add(50 * time.Millisecond)
|
||||
ctx, cancel := context.WithDeadline(context.Background(), d)
|
||||
defer cancel()
|
||||
_, err := db.ExecContext(ctx, `DROP TABLE "user_role"; DROP TABLE "test_user"`)
|
||||
if err != nil {
|
||||
fmt.Println("Error occurred while dropping tables", err.Error())
|
||||
}
|
||||
fmt.Println("Dropped tables user_role and test_user")
|
||||
// Output:
|
||||
// Dropped tables user_role and test_user
|
||||
}
|
||||
|
||||
func ExampleDatabase_From() {
|
||||
db := getDB()
|
||||
var names []string
|
||||
|
||||
if err := db.From("test_user").Select("first_name").ScanVals(&names); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
} else {
|
||||
fmt.Println("Fetched Users names:", names)
|
||||
}
|
||||
// Output:
|
||||
// Fetched Users names: [Bob Sally Vinita John]
|
||||
}
|
||||
Reference in New Issue
Block a user