expr

package
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(expr func(scope query.Scope, b *strings.Builder)) neogo.Expression

The `New` function creates a new instance of the `expression` struct, which implements the `neogo.Expression` interface. It takes a function `expr` as an argument, which defines the behavior of the expression.

Types

type Client

type Client struct {
	*Reader
	*Updater[*Querier, *internal.CypherQuerier]
	// contains filtered or unexported fields
}

func (Client) Compile

func (c Client) Compile(s query.Scope, b *strings.Builder)

func (Client) Print

func (c Client) Print()

func (*Client) Union

func (e *Client) Union(unions ...func(*Client) Runner) *Querier

func (*Client) UnionAll

func (e *Client) UnionAll(unions ...func(*Client) Runner) *Querier

func (*Client) Use

func (e *Client) Use(graphExpr string) *Querier

type Querier

type Querier struct {
	*Reader
	*Updater[*Querier, *internal.CypherQuerier]
	Runner
	// contains filtered or unexported fields
}

func Create

func Create(pattern internal.Patterns) *Querier
Example
var p any

Create(db.Path(
	db.Node(db.Var(tests.Person{}, db.Props{"name": "'Andy'"})).
		To(tests.WorksAt{}, db.Var(tests.Company{}, db.Props{"name": "'Neo4j'"})).
		From(tests.WorksAt{}, db.Var(tests.Person{}, db.Props{"name": "'Michael'"})),
	"p",
)).
	Return(db.Qual(&p, "p")).
	Print()
Output:

CREATE p = (:Person {name: 'Andy'})-[:WORKS_AT]->(:Company {name: 'Neo4j'})<-[:WORKS_AT]-(:Person {name: 'Michael'})
RETURN p

func Cypher

func Cypher(query string) *Querier
Example
var n any

Match(db.Node(db.Qual(&n, "n"))).
	Cypher(`WHERE n.name = 'Bob'`).
	Return(&n).
	Print()
Output:

MATCH (n)
WHERE n.name = 'Bob'
RETURN n

func Delete

func Delete(identifiers ...query.PropertyIdentifier) *Querier
Example
var (
	n tests.Person
	r tests.ActedIn
)

Match(
	db.Node(db.Qual(&n, "n", db.Props{"name": "'Laurence Fishburne'"})).
		To(db.Qual(&r, "r"), nil),
).
	Delete(&r).
	Print()
Output:

MATCH (n:Person {name: 'Laurence Fishburne'})-[r:ACTED_IN]->()
DELETE r

func DetachDelete

func DetachDelete(identifiers ...query.PropertyIdentifier) *Querier
Example
var n tests.Person

Match(
	db.Node(
		db.Qual(&n, "n",
			db.Props{"name": "'Carrie-Anne Moss'"},
		),
	),
).
	DetachDelete(&n).
	Print()
Output:

MATCH (n:Person {name: 'Carrie-Anne Moss'})
DETACH DELETE n

func ForEach

func ForEach(identifier query.Identifier, inValue query.ValueIdentifier, do func(c *Updater[any, any])) *Querier
Example
Match(
	db.Path(db.Node("start").To(db.Var(nil, db.VarLength("*")), "finish"), "p"),
).
	Where(db.And(
		db.Cond("start.name", "=", "'A'"),
		db.Cond("finish.name", "=", "'D'"),
	)).
	ForEach("n", "nodes(p)", func(c *Updater[any, any]) {
		c.Set(db.SetPropValue("n.marked", true))
	}).
	Print()
Output:

MATCH p = (start)-[*]->(finish)
WHERE start.name = 'A' AND finish.name = 'D'
FOREACH (n IN nodes(p) | SET n.marked = true)

func Match

func Match(pattern internal.Patterns) *Querier
Example
var m tests.Movie

Match(
	db.Node(db.Var(
		tests.Person{},
		db.Props{
			"name": "'Oliver Stone'",
		},
	)).To(nil, db.Var("movie")),
).
	Return(db.Qual(
		&m.Title,
		"movie.title",
	)).Print()
Output:

MATCH (:Person {name: 'Oliver Stone'})-->(movie)
RETURN movie.title

func Merge

func Merge(pattern internal.Pattern, opts ...internal.MergeOption) *Querier
Example
var person tests.Person

Merge(
	db.Node(db.Qual(&person, "person")),
	db.OnMatch(
		db.SetPropValue(&person.Found, true),
		db.SetPropValue(&person.LastSeen, "timestamp()"),
	),
).
	Return(&person.Name, &person.Found, &person.LastSeen).
	Print()
Output:

MERGE (person:Person)
ON MATCH
  SET
    person.found = true,
    person.lastSeen = timestamp()
RETURN person.name, person.found, person.lastSeen

func OptionalMatch

func OptionalMatch(pattern internal.Patterns) *Querier
Example
a := tests.Person{}
r := tests.Directed{}

Match(
	db.Node(db.Qual(
		&a, "a",
		db.Props{
			"name": "'Martin Sheen'",
		},
	)),
).
	OptionalMatch(
		db.Node(&a).To(db.Qual(&r, "r"), nil),
	).Return(&a.Name, &r).Print()
Output:

MATCH (a:Person {name: 'Martin Sheen'})
OPTIONAL MATCH (a)-[r:DIRECTED]->()
RETURN a.name, r

func Remove

func Remove(items ...internal.RemoveItem) *Querier
Example
var n tests.Person
var labels []string

Match(db.Node(db.Qual(&n, "n", db.Props{"name": "'Peter'"}))).
	Remove(db.RemoveLabels(&n, "German", "Swedish")).
	Return(&n.Name, db.Qual(&labels, "labels(n)")).
	Print()
Output:

MATCH (n:Person {name: 'Peter'})
REMOVE n:German:Swedish
RETURN n.name, labels(n)

func Set

func Set(items ...internal.SetItem) *Querier
Example
var n tests.Person

Match(
	db.Node(db.Qual(&n, "n", db.Props{"name": "'Andy'"})),
).
	Set(
		db.SetPropValue(&n.Position, "'Developer'"),
		db.SetPropValue(&n.Surname, "'Taylor'"),
	).
	Print()
Output:

MATCH (n:Person {name: 'Andy'})
SET
  n.position = 'Developer',
  n.surname = 'Taylor'

func Subquery

func Subquery(subquery func(c *Client) Runner) *Querier
Example
var (
	p       tests.Person
	numConn int
)

Match(db.Node(db.Qual(&p, "p"))).
	Subquery(func(c *Client) Runner {
		return c.With(&p).
			Match(db.Node(&p).Related(nil, db.Var("c"))).
			Return(
				db.Qual(&numConn, "count(c)", db.Name("numberOfConnections")),
			)
	}).
	Return(&p.Name, &numConn).
	Print()
Output:

MATCH (p:Person)
CALL {
  WITH p
  MATCH (p)--(c)
  RETURN count(c) AS numberOfConnections
}
RETURN p.name, numberOfConnections

func Union

func Union(unions ...func(*Client) Runner) *Querier
Example
var name string
Union(
	func(c *Client) Runner {
		return c.
			Match(db.Node(db.Var("n", db.Label("Person")))).
			Return(db.Qual(&name, "n.name", db.Name("name")))
	},
	func(c *Client) Runner {
		return c.
			Match(db.Node(db.Var("n", db.Label("Movie")))).
			Return(db.Qual(&name, "n.title", db.Name("name")))
	},
).Print()
Output:

MATCH (n:Person)
RETURN n.name AS name
UNION
MATCH (n:Movie)
RETURN n.title AS name

func UnionAll

func UnionAll(unions ...func(*Client) Runner) *Querier
Example
var name string
UnionAll(
	func(c *Client) Runner {
		return c.
			Match(db.Node(db.Var("n", db.Label("Person")))).
			Return(db.Qual(&name, "n.name", db.Name("name")))
	},
	func(c *Client) Runner {
		return c.
			Match(db.Node(db.Var("n", db.Label("Movie")))).
			Return(db.Qual(&name, "n.title", db.Name("name")))
	},
).Print()
Output:

MATCH (n:Person)
RETURN n.name AS name
UNION ALL
MATCH (n:Movie)
RETURN n.title AS name

func Unwind

func Unwind(identifier query.Identifier, as string) *Querier
Example
events := map[string]any{
	"events": []map[string]any{
		{
			"id":   1,
			"year": 2014,
		},
		{
			"id":   2,
			"year": 2015,
		},
	},
}
type Year struct {
	internal.Node `neo4j:"Year"`

	Year int `json:"year"`
}
type Event struct {
	internal.Node `neo4j:"Event"`

	ID   int `json:"id"`
	Year int `json:"year"`
}
type In struct {
	internal.Relationship `neo4j:"IN"`
}
var (
	y Year
	e Event
)

Unwind(db.Qual(&events, "events"), "event").
	Merge(
		db.Node(db.Qual(&y, "y", db.Props{"year": "event.year"})),
	).
	Merge(
		db.Node(&y).
			From(In{}, db.Qual(&e, "e", db.Props{"id": "event.id"})),
	).
	Return(db.Return(db.Qual(&e.ID, "x"), db.OrderBy("", true))).
	Print()
Output:

UNWIND $events AS event
MERGE (y:Year {year: event.year})
MERGE (y)<-[:IN]-(e:Event {id: event.id})
RETURN e.id AS x
ORDER BY x

func Use

func Use(graphExpr string) *Querier
Example
var n any

Use("myDatabase").
	Match(db.Node(db.Qual(&n, "n"))).
	Return("n").
	Print()
Output:

USE myDatabase
MATCH (n)
RETURN n

func Where

func Where(opts ...internal.WhereOption) *Querier
Example
var n tests.Person

Match(db.Node(db.Qual(&n, "n"))).
	Where(
		db.Or(
			db.Xor(
				db.Cond(&n.Name, "=", "'Peter'"),
				db.And(
					db.Cond(&n.Age, "<", "30"),
					db.Cond(&n.Name, "=", "'Timothy'"),
				),
			),
			db.Not(db.Or(
				db.Cond(&n.Name, "=", "'Timothy'"),
				db.Cond(&n.Name, "=", "'Peter'"),
			)),
		),
	).
	Return(
		db.Return(db.Qual(&n.Name, "name"), db.OrderBy("", true)),
		db.Qual(&n.Age, "age"),
	).Print()
Output:

MATCH (n:Person)
WHERE (n.name = 'Peter' XOR (n.age < 30 AND n.name = 'Timothy')) OR NOT (n.name = 'Timothy' OR n.name = 'Peter')
RETURN n.name AS name, n.age AS age
ORDER BY name

func With

func With(identifiers ...query.Identifier) *Querier
Example
var names []string

Match(
	db.Node(db.Var("n", db.Props{"name": "'Anders'"})).
		Related(nil, "m"),
).
	With(
		db.With("m", db.OrderBy("name", false), db.Limit("1")),
	).
	Match(db.Node("m").Related(nil, "o")).
	Return(db.Qual(names, "o.name")).Print()
Output:

MATCH (n {name: 'Anders'})--(m)
WITH m
ORDER BY m.name DESC
LIMIT 1
MATCH (m)--(o)
RETURN o.name

func Yield

func Yield(identifiers ...query.Identifier) *Querier
Example
var labels []string

Call("db.labels()").
	Yield(db.Qual(&labels, "label")).
	Return(&labels).
	Print()
Output:

CALL db.labels()
YIELD label
RETURN label

func (Querier) Compile

func (c Querier) Compile(s query.Scope, b *strings.Builder)

func (Querier) Print

func (c Querier) Print()

func (*Querier) Where

func (e *Querier) Where(opts ...internal.WhereOption) *Querier

type Reader

type Reader struct {
	Runner
	// contains filtered or unexported fields
}

func (*Reader) Call

func (e *Reader) Call(procedure string) *Yielder

func (*Reader) Cypher

func (e *Reader) Cypher(query string) *Querier

func (*Reader) Eval

func (e *Reader) Eval(expression neogo.Expression) *Querier

func (*Reader) Match

func (e *Reader) Match(pattern internal.Patterns) *Querier

func (*Reader) OptionalMatch

func (e *Reader) OptionalMatch(pattern internal.Patterns) *Querier

func (*Reader) Return

func (e *Reader) Return(identifiers ...query.Identifier) Runner

func (*Reader) Show

func (e *Reader) Show(command string) *Yielder

func (*Reader) Subquery

func (e *Reader) Subquery(subquery func(c *Client) Runner) *Querier

func (*Reader) Unwind

func (e *Reader) Unwind(identifier query.Identifier, as string) *Querier

func (*Reader) With

func (e *Reader) With(identifiers ...query.Identifier) *Querier

type Runner

type Runner interface {
	neogo.Expression
	Print()
	// contains filtered or unexported methods
}

func Return

func Return(identifiers ...query.Identifier) Runner
Example
var p tests.Person

Match(db.Node(db.Qual(&p, "p", db.Props{"name": "'Keanu Reeves'"}))).
	Return(db.Qual(&p.Nationality, "citizenship")).Print()
Output:

MATCH (p:Person {name: 'Keanu Reeves'})
RETURN p.nationality AS citizenship

type Updater

type Updater[To any, ToCypher any] struct {
	To func(ToCypher) To
	// contains filtered or unexported fields
}

func (Updater) Compile

func (c Updater) Compile(s query.Scope, b *strings.Builder)

func (*Updater[To, CypherTo]) Create

func (e *Updater[To, CypherTo]) Create(pattern internal.Patterns) To

func (*Updater[To, CypherTo]) Delete

func (e *Updater[To, CypherTo]) Delete(identifiers ...query.Identifier) To

func (*Updater[To, CypherTo]) DetachDelete

func (e *Updater[To, CypherTo]) DetachDelete(identifiers ...query.PropertyIdentifier) To

func (*Updater[To, CypherTo]) ForEach

func (e *Updater[To, CypherTo]) ForEach(identifier query.Identifier, inValue query.ValueIdentifier, do func(c *Updater[any, any])) To

func (*Updater[To, CypherTo]) Merge

func (e *Updater[To, CypherTo]) Merge(pattern internal.Pattern, opts ...internal.MergeOption) To

func (Updater) Print

func (c Updater) Print()

func (*Updater[To, CypherTo]) Remove

func (e *Updater[To, CypherTo]) Remove(items ...internal.RemoveItem) To

func (*Updater[To, CypherTo]) Set

func (e *Updater[To, CypherTo]) Set(items ...internal.SetItem) To

type Yielder

type Yielder struct {
	*Querier
	Runner
	// contains filtered or unexported fields
}

func Call

func Call(procedure string) *Yielder
Example
var labels []string

Call("db.labels()").
	Yield(db.Qual(&labels, "label")).
	Return(&labels).
	Print()
Output:

CALL db.labels()
YIELD label
RETURN label

func Show

func Show(command string) *Yielder
Example
var (
	name any
	sig  string
)

Show("PROCEDURES").
	Yield(
		db.Qual(&name, "name"),
		db.Qual(&sig, "signature"),
	).
	Where(db.Cond(&name, "=", "'dbms.listConfig'")).
	Return(&sig).
	Print()
Output:

SHOW PROCEDURES
YIELD name, signature
WHERE name = 'dbms.listConfig'
RETURN signature

func (Yielder) Compile

func (c Yielder) Compile(s query.Scope, b *strings.Builder)

func (Yielder) Print

func (c Yielder) Print()

func (*Yielder) Yield

func (e *Yielder) Yield(identifiers ...query.Identifier) *Querier

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL