Permalink
Browse files

support for query result paging

  • Loading branch information...
theganyo committed Jan 3, 2013
1 parent aa2a86c commit f824de3aac2ea7ba51f1dc5e0949747132e4c55e
Showing with 57 additions and 56 deletions.
  1. +22 −0 README.md
  2. +2 −26 lib/ugc/commands/query.rb
  3. +10 −29 lib/ugc/helpers/formatters.rb
  4. +22 −0 lib/ugc/helpers/sql.rb
  5. +1 −1 lib/ugc/version.rb
View
@@ -46,3 +46,25 @@ Connect to an Apigee administrator account:
### Examples
![image](https://github.com/scottganyo/ugc/raw/master/examples.jpeg)
+
+## Release notes
+
+### 0.0.3
+* New features
+ 1. support for query result paging
+
+
+## Copyright
+Copyright (c) 2013 Scott Ganyo
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use the included files except in compliance with the License.
+
+You may obtain a copy of the License at
+
+ <http://www.apache.org/licenses/LICENSE-2.0>
+
+Unless required by applicable law or agreed to in writing, software distributed under
+the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+either express or implied. See the License for the specific language governing permissions and
+limitations under the License.
View
@@ -22,33 +22,9 @@
query.gsub! /from\s+#{type}/i, ''
end
- result = $application[type].query query
+ response = $application[type].query query
- collection = result.collection
- format_collection collection, parsed_query['select']
+ format_collection response.collection, parsed_query['select']
end
end
-
-def parse_sql(query)
- result = {}
- keywords = %w(select from where)
- current = nil
- query.downcase.split(/[\s,*]/).each do |ea|
- next if ea == ''
- if keywords.include? ea
- current = ea
- elsif current
- if result[current]
- if result[current].is_a? Array
- result[current] << ea
- else
- result[current] = [result[current]] << ea
- end
- else
- result[current] = ea
- end
- end
- end
- result
-end
@@ -1,10 +1,10 @@
SKIP_ATTRS = %w(metadata uri type)
-def format_result(result)
- if result.multiple_entities? && result.collection.size > 1
- format_collection(result.collection)
+def format_result(response)
+ if response.multiple_entities? && response.collection.size > 1
+ format_collection(response.collection)
else
- format_entity(result.entity)
+ format_entity(response.entity)
end
end
@@ -23,7 +23,7 @@ def format_collection(collection, headers=nil)
end
collection.each_with_index do |entity, index|
row do
- column index
+ column index+1
if entity.is_a? Array
entity.each do |v|
column v
@@ -36,6 +36,9 @@ def format_collection(collection, headers=nil)
end
end
end
+ if collection.cursor && agree('Next Page? (Y/N)') {|q| q.default = 'Y'}
+ format_collection(collection.next_page, headers)
+ end
else
puts "0 results"
end
@@ -65,27 +68,5 @@ def equal_column_size(num_cols)
end
def terminal_columns
- size = detect_terminal_size
- size ? size[0] : 80
-end
-
-# Returns [width, height] of terminal when detected, nil if not detected.
-# Think of this as a simpler version of Highline's Highline::SystemExtensions.terminal_size()
-def detect_terminal_size
- if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
- [ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
- elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
- [`tput cols`.to_i, `tput lines`.to_i]
- elsif STDIN.tty? && command_exists?('stty')
- `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
- else
- nil
- end
-rescue
- nil
-end
-
-# Determines if a shell command exists by searching for it in ENV['PATH'].
-def command_exists?(command)
- ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
-end
+ HighLine.new.output_cols
+end
View
@@ -0,0 +1,22 @@
+def parse_sql(query)
+ result = {}
+ keywords = %w(select from where)
+ current = nil
+ query.downcase.split(/[\s,*]/).each do |ea|
+ next if ea == ''
+ if keywords.include? ea
+ current = ea
+ elsif current
+ if result[current]
+ if result[current].is_a? Array
+ result[current] << ea
+ else
+ result[current] = [result[current]] << ea
+ end
+ else
+ result[current] = ea
+ end
+ end
+ end
+ result
+end
View
@@ -1,3 +1,3 @@
module Ugc
- VERSION = '0.0.2'
+ VERSION = '0.0.3'
end

0 comments on commit f824de3

Please sign in to comment.