|
| 1 | +# Oracle NoSQL Database Go SDK: using native structs in put/get/query operations |
| 2 | + |
| 3 | +The Oracle NoSQL go SDK supports directly writing and reading data to/from the NoSQL database using native Go structs. This bypasses having to convert data from structs into NoSQL MapValues and vice versa. |
| 4 | + |
| 5 | +## Directly writing structs using PutRequest |
| 6 | + |
| 7 | +`PutRequest` has an optional field called `StructValue`, which can be used instead of the normal `Value` field which requires a `types.MapValue`. To use this, set the `StructValue` field to a pointer to your native struct: |
| 8 | +``` |
| 9 | + sval := &MyStruct{...} |
| 10 | + putReq := &nosqldb.PutRequest{ |
| 11 | + TableName: tableName, |
| 12 | + StructValue: sval, |
| 13 | + } |
| 14 | +``` |
| 15 | +The fields in the struct will be mapped to NoSQL row columns based on their field names, and optional annotations given in the struct fields. The annotation `nosql:"nnnn"` can be used to specify a database field name to map the struct field to, similar to the go `encoding/json` methods as documented for the [json Marshal function](https://pkg.go.dev/encoding/json#Marshal). If the struct already uses JSON annotations, those will be used if there are no additional `nosql` annotations: |
| 16 | +``` |
| 17 | + type MyStruct struct { |
| 18 | + // Will be written as column "Id" |
| 19 | + Id int |
| 20 | + // Will be written as column "phone_number" |
| 21 | + Phone string `nosql:"phone_number"` |
| 22 | + // Json annotations can be used as well: will be written as column "userAge" |
| 23 | + Age int `json:"userAge"` |
| 24 | + // Timestamp values are supported: this will be written as column "StartDate" |
| 25 | + StartDate time.Time |
| 26 | + } |
| 27 | +``` |
| 28 | +Complex fields (maps, arrays) are supported, provided that their corresponding NoSQL columns are of the same type. |
| 29 | + |
| 30 | +Note: only exported fields will be written. Any private fields (fields starting with a lowercase letter) will be ignored. |
| 31 | + |
| 32 | +Native structs written to the NoSQL database in this way will be serialized directly to the internal NoSQL byte output stream, bypassing all `types.MapValue` processing. As such, using this method is more efficient, using less memory and CPU cycles. |
| 33 | + |
| 34 | + |
| 35 | +## Directly reading structs using GetRequest |
| 36 | + |
| 37 | +There are two methods for retrieving data from NoSQL into native structs: |
| 38 | +1. Supply an allocated struct with primary key fields populated: on successful return, remaining fields in the struct will be filled in with row data |
| 39 | +2. Specify the desired type of struct returned: the SDK will allocate a new struct of this type and populate its fields with row data |
| 40 | + |
| 41 | +### Supplying an existing struct |
| 42 | +``` |
| 43 | + nval := &MyStruct{Id: 10} |
| 44 | + getReq := &nosqldb.GetRequest{ |
| 45 | + TableName: tableName, |
| 46 | + StructValue: nval, |
| 47 | + } |
| 48 | + getRes, err := client.Get(getReq) |
| 49 | + if err != nil { |
| 50 | + // nval now has all row data filled in |
| 51 | + // nval is also available via getRes.StructValue |
| 52 | + } |
| 53 | +``` |
| 54 | + |
| 55 | +### Specifying desired type of allocated struct |
| 56 | +``` |
| 57 | +import "reflect" |
| 58 | +
|
| 59 | +
|
| 60 | + nval := &MyStruct{Id: 10} |
| 61 | + getReq := &nosqldb.GetRequest{ |
| 62 | + TableName: tableName, |
| 63 | + StructValue: nval, |
| 64 | + StructType: reflect.TypeOf((*MyStruct)(nil)).Elem(), |
| 65 | + } |
| 66 | + getRes, err := client.Get(getReq) |
| 67 | + if err != nil { |
| 68 | + // nval is left unchanged |
| 69 | + // getRes.StructValue contains a newly allocated populated struct |
| 70 | + } |
| 71 | +``` |
| 72 | +It is not required to use a native struct for the primary key in the `GetRequest`. A MapValue can be used instead: |
| 73 | +``` |
| 74 | +import "reflect" |
| 75 | +
|
| 76 | + key := &types.MapValue{} |
| 77 | + key.Put("id", 10) |
| 78 | + getReq := &nosqldb.GetRequest{ |
| 79 | + TableName: tableName, |
| 80 | + Key: key, |
| 81 | + StructType: reflect.TypeOf((*MyStruct)(nil)).Elem(), |
| 82 | + } |
| 83 | + getRes, err := client.Get(getReq) |
| 84 | + if err != nil { |
| 85 | + // getRes.StructValue contains a newly allocated populated struct |
| 86 | + } |
| 87 | +``` |
| 88 | +Native structs read from the NoSQL database in this way will be deserialized directly from the internal NoSQL byte input stream, bypassing all `types.MapValue` processing. As such, using this method is more efficient, using less memory and CPU cycles. |
| 89 | + |
| 90 | + |
| 91 | +## Reading native structs in queries |
| 92 | + |
| 93 | +Query results can be internally converted to native structs by specifying `StructType` in `QueryRequest`, and using `GetStructResults`: |
| 94 | + |
| 95 | +``` |
| 96 | +import "reflect" |
| 97 | +
|
| 98 | + stmt = fmt.Sprintf("SELECT * FROM %s", tableName) |
| 99 | + queryReq := &nosqldb.QueryRequest{ |
| 100 | + Statement: stmt, |
| 101 | + StructType: reflect.TypeOf((*MyStruct)(nil)).Elem(), |
| 102 | + } |
| 103 | + queryRes, err := client.Query(queryReq) |
| 104 | + if err != nil { |
| 105 | + // retrieve slice of direct structs using GetStructResults: |
| 106 | + res, err := queryRes.GetStructResults() |
| 107 | + if err != nil { |
| 108 | + // res is a slice of MyStruct |
| 109 | + } |
| 110 | + } |
| 111 | +``` |
| 112 | +Unlike Put and Get, query results will be internally converted from `types.MapValue` to native structs after all query processing is complete. |
| 113 | + |
0 commit comments