age

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: Apache-2.0 Imports: 14 Imported by: 1

README

Age Go Driver

Go Reference Go Report Card Apache License

A high-performance and easy-to-use Go driver for Apache AGE. This driver is written entirely in Go without any Java dependencies and utilizes an ANTLR4-based query parser.

Features

  • Java-Free: Written purely in Go, eliminating external dependencies.
  • ANTLR4 Integration: Accurately and efficiently parses Apache AGE's Cypher-like query language.
  • Type-Safe Mapping: Easily map database objects (vertices, edges) to Go structs.
  • Transaction Support: Provides transaction management with ACID guarantees.
  • Simple and Intuitive API: Offers a user-friendly and straightforward API for rapid development.

Installation

go get -u github.com/9ssi7/age

Running Tests

To run the tests, you'll need a running Apache AGE instance. You can easily spin one up using Docker:

docker run \
--name age  \
-p 5455:5432 \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=postgres \
-d \
apache/age

Examples

If you want to see some examples of how to use the driver, check out the samples repository.

Usage

package main

import (
    "fmt"

    "github.com/9ssi7/age"
)

// ... (Person and WorksWith structs) ...

func main() {
    // ... (connection settings) ...

    ag := age.New(age.Config{
        GraphName: graphName,
        Dsn:       dsn,
    })
    ok, err := ag.Prepare()

    // ... (query and data processing examples) ...
}

Examples

  • Creating Vertices:
_, err = tx.Exec(0, "CREATE (n:Person {name: '%s', age: %d})", "Alice", 30)
  • Creating Edges:
_, err = tx.Exec(0, "MATCH (a:Person), (b:Person) WHERE a.name = 'Alice' AND b.name = 'Bob' CREATE (a)-[:KNOWS]->(b)")
  • Querying and Processing Data:
cursor, err := tx.Exec(1, "MATCH (n:Person) RETURN n")

for cursor.Next() {
    entities, _ := cursor.GetRow()
    vertex := entities[0].(*age.Vertex)

    // Mapping age.Vertex to Person struct
    person := &Person{}
    mapper.Unmarshal(vertex, person) // Or use ParseStruct for more control

    // age.ParseStruct(entities[0], person) // Alternative method

    fmt.Println(person)
}
Using ParseStruct
// Assuming entities[0] is a Vertex
person := &Person{}
err := age.ParseStruct(entities[0], person)
if err != nil {
    // Handle error
}

Contributing

Contributions are welcome via pull requests. Bug fixes, new features, and improvements are always appreciated. Please read CONTRIBUTING.md before contributing.

License

Apache License 2.0

Documentation

Index

Constants

View Source
const MaxInt = int(MaxUint >> 1)
View Source
const MaxUint = ^uint(0)
View Source
const MinInt = -MaxInt - 1
View Source
const MinUint = 0

Variables

This section is empty.

Functions

func IsEntity

func IsEntity(v interface{}) bool

func ParseStruct

func ParseStruct(entity Entity, target interface{}) error

Types

type AGErrorListener

type AGErrorListener struct {
	*antlr.DefaultErrorListener
	// contains filtered or unexported fields
}

func NewAGErrorListener

func NewAGErrorListener() *AGErrorListener

func (*AGErrorListener) SyntaxError

func (el *AGErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException)

type AGUnmarshaler

type AGUnmarshaler struct {
	Unmarshaller
	// contains filtered or unexported fields
}

func NewAGUnmarshaler

func NewAGUnmarshaler() *AGUnmarshaler

func (*AGUnmarshaler) Unmarshal

func (p *AGUnmarshaler) Unmarshal(text string) (Entity, error)

type AgeError

type AgeError struct {
	// contains filtered or unexported fields
}

func (*AgeError) Error

func (e *AgeError) Error() string

type AgeParseError

type AgeParseError struct {
	// contains filtered or unexported fields
}

func (*AgeParseError) Error

func (e *AgeParseError) Error() string

type Config

type Config struct {
	GraphName string
	Db        *sql.DB
	Dsn       string
}

type Cursor

type Cursor interface {
	Next() bool
	Close() error
}

func NewCypherCursor

func NewCypherCursor(columnCount int, rows *sql.Rows) Cursor

func NewCypherMapCursor

func NewCypherMapCursor(columnCount int, rows *sql.Rows) Cursor

type CursorProvider

type CursorProvider func(columnCount int, rows *sql.Rows) Cursor

type CypherCursor

type CypherCursor struct {
	Cursor
	// contains filtered or unexported fields
}

func (*CypherCursor) Close

func (c *CypherCursor) Close() error

func (*CypherCursor) GetRow

func (c *CypherCursor) GetRow() ([]Entity, error)

func (*CypherCursor) Next

func (c *CypherCursor) Next() bool

type CypherMapCursor

type CypherMapCursor struct {
	CypherCursor
	// contains filtered or unexported fields
}

func (*CypherMapCursor) GetRow

func (c *CypherMapCursor) GetRow() ([]interface{}, error)

func (*CypherMapCursor) PutType

func (c *CypherMapCursor) PutType(label string, tp reflect.Type)

type Driver

type Driver interface {
	Prepare() (bool, error)
	Close() error
	DB() *sql.DB
	Begin() (Tx, error)
}

func New

func New(cnf Config) Driver

type Edge

type Edge struct {
	*LabeledEntity
	// contains filtered or unexported fields
}

func NewEdge

func NewEdge(id int64, label string, start int64, end int64, props map[string]interface{}) *Edge

func (*Edge) EndId

func (e *Edge) EndId() int64

func (*Edge) GType

func (e *Edge) GType() GTYPE

func (*Edge) StartId

func (e *Edge) StartId() int64

func (*Edge) String

func (e *Edge) String() string

type Entity

type Entity interface {
	GType() GTYPE
	String() string
}

Entity object interface for parsed AGE result data : Vertex, Edge, Path and SimpleEntity

type GTYPE

type GTYPE uint8

GTYPE representing entity types for AGE result data : Vertex, Edge, Path and SimpleEntity

const (
	G_OTHER GTYPE = 1 + iota
	G_VERTEX
	G_EDGE
	G_PATH
	G_MAP_PATH
	G_STR
	G_INT
	G_INTBIG
	G_FLOAT
	G_FLOATBIG
	G_BOOL
	G_NULL
	G_MAP
	G_ARR
)

type LabeledEntity

type LabeledEntity struct {
	Entity
	// contains filtered or unexported fields
}

func (*LabeledEntity) Id

func (n *LabeledEntity) Id() int64

func (*LabeledEntity) Label

func (n *LabeledEntity) Label() string

func (*LabeledEntity) Prop

func (n *LabeledEntity) Prop(key string) interface{}

func (*LabeledEntity) Props

func (n *LabeledEntity) Props() map[string]interface{}

return properties

type MapPath

type MapPath struct {
	Entity
	// contains filtered or unexported fields
}

func NewMapPath

func NewMapPath(entities []interface{}) *MapPath

func (*MapPath) GType

func (e *MapPath) GType() GTYPE

func (*MapPath) Get

func (e *MapPath) Get(index int) interface{}

func (*MapPath) GetAs

func (e *MapPath) GetAs(index int, val interface{})

func (*MapPath) Size

func (e *MapPath) Size() int

func (*MapPath) String

func (p *MapPath) String() string

type Mapper

type Mapper struct {
	AGUnmarshaler
}

func NewMapper

func NewMapper(typeMap map[string]reflect.Type) *Mapper

func (*Mapper) PutType

func (m *Mapper) PutType(label string, val interface{})

Add a type to the Mapper label: the label of the type tp: the type of the struct example:

Mapper.PutType("Person", VPerson{})

type Path

type Path struct {
	Entity
	// contains filtered or unexported fields
}

func NewPath

func NewPath(entities []Entity) *Path

func (*Path) GType

func (e *Path) GType() GTYPE

func (*Path) Get

func (e *Path) Get(index int) Entity

func (*Path) GetAsEdge

func (e *Path) GetAsEdge(index int) *Edge

func (*Path) GetAsVertex

func (e *Path) GetAsVertex(index int) *Vertex

func (*Path) Size

func (e *Path) Size() int

func (*Path) String

func (p *Path) String() string

type SimpleEntity

type SimpleEntity struct {
	Entity
	// contains filtered or unexported fields
}

func NewSimpleEntity

func NewSimpleEntity(value interface{}) *SimpleEntity

func (*SimpleEntity) AsArr

func (e *SimpleEntity) AsArr() []interface{}

func (*SimpleEntity) AsBigFloat

func (e *SimpleEntity) AsBigFloat() *big.Float

func (*SimpleEntity) AsBigInt

func (e *SimpleEntity) AsBigInt() *big.Int

func (*SimpleEntity) AsBool

func (e *SimpleEntity) AsBool() bool

func (*SimpleEntity) AsFloat

func (e *SimpleEntity) AsFloat() float64

func (*SimpleEntity) AsInt

func (e *SimpleEntity) AsInt() int

func (*SimpleEntity) AsInt64

func (e *SimpleEntity) AsInt64() int64

func (*SimpleEntity) AsMap

func (e *SimpleEntity) AsMap() map[string]interface{}

func (*SimpleEntity) AsStr

func (e *SimpleEntity) AsStr() string

func (*SimpleEntity) GType

func (e *SimpleEntity) GType() GTYPE

func (*SimpleEntity) IsNull

func (e *SimpleEntity) IsNull() bool

func (*SimpleEntity) String

func (e *SimpleEntity) String() string

func (*SimpleEntity) Value

func (e *SimpleEntity) Value() interface{}

type Tx

type Tx interface {
	Commit() error
	Rollback() error
	Exec(columnCount int, cypher string, args ...interface{}) (*CypherCursor, error)
	ExecMap(columnCount int, cypher string, args ...interface{}) (*CypherMapCursor, error)
}

type UnmarshalVisitor

type UnmarshalVisitor struct {
	parser.AgeVisitor
	// contains filtered or unexported fields
}

func (*UnmarshalVisitor) Visit

func (v *UnmarshalVisitor) Visit(tree antlr.ParseTree) interface{}

func (*UnmarshalVisitor) VisitAgeout

func (v *UnmarshalVisitor) VisitAgeout(ctx *parser.AgeoutContext) interface{}

func (*UnmarshalVisitor) VisitArr

func (v *UnmarshalVisitor) VisitArr(ctx *parser.ArrContext) interface{}

Visit a parse tree produced by AgeParser#arr.

func (*UnmarshalVisitor) VisitChildren

func (v *UnmarshalVisitor) VisitChildren(node antlr.RuleNode) interface{}

func (*UnmarshalVisitor) VisitEdge

func (v *UnmarshalVisitor) VisitEdge(ctx *parser.EdgeContext) interface{}

Visit a parse tree produced by AgeParser#edge.

func (*UnmarshalVisitor) VisitErrorNode

func (v *UnmarshalVisitor) VisitErrorNode(node antlr.ErrorNode) interface{}

func (*UnmarshalVisitor) VisitPair

func (v *UnmarshalVisitor) VisitPair(ctx *parser.PairContext) interface{}

Visit a parse tree produced by AgeParser#pair.

func (*UnmarshalVisitor) VisitPath

func (v *UnmarshalVisitor) VisitPath(ctx *parser.PathContext) interface{}

Visit a parse tree produced by AgeParser#path.

func (*UnmarshalVisitor) VisitProperties

func (v *UnmarshalVisitor) VisitProperties(ctx *parser.PropertiesContext) interface{}

Visit a parse tree produced by AgeParser#properties.

func (*UnmarshalVisitor) VisitTerminal

func (v *UnmarshalVisitor) VisitTerminal(node antlr.TerminalNode) interface{}

func (*UnmarshalVisitor) VisitValue

func (v *UnmarshalVisitor) VisitValue(ctx *parser.ValueContext) interface{}

Visit a parse tree produced by AgeParser#value.

func (*UnmarshalVisitor) VisitVertex

func (v *UnmarshalVisitor) VisitVertex(ctx *parser.VertexContext) interface{}

Visit a parse tree produced by AgeParser#vertex.

type Unmarshaller

type Unmarshaller interface {
	Unmarshal(text string) (Entity, error)
}

type Vertex

type Vertex struct {
	*LabeledEntity
}

func NewVertex

func NewVertex(id int64, label string, props map[string]interface{}) *Vertex

func (*Vertex) GType

func (v *Vertex) GType() GTYPE

func (*Vertex) String

func (v *Vertex) String() string

type Visitor

type Visitor struct {
	UnmarshalVisitor
	// contains filtered or unexported fields
}

func (*Visitor) PutType

func (v *Visitor) PutType(label string, tp reflect.Type)

func (*Visitor) VisitAgeout

func (v *Visitor) VisitAgeout(ctx *parser.AgeoutContext) interface{}

func (*Visitor) VisitChildren

func (v *Visitor) VisitChildren(node antlr.RuleNode) interface{}

func (*Visitor) VisitEdge

func (v *Visitor) VisitEdge(ctx *parser.EdgeContext) interface{}

Visit a parse tree produced by AgeParser#edge.

func (*Visitor) VisitPath

func (v *Visitor) VisitPath(ctx *parser.PathContext) interface{}

func (*Visitor) VisitVertex

func (v *Visitor) VisitVertex(ctx *parser.VertexContext) interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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