Permalink
Browse files

Add size to multi_query; Improve comments parsing

  • Loading branch information...
1 parent ed4859c commit ed71c70ca13285ea4b56af10791ae9d987e1a3bf @wdavidw committed Feb 9, 2012
Showing with 16 additions and 15 deletions.
  1. +1 −1 README.md
  2. +11 −9 lib/hive.coffee
  3. +4 −5 test/EscapeTest.coffee
View
@@ -169,7 +169,7 @@ client.execute('use default', function(err){
For conveniency, we've added two functions, `multi_execute` and `multi_query` which
may run multiple requests in sequential mode inside a same client connection. They
-are both the same at the exeption of the last requests:
+are both the same except how the last query is handled:
- `multi_execute` will end with an `execute` call, thus it's API is the same
as the `execute` function.
View
@@ -8,17 +8,17 @@ EventEmitter = require('events').EventEmitter
split = module.exports.split = (hqls) ->
return hqls if Array.isArray hqls
- commented = false
+ commented = false # Are we in a commented mode
hqls = hqls.split('\n').filter( (line) ->
line = line.trim()
- skip = false
+ skip = false # Should we skip the current line
if not commented and line.indexOf('/*') isnt -1
commented = '/*'
skip = true
else if not commented and line is '--'
commented = '--'
skip = true
- else if commented is '/*' and (line.lastIndexOf('*/') is (line.length - 2))
+ else if commented is '/*' and line.lastIndexOf('*/') isnt -1 and line.lastIndexOf('*/') is (line.length - 2)
commented = false
skip = true
else if commented is '--' and line is '--'
@@ -65,7 +65,7 @@ module.exports.createClient = (options = {}) ->
query: (query, size) ->
if arguments.length is 2 and typeof size is 'function'
callback = size
- size = -1
+ size = null
exec = ->
emitter.emit 'before', query
client.execute query, (err) ->
@@ -86,12 +86,14 @@ module.exports.createClient = (options = {}) ->
#emitter = new EventEmitter
count = 0
emitter = new Stream
+ emitter.size = size
emitter.readable = true
emitter.paused = 0
emitter.query = (q) ->
throw new Error 'Query already defined' if query
query = q
exec()
+ @
emitter.pause = ->
@paused = 1
emitter.resume = ->
@@ -114,7 +116,7 @@ module.exports.createClient = (options = {}) ->
for row in rows
emitter.emit 'row-first', row, 0 if count is 0
emitter.emit 'row', row, count++
- if rows.length is size
+ if rows.length is emitter.size
fetch() unless emitter.paused
else
emitter.emit 'row-last', row, count - 1
@@ -123,8 +125,8 @@ module.exports.createClient = (options = {}) ->
emitter.emit 'both', null, query
fetch = ->
return if emitter.paused or not emitter.readable
- if size
- then client.fetchN size, handle
+ if emitter.size
+ then client.fetchN emitter.size, handle
else client.fetchAll handle
emitter
multi_execute: (hqls, callback) ->
@@ -141,7 +143,7 @@ module.exports.createClient = (options = {}) ->
emitter.emit.call emitter, 'both', arguments...
callback err if callback
emitter
- multi_query: (hqls) ->
+ multi_query: (hqls, size) ->
hqls = split(hqls)
query = @query()
each(hqls)
@@ -161,6 +163,6 @@ module.exports.createClient = (options = {}) ->
query.emit 'both', err, query
exec.on 'end', -> next()
else
- query.query(hql)
+ query.query(hql, size)
query
View
@@ -17,7 +17,6 @@ module.exports =
/*
create table
-- with some dash
- */
CREATE TABLE IF NOT EXISTS #{config.table} (
a_bigint BIGINT,
an_int INT,
@@ -29,18 +28,18 @@ module.exports =
LOAD DATA LOCAL INPATH '#{__dirname}/data.csv' OVERWRITE INTO TABLE #{config.table};
-- return data
SELECT * FROM #{config.table};
+ */
+ show databases;
""")
.on 'before', (query) ->
count_before++
.on 'row', (row) ->
count_row++
.on 'error', (err) ->
- console.log err
assert.ifError err
.on 'end', (query) ->
- assert.eql count_before, 4
- assert.eql count_row, 54
- assert.eql query, "SELECT * FROM #{config.table}"
+ assert.eql count_before, 2
+ assert.eql query, "show databases"
next()
'Close': (next) ->
client.end()

0 comments on commit ed71c70

Please sign in to comment.