Sha256: a351d14d5c879f4892e8b82536231111dd23338de2ade5c392adb791aa79c51d

Contents?: true

Size: 1.23 KB

Versions: 30

Compression:

Stored size: 1.23 KB

Contents

class Gratan::GrantParser
  def initialize(stmt)
    @stmt = stmt.strip
    @parsed = {}
  end

  def self.parse(stmt)
    parser = self.new(stmt)
    parser.parse!
  end

  def parse!
    parse_grant
    parse_require
    parse_identified
    parse_main
    @parsed
  end

  private

  def parse_grant
    @stmt.slice!(/\s+WITH\s+(.+?)\z/)
    with_option = $1

    if with_option
      @parsed[:with] = with_option.strip
    end
  end

  def parse_require
    @stmt.slice!(/\s+REQUIRE\s+(.+?)\z/)
    required = $1

    if required
      @parsed[:require] = required.strip
    end
  end

  def parse_identified
    @stmt.slice!(/\s+IDENTIFIED BY\s+(.+?)\z/)
    identified = $1

    if identified
      @parsed[:identified] = identified.strip
    end
  end

  def parse_main
    md = /\AGRANT\s+(.+?)\s+ON\s+(.+?)\s+TO\s+'(.*)'@'(.+)'\z/.match(@stmt)
    privs, object, user, host = md.captures
    @parsed[:privs] = parse_privs(privs.strip)
    @parsed[:object] = object.gsub('`', '').strip
    @parsed[:user] = user
    @parsed[:host] = host
  end

  def parse_privs(privs)
    privs << ','
    priv_list = []

    while priv = privs.slice!(/\A[^,(]+(?:\([^)]+\))?\s*,\s*/)
      priv_list << priv.strip.sub(/,\z/, '').strip
    end

    priv_list
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
gratan-0.3.1.beta2 lib/gratan/grant_parser.rb
gratan-0.3.1.beta lib/gratan/grant_parser.rb
gratan-0.3.0 lib/gratan/grant_parser.rb
gratan-0.3.0.beta lib/gratan/grant_parser.rb
gratan-0.2.9 lib/gratan/grant_parser.rb
gratan-0.2.9.beta3 lib/gratan/grant_parser.rb
gratan-0.2.9.beta2 lib/gratan/grant_parser.rb
gratan-0.2.9.beta lib/gratan/grant_parser.rb
gratan-0.2.8 lib/gratan/grant_parser.rb
gratan-0.2.8.beta3 lib/gratan/grant_parser.rb
gratan-0.2.8.beta2 lib/gratan/grant_parser.rb
gratan-0.2.8.beta lib/gratan/grant_parser.rb
gratan-0.2.7 lib/gratan/grant_parser.rb
gratan-0.2.6 lib/gratan/grant_parser.rb
gratan-0.2.5 lib/gratan/grant_parser.rb
gratan-0.2.4 lib/gratan/grant_parser.rb
gratan-0.2.3 lib/gratan/grant_parser.rb
gratan-0.2.2 lib/gratan/grant_parser.rb
gratan-0.2.1 lib/gratan/grant_parser.rb
gratan-0.2.0 lib/gratan/grant_parser.rb