#!/usr/bin/env ruby require 'bundler/setup' require 'shiba' require 'shiba/analyzer' require 'shiba/index' require 'shiba/configure' require 'shiba/output' require 'optionparser' options = {} parser = Shiba::Configure.make_options_parser(options) parser.banner = "Run a list of queries through shiba's analyzer." parser.parse! ["database", "username"].each do |opt| if !options[opt] $stderr.puts "Required: #{opt}" $stderr.puts parser.banner exit 1 end end file = options.delete("file") file = File.open(file, "r") if file output = options.delete("explain") output = File.open(output, 'w') if output Shiba.configure(options) schema_stats_fname = options["stats"] if schema_stats_fname && !File.exist?(schema_stats_fname) $stderr.puts "No such file: #{schema_stats_fname}" exit 1 end if schema_stats_fname schema_stats = Shiba::Index.parse(schema_stats_fname) local_db_stats = Shiba::Index.query(Shiba.connection) Shiba::Index.fuzz!(local_db_stats) local_db_stats.each do |table, values| schema_stats[table] = values unless schema_stats[table] end else schema_stats = Shiba::Index.query(Shiba.connection) if Shiba::Index.insufficient_stats?(schema_stats) $stderr.puts "WARN: insufficient stats available in the #{options["database"]} database, guessing at numbers." $stderr.puts "To get better analysis please specify an index statistics file." sleep 0.5 Shiba::Index.fuzz!(schema_stats) end end file = $stdin if file.nil? output = $stdout if output.nil? queries = Shiba::Analyzer.analyze(file, output, schema_stats, options) Shiba::Output.new(queries, { 'output' => options['output'] }).make_web!