跳到主要内容

MySQL Server API

Server

  • createServer() - creates server instance
  • Server.listen - listen port / unix socket (same arguments as net.Server.listen)

Events

  • connect
    • new incoming connection.

Connection

  • serverHandshake({ serverVersion, protocolVersion, connectionId, statusFlags, characterSet, capabilityFlags })
    • send server handshake initialisation packet, wait handshake response and start listening for commands
  • writeOk({ affectedRows: num, insertId: num })
  • writeEof(warnings, statusFlags)
    • send EOF packet
  • writeTextResult(rows, fields)
    • write query result to client. Rows and fields are in the same format as in connection.query callback.
  • writeColumns(fields)
    • write fields + EOF packets.
  • writeTextRow(row)
    • write array (not hash!) of values as result row
  • TODO: binary protocol

Events

Every command packet received by the server will be emitted as a packet event with the parameters:

  • packet: Packet
    • The packet itself
  • knownCommand: boolean
    • is this command known to the server
  • *commandCode: number
    • the parsed command code (first byte)

In addition special events are emitted for commands received from the client. If no listener is present a fallback behavior will be invoked.

  • quit()
    • Default: close the connection
  • init_db(schemaName: string)
    • Default: return OK
  • query(sql: string)
    • Please attach a listener to this. Default: return HA_ERR_INTERNAL_ERROR
  • field_list(table: string, fields: string)
    • Default: return ER_WARN_DEPRECATED_SYNTAX
  • ping() - Default: return OK