Last active
June 23, 2019 19:11
-
-
Save jgaskins/c1e92a57f3ac8a9953851d1e151b3125 to your computer and use it in GitHub Desktop.
Neo4j::Bolt::Connection#exec_cast with a block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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