Skip to content

Commit

Permalink
support for query result paging
Browse files Browse the repository at this point in the history
  • Loading branch information
theganyo committed Jan 3, 2013
1 parent aa2a86c commit f824de3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 56 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -46,3 +46,25 @@ Connect to an Apigee administrator account:
### Examples ### Examples


![image](https://github.com/scottganyo/ugc/raw/master/examples.jpeg) ![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.
28 changes: 2 additions & 26 deletions lib/ugc/commands/query.rb
Expand Up @@ -22,33 +22,9 @@
query.gsub! /from\s+#{type}/i, '' query.gsub! /from\s+#{type}/i, ''
end end


result = $application[type].query query response = $application[type].query query


collection = result.collection format_collection response.collection, parsed_query['select']
format_collection collection, parsed_query['select']
end end


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
39 changes: 10 additions & 29 deletions lib/ugc/helpers/formatters.rb
@@ -1,10 +1,10 @@
SKIP_ATTRS = %w(metadata uri type) SKIP_ATTRS = %w(metadata uri type)


def format_result(result) def format_result(response)
if result.multiple_entities? && result.collection.size > 1 if response.multiple_entities? && response.collection.size > 1
format_collection(result.collection) format_collection(response.collection)
else else
format_entity(result.entity) format_entity(response.entity)
end end
end end


Expand All @@ -23,7 +23,7 @@ def format_collection(collection, headers=nil)
end end
collection.each_with_index do |entity, index| collection.each_with_index do |entity, index|
row do row do
column index column index+1
if entity.is_a? Array if entity.is_a? Array
entity.each do |v| entity.each do |v|
column v column v
Expand All @@ -36,6 +36,9 @@ def format_collection(collection, headers=nil)
end end
end end
end end
if collection.cursor && agree('Next Page? (Y/N)') {|q| q.default = 'Y'}
format_collection(collection.next_page, headers)
end
else else
puts "0 results" puts "0 results"
end end
Expand Down Expand Up @@ -65,27 +68,5 @@ def equal_column_size(num_cols)
end end


def terminal_columns def terminal_columns
size = detect_terminal_size HighLine.new.output_cols
size ? size[0] : 80 end
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
22 changes: 22 additions & 0 deletions lib/ugc/helpers/sql.rb
@@ -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
2 changes: 1 addition & 1 deletion lib/ugc/version.rb
@@ -1,3 +1,3 @@
module Ugc module Ugc
VERSION = '0.0.2' VERSION = '0.0.3'
end end

0 comments on commit f824de3

Please sign in to comment.