class Neo4j::Bolt::Connection

Defined in:

neo4j/bolt/connection.cr

Constant Summary

EXCEPTIONS = {"Neo.ClientError.Schema.IndexAlreadyExists" => IndexAlreadyExists, "Neo.ClientError.Schema.ConstraintValidationFailed" => ConstraintValidationFailed, "Neo.ClientError.Schema.EquivalentSchemaRuleAlreadyExists" => EquivalentSchemaRuleAlreadyExists, "Neo.ClientError.Procedure.ProcedureCallFailed" => ProcedureCallFailed, "Neo.ClientError.Statement.ParameterMissing" => ParameterMissing, "Neo.ClientError.Statement.SyntaxError" => SyntaxError, "Neo.ClientError.Statement.ArgumentError" => ArgumentError}
GOGOBOLT = "``\xB0\u0017".to_slice
SUPPORTED_VERSIONS = {516, 260, 4, 0}

Constructors

Instance Method Summary

Instance methods inherited from class Object

to_bolt_params : Neo4j::Value to_bolt_params

Constructor Detail

def self.new(url : String, ssl : Bool = true) #

Initializes this connection with the given URL string and SSL flag.

connection = Neo4j::Bolt::Connection.new("bolt://neo4j:password@localhost", ssl: false)

[View source]
def self.new(uri : URI, ssl : Bool = true, connection_retries = 5) #

Initializes this connection with the given URI and SSL flag. SSL defaults to true so that, if you omit it by mistake, you aren't sending your database credentials in plaintext.

uri = URI.parse("bolt://neo4j:password@localhost")
connection = Neo4j::Bolt::Connection.new(uri, ssl: false)

[View source]
def self.new #

[View source]

Instance Method Detail

def begin(metadata = Map.new) #

[View source]
def close #

[View source]
def commit #

[View source]
def exec_cast(query : String, parameters : NamedTuple, types : Tuple) #

Execute the given query with the given parameters, returning an array containing the results cast into the given types.

struct User
  Neo4j.map_node(
    id: UUID,
    email: String,
  )
end

connection.exec_cast(<<-CYPHER, {email: "[email protected]"}, {User})
  MATCH (user:User { email: $email })
  RETURN user
CYPHER

[View source]
def exec_cast(query : String, parameters : Map, types : Tuple(*TYPES)) forall TYPES #

Execute the given query with the given parameters, returning an array containing the results cast into the given types.

struct User
  Neo4j.map_node(
    id: UUID,
    email: String,
  )
end

connection.exec_cast(<<-CYPHER, Neo4j::Map{"email" => "[email protected]"}, {User})
  MATCH (user:User { email: $email })
  RETURN user
CYPHER
# => [{User(@id="4478440e-1897-41a9-812d-91f6d21b994b", @email="[email protected]")}]

[View source]
def exec_cast(query : String, types : Tuple(*TYPES)) forall TYPES #

[View source]
def exec_cast(query : String, parameters : NamedTuple, types : Tuple, &) #

Execute the given query with the given parameters, yielding a tuple containing the results cast into the given types.

struct User
  Neo4j.map_node(
    id: UUID,
    email: String,
  )
end

connection.exec_cast(<<-CYPHER, {email: "[email protected]"}, {User}) do |(user)|
  MATCH (user:User { email: $email })
  RETURN user
CYPHER
  process user
end

[View source]
def exec_cast(query : String, parameters : Map, types : Tuple(*TYPES), metadata = Map.new, &) : Nil forall TYPES #

Execute the given query with the given parameters, yielding a tuple containing the results cast into the given types.

struct User
  Neo4j.map_node(
    id: UUID,
    email: String,
  )
end

connection.exec_cast(<<-CYPHER, Neo4j::Map{"email" => "[email protected]"}, {User}) do |(user)|
  MATCH (user:User { email: $email })
  RETURN user
CYPHER
  process user
end

[View source]
def exec_cast(query : String, types : Tuple(*TYPES), &) : Nil forall TYPES #

[View source]
def exec_cast_scalar(query : String, parameters : Map, type : T) forall T #

Execute the given query with the given parameters, returning a single result cast to the given type.

struct User
  Neo4j.map_node(
    id: UUID,
    email: String,
  )
end

connection.exec_cast_scalar(<<-CYPHER, Neo4j::Map{"email" => "[email protected]"}, User)
  MATCH (user:User { email: $email })
  RETURN user
  LIMIT 1
CYPHER
# => User(@id="4478440e-1897-41a9-812d-91f6d21b994b", @email="[email protected]")

[View source]
def exec_cast_scalar(query : String, type : T) forall T #

[View source]
def exec_cast_single(query : String, parameters : Map, types : Tuple(*TYPES)) forall TYPES #

Execute the given query with the given parameters, returning an array containing the results cast into the given types.

struct User
  Neo4j.map_node(
    id: UUID,
    email: String,
  )
end

connection.exec_cast_single(<<-CYPHER, Neo4j::Map{"email" => "[email protected]"}, {User})
  MATCH (user:User { email: $email })
  RETURN user
  LIMIT 1
CYPHER
# => {User(@id="4478440e-1897-41a9-812d-91f6d21b994b", @email="[email protected]")}

[View source]
def execute(query, parameters : Map, metadata = Map.new, &block : List -> ) #

Executes the given query with the given parameters and executes the block once for each result returned from the database.

connection.execute <<-CYPHER, Neo4j::Map{"id" => 123} do |(user)|
  MATCH (user:User { id: $id })
  RETURN user
CYPHER
  process User.new(user.as Neo4j::Node)
end

In the example above, we used () in the block arguments to destructure the list of RETURNed values. Also note that we need to cast it down to a Neo4j::Node. All values in results have the compile-time type of Neo4j::Value and so will need to be cast down to its specific type.


[View source]
def execute(_query, **parameters, &block : List -> ) #

[View source]
def execute(query, parameters : Map, metadata = Map.new) #

Execute the given query with the given parameters, returning a Result object containing query metadata and the query results in an array.

connection.execute(query, Neo4j::Map{"id" => 123})

[View source]
def execute(_query query, **params) #

Execute the given query with the given parameters, returning a Result object containing query metadata and the query results in an array.

This method provides a convenient shorthand for providing a Neo4j::Map of query parameters.

connection.execute(query, id: 123)

[View source]
def reset #

If the connection gets into a wonky state, this method tells the server to reset it back to a normal state, but you lose everything you haven't pulled down yet.


[View source]
def rollback #

[View source]
def transaction(metadata = Map.new, &) #

Wrap a group of queries into an atomic transaction. Yields a Neo4j::Bolt::Transaction.

connection.transaction do |txn|
  connection.execute query1
  connection.execute query2
end

Exceptions raised within the block will roll back the transaction. To roll back the transaction manually and exit the block, call txn.rollback.


[View source]