The results from database read and write operations must be properly processed before being utilized in your application.
Return a Response
Methods such as Gql()
and Uql()
return a Response
value. To serve the application, you need to first extract the DataItem
from the Response
value, and then transform it into an appropriate driver data struct.
A Response
value includes the following fields:
Field |
Type |
Description |
---|---|---|
Aliases |
[]Alias |
The list of result aliases; each Alias includes fields Name and Type . |
Items |
map[string]struct{} | A map where each key is an alias name and each value is the corresponding data item. |
ExplainPlan |
ExplainPlan |
The execution plan. |
Status |
Status |
The status of the execution, inlcuding fields Code and Message . |
Statistics |
Statistics |
Statistics related to the execution, including fields NodeAffected , EdgeAffected , TotalCost , and EngineCost . |
Extract DataItem
To extract DataItem
from a Response
value, use the Get()
or Alias()
method.
A DataItem
value includes the following fields:
Field |
Type |
Description |
---|---|---|
Alias |
string | The alias name. |
Type |
ResultType |
The type of the results. |
Data |
interface{} | The data contained in the results. |
Get()
Retrieves data by the alias index.
Parameters
index: int
: Index of the alias.
Returns
DataItem
: The returned data.
response, _ := driver.Gql("MATCH (n)-[e]->() RETURN n, e LIMIT 3", nil)
fmt.Println(response.Get(0))
The GQL query returns two aliases (n
, e
), and the Get()
method gets the DataItem
of the alias n
at index 0.
&{ RESULT_TYPE_NODE node_table:{schemas:{schema_name:"User" properties:{property_name:"name" property_type:STRING} schema_id:2} entity_rows:{uuid:1080866109592174597 id:"U04" schema_name:"User" values:"mochaeach" schema_id:2} entity_rows:{uuid:1080866109592174597 id:"U04" schema_name:"User" values:"mochaeach" schema_id:2} entity_rows:{uuid:4179342653223075843 id:"U02" schema_name:"User" values:"Brainy" schema_id:2}} alias:"n"}
Alias()
Retrieves data by the alias name.
Parameters
alias: string
: Name of the alias.
Returns
DataItem
: The returned data.
response, _ := driver.Gql("MATCH (n)-[e]->() RETURN n, e LIMIT 3", nil)
fmt.Println(response.Alias("e"))
The GQL query returns two aliases (n
, e
), and the Alias()
method gets DataItem
of the alias e
.
&{ RESULT_TYPE_EDGE edge_table:{schemas:{schema_name:"Follows" properties:{property_name:"createdOn" property_type:DATETIME} schema_id:2} schemas:{schema_name:"Joins" properties:{property_name:"memberNo" property_type:UINT32} schema_id:3} entity_rows:{uuid:2 schema_name:"Follows" from_uuid:1080866109592174597 to_uuid:4179342653223075843 from_id:"U04" to_id:"U02" values:"\x19\xb2\x94\x00\x00\x00\x00\x00" schema_id:2} entity_rows:{uuid:7 schema_name:"Joins" from_uuid:1080866109592174597 to_uuid:17870286619941011464 from_id:"U04" to_id:"C02" values:"\x00\x00\x00\t" schema_id:3} entity_rows:{uuid:3 schema_name:"Follows" from_uuid:4179342653223075843 to_uuid:12393908373546860548 from_id:"U02" to_id:"U03" values:"\x19\xb2\x82\x00\x00\x00\x00\x00" schema_id:2}} alias:"e"}
Transform DataItem
You should use a As<DataStructure>()
method to convert the DataItem.Data
into the corresponding driver data struct.
For example, the GQL query contained in the request below retrieves 3 nodes from the graph, the AsNodes()
method is used to convert the DataItem
associated with the alias n
into a slice of pointers to Node
structs:
response, _ := driver.Gql("MATCH (n) RETURN n LIMIT 3", nil)
nodeList, _, _ := response.Alias("n").AsNodes()
for _, node := range nodeList {
println(node.GetID())
}
The following lists all the transformation methods available on DataItem
. Please note the applicable DataItem.Type
and DataItem.Alias
of each method.
Method |
DataItem.Type |
DataItem.Alias |
Returns |
Description |
---|---|---|---|---|
AsNodes() |
NODE | Any | []*structs.Node |
Converts to a slice of pointers to Node structs. |
AsFirstNode() |
NODE | Any | *structs.Node |
Converts the first returned entity to a pointer to a Node struct. |
AsEdges() |
EDGE | Any | []*structs.Edge |
Converts to a slice of pointers to Edge structs. |
AsFirstEdge() |
EDGE | Any | *structs.Edge |
Converts the first returned entity to a pointer to an Edge struct. |
AsGraph() |
GRAPH | Any | *structs.Graph |
Converts to a pointers to a Graph struct. |
AsGraphSets() |
TABLE | _graph |
[]*structs.GraphSet |
Converts to a slice of pointers to GraphSet structs. |
AsSchemas() |
TABLE | _nodeSchema , _edgeSchema |
[]*structs.Schema |
Converts to a slice of pointers to Schema structs. |
AsProperties() |
TABLE | _nodeProperty , _edgeProperty |
[]*structs.Property |
Converts to a slice of pointers to Property structs. |
AsAttr() |
ATTR | Any | *structs.Attr |
Converts to a pointers to an Attr struct. |
AsTable() |
TABLE | Any | *structs.Table |
Converts to a pointers to a Table struct. |
AsHDCGraphs() |
TABLE | _hdcGraphList |
[]*structs.HDCGraph |
Converts to a slice of pointers to HDCGraph structs. |
AsAlgos() |
TABLE | _algoList |
[]*structs.Algo |
Converts to a slice of pointers to Algo structs. |
AsProjections() |
TABLE | _projectionList |
[]*structs.Projection |
Converts to a slice of pointers to Projection structs. |
AsIndexes() |
TABLE | _nodeIndex , _edgeIndex , _nodeFulltext , _edgeFulltext |
[]*structs.Index |
Converts to a slice of pointers to Index structs. |
AsPrivilieges() |
TABLE | _privilege |
[]*structs.Priviliege |
Converts to a slice of pointers to Priviliege structs. |
AsPolicies() |
TABLE | _policy |
[]*structs.Policy |
Converts to a slice of pointers to Policy structs. |
AsUsers() |
TABLE | _user |
[]*structs.User |
Converts to a slice of pointers to User structs. |
AsProcesses() |
TABLE | _top |
[]*structs.Process |
Converts to a slice of pointers to Process structs. |
AsJobs() |
TABLE | _job |
[]*structs.Job |
Converts to a slice of pointers to Job structs. |
Return a ResponseWithExistCheck
Methods such as CreateGraphIfNotExist()
return a ResponseWithExistCheck
value.
A ResponseWithExistCheck
value includes the following fields:
Field |
Type |
Description |
---|---|---|
Exist |
bool | Whether the object to create already exist. |
Response |
Response |
Main response of the method. |
Return an InsertResponse
Some data insertion methods such as InsertNodesBatchBySchema()
and InsertNodesBatchAuto()
return an InsertResponse
value.
An InsertResponse
object includes the following fields:
Field |
Type |
Description |
---|---|---|
IDs |
[]types.ID |
The list of _id s of the inserted nodes; note that the list is empty when InsertRequestConfig.Silent is True . |
UUIDs |
[]types.UUID |
The list of _uuid s of the inserted edges; note that the list is empty when InsertRequestConfig.Silent is True . |
ErrorItems |
map[int]string | A map containing error details, where each key represents the index of a node or edge with an error, and the value is the error code. |
Status |
Status |
The status of the execution, inlcuding fields Code and Message . |
Statistics |
Statistics |
Statistics releated to the execution, including fields NodeAffected , EdgeAffected , TotalCost , and EngineCost . |
Return a JobResponse
Methods such as CreateFullText()
return a JobResponse
object.
A JobResponse
object includes the following fields:
Field |
Type |
Description |
---|---|---|
JobId |
string | ID of the job of the execution. |
Status |
Status |
The status of the execution, inlcuding fields Code and Message . |
Statistics |
Statistics |
Statistics releated to the execution, including fields NodeAffected , EdgeAffected , TotalCost , and EngineCost . |
Return Driver Data Structs
Some methods return either a single pointer or a slice of pointers to driver data structs, which can be used directly. For example:
- The
GetGraph()
method returns a pointers to aGraphSet
struct. - The
ShowGraph()
method returns a slice of pointers toGraphSet
structs.