diff --git a/lib/async/mysql/client.rb b/lib/async/mysql/client.rb index 2fb03ad..5a0ac56 100644 --- a/lib/async/mysql/client.rb +++ b/lib/async/mysql/client.rb @@ -19,6 +19,7 @@ # THE SOFTWARE. require 'async/wrapper' +require_relative 'stream' require 'mysql2' @@ -36,11 +37,15 @@ def query(sql, **options) wait_readable - return @client.async_result + if options[:stream] + return Stream.new(self, @client.async_result) + else + return @client.async_result + end end def respond_to?(*args) - @client.respond_to(*args) + @client.respond_to?(*args) end def method_missing(*args) diff --git a/lib/async/mysql/stream.rb b/lib/async/mysql/stream.rb new file mode 100644 index 0000000..f5bc8dc --- /dev/null +++ b/lib/async/mysql/stream.rb @@ -0,0 +1,46 @@ +# Copyright, 2018, by Samuel G. D. Williams. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +module Async + module MySQL + class Stream + def initialize(client, result) + @client = client + @result = result + end + + def each(&block) + @result.each do |fields| + yield fields + + # @client.wait_readable + end + end + + def respond_to?(*args) + @result.respond_to?(*args) + end + + def method_missing(*args) + @result.send(*args) + end + end + end +end diff --git a/spec/async/mysql/client_spec.rb b/spec/async/mysql/client_spec.rb index f4f3f29..860caae 100644 --- a/spec/async/mysql/client_spec.rb +++ b/spec/async/mysql/client_spec.rb @@ -15,4 +15,15 @@ connection.close end end + + it "can stream results" do + results = connection.query("SELECT * FROM seq_1_to_3", stream: true) + + results.each do |fields| + pp fields + end + # expect(.to_a).to be == [{"seq" => 1}, {"seq" => 2}, {"seq" => 3}] + + connection.close + end end