Categorygithub.com/cloudprivacylabs/opencypher
modulepackage
1.0.0
Repository: https://github.com/cloudprivacylabs/opencypher.git
Documentation: pkg.go.dev

# README

GoDoc Go Report Card Build Status

Embedded openCypher interpreter

openCypher is a query language for labeled property graphs. This Go module contains an openCypher interpreter (partial) that works on the Go LPG implementation given in https://github.com/cloudprivacylabs/lpg.

More information on openCypher can be found here:

https://opencypher.org/

openCypher

At this point, this library provides partial support for openCypher expressions. More support will be added as needed.

openCypher expressions are evaluated using an evaluation context.

Create Nodes

See examples/create directory.

import (
	"fmt"

	"github.com/cloudprivacylabs/opencypher"
	"github.com/cloudprivacylabs/lpg"
)

func main() {
	grph := graph.NewGraph()
	ectx := opencypher.NewEvalContext(grph)
	_, err := opencypher.ParseAndEvaluate(`CREATE (n:Person), (m)`, ectx)
	if err != nil {
		panic(err)
	}
	v, err := opencypher.ParseAndEvaluate(`MATCH (x:Person) return x as person`, ectx)
	if err != nil {
		panic(err)
	}
	fmt.Println(v.Get().(opencypher.ResultSet).Rows[0]["person"])
}

Evaluation Context

Variables defined in an expression will be in the evaluation context, and can be used to affect the results of subsequent expression.

See examples/context.

func main() {
	// Create an empty graph
	grph := graph.NewGraph()
	// Evaluation context knows the graph we are working on
	ectx := opencypher.NewEvalContext(grph)
	// CREATE a path
	_, err := opencypher.ParseAndEvaluate(`CREATE (andy {name:"Andy"})-[:KNOWS]-> (stephen {name:"Stephen"})`, ectx)
	if err != nil {
		panic(err)
	}
	// ectx knows andy and stephen. So this will only update stephen, and not andy
	v, err := opencypher.ParseAndEvaluate(`MATCH (stephen) SET stephen.age=34 return stephen`, ectx)
	if err != nil {
		panic(err)
	}
	age, _ := v.Get().(opencypher.ResultSet).Rows[0]["1"].Get().(*graph.Node).GetProperty("age")
	fmt.Println(age) // This will print 34
}

Querying and result sets

Using the example in https://neo4j.com/docs/cypher-manual/current/clauses/match/

	// Get all nodes
	ectx = opencypher.NewEvalContext(grph)
	res, err := opencypher.ParseAndEvaluate(`match (n) return n`, ectx)
	fmt.Println("match (n) return n:", res.Get().(opencypher.ResultSet).Rows)

	// Get all movies
	ectx = opencypher.NewEvalContext(grph)
	res, err = opencypher.ParseAndEvaluate(`match (n:Movie) return n.title`, ectx)
	fmt.Println("match (n:Movie) return n.title:", res.Get().(opencypher.ResultSet).Rows)

	// Get related node
	ectx = opencypher.NewEvalContext(grph)
	res, err = opencypher.ParseAndEvaluate(`match (director {name: 'Oliver Stone'}) --(movie:Movie) return movie.title`, ectx)
	fmt.Println("match (director {name: 'Oliver Stone'}) --(movie:Movie) return movie.title:", res.Get().(opencypher.ResultSet).Rows)
	ectx = opencypher.NewEvalContext(grph)
	res, err = opencypher.ParseAndEvaluate(`match (director {name: 'Oliver Stone'}) --> (movie:Movie) return movie.title`, ectx)
	fmt.Println("match (director {name: 'Oliver Stone'}) --> (movie:Movie) return movie.title:", res.Get().(opencypher.ResultSet).Rows)

	// Get relationship type
	ectx = opencypher.NewEvalContext(grph)
	res, err = opencypher.ParseAndEvaluate(`match (:Person {name: 'Oliver Stone'}) -[r]->(movie) return r`, ectx)
	fmt.Println("match (:Person {name: 'Oliver Stone'}) -[r]->(movie) return r:", res.Get().(opencypher.ResultSet).Rows)

Values

Opencypher expressions return an object of type Value. Value.Get returns the value contained in the value object. For most queries, this value is of type opencypher.ResultSet. A ResultSet contains Rows that are map[string]Value objects. If the query explicitly names its columns, the map will contains those names as the keys. Otherwise, the columns will be "1", "2", etc.

The number of rows in the result set:

rs:=value.Get().(opencypher.ResultSet)
numResults:=len(rs.Rows)

Iterate the results:

for _,row := range resultSet.Rows {
   for colName, colValue := range row {
      // colName is the column name
      // colValue is an opencypher.Value object
      fmt.Println(colName,value.Get())
   }
}

The return type of Value.Get is one of the following:

  • int
  • float64
  • bool
  • string
  • Duration
  • Date
  • Time
  • LocalDateTime
  • LocalTime
  • []Value
  • map[string]Value
  • lpg.StringSet
  • *lpg.Node
  • *lpg.Path
  • ResultSet

This Go module is part of the Layered Schema Architecture.

# Packages

No description provided by the author
No description provided by the author

# Functions

BuildPatternSymbols copies all the symbols referenced in the pattern from the context, and puts them in a map.
CartesianProuduct builds the product of all the resultsets.
EscapeLabelLiteral escape a literal that can be used as a label.
EscapePropertyKeyLiteral escapes a literal that can be used as a property key.
EscapeStringLiteral returns "s" where backslashes and quotes in s are escaped.
GetParser returns a parser that will parse the input string.
IsNamedResult checks if the given name is a symbol name (i.e.
No description provided by the author
IsValuePrimitive returns true if the value is int, float64, bool, string, duration, date, datetime, localDateTime, or localTime.
IsValueSame compares two values and decides if the two are the same.
NewDate creates a Date Hour, minute, second and nanoseconds are set to zero and location is set to UTC.
No description provided by the author
NewLocalDateTime creates a LocalDateTime from time.Time.
NewLocalTime creates a LocalTime from time.Time.
NewLValue returns an LValue from the given value.
No description provided by the author
GetEvaluatable returns an evaluatable object.
No description provided by the author
ParsePatternExpr parses the pattern expression that starts at the current node named 'this', and describes a path reaching one or more nodes named 'target'.
No description provided by the author
ValueAsBool returns the bool value, or if it is not bool, false,false.
ValueAsInt returns the int value.
ValueAsString returns the string value.
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Duration represents a duration in opencypher.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Function describes a function.
LValue is a pointer to a value.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ResultSet is a table of values.
RValue is a value.

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author