Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
Initial support for streaming responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 30, 2019
1 parent 6908362 commit f861d4d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/async/mysql/client.rb
Expand Up @@ -19,6 +19,7 @@
# THE SOFTWARE.

require 'async/wrapper'
require_relative 'stream'

require 'mysql2'

Expand All @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions lib/async/mysql/stream.rb
@@ -0,0 +1,46 @@
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
#
# 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
11 changes: 11 additions & 0 deletions spec/async/mysql/client_spec.rb
Expand Up @@ -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

0 comments on commit f861d4d

Please sign in to comment.