Class: DataObject::Mysql::Connection

Inherits:
Connection
  • Object
show all
Defined in:
lib/do_mysql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_string) ⇒ Connection

Returns a new instance of Connection.



15
16
17
18
19
20
21
22
23
24
# File 'lib/do_mysql.rb', line 15

def initialize(connection_string)        
  @state = STATE_CLOSED
  @connection_string = connection_string
  opts = connection_string.split(" ")
  opts.each do |opt|
    k, v = opt.split("=")
    raise ArgumentError, "you specified an invalid connection component: #{opt}" unless k && v
    instance_variable_set("@#{k}", v)
  end
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



13
14
15
# File 'lib/do_mysql.rb', line 13

def db
  @db
end

Instance Method Details

#change_database(database_name) ⇒ Object



26
27
28
29
# File 'lib/do_mysql.rb', line 26

def change_database(database_name)
  @dbname = database_name
  @connection_string.gsub(/db_name=[^ ]*/, "db_name=#{database_name}")
end

#closeObject



40
41
42
43
44
45
46
47
48
# File 'lib/do_mysql.rb', line 40

def close
  if @state == STATE_OPEN
    Mysql_c.mysql_close(@db)
    @state = STATE_CLOSED        
    true
  else
    false
  end
end

#create_command(text) ⇒ Object



50
51
52
# File 'lib/do_mysql.rb', line 50

def create_command(text)
  Command.new(self, text)
end

#openObject

Raises:

  • (ConnectionFailed)


31
32
33
34
35
36
37
38
# File 'lib/do_mysql.rb', line 31

def open
  @db = Mysql_c.mysql_init(nil)
  raise ConnectionFailed, "could not allocate a MySQL connection" unless @db
  conn = Mysql_c.mysql_real_connect(@db, @host, @user, @password, @dbname, @port || 0, @socket, @flags || 0)
  raise ConnectionFailed, "The connection with connection string #{@connection_string} failed\n#{Mysql_c.mysql_error(@db)}" unless conn
  @state = STATE_OPEN
  true
end