2019-07-12 09:03:28 +12:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'bundler/setup'
|
|
|
|
require 'ace_of_base'
|
|
|
|
require 'optionparser'
|
|
|
|
|
|
|
|
options = {}
|
|
|
|
OptionParser.new do |opts|
|
|
|
|
opts.on('-h', '--help', 'Show this help message') do
|
|
|
|
puts opts
|
|
|
|
exit(0)
|
|
|
|
end
|
|
|
|
|
2019-07-15 19:03:58 +12:00
|
|
|
opts.on('-s FIELDS',
|
|
|
|
'--select FIELDS',
|
|
|
|
'Specify fields to select out of the results. Defaults to all fields.') do |value|
|
|
|
|
options[:select] = value.split(/\s*,\s*/).map { |f| f.split(/\s*:\s*/).take(2) }
|
2019-07-12 09:03:28 +12:00
|
|
|
end
|
|
|
|
|
2019-07-15 19:03:58 +12:00
|
|
|
opts.on('-f FUNCTION',
|
|
|
|
'--filter FUNCTION',
|
|
|
|
'Restrict the results to those that match the specified filters.') do |value|
|
|
|
|
options[:filter] = AceOfBase::Filter.new(value).to_query
|
2019-07-12 09:03:28 +12:00
|
|
|
end
|
|
|
|
|
2019-07-15 19:03:58 +12:00
|
|
|
opts.on('-g FIELDS',
|
|
|
|
'--group FIELDS',
|
|
|
|
'Group the results by specified fields, required if the select contains aggregations.') do |value|
|
2019-07-12 09:03:28 +12:00
|
|
|
options[:group] = value.split(/\s*,\s*/)
|
|
|
|
end
|
2019-07-15 19:03:58 +12:00
|
|
|
|
|
|
|
opts.on('-o FIELDS',
|
|
|
|
'--order_by FIELDS',
|
|
|
|
'Sort the matching rows by the provided fields') do |value|
|
|
|
|
options[:order_by] = value.split(/\s*,\s*/)
|
|
|
|
end
|
2019-07-12 09:03:28 +12:00
|
|
|
end.parse!
|
|
|
|
|
2019-07-15 19:03:58 +12:00
|
|
|
def format_column(column)
|
|
|
|
case column
|
|
|
|
when Array
|
|
|
|
result = column.map { |v| format_column(v) }.join(',')
|
|
|
|
"[#{result}]"
|
|
|
|
when Float
|
|
|
|
format('%0.2f', column)
|
|
|
|
else
|
|
|
|
column.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
storage = AceOfBase::Storage.new
|
|
|
|
query = AceOfBase::Query.new(storage)
|
|
|
|
|
|
|
|
fields = options.fetch(:select, []).map { |field| field.first.downcase.to_sym }
|
|
|
|
query.select(*fields) unless fields.empty?
|
|
|
|
|
|
|
|
aggregates = options
|
|
|
|
.fetch(:select, [])
|
|
|
|
.select { |field| field.size == 2 }
|
|
|
|
.map { |(key, value)| [key.downcase.to_sym, value.downcase.to_sym] }
|
|
|
|
query.aggregate(Hash[aggregates]) unless aggregates.empty?
|
|
|
|
|
|
|
|
groups = options.fetch(:group, []).map { |field| field.downcase.to_sym }
|
|
|
|
query.group_by(*groups)
|
|
|
|
|
|
|
|
order_bys = options.fetch(:order_by, []).map { |field| field.downcase.to_sym }
|
|
|
|
query.order_by(*order_bys)
|
|
|
|
|
|
|
|
filters = options[:filter]
|
|
|
|
query.filter(*filters) if options[:filter]
|
|
|
|
|
|
|
|
query.execute.each do |row|
|
|
|
|
puts row.map { |c| format_column(c) }.join(',')
|
|
|
|
end
|