Sha256: 67dd0472cccff5cae7eedda589d46027614748f3d3094fd957125722c4ded3c0

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'niceql'

module Sqlcomposer
  module Cli
    class << self
      def start
        prepared_sql = nil
        ARGF.each do |line|
          prepared_sql = parse_prepared_sql(line) if line =~ /=>\s+Preparing:/
          params = parse_params(prepared_sql, line) if line =~ /=>\s+Parameters:/
          if prepared_sql && params
            sql = compose_sql(prepared_sql, params)
            if $stdout.isatty
              puts format_sql(sql, true)
            else
              puts format_sql(sql, false)
            end
            puts "\n\n"
            prepared_sql = nil
          end
        end
      end

      def parse_prepared_sql(line)
        line.split(/=>\s+Preparing:/).last.chomp
      end

      def parse_params(raw_sql, line)
        return [] unless raw_sql
        raw_params = line.split(/=>\s+Parameters:/).last.chomp.strip
        raw_params.split(/(?<=\)), /).map do |raw_param|
          raw_param.strip.scan(/([^()]+)\((\w+?)\)/).first
        end
      end

      def compose_sql(raw_sql, params)
        return raw_sql if params.nil? || params.empty?
        sql = raw_sql.dup
        sql.scan(/\?/).each_with_index do |place_holder, idx|
          sql.sub!(/\?/) do |_|
            quote_param(params[idx])
          end
        end
        sql
      end

      def quote_param(param)
        value, type = param
        if %w[Integer Byte BigDecimal].include?(type)
          value
        else
          "'#{value}'"
        end
      end

      def format_sql(sql, color)
        Niceql::Prettifier.prettify_sql(sql + ';', color)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sqlcomposer-0.1.1 lib/sqlcomposer/cli.rb