Sha256: c47dfc6178068608537692f95cb36f06a21c1a08b2aee3cf248de3280ed0ba91

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

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

  def self.parse(stmt, create_user = nil)
    parser = self.new(stmt, create_user)
    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 @create_user
      @create_user.slice!(/\s+REQUIRE\s+(\S+(?:\s+'[^']+')?)(?:\s+.+)?\s+PASSWORD\s+.+\z/)
      required = $1
    end

    if required && required != 'NONE'
      @parsed[:require] = required.strip
    end
  end

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

    if @create_user
      @create_user.slice!(/\s+IDENTIFIED\s+WITH\s+'[^']+'\s+AS\s+('[^']+')/)
      identified = $1
      identified = "PASSWORD #{identified}" if identified
    end

    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

1 entries across 1 versions & 1 rubygems

Version Path
gratan-0.3.1.beta3 lib/gratan/grant_parser.rb