数据库读取和写入操作的结果必须先进行适当处理,然后才能用于应用程序。
返回Response
有些方法(如Gql()
和Uql()
)返回一个Response
值。为在应用中使用其中包含的数据,首先要从Response
值提取DataItem
,再将DataItem
转换成合适的驱动数据结构体。
一个Response
值有以下字段:
字段 |
类型 |
描述 |
---|---|---|
Aliases |
[]Alias |
结果别名列表;每个Alias 包括字段Name 和Type |
Items |
map[string]struct{} | 一个映射,键是别名名称,值是别名代表的数据 |
ExplainPlan |
ExplainPlan |
执行计划 |
Status |
Status |
执行状态,包括字段Code 和Message |
Statistics |
Statistics |
执行的统计信息,包括字段NodeAffected 、EdgeAffected 、TotalCost 和EngineCost |
提取DataItem
使用Get()
或Alias()
方法从一个Response
值提取DataItem
。
一个DataItem
值有以下字段:
字段 |
类型 |
描述 |
---|---|---|
Alias |
string | 别名名称 |
Type |
ResultType |
结果类型 |
Data |
interface{} | 结果数据 |
Get()
通过别名索引获取数据。
参数
index: int
:别名索引。
返回值
DataItem
:返回的数据。
response, _ := driver.Gql("MATCH (n)-[e]->() RETURN n, e LIMIT 3", nil)
fmt.Println(response.Get(0))
GQL语句返回n
和e
两个别名;Get()
方法获取与索引为0的别名n
对应的DataItem
。
&{ 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()
通过别名名称获取数据。
参数
alias: string
:别名名称。
返回值
DataItem
:返回的数据。
response, _ := driver.Gql("MATCH (n)-[e]->() RETURN n, e LIMIT 3", nil)
fmt.Println(response.Alias("e"))
GQL语句返回n
和e
两个别名;Alias()
方法获取别名e
对应的DataItem
。
&{ 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"}
转换DataItem
使用一个as<DataStructure>()
方法将DataItem.Data
转换成对应的驱动数据结构体。
例如,以下请求中的GQL语句从图中获取三个点,AsNodes()
方法将与别名n
对应的DataItem
转换为指向Node
结构体的指针切片:
response, _ := driver.Gql("MATCH (n) RETURN n LIMIT 3", nil)
nodeList, _, _ := response.Alias("n").AsNodes()
for _, node := range nodeList {
println(node.GetID())
}
以下是DataItem
所有的转换方法。请留意每种方法适用的DataItem.Type
和DataItem.Alias
。
方法 |
DataItem.Type |
DataItem.Alias |
返回 |
描述 |
---|---|---|---|---|
AsNodes() |
NODE | 任何 | []*structs.Node |
转换成Node 结构体指针的切片 |
AsFirstNode() |
NODE | 任何 | *structs.Node |
将返回的第一个结果数据转换成指向Node 结构体的指针 |
AsEdges() |
EDGE | 任何 | []*structs.Edge |
转换成Edge 结构体指针的切片 |
AsFirstEdge() |
EDGE | 任何 | *structs.Edge |
将返回的第一个结果数据转换成指向Edge 结构体的指针 |
AsGraph() |
GRAPH | 任何 | *structs.Graph |
转换成指向Graph 结构体的指针 |
AsGraphSets() |
TABLE | _graph |
[]*structs.GraphSet |
转换成GraphSet 结构体指针的切片 |
AsSchemas() |
TABLE | _nodeSchema , _edgeSchema |
[]*structs.Schema |
转换成Schema 结构体指针的切片 |
AsProperties() |
TABLE | _nodeProperty , _edgeProperty |
[]*structs.Property |
转换成Property 结构体指针的切片 |
AsAttr() |
ATTR | 任何 | *structs.Attr |
转换成指向Attr 结构体的指针 |
AsTable() |
TABLE | 任何 | *structs.Table |
转换成指向Table 结构体的指针 |
AsHDCGraphs() |
TABLE | _hdcGraphList |
[]*structs.HDCGraph |
转换成HDCGraph 结构体指针的切片 |
AsAlgos() |
TABLE | _algoList |
[]*structs.Algo |
转换成Algo 结构体指针的切片 |
AsProjections() |
TABLE | _projectionList |
[]*structs.Projection |
转换成Projection 结构体指针的切片 |
AsIndexes() |
TABLE | _nodeIndex , _edgeIndex , _nodeFulltext , _edgeFulltext |
[]*structs.Index |
转换成Index 结构体指针的切片 |
AsPrivilieges() |
TABLE | _privilege |
[]*structs.Priviliege |
转换成Priviliege 结构体指针的切片 |
AsPolicies() |
TABLE | _policy |
[]*structs.Policy |
转换成Policy 结构体指针的切片 |
AsUsers() |
TABLE | _user |
[]*structs.User |
转换成User 结构体指针的切片 |
AsProcesses() |
TABLE | _top |
[]*structs.Process |
转换成Process 结构体指针的切片 |
AsJobs() |
TABLE | _job |
[]*structs.Job |
转换成Job 结构体指针的切片 |
返回ResponseWithExistCheck
有些方法(如createGraphIfNotExist()
)返回一个ResponseWithExistCheck
值。
一个ResponseWithExistCheck
值有以下字段:
字段 |
类型 |
描述 |
---|---|---|
Exist |
bool | 要创建的对象是否已经存在 |
Response |
Response |
主要请求结果 |
返回InsertResponse
有些数据插入方法(如InsertNodesBatchBySchema()
和InsertNodesBatchAuto()
)返回一个InsertResponse
值。
一个InsertResponse
值有以下字段:
字段 |
类型 |
描述 |
---|---|---|
IDs |
[]types.ID |
插入的点_id 列表;请留意,InsertRequestConfig.Silent 为True 时列表为空 |
UUIDs |
[]types.UUID |
插入的边_uuid 列表;请留意,InsertRequestConfig.Silent 为True 时列表为空 |
ErrorItems |
map[int]string | 错误信息映射,键是发生错误的点或边的顺序索引,值是错误代码 |
Status |
Status |
执行状态,包括字段Code 和Message |
statistics |
Statistics |
执行的统计信息,包括字段NodeAffected 、EdgeAffected 、TotalCost 和EngineCost |
返回JobResponse
有些方法(如CreateFullText()
)返回一个JobResponse
值。
一个JobResponse
值有以下字段:
字段 |
类型 |
描述 |
---|---|---|
JobId |
string | 执行的作业ID |
Status |
Status |
执行状态,包括字段Code 和Message |
statistics |
Statistics |
执行的统计信息,包括字段NodeAffected 、EdgeAffected 、TotalCost 和EngineCost |
返回驱动数据结构
一些方法直接返回指向驱动数据结构体的一个指针或指针的切片,可直接使用。例如:
GetGraph()
方法返回指向GraphSet
结构体的指针。ShowGraph()
方法返回GraphSet
结构体指针的切片。