Class Mysql
In: lib/mysql.rb
Parent: Object

$Id: mysql.rb,v 1.24 2005/02/12 11:37:15 tommy Exp $

Copyright (C) 2003-2005 TOMITA Masahiro tommy@tmtm.org

Methods

Classes and Modules

Class Mysql::Error
Class Mysql::Field
Class Mysql::Net
Class Mysql::Random
Class Mysql::Result

Constants

VERSION = "4.0-ruby-0.2.5"
MAX_PACKET_LENGTH = 256*256*256-1
MAX_ALLOWED_PACKET = 1024*1024*1024
MYSQL_UNIX_ADDR = "/var/lib/mysql/mysql.sock"
MYSQL_PORT = 3306
PROTOCOL_VERSION = 10
COM_SLEEP = 0   Command
COM_QUIT = 1
COM_INIT_DB = 2
COM_QUERY = 3
COM_FIELD_LIST = 4
COM_CREATE_DB = 5
COM_DROP_DB = 6
COM_REFRESH = 7
COM_SHUTDOWN = 8
COM_STATISTICS = 9
COM_PROCESS_INFO = 10
COM_CONNECT = 11
COM_PROCESS_KILL = 12
COM_DEBUG = 13
COM_PING = 14
COM_TIME = 15
COM_DELAYED_INSERT = 16
COM_CHANGE_USER = 17
COM_BINLOG_DUMP = 18
COM_TABLE_DUMP = 19
COM_CONNECT_OUT = 20
COM_REGISTER_SLAVE = 21
CLIENT_LONG_PASSWORD = 1   Client flag
CLIENT_FOUND_ROWS = 1 << 1
CLIENT_LONG_FLAG = 1 << 2
CLIENT_CONNECT_WITH_DB = 1 << 3
CLIENT_NO_SCHEMA = 1 << 4
CLIENT_COMPRESS = 1 << 5
CLIENT_ODBC = 1 << 6
CLIENT_LOCAL_FILES = 1 << 7
CLIENT_IGNORE_SPACE = 1 << 8
CLIENT_INTERACTIVE = 1 << 10
CLIENT_SSL = 1 << 11
CLIENT_IGNORE_SIGPIPE = 1 << 12
CLIENT_TRANSACTIONS = 1 << 13
CLIENT_CAPABILITIES = CLIENT_LONG_PASSWORD|CLIENT_LONG_FLAG|CLIENT_TRANSACTIONS
OPT_CONNECT_TIMEOUT = 0   Connection Option
OPT_COMPRESS = 1
OPT_NAMED_PIPE = 2
INIT_COMMAND = 3
READ_DEFAULT_FILE = 4
READ_DEFAULT_GROUP = 5
SET_CHARSET_DIR = 6
SET_CHARSET_NAME = 7
OPT_LOCAL_INFILE = 8
SERVER_STATUS_IN_TRANS = 1   Server Status
SERVER_STATUS_AUTOCOMMIT = 2
REFRESH_GRANT = 1   Refresh parameter
REFRESH_LOG = 2
REFRESH_TABLES = 4
REFRESH_HOSTS = 8
REFRESH_STATUS = 16
REFRESH_THREADS = 32
REFRESH_SLAVE = 64
REFRESH_MASTER = 128

Attributes

affected_rows  [R] 
field_count  [R] 
info  [R] 
insert_id  [R] 
query_with_result  [RW] 
status  [RW] 
thread_id  [R] 

Public Class methods

[Source]

# File lib/mysql.rb, line 1114
  def debug(str)
    raise "not implemented"
  end

[Source]

# File lib/mysql.rb, line 1096
  def escape_string(str)
    str.gsub(/([\0\n\r\032\'\"\\])/) do
      case $1
      when "\0" then "\\0"
      when "\n" then "\\n"
      when "\r" then "\\r"
      when "\032" then "\\Z"
      else "\\"+$1
      end
    end
  end

[Source]

# File lib/mysql.rb, line 1089
  def finalizer(net)
    proc {
      net.clear
      net.write Mysql::COM_QUIT.chr
    }
  end

[Source]

# File lib/mysql.rb, line 1109
  def get_client_info()
    Mysql::VERSION
  end

[Source]

# File lib/mysql.rb, line 1080
  def init()
    Mysql::new :INIT
  end

[Source]

# File lib/mysql.rb, line 85
  def initialize(*args)
    @client_flag = 0
    @max_allowed_packet = MAX_ALLOWED_PACKET
    @query_with_result = true
    @status = :STATUS_READY
    if args[0] != :INIT then
      real_connect(*args)
    end
  end

[Source]

# File lib/mysql.rb, line 1084
  def real_connect(*args)
    Mysql::new(*args)
  end

Public Instance methods

[Source]

# File lib/mysql.rb, line 185
  def change_user(user="", passwd="", db="")
    data = user+"\0"+scramble(passwd, @scramble_buff, @protocol_version==9)+"\0"+db
    command COM_CHANGE_USER, data
    @user = user
    @passwd = passwd
    @db = db
  end

[Source]

# File lib/mysql.rb, line 193
  def character_set_name()
    raise "not implemented"
  end
client_info()

Alias for get_client_info

client_info()

Alias for get_client_info

[Source]

# File lib/mysql.rb, line 197
  def close()
    @status = :STATUS_READY
    command COM_QUIT, nil, true
    @net.close
    self
  end
connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, socket=nil, flag=nil)

Alias for real_connect

connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, socket=nil, flag=nil)

Alias for real_connect

[Source]

# File lib/mysql.rb, line 204
  def create_db(db)
    command COM_CREATE_DB, db
    self
  end

[Source]

# File lib/mysql.rb, line 209
  def drop_db(db)
    command COM_DROP_DB, db
    self
  end

[Source]

# File lib/mysql.rb, line 214
  def dump_debug_info()
    command COM_DEBUG
    self
  end

[Source]

# File lib/mysql.rb, line 136
  def escape_string(str)
    Mysql::escape_string str
  end

[Source]

# File lib/mysql.rb, line 141
  def get_client_info()
    VERSION
  end

[Source]

# File lib/mysql.rb, line 219
  def get_host_info()
    @host_info
  end

[Source]

# File lib/mysql.rb, line 224
  def get_proto_info()
    @protocol_version
  end

[Source]

# File lib/mysql.rb, line 229
  def get_server_info()
    @server_version
  end
host_info()

Alias for get_host_info

[Source]

# File lib/mysql.rb, line 338
  def inspect()
    "#<#{self.class}>"
  end

[Source]

# File lib/mysql.rb, line 234
  def kill(id)
    command COM_PROCESS_KILL, Net::int4str(id)
    self
  end

[Source]

# File lib/mysql.rb, line 239
  def list_dbs(db=nil)
    real_query "show databases #{db}"
    @status = :STATUS_READY
    read_rows(1).flatten
  end

[Source]

# File lib/mysql.rb, line 245
  def list_fields(table, field=nil)
    command COM_FIELD_LIST, "#{table}\0#{field}", true
    f = read_rows 6
    fields = unpack_fields(f, @server_capabilities & CLIENT_LONG_FLAG != 0)
    res = Result::new self, fields, f.length
    res.eof = true
    res
  end

[Source]

# File lib/mysql.rb, line 254
  def list_processes()
    data = command COM_PROCESS_INFO
    @field_count = get_length data
    fields = read_rows 5
    @fields = unpack_fields(fields, @server_capabilities & CLIENT_LONG_FLAG != 0)
    @status = :STATUS_GET_RESULT
    store_result
  end

[Source]

# File lib/mysql.rb, line 263
  def list_tables(table=nil)
    real_query "show tables #{table}"
    @status = :STATUS_READY
    read_rows(1).flatten
  end

[Source]

# File lib/mysql.rb, line 146
  def options(option, arg=nil)
    if option == OPT_LOCAL_INFILE then
      if arg == false or arg == 0 then
        @client_flag &= ~CLIENT_LOCAL_FILES
      else
        @client_flag |= CLIENT_LOCAL_FILES
      end
    else
      raise "not implemented"
    end
  end

[Source]

# File lib/mysql.rb, line 269
  def ping()
    command COM_PING
    self
  end
proto_info()

Alias for get_proto_info

[Source]

# File lib/mysql.rb, line 274
  def query(query)
    real_query query
    if not @query_with_result then
      return self
    end
    if @field_count == 0 then
      return nil
    end
    store_result
  end
quote(str)

Alias for escape_string

quote(str)

Alias for escape_string

[Source]

# File lib/mysql.rb, line 313
  def read_one_row(field_count)
    data = read
    return if data[0] == 254 and data.length == 1
    rec = []
    field_count.times do
      len = get_length data
      if len == nil then
        rec << len
      else
        rec << data.slice!(0,len)
      end
    end
    rec
  end

[Source]

# File lib/mysql.rb, line 95
  def real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, socket=nil, flag=nil)
    @server_status = SERVER_STATUS_AUTOCOMMIT
    if (host == nil or host == "localhost") and defined? UNIXSocket then
      unix_socket = socket || ENV["MYSQL_UNIX_PORT"] || MYSQL_UNIX_ADDR
      sock = UNIXSocket::new(unix_socket)
      @host_info = Error::err(Error::CR_LOCALHOST_CONNECTION)
      @unix_socket = unix_socket
    else      
      sock = TCPSocket::new(host, port||ENV["MYSQL_TCP_PORT"]||(Socket::getservbyname("mysql","tcp") rescue MYSQL_PORT))
      @host_info = sprintf Error::err(Error::CR_TCP_CONNECTION), host
    end
    @host = host ? host.dup : nil
    sock.setsockopt Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true
    @net = Net::new sock

    a = read
    @protocol_version = a.slice!(0)
    @server_version, a = a.split(/\0/,2)
    @thread_id, @scramble_buff = a.slice!(0,13).unpack("La8")
    if a.size >= 2 then
      @server_capabilities, = a.slice!(0,2).unpack("v")
    end
    if a.size >= 16 then
      @server_language, @server_status = a.unpack("cv")
    end

    flag = 0 if flag == nil
    flag |= @client_flag | CLIENT_CAPABILITIES
    flag |= CLIENT_CONNECT_WITH_DB if db
    data = Net::int2str(flag)+Net::int3str(@max_allowed_packet)+(user||"")+"\0"+scramble(passwd, @scramble_buff, @protocol_version==9)
    if db and @server_capabilities & CLIENT_CONNECT_WITH_DB != 0 then
      data << "\0"+db
      @db = db.dup
    end
    write data
    read
    ObjectSpace.define_finalizer(self, Mysql.finalizer(@net))
    self
  end

[Source]

# File lib/mysql.rb, line 158
  def real_query(query)
    command COM_QUERY, query, true
    read_query_result
    self
  end

[Source]

# File lib/mysql.rb, line 285
  def refresh(r)
    command COM_REFRESH, r.chr
    self
  end

[Source]

# File lib/mysql.rb, line 290
  def reload()
    refresh REFRESH_GRANT
    self
  end

[Source]

# File lib/mysql.rb, line 295
  def select_db(db)
    command COM_INIT_DB, db
    @db = db
    self
  end
server_info()

Alias for get_server_info

[Source]

# File lib/mysql.rb, line 301
  def shutdown()
    command COM_SHUTDOWN
    self
  end

[Source]

# File lib/mysql.rb, line 328
  def skip_result()
    if @status == :STATUS_USE_RESULT then
      loop do
        data = read
        break if data[0] == 254 and data.length == 1
      end
      @status = :STATUS_READY
    end
  end

[Source]

# File lib/mysql.rb, line 306
  def stat()
    command COM_STATISTICS
  end

[Source]

# File lib/mysql.rb, line 173
  def store_result()
    if @status != :STATUS_GET_RESULT then
      error Error::CR_COMMANDS_OUT_OF_SYNC
    end
    @status = :STATUS_READY
    data = read_rows @field_count
    res = Result::new self, @fields, @field_count, data
    @fields = nil
    @affected_rows = data.length
    res
  end

[Source]

# File lib/mysql.rb, line 164
  def use_result()
    if @status != :STATUS_GET_RESULT then
      error Error::CR_COMMANDS_OUT_OF_SYNC
    end
    res = Result::new self, @fields, @field_count
    @status = :STATUS_USE_RESULT
    res
  end

[Validate]