Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improve multi_exec `before` events

  • Loading branch information...
commit e37cb1f6a527652759f4bd62888baed85614bc57 1 parent d1fb70b
@wdavidw authored
Showing with 53 additions and 17 deletions.
  1. +31 −12 lib/hive.coffee
  2. +22 −5 test/MultiTest.coffee
View
43 lib/hive.coffee
@@ -26,13 +26,21 @@ module.exports.createClient = (options = {}) ->
client: client
end: connection.end.bind connection
execute: (query, callback) ->
- client.execute query, (err) ->
- callback err if callback
+ emitter = new EventEmitter
+ process.nextTick ->
+ emitter.emit 'before', query
+ client.execute query, (err) ->
+ #if err
+ #then emitter.emit 'error', err
+ #else emitter.emit 'end', query
+ callback err, callback
+ emitter
query: (query, size) ->
if arguments.length is 2 and typeof size is 'function'
callback = size
size = -1
exec = ->
+ emitter.emit 'before', query
client.execute query, (err) ->
if err
emitter.readable = false
@@ -43,10 +51,10 @@ module.exports.createClient = (options = {}) ->
lboth = emitter.listeners('both').length
emitError = lerror or (not lerror and not lboth)
emitter.emit 'error', err if emitError
- emitter.emit 'both', err
+ emitter.emit 'both', err, query
return
fetch()
- exec() if query
+ process.nextTick exec if query
buffer = []
#emitter = new EventEmitter
count = 0
@@ -73,7 +81,7 @@ module.exports.createClient = (options = {}) ->
lboth = emitter.listeners('both').length
emitError = lerror or (not lerror and not lboth)
emitter.emit 'error', err if emitError
- emitter.emit 'both', err
+ emitter.emit 'both', err, query
return
rows = rows.map (row) -> row.split '\t'
for row in rows
@@ -84,8 +92,8 @@ module.exports.createClient = (options = {}) ->
else
emitter.emit 'row-last', row, count - 1
emitter.readable = false
- emitter.emit 'end'
- emitter.emit 'both'
+ emitter.emit 'end', query
+ emitter.emit 'both', null, query
fetch = ->
return if emitter.paused or not emitter.readable
if size
@@ -93,20 +101,31 @@ module.exports.createClient = (options = {}) ->
else client.fetchAll handle
emitter
multi_execute: (queries, callback) ->
+ emitter = new EventEmitter
queries = split(queries)
each(queries)
.on 'item', (next, query) =>
- @execute query, next
- .on 'both', (err) -> callback err
+ exec = @execute query, next
+ exec.on 'before', -> emitter.emit.call emitter, 'before', arguments...
+ exec.on 'error', -> emitter.emit.call emitter, 'error', arguments...
+ .on 'both', (err) ->
+ if err
+ then emitter.emit.call emitter, 'error', arguments...
+ else emitter.emit.call emitter, 'end', arguments...
+ emitter.emit.call emitter, 'both', arguments...
+ callback err if callback
+ emitter
multi_query: (hqls, callback) ->
hqls = split(hqls)
+ query = @query()
each(hqls)
.on 'item', (next, hql, i) =>
unless hqls.length is i + 1
- @execute hql, next
+ exec = @execute hql, next
+ exec.on 'before', -> query.emit.call query, 'before', arguments...
+ exec.on 'error', -> query.emit.call query, 'error', arguments...
else
query.query(hql)
.on 'both', (err) -> callback err
- query = @query()
-
+ query
View
27 test/MultiTest.coffee
@@ -9,7 +9,10 @@ client = hive.createClient require './config.json'
module.exports =
'Multi # Execute # String': (next) ->
- client.multi_execute """
+ count_before = 0
+ count_end = 0
+ count_both = 0
+ execute = client.multi_execute """
-- create db
CREATE DATABASE IF NOT EXISTS #{db};
-- create table
@@ -24,9 +27,19 @@ module.exports =
LOAD DATA LOCAL INPATH '#{__dirname}/data.csv' OVERWRITE INTO TABLE #{table};
""", (err) ->
assert.ifError err
+ assert.eql count_before, 3
+ assert.eql count_end, 1
+ assert.eql count_both, 1
next()
+ execute.on 'before', (query) ->
+ count_before++
+ execute.on 'end', (query) ->
+ count_end++
+ execute.on 'both', (query) ->
+ count_both++
'Multi # Query # String': (next) ->
- count = 0
+ count_before = 0
+ count_row = 0
client.multi_query("""
-- create db
CREATE DATABASE IF NOT EXISTS #{db};
@@ -43,12 +56,16 @@ module.exports =
-- return data
SELECT * FROM #{table};
""")
+ .on 'before', (query) ->
+ count_before++
.on 'row', (row) ->
- count++
+ count_row++
.on 'error', (err) ->
assert.ifError err
- .on 'end', (row) ->
- assert.eql count, 54
+ .on 'end', (query) ->
+ assert.eql count_before, 4
+ assert.eql count_row, 54
+ assert.eql query, "SELECT * FROM #{table}"
next()
'Close': (next) ->
client.end()
Please sign in to comment.
Something went wrong with that request. Please try again.