Method: Sequel::MySQL::Database#connect

Defined in:
lib/sequel/adapters/mysql.rb

#connect(server) ⇒ Object

Connect to the database. In addition to the usual database options, the following options have effect:

:auto_is_null

Set to true to use MySQL default behavior of having a filter for an autoincrement column equals NULL to return the last inserted row.

:charset

Same as :encoding (:encoding takes precendence)

:compress

Set to false to not compress results from the server

:config_default_group

The default group to read from the in the MySQL config file.

:config_local_infile

If provided, sets the Mysql::OPT_LOCAL_INFILE option on the connection with the given value.

:connect_timeout

Set the timeout in seconds before a connection attempt is abandoned.

:encoding

Set all the related character sets for this connection (connection, client, database, server, and results).

:read_timeout

Set the timeout in seconds for reading back results to a query.

:socket

Use a unix socket file instead of connecting via TCP/IP.

:timeout

Set the timeout in seconds before the server will disconnect this connection (a.k.a @@wait_timeout).



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sequel/adapters/mysql.rb', line 72

def connect(server)
  opts = server_opts(server)

  if Mysql.respond_to?(:init)
    conn = Mysql.init
    conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client")
    conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile)
    if encoding = opts[:encoding] || opts[:charset]
      # Set encoding before connecting so that the mysql driver knows what
      # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP.
      conn.options(Mysql::SET_CHARSET_NAME, encoding)
    end
    if read_timeout = opts[:read_timeout] and defined? Mysql::OPT_READ_TIMEOUT
      conn.options(Mysql::OPT_READ_TIMEOUT, read_timeout)
    end
    if connect_timeout = opts[:connect_timeout] and defined? Mysql::OPT_CONNECT_TIMEOUT
      conn.options(Mysql::OPT_CONNECT_TIMEOUT, connect_timeout)
    end
  else
    # ruby-mysql 3 API
    conn = Mysql.new
    # no support for default group
    conn.local_infile = opts[:config_local_infile] if opts.has_key?(:config_local_infile)
    if encoding = opts[:encoding] || opts[:charset]
      conn.charset = encoding
    end
    if read_timeout = opts[:read_timeout]
      conn.read_timeout = read_timeout
    end
    if connect_timeout = opts[:connect_timeout]
      conn.connect_timeout = connect_timeout
    end
    conn.singleton_class.class_eval do
      alias real_connect connect
      alias use_result store_result
    end
  end

  conn.ssl_set(opts[:sslkey], opts[:sslcert], opts[:sslca], opts[:sslcapath], opts[:sslcipher]) if opts[:sslca] || opts[:sslkey]
  conn.real_connect(
    opts[:host] || 'localhost',
    opts[:user],
    opts[:password],
    opts[:database],
    (opts[:port].to_i if opts[:port]),
    opts[:socket],
    Mysql::CLIENT_MULTI_RESULTS +
    Mysql::CLIENT_MULTI_STATEMENTS +
    (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS)
  )
  sqls = mysql_connection_setting_sqls

  # Set encoding a slightly different way after connecting,
  # in case the READ_DEFAULT_GROUP overrode the provided encoding.
  # Doesn't work across implicit reconnects, but Sequel doesn't turn on
  # that feature.
  sqls.unshift("SET NAMES #{literal(encoding.to_s)}") if encoding

  sqls.each{|sql| log_connection_yield(sql, conn){conn.query(sql)}}

  add_prepared_statements_cache(conn)
  conn
end