Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Last active June 23, 2019 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgaskins/c1e92a57f3ac8a9953851d1e151b3125 to your computer and use it in GitHub Desktop.
Save jgaskins/c1e92a57f3ac8a9953851d1e151b3125 to your computer and use it in GitHub Desktop.
Neo4j::Bolt::Connection#exec_cast with a block
def exec_cast(query : String, parameters : Map, types : Tuple(*TYPES), &block) forall TYPES
send Commands::Run, query, parameters
send Commands::PullAll
result = read_raw_result
until result[1] != 0x71
# First 3 bytes are Structure, Record, and List
# TODO: If the RETURN clause in the query has more than 16 items,
# this will break because the List byte marker and its size won't be
# in a single byte. We'll need to detect this here.
io = IO::Memory.new(result + 3)
{% begin %}
row = {
{% for type in TYPES %}
{{type.stringify.gsub(/\.class$/, "").id}}.from_bolt(io),
{% end %}
}
yield row
{% end %}
result = read_raw_result
end
end
connection.exec_cast(query, parameters, { User, Group }) do |(user, group)|
user.should be_a User
group.should be_a Group
end
results = Array({ User, Group }).new
connection.exec_cast(query, parameters, { User, Group }) do |row|
results << row
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment