Documentation ¶
Index ¶
- Constants
- func GetColsFromStruct(src interface{}) ([]string, error)
- type MonkeyWrench
- func (m *MonkeyWrench) CreateClient(sessionPoolConfig spanner.SessionPoolConfig) error
- func (m *MonkeyWrench) Delete(table string, key spanner.Key) error
- func (m *MonkeyWrench) DeleteKeyRange(table string, startKey, endKey spanner.Key, rangeKind spanner.KeyRangeKind) error
- func (m *MonkeyWrench) DeleteMulti(table string, keys []spanner.Key) error
- func (m *MonkeyWrench) Insert(table string, cols []string, vals []interface{}) error
- func (m *MonkeyWrench) InsertMap(table string, sourceData map[string]interface{}) error
- func (m *MonkeyWrench) InsertMapMulti(table string, sourceData []map[string]interface{}) error
- func (m *MonkeyWrench) InsertMulti(table string, cols []string, sourceData [][]interface{}) error
- func (m *MonkeyWrench) InsertOrUpdate(table string, cols []string, vals []interface{}) error
- func (m *MonkeyWrench) InsertOrUpdateMap(table string, sourceData map[string]interface{}) error
- func (m *MonkeyWrench) InsertOrUpdateMapMulti(table string, sourceData []map[string]interface{}) error
- func (m *MonkeyWrench) InsertOrUpdateMulti(table string, cols []string, sourceData [][]interface{}) error
- func (m *MonkeyWrench) InsertOrUpdateStruct(table string, sourceData interface{}) error
- func (m *MonkeyWrench) InsertOrUpdateStructMulti(table string, sourceData interface{}) error
- func (m *MonkeyWrench) InsertStruct(table string, sourceData interface{}) error
- func (m *MonkeyWrench) InsertStructMulti(table string, sourceData interface{}) error
- func (m *MonkeyWrench) Query(statement string, params ...map[string]interface{}) ([]*spanner.Row, error)
- func (m *MonkeyWrench) QueryCtx(ctx context.Context, statement string, params ...map[string]interface{}) ([]*spanner.Row, error)
- func (m *MonkeyWrench) Read(table string, keys []spanner.KeySet, columns []string) ([]*spanner.Row, error)
- func (m *MonkeyWrench) ReadToStruct(table string, key spanner.Key, dst interface{}) error
- func (m *MonkeyWrench) ReadUsingIndex(table, index string, keys []spanner.KeySet, columns []string) ([]*spanner.Row, error)
- func (m *MonkeyWrench) Update(table string, cols []string, vals []interface{}) error
- func (m *MonkeyWrench) UpdateMap(table string, sourceData map[string]interface{}) error
- func (m *MonkeyWrench) UpdateMapMulti(table string, sourceData []map[string]interface{}) error
- func (m *MonkeyWrench) UpdateMulti(table string, cols []string, sourceData [][]interface{}) error
- func (m *MonkeyWrench) UpdateStruct(table string, sourceData interface{}) error
- func (m *MonkeyWrench) UpdateStructMulti(table string, sourceData interface{}) error
Examples ¶
- GetColsFromStruct
- MonkeyWrench.Delete
- MonkeyWrench.DeleteKeyRange
- MonkeyWrench.DeleteMulti
- MonkeyWrench.Insert
- MonkeyWrench.InsertMap
- MonkeyWrench.InsertMapMulti
- MonkeyWrench.InsertMulti
- MonkeyWrench.InsertOrUpdate
- MonkeyWrench.InsertOrUpdateMap
- MonkeyWrench.InsertOrUpdateMapMulti
- MonkeyWrench.InsertOrUpdateMulti
- MonkeyWrench.InsertOrUpdateStruct
- MonkeyWrench.InsertOrUpdateStructMulti
- MonkeyWrench.InsertStruct
- MonkeyWrench.InsertStructMulti
- MonkeyWrench.Query
- MonkeyWrench.Query (WithParams)
- MonkeyWrench.Read
- MonkeyWrench.Read (Multistruct)
- MonkeyWrench.ReadToStruct
- MonkeyWrench.ReadUsingIndex
- MonkeyWrench.Update
- MonkeyWrench.UpdateMap
- MonkeyWrench.UpdateMapMulti
- MonkeyWrench.UpdateMulti
- MonkeyWrench.UpdateStruct
- MonkeyWrench.UpdateStructMulti
Constants ¶
const ( // FqParentPattern - The pattern to build the fully qualified Cloud Spanner // parent name. FqParentPattern = "projects/%s/instances/%s" // FqDbPattern - The pattern to build the fully qualified Cloud Spanner // database name. FqDbPattern = "projects/%s/instances/%s/databases/%s" )
Variables ¶
This section is empty.
Functions ¶
func GetColsFromStruct ¶
GetColsFromStruct - Get the names of all fields in a struct.
Params:
src interface{} - The struct to get field names from.
Return:
[]string - Slice of field names from the struct. error - An error if it occurred.
Example ¶
ExampleGetColsFromStruct - Example usage of the GetColsFromStruct usage.
// This is a singer. type Singer struct { SingerID int64 FirstName string LastName string } // Get the columns from the struct. cols, err := GetColsFromStruct(&Singer{}) if err != nil { log.Fatal(err) } // Check each expected column is there. for _, col := range cols { fmt.Printf("Found column %s", col) }
Output:
Types ¶
type MonkeyWrench ¶
type MonkeyWrench struct { Context context.Context Project string Instance string Db string Opts []option.ClientOption Client *spanner.Client }
MonkeyWrench - Wrapper for Cloud Spanner.
func (*MonkeyWrench) CreateClient ¶
func (m *MonkeyWrench) CreateClient(sessionPoolConfig spanner.SessionPoolConfig) error
CreateClient - Create a new Spanner client.
Return:
error - An error if it occurred.
func (*MonkeyWrench) Delete ¶
func (m *MonkeyWrench) Delete(table string, key spanner.Key) error
Delete - Delete a row from a table by key.
Params:
table string - The table to delete from. key spanner.Key - The key to delete.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_Delete - Example usage for the Delete function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // Delete a row. if deleteErr := mW.Delete("Singers", spanner.Key{2}); deleteErr != nil { fmt.Fprintf(os.Stderr, "Failed to delete from Spanner. Reason - %+v\n", deleteErr) os.Exit(1) }
Output:
func (*MonkeyWrench) DeleteKeyRange ¶
func (m *MonkeyWrench) DeleteKeyRange(table string, startKey, endKey spanner.Key, rangeKind spanner.KeyRangeKind) error
DeleteKeyRange - Delete a range of rows by key.
Params:
table string - The table to delete rows from. startKey interface{} - The starting value of the range. endKey interface{} - The ending value of the range. rangeKind spanner.KeyRangeKind - The kind of range (includes keys or not)
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_DeleteKeyRange - Example usage for the DeleteKeyRange function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // Delete multiple rows. if deleteErr := mW.DeleteKeyRange("Singers", spanner.Key{2}, spanner.Key{4}, spanner.ClosedClosed); deleteErr != nil { fmt.Fprintf(os.Stderr, "Failed to delete from Spanner. Reason - %+v\n", deleteErr) os.Exit(1) }
Output:
func (*MonkeyWrench) DeleteMulti ¶
func (m *MonkeyWrench) DeleteMulti(table string, keys []spanner.Key) error
DeleteMulti - Delete multiple rows from a table by key.
Params:
table string - The table to delete from. keys []spanner.Key - The list of keys to delete.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_DeleteMulti - Example usage for the DeleteMulti function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // Delete multiple rows. if deleteErr := mW.DeleteMulti("Singers", []spanner.Key{{1}, {4}}); deleteErr != nil { fmt.Fprintf(os.Stderr, "Failed to delete from Spanner. Reason - %+v\n", deleteErr) os.Exit(1) }
Output:
func (*MonkeyWrench) Insert ¶
func (m *MonkeyWrench) Insert(table string, cols []string, vals []interface{}) error
Insert - Insert a row into a table.
The supplied must match the names of the columns.
Params:
table string - The name of the table to insert into. cols []string - The columns to insert data into. vals interface{} - The data to import.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_Insert - Example usage for the Insert function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } singerCols := []string{ "SingerId", "FirstName", "LastName", } // Insert a single row. if err := mW.Insert("Singers", singerCols, []interface{}{1, "Joe", "Bloggs"}); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertMap ¶
func (m *MonkeyWrench) InsertMap(table string, sourceData map[string]interface{}) error
InsertMap - Insert a row, based on a map, into a table.
Params:
table string - The name of the table to insert into. sourceData map[string]interface{} - The map of col => value data to insert into the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertMap - Example usage for the InsertMap function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // Insert a single row. if err := mW.InsertMap("Singers", map[string]interface{}{"SingerId": 1, "FirstName": "Joe", "LastName": "Bloggs"}); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertMapMulti ¶
func (m *MonkeyWrench) InsertMapMulti(table string, sourceData []map[string]interface{}) error
InsertMapMulti - Insert multiple rows, based on maps, into a table.
Params:
table string - The name of the table to insert into. sourceData []map[string]interface{} - Nested map of col => value data to insert into the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertMapMulti - Example usage for the InsertMapMulti function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } singers := []map[string]interface{}{ {"SingerId": 2, "FirstName": "John", "LastName": "Smith"}, {"SingerId": 3, "FirstName": "Anne", "LastName": "Other"}, } // Insert multiple rows. if err := mW.InsertMapMulti("Singers", singers); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertMulti ¶
func (m *MonkeyWrench) InsertMulti(table string, cols []string, sourceData [][]interface{}) error
InsertMulti - Insert multiple rows into a table.
The slice of values supplied must match the names of the columns.
Params:
table string - The name of the table to insert into. cols []string - The columns to insert data into. sourceData [][]interface{} - A slice of data to import.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertMulti - Example usage for the InsertMulti function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } singerCols := []string{ "SingerId", "FirstName", "LastName", } singers := [][]interface{}{ {2, "John", "Smith"}, {3, "Anne", "Other"}, } // Insert multiple rows. if err := mW.InsertMulti("Singers", singerCols, singers); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertOrUpdate ¶
func (m *MonkeyWrench) InsertOrUpdate(table string, cols []string, vals []interface{}) error
InsertOrUpdate - Insert or update a row into a table.
The slice of values supplied must match the names of the columns.
Params:
table string - The name of the table to insert into. cols []string - The columns to insert data into. sourceData [][]interface{} - The values to import.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertOrUpdate - Example usage for the InsertOrUpdate function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } singerCols := []string{ "SingerId", "FirstName", "LastName", } // Insert/update a single row. if err := mW.InsertOrUpdate("Singers", singerCols, []interface{}{1, "J", "Bloggs"}); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertOrUpdateMap ¶
func (m *MonkeyWrench) InsertOrUpdateMap(table string, sourceData map[string]interface{}) error
InsertOrUpdateMap - Insert or update a row, based on a map, into a table.
Params:
table string - The name of the table to insert into. sourceData map[string]interface{} - The map of col => value data to insert into the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertOrUpdateMap - Example usage for the InsertOrUpdateMap function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // Insert/update a single row. if err := mW.InsertOrUpdateMap("Singers", map[string]interface{}{"SingerId": 1, "FirstName": "J", "LastName": "Bloggs"}); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertOrUpdateMapMulti ¶
func (m *MonkeyWrench) InsertOrUpdateMapMulti(table string, sourceData []map[string]interface{}) error
InsertOrUpdateMapMulti - Insert or update multiple rows, based on maps, into a table.
Params:
table string - The name of the table to insert into. sourceData []map[string]interface{} - Nested map of col => value data to insert into the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertOrUpdateMapMulti - Example usage for the InsertOrUpdateMapMulti function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } singers := []map[string]interface{}{ {"SingerId": 2, "FirstName": "J", "LastName": "Smith"}, {"SingerId": 3, "FirstName": "A", "LastName": "Other"}, } // Insert/update multiple rows. if err := mW.InsertOrUpdateMapMulti("Singers", singers); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertOrUpdateMulti ¶
func (m *MonkeyWrench) InsertOrUpdateMulti(table string, cols []string, sourceData [][]interface{}) error
InsertOrUpdateMulti - Insert or update multiple rows into a table.
The slice of values supplied must match the names of the columns.
Params:
table string - The name of the table to insert into. cols []string - The columns to insert data into. sourceData [][]interface{} - The values to import.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertOrUpdateMulti - Example usage for the InsertOrUpdateMulti function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } singerCols := []string{ "SingerId", "FirstName", "LastName", } singers := [][]interface{}{ {2, "J", "Smith"}, {3, "A", "Other"}, } // Insert/update multiple rows. if err := mW.InsertOrUpdateMulti("Singers", singerCols, singers); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertOrUpdateStruct ¶
func (m *MonkeyWrench) InsertOrUpdateStruct(table string, sourceData interface{}) error
InsertOrUpdateStruct - Insert or update a row, based on a struct, into a table.
Params:
table string - The name of the table to insert into. sourceData interface - The data struct to insert into the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertOrUpdateStruct - Example usage for the InsertOrUpdateStruct function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // This is a singer. type Singer struct { SingerID int64 `spanner:"SingerId"` FirstName string LastName string } // Insert/update a single row. if err := mW.InsertOrUpdateStruct("Singers", Singer{SingerID: 1, FirstName: "Joe", LastName: "Bloggs"}); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertOrUpdateStructMulti ¶
func (m *MonkeyWrench) InsertOrUpdateStructMulti(table string, sourceData interface{}) error
InsertOrUpdateStructMulti - Insert or update multiple rows, based on a struct, into a table.
Params:
table string - The name of the table to insert into. sourceData interface - The data struct to insert into the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertOrUpdateStructMulti - Example usage for the InsertOrUpdateStructMulti function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // This is a singer. type Singer struct { SingerID int64 `spanner:"SingerId"` FirstName string LastName string } singers := []Singer{ {SingerID: 2, FirstName: "John", LastName: "Smith"}, {SingerID: 3, FirstName: "Anne", LastName: "Other"}, } // Insert/update multiple rows. if err := mW.InsertOrUpdateStructMulti("Singers", singers); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertStruct ¶
func (m *MonkeyWrench) InsertStruct(table string, sourceData interface{}) error
InsertStruct - Insert a row, based on a struct, into a table.
Params:
table string - The name of the table to insert into. sourceData interface - The data struct to insert into the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertStruct - Example usage for the InsertStruct function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // This is a singer. type Singer struct { SingerID int64 `spanner:"SingerId"` FirstName string LastName string } // Insert a single row. if err := mW.InsertStruct("Singers", Singer{SingerID: 1, FirstName: "Joe", LastName: "Bloggs"}); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) InsertStructMulti ¶
func (m *MonkeyWrench) InsertStructMulti(table string, sourceData interface{}) error
InsertStructMulti - Insert multiple rows, based on a struct, into a table.
Params:
table string - The name of the table to insert into. sourceData interface - The data struct to insert into the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_InsertStructMulti - Example usage for the InsertStructMulti function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // This is a singer. type Singer struct { SingerID int64 `spanner:"SingerId"` FirstName string LastName string } singers := []Singer{ {SingerID: 2, FirstName: "John", LastName: "Smith"}, {SingerID: 3, FirstName: "Anne", LastName: "Other"}, } // Insert multiple rows. if err := mW.InsertStructMulti("Singers", singers); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) Query ¶
func (m *MonkeyWrench) Query(statement string, params ...map[string]interface{}) ([]*spanner.Row, error)
Query - Executes a query against Cloud Spanner.
Example ¶
ExampleMonkeyWrench_Query - Example usage for the Query function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // Run the query. results, err := mW.Query(`SELECT FirstName, LastName FROM Singers`) if err != nil { fmt.Fprintf(os.Stderr, "Failed to query Spanner. Reason - %+v\n", err) os.Exit(1) } // Print the results. for _, result := range results { var FirstName, LastName string // Get each result. result.ColumnByName("FirstName", &FirstName) result.ColumnByName("LastName", &LastName) // What did we get? fmt.Printf("Found singer: %s %s\n", FirstName, LastName) }
Output:
Example (WithParams) ¶
ExampleMonkeyWrench_Query_withParams - Example usage for the Query function with parameters.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // Prepare the query. query := `SELECT FirstName FROM Singers Where LastName = @surname` params := map[string]interface{}{ "surname": "smith", } // Run the query. results, err := mW.Query(query, params) if err != nil { fmt.Fprintf(os.Stderr, "Failed to query Spanner. Reason - %+v\n", err) os.Exit(1) } // Print the results. for _, result := range results { var FirstName, LastName string // Get each result. result.ColumnByName("FirstName", &FirstName) result.ColumnByName("LastName", &LastName) // What did we get? fmt.Printf("Found singer: %s %s\n", FirstName, LastName) }
Output:
func (*MonkeyWrench) QueryCtx ¶
func (m *MonkeyWrench) QueryCtx(ctx context.Context, statement string, params ...map[string]interface{}) ([]*spanner.Row, error)
QueryCtx is the same as Query but allows passing your own cancellable context
func (*MonkeyWrench) Read ¶
func (m *MonkeyWrench) Read(table string, keys []spanner.KeySet, columns []string) ([]*spanner.Row, error)
Read - Read multiple rows from Cloud Spanner.
Params:
table string - Name of the table to read rows from. index string - Name of the index to use from the table. keys []spanner.KeySet - Slice of keys for the rows to read. Passing an empty slice will cause all rows to be returned. columns []string - List of columns to read for each row.
Return:
[]*spanner.Row - A list of all rows returned by the query. error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_Read - Example usage for the Read function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // Run the query for select keys. results, err := mW.Read("Singers", []spanner.KeySet{spanner.Key{1}, spanner.Key{4}}, []string{"FirstName", "LastName"}) if err != nil { fmt.Fprintf(os.Stderr, "Failed to read Spanner. Reason - %+v\n", err) os.Exit(1) } // Print the results. for _, result := range results { var FirstName, LastName string // Get each result. result.ColumnByName("FirstName", &FirstName) result.ColumnByName("LastName", &LastName) // What did we get? fmt.Printf("Found singer: %s %s\n", FirstName, LastName) } // Get all the things! allResults, err := mW.Read("Singers", []spanner.KeySet{}, []string{"FirstName", "LastName"}) if err != nil { fmt.Fprintf(os.Stderr, "Failed to read Spanner. Reason - %+v\n", err) os.Exit(1) } // Print the results. for _, aResult := range allResults { var FirstName, LastName string // Get each result. aResult.ColumnByName("FirstName", &FirstName) aResult.ColumnByName("LastName", &LastName) // What did we get? fmt.Printf("Found singer: %s %s\n", FirstName, LastName) }
Output:
Example (Multistruct) ¶
ExampleMonkeyWrench_Read_multistruct - Example usage of the Read function to get multiple structs from result rows.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // This is a singer. type Singer struct { SingerID int64 `spanner:"SingerId"` FirstName string LastName string } // Get the columns from the struct. structCols, err := GetColsFromStruct(&Singer{}) if err != nil { fmt.Fprintf(os.Stderr, "Failed to get columns from struct. Reason - %+v\n", err) os.Exit(1) } // Get all the things! rows, err := mW.Read("Singers", []spanner.KeySet{}, structCols) if err != nil { fmt.Fprintf(os.Stderr, "Failed to read Spanner. Reason - %+v\n", err) os.Exit(1) } // Print the results for _, row := range rows { var singer Singer row.ToStruct(&singer) fmt.Printf("Found a singer (%d) called %s %s\n", singer.SingerID, singer.FirstName, singer.LastName) }
Output:
func (*MonkeyWrench) ReadToStruct ¶
func (m *MonkeyWrench) ReadToStruct(table string, key spanner.Key, dst interface{}) error
ReadToStruct - Read a row from Spanner table to a struct.
Params:
table string - Name of the table to read from. key spanner.Key - The key for the row to read. dst interface - Destination struct.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_ReadToStruct - Example usage for the ReadToStruct function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // This is a singer. type Singer struct { SingerID int64 `spanner:"SingerId"` FirstName string LastName string } var aSinger Singer // Read a single result to a struct. structErr := mW.ReadToStruct("Singers", spanner.Key{1}, &aSinger) if structErr != nil { fmt.Fprintf(os.Stderr, "Failed to read Spanner. Reason - %+v\n", structErr) os.Exit(1) } // What did we get? fmt.Printf("Found one singer (%d) called %s %s\n", aSinger.SingerID, aSinger.FirstName, aSinger.LastName)
Output:
func (*MonkeyWrench) ReadUsingIndex ¶
func (m *MonkeyWrench) ReadUsingIndex(table, index string, keys []spanner.KeySet, columns []string) ([]*spanner.Row, error)
ReadUsingIndex - Read multiple rows from Cloud Spanner using an index.
Params:
table string - Name of the table to read rows from. index string - Name of the index to use from the table. keys []spanner.KeySet - List of keys for the rows to read. Passing an empty slice will cause all rows to be returned. columns []string - List of columns to read for each row.
Return:
[]*spanner.Row - A list of all rows returned by the query. error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_ReadUsingIndex - Example usage for the ReadUsingIndex function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // Simple index // Index DDL - `CREATE INDEX SingersByLastName ON Singers(LastName)` results, err := mW.ReadUsingIndex("Singers", "SingersByLastName", []spanner.KeySet{}, []string{"LastName"}) if err != nil { fmt.Fprintf(os.Stderr, "Failed to read Spanner using index. Reason - %+v\n", err) os.Exit(1) } // Print the results. for _, result := range results { var LastName string // Get each result. result.ColumnByName("LastName", &LastName) // What did we get? fmt.Printf("Found last name: %s\n", LastName) } // Storing index // Index DDL - `CREATE INDEX SingersByLastNameWithFirstName ON Singers(LastName) STORING (FirstName)` storingResults, err := mW.ReadUsingIndex("Singers", "SingersByLastNameWithFirstName", []spanner.KeySet{}, []string{"FirstName", "LastName"}) if err != nil { fmt.Fprintf(os.Stderr, "Failed to read Spanner using index. Reason - %+v\n", err) os.Exit(1) } // Print the results. for _, sResult := range storingResults { var FirstName, LastName string // Get each result. sResult.ColumnByName("FirstName", &FirstName) sResult.ColumnByName("LastName", &LastName) // What did we get? fmt.Printf("Found singer: %s %s\n", FirstName, LastName) }
Output:
func (*MonkeyWrench) Update ¶
func (m *MonkeyWrench) Update(table string, cols []string, vals []interface{}) error
Update - Update a row in a table.
The supplied must match the names of the columns.
Params:
table string - The name of the table to update. cols []string - The columns to update. vals interface{} - The data to update.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_Update - Example usage for the Update function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } singerCols := []string{ "SingerId", "FirstName", "LastName", } // Update a single row. if err := mW.Update("Singers", singerCols, []interface{}{1, "J", "Bloggs"}); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) UpdateMap ¶
func (m *MonkeyWrench) UpdateMap(table string, sourceData map[string]interface{}) error
UpdateMap - Update a row, based on a map, in a table.
Params:
table string - The name of the table to update. sourceData map[string]interface{} - The map of col => value data to update in the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_UpdateMap - Example usage for the UpdateMap function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // Update a single row. if insertErr := mW.UpdateMap("Singers", map[string]interface{}{"SingerId": 1, "FirstName": "J", "LastName": "Bloggs"}); insertErr != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", insertErr) os.Exit(1) }
Output:
func (*MonkeyWrench) UpdateMapMulti ¶
func (m *MonkeyWrench) UpdateMapMulti(table string, sourceData []map[string]interface{}) error
UpdateMapMulti - Update multiple rows, based on maps, in a table.
Params:
table string - The name of the table to update. sourceData []map[string]interface{} - Nested map of col => value data to update in the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_UpdateMapMulti - Example usage for the UpdateMapMulti function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } singers := []map[string]interface{}{ {"SingerId": 2, "FirstName": "J", "LastName": "Smith"}, {"SingerId": 3, "FirstName": "A", "LastName": "Other"}, } // Update multiple rows. if insertErr := mW.UpdateMapMulti("Singers", singers); insertErr != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", insertErr) os.Exit(1) }
Output:
func (*MonkeyWrench) UpdateMulti ¶
func (m *MonkeyWrench) UpdateMulti(table string, cols []string, sourceData [][]interface{}) error
UpdateMulti - Update multiple rows in a table.
The slice of values supplied must match the names of the columns.
Params:
table string - The name of the table to update cols []string - The columns to update. sourceData [][]interface{} - A slice of data to update.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_UpdateMulti - Example usage for the UpdateMulti function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } singerCols := []string{ "SingerId", "FirstName", "LastName", } singers := [][]interface{}{ {2, "J", "Smith"}, {3, "A", "Other"}, } // Update multiple rows. if err := mW.UpdateMulti("Singers", singerCols, singers); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) UpdateStruct ¶
func (m *MonkeyWrench) UpdateStruct(table string, sourceData interface{}) error
UpdateStruct - Update a row, based on a struct, into a table.
Params:
table string - The name of the table to update. sourceData interface - The data struct to update in the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_UpdateStruct - Example usage for the UpdateStruct function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // This is a singer. type Singer struct { SingerID int64 `spanner:"SingerId"` FirstName string LastName string } // Update a single row. if err := mW.UpdateStruct("Singers", Singer{SingerID: 1, FirstName: "Joe", LastName: "Bloggs"}); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output:
func (*MonkeyWrench) UpdateStructMulti ¶
func (m *MonkeyWrench) UpdateStructMulti(table string, sourceData interface{}) error
UpdateStructMulti - Update multiple rows, based on a struct, in a table.
Params:
table string - The name of the table to update. sourceData interface - The data struct to update in the table.
Return:
error - An error if it occurred.
Example ¶
ExampleMonkeyWrench_UpdateStructMulti - Example usage for the UpdateStructMulti function.
ctx := context.Background() // Create Cloud Spanner wrapper. mW := &MonkeyWrench{ Context: ctx, Project: "my-awesome-project", Instance: "my-awesome-spanner-instance", Db: "my-awesome-spanner-database", } // Create a Spanner client. if spannerErr := mW.CreateClient(); spannerErr != nil { fmt.Fprintf(os.Stderr, "Failed to create Spanner client. Reason - %+v\n", spannerErr) os.Exit(1) } // This is a singer. type Singer struct { SingerID int64 `spanner:"SingerId"` FirstName string LastName string } singers := []Singer{ {SingerID: 2, FirstName: "John", LastName: "Smith"}, {SingerID: 3, FirstName: "Anne", LastName: "Other"}, } // Update multiple rows. if err := mW.UpdateStructMulti("Singers", singers); err != nil { fmt.Fprintf(os.Stderr, "Failed to insert into Spanner. Reason - %+v\n", err) os.Exit(1) }
Output: