class Mysql::ServerProtocol

Simple class which doesn't do connection setup

Public Class Methods

new(socket) click to toggle source
# File lib/nose/proxy/mysql.rb, line 93
def initialize(socket)
  # We need a much simpler initialization than the default class
  @sock = socket
end

Public Instance Methods

authenticate() click to toggle source

Perform authentication

# File lib/nose/proxy/mysql.rb, line 99
def authenticate
  reset
  write InitialPacket.serialize
  AuthenticationPacket.parse read # TODO: Check auth
  write ResultPacket.serialize 0
end
error(errno, message) click to toggle source

Send an error message with the given number and text

# File lib/nose/proxy/mysql.rb, line 107
def error(errno, message)
  write ErrorPacket.serialize errno, message
end
process_command(&block) click to toggle source

Process a single incoming command

# File lib/nose/proxy/mysql.rb, line 112
def process_command(&block)
  reset
  pkt = read
  command = pkt.utiny

  case command
  when COM_QUIT
    # Stop processing because the client left
    return
  when COM_QUERY
    process_query pkt.to_s, &block
  when COM_PING
    write ResultPacket.serialize 0
  else
    # Return error for invalid commands
    protocol.error ::Mysql::ServerError::ER_NOT_SUPPORTED_YET,
                   'Command not supported'
  end
end

Private Instance Methods

process_query(query) { |self, query| ... } click to toggle source

Handle an individual query

# File lib/nose/proxy/mysql.rb, line 135
def process_query(query)
  # Execute the query on the backend
  result = yield self, query
  return if result.nil?

  # Return the list of fields in the result
  field_names = result.any? ? result.peek.keys : []
  write_fields result, field_names
  write_rows result, field_names
end
write_fields(result, field_names) click to toggle source

Write the list of fields for the resulting rows

# File lib/nose/proxy/mysql.rb, line 147
def write_fields(result, field_names)
  write ResultPacket.serialize field_names.count
  field_names.each do |field_name|
    type, = Protocol.value2net result.first[field_name]

    write FieldPacket.serialize '', '', '', field_name, '', 1, type,
                                Field::NOT_NULL_FLAG, 0, ''
  end
  write EOFPacket.serialize
end
write_rows(result, field_names) click to toggle source

Write a packet for each row in the results

# File lib/nose/proxy/mysql.rb, line 159
def write_rows(result, field_names)
  result.each do |row|
    values = field_names.map { |field_name| row[field_name] }
    write(values.map do |value|
      Protocol.value2net(value.to_s).last
    end.inject('', &:+))
  end
  write EOFPacket.serialize
end