lib/ddbcli/ddb-driver.rb in ddbcli-0.2.5 vs lib/ddbcli/ddb-driver.rb in ddbcli-0.2.6
- old
+ new
@@ -1,10 +1,11 @@
require 'ddbcli/ddb-client'
require 'ddbcli/ddb-parser.tab'
require 'ddbcli/ddb-iteratorable'
require 'forwardable'
+require 'strscan'
module DynamoDB
class Driver
extend Forwardable
@@ -113,14 +114,14 @@
f.puts(retval.kind_of?(Array) ? retval.map {|i| i.to_s }.join("\n") : retval.to_s)
f.close_write
f.read
end
when :overwrite
- open(script, 'wb') {|f| print_json(retval, f, opts) }
+ open(script, 'wb') {|f| print_json(retval, f, opts.merge(:show_rows => false, :strip => true)) }
retval = nil
when :append
- open(script, 'ab') {|f| print_json(retval, f, opts) }
+ open(script, 'ab') {|f| print_json(retval, f, opts.merge(:show_rows => false, :strip => true)) }
retval = nil
else
retval
end
rescue Exception => e
@@ -162,14 +163,15 @@
end
private
def do_show_tables(parsed)
- do_show_tables0(parsed.limit)
+ do_show_tables0(parsed.like, parsed.limit)
end
- def do_show_tables0(limit = nil)
+ def do_show_tables0(like, limit = nil)
+ like = like ? like_to_regexp(like) : nil
req_hash = {}
table_names = []
req_hash['Limit'] = limit if limit
@@ -188,15 +190,15 @@
if limit or not letn
break
end
end
- return table_names
+ return like ? table_names.select {|i| i =~ like } : table_names
end
def do_show_table_status(parsed)
- table_names = do_show_tables0
+ table_names = do_show_tables0(parsed.like)
h = {}
table_names.map do |table_name|
table_info = @client.query('DescribeTable', 'TableName' => table_name)['Table']
h[table_name] = {}
@@ -847,9 +849,33 @@
until (res_data['UnprocessedItems'] || {}).empty?
req_hash['RequestItems'] = res_data['UnprocessedItems']
res_data = @client.query('BatchWriteItem', req_hash)
end
+ end
+
+ def like_to_regexp(like)
+ ss = StringScanner.new(like)
+ tok = nil
+ regexp = ''
+
+ until ss.eos?
+ if (tok = ss.scan /\\\\/)
+ regexp << '\\'
+ elsif (tok = ss.scan /\\%/)
+ regexp << '%'
+ elsif (tok = ss.scan /\\_/)
+ regexp << '_'
+ elsif (tok = ss.scan /%/)
+ regexp << '.*'
+ elsif (tok = ss.scan /_/)
+ regexp << '.'
+ elsif (tok = ss.scan /[^\\%_]+/)
+ regexp << tok
+ end
+ end
+
+ Regexp.compile("\\A#{regexp}\\Z")
end
end # Driver
end # DynamoDB