class Soar::Registry::Directory::Provider::Mysql

Attributes

client[RW]

Public Class Methods

new(config: , attributes: '*', index: , credentials: ) click to toggle source

@param [Hash{String => String, Number}] config @option config [String] host @option config [Number] port @option config [String] database @option config [String] table @param [Array<String>] attributes array of attributes to return for queries @param [Array<String>] index array of indexes starting with the primary key @param [Hash{String => String}] credentials @option credentials [String] 'username' @option credentials [String] 'password'

# File lib/soar/registry/directory/provider/mysql.rb, line 24
def initialize(config: , attributes: '*', index: , credentials: )
  adapt_exceptions do
    @attributes = attributes.kind_of?(Array) ? attributes.join(', ') : attributes
    @index = index
    @table = config['table']
    @config = {
      host: config['host'],
      port: config['port'],
      database: config['database'],
      username: credentials['username'],
      password: credentials['password']
    }
    @client = ::Mysql.new(@config[:host], @config[:username], @config[:password], @config[:database])
  end
end

Public Instance Methods

fetch(primary_key) click to toggle source

@param [String] primary key the first index @return [Hash{String => String, Number}] single matching entry @raise [Soar::Registry::Directory::Error::NoEntriesFoundError] if primary key not found @raise [Soar::Registry::Directory::Error::MultipleEntriesFound] if multiple matches found

# File lib/soar/registry/directory/provider/mysql.rb, line 58
def fetch(primary_key)
  adapt_exceptions do
    statement = @client.prepare("SELECT #{@attributes} FROM #{@table} WHERE #{@index[0]} = ? LIMIT 1")
    mysql_statement = statement.execute(primary_key)
    raise Soar::Registry::Directory::Error::NoEntriesFoundError, "No entries found for #{@index[0]} = #{primary_key}" if mysql_statement.num_rows == 0
    mysql_result = mysql_statement.result_metadata
    fields = mysql_result.fetch_fields
    field_names = fields.map { |field| 
      field.name
    }
    result = mysql_statement.fetch
    return Hash[result.map.with_index { |value, index| 
      [field_names[index.to_i], value]
    }]
  end
end
index() click to toggle source

@return [Array<String>] a list of indexes the first being the primary key

# File lib/soar/registry/directory/provider/mysql.rb, line 103
def index
  adapt_exceptions do
    index = []
    @client.query("SHOW INDEX FROM Client").each_hash { |entry| 
      index << entry['Column_name']
    }
    index
  end
end
put(entry) click to toggle source

@param [Hash{String => String, Number}] entry @return [Boolean]

# File lib/soar/registry/directory/provider/mysql.rb, line 44
def put(entry)
  adapt_exceptions do
    statement = @client.prepare("INSERT INTO #{@table} (#{entry.keys.join(', ')}) VALUES(#{entry.values.map { |s| "?" }.join(', ')})")
    statement.execute(*entry.values)
    return true
  end
end

Private Instance Methods

adapt_exceptions() { || ... } click to toggle source
# File lib/soar/registry/directory/provider/mysql.rb, line 115
def adapt_exceptions
  begin
    yield
  rescue ::Mysql::Error => e
    raise Soar::Registry::Directory::Error::NetworkingError, e.message
  end
end