Skip to content

Commit

Permalink
Improve multi_exec before events
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Dec 5, 2011
1 parent d1fb70b commit e37cb1f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
43 changes: 31 additions & 12 deletions lib/hive.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -84,29 +92,40 @@ 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
then client.fetchN size, handle
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

27 changes: 22 additions & 5 deletions test/MultiTest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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};
Expand All @@ -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()
Expand Down

0 comments on commit e37cb1f

Please sign in to comment.