Documentation ¶
Overview ¶
Package ag is a set of types that implements the Scanner and Valuer interfaces and supports additional methods for AgensGraph.
These types are intended to use with github.com/lib/pq in text format mode.
Index ¶
- func Array(dest interface{}) interface{ ... }
- func ScanEntity(src interface{}, entity Entity) error
- func ScanPath(src interface{}, saver PathSaver) error
- type BasicEdge
- type BasicPath
- type BasicVertex
- type Edge
- type EdgeCore
- type EdgeHeader
- type Entity
- type EntitySaver
- type GraphId
- type NullArrayError
- type PathSaver
- type PropertiesSaver
- type Vertex
- type VertexCore
- type VertexHeader
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Array ¶
Array returns database/sql Scanner and database/sql/driver Valuer for dest that is a slice or an array of the following types; GraphId, and entities for vertex and edge.
If the type of dest is not *[]GraphId and []GraphId, Value of Array returns an error since passing entities as parameters is not allowed.
Example (BasicEdge) ¶
var es []ag.BasicEdge err := db.QueryRow(`MATCH p=(:v)-[:e]->(:v)-[:e]->(:v) RETURN relationships(p) LIMIT 1`).Scan(ag.Array(&es)) if err == nil && es != nil { // Valid _edge } else { // An error occurred or the _edge is NULL }
Output:
Example (BasicVertex) ¶
var vs []ag.BasicVertex err := db.QueryRow(`MATCH p=(:v)-[:e]->(:v) RETURN nodes(p) LIMIT 1`).Scan(ag.Array(&vs)) if err == nil && vs != nil { // Valid _vertex } else { // An error occurred or the _vertex is NULL }
Output:
Example (GraphId) ¶
var gids []ag.GraphId // Scan() db.QueryRow(`MATCH ()-[es*]->() RETURN [e IN es | id(e)] LIMIT 1`).Scan(ag.Array(&gids)) // Value() gid0, _ := ag.NewGraphId("1.1") gid1, _ := ag.NewGraphId("65535.281474976710655") gids = []ag.GraphId{gid0, gid1} db.QueryRow(`MATCH (n) WHERE id(n) IN $1 RETURN n LIMIT 1`, ag.Array(gids))
Output:
func ScanEntity ¶
ScanEntity reads an entity for vertex or edge from src and stores the result in the given entity.
An error will be returned if the type of src is not []byte, or src is invalid for the given entity.
Example ¶
package main import ( "encoding/json" "fmt" "log" "github.com/bitnine-oss/agensgraph-golang" ) type knows struct { ag.Edge meta struct { null bool id ag.GraphId } who ag.GraphId whom ag.GraphId since yearMonth } type knowsBody struct { Type string Since json.RawMessage } type yearMonth struct { Year int Month int } func (e knows) String() string { if e.meta.null { return "NULL" } else { return fmt.Sprintf("%s knows %s since %d, %d", e.who, e.whom, e.since.Month, e.since.Year) } } func (e *knows) SaveEntity(valid bool, core interface{}) error { e.meta.null = !valid if !valid { return nil } c, ok := core.(ag.EdgeCore) if !ok { return fmt.Errorf("invalid edge core: %T", core) } e.meta.id = c.Id e.who = c.Start e.whom = c.End return nil } func (e *knows) SaveProperties(b []byte) error { var body knowsBody err := json.Unmarshal(b, &body) if err != nil { return err } switch body.Type { case "array": var ym [2]int err = json.Unmarshal(body.Since, &ym) if err != nil { return err } e.since.Year, e.since.Month = ym[0], ym[1] case "object": err := json.Unmarshal(body.Since, &e.since) if err != nil { return err } default: log.Panicf("unknown body type: %q", body.Type) } return nil } func (e *knows) Scan(src interface{}) error { return ag.ScanEntity(src, e) } func main() { ds := [][]byte{ []byte(`knows[4.1][3.1,3.2]{"type": "array", "since": [1970, 1]}`), []byte(`knows[4.2][3.3,3.4]{"type": "object", "since": {"year": 2009, "month": 10}}`), } for _, d := range ds { var r knows err := r.Scan(d) if err != nil { log.Println(err) } else { fmt.Printf("%s\n", r) } } }
Output:
Types ¶
type BasicEdge ¶
type BasicEdge struct { EdgeHeader Properties map[string]interface{} }
BasicEdge can be used to scan the value from the database driver as a edge.
This is a reference implementation of an entity for edge using all the basic building blocks(Edge, EdgeCore, EdgeHeader, EntitySaver, PropertiesSaver, and ScanEntity.)
func (*BasicEdge) SaveProperties ¶
SaveProperties implements PropertiesSaver interface. It calls json.Unmarshal to unmarshal b and store the result in Properties.
type BasicPath ¶
type BasicPath struct { Valid bool Vertices []BasicVertex Edges []BasicEdge }
BasicPath can be used to scan the value from the database driver as a path.
This is a reference implementation that uses PathSaver and ScanPath.
func (*BasicPath) Scan ¶
Scan implements the database/sql Scanner interface. It calls ScanPath.
Example ¶
var p ag.BasicPath err := db.QueryRow(`MATCH p=(:v)-[:e]->(:v)-[:e]->(:v) RETURN p LIMIT 1`).Scan(&p) if err == nil && p.Valid { // Valid graphpath } else { // An error occurred or the graphpath is NULL }
Output:
type BasicVertex ¶
type BasicVertex struct { VertexHeader Properties map[string]interface{} }
BasicVertex can be used to scan the value from the database driver as a vertex.
This is a reference implementation of an entity for vertex using all the basic building blocks(Vertex, VertexCore, VertexHeader, EntitySaver, PropertiesSaver, and ScanEntity.)
func (*BasicVertex) SaveProperties ¶
func (v *BasicVertex) SaveProperties(b []byte) error
SaveProperties implements PropertiesSaver interface. It calls json.Unmarshal to unmarshal b and store the result in Properties.
func (*BasicVertex) Scan ¶
func (v *BasicVertex) Scan(src interface{}) error
Scan implements the database/sql Scanner interface. It calls ScanEntity.
Example ¶
var v ag.BasicVertex err := db.QueryRow(`MATCH (n) RETURN n LIMIT 1`).Scan(&v) if err == nil && v.Valid { // Valid vertex } else { // An error occurred or the vertex is NULL }
Output:
func (BasicVertex) String ¶
func (v BasicVertex) String() string
type Edge ¶
type Edge struct{}
Edge gives any struct an ability to read the value from the database driver as an edge if the struct has Edge as its embedded field.
type EdgeHeader ¶
type EdgeHeader struct { Edge Valid bool // Valid is true if the edge is not NULL EdgeCore // EdgeCore is valid only if Valid is true }
EdgeHeader may be used as an embedded field of any struct to change the struct to an entity for edge.
func (*EdgeHeader) SaveEntity ¶
func (h *EdgeHeader) SaveEntity(valid bool, core interface{}) error
SaveEntity implements EntitySaver interface.
type Entity ¶
type Entity interface { EntitySaver // contains filtered or unexported methods }
Entity is an interface used by ScanEntity. Any struct that has Vertex or Edge as its embedded field and implements EntitySaver can be an entity for vertex or edge.
type EntitySaver ¶
type EntitySaver interface { // SaveEntity assigns an entity from the database driver. // // valid is true if the entity is not NULL. // // core is VertexCore or EdgeCore that will be stored in the entity for // vertex or edge respectively. If valid is false, core will be nil. // // An error should be returned if the entity cannot be stored without // loss of information. SaveEntity(valid bool, core interface{}) error }
EntitySaver is an interface used by ScanEntity.
type GraphId ¶
type GraphId struct { // Valid is true if GraphId is not NULL Valid bool // contains filtered or unexported fields }
GraphId is a unique ID for a vertex and an edge.
func NewGraphId ¶
NewGraphId returns GraphId of str if str is between "1.1" and "65535.281474976710656". If str is "NULL", it returns GraphId whose Valid is false. Otherwise, it returns an error.
type NullArrayError ¶
type NullArrayError struct{}
NullArrayError is returned by Scan if the type of dest for Array(dest) is array and the value from the database driver is NULL.
func (NullArrayError) Error ¶
func (_ NullArrayError) Error() string
type PathSaver ¶
type PathSaver interface { // SavePath assigns a path from the database driver. // // valid is true if the path is not NULL. // // ds is a series of connected vertices and edges. Each element of ds // can be stored in an entity for vertex or edge by calling ScanEntity. // If valid is false, ds will be nil. // // An error should be returned if the path cannot be stored without // loss of information. SavePath(valid bool, ds []interface{}) error }
PathSaver is an interface used by ScanPath.
type PropertiesSaver ¶
type PropertiesSaver interface { // By default, properties of an entity read by ScanEntity are stored in // the entity itself by calling json.Unmarshal over it. To modify this // default behavior, one may implement PropertiesSaver for the entity. // // The underlying array of b may be reused. // // An error should be returned if the properties cannot be stored // without loss of information. SaveProperties(b []byte) error }
PropertiesSaver is an interface used by ScanEntity.
type Vertex ¶
type Vertex struct{}
Vertex gives any struct an ability to read the value from the database driver as a vertex if the struct has Vertex as its embedded field.
type VertexCore ¶
VertexCore represents essential data to identify a vertex.
type VertexHeader ¶
type VertexHeader struct { Vertex Valid bool // Valid is true if the vertex is not NULL VertexCore // VertexCore is valid only if Valid is true }
VertexHeader may be used as an embedded field of any struct to change the struct to an entity for vertex.
func (*VertexHeader) SaveEntity ¶
func (h *VertexHeader) SaveEntity(valid bool, core interface{}) error
SaveEntity implements EntitySaver interface.