Class: Mysql

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-mysql/mysql.rb,
lib/ffi-mysql.rb,
lib/ffi-mysql/stmt.rb,
lib/ffi-mysql/time.rb,
lib/ffi-mysql/error.rb,
lib/ffi-mysql/field.rb,
lib/ffi-mysql/result.rb,
lib/ffi-mysql/version.rb,
lib/ffi-mysql/constants.rb

Overview

Basic MySQL class, provides interface to a server.

Author:

  • Frank Fischer

Defined Under Namespace

Modules: C Classes: Error, Field, Result, Stmt, Time

Constant Summary collapse

LIBPATH =
::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
PATH =
::File.dirname(LIBPATH) + ::File::SEPARATOR
MAJOR =

Major version

1
MINOR =

Minor version

1
TINY =

Tiny version

6
VERSION =

Version string

[MAJOR, MINOR, TINY].join('.')
COM_SLEEP =

Command

0
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
COM_STMT_PREPARE =
22
COM_STMT_EXECUTE =
23
COM_STMT_SEND_LONG_DATA =
24
COM_STMT_CLOSE =
25
COM_STMT_RESET =
26
COM_SET_OPTION =
27
COM_STMT_FETCH =
28
CLIENT_LONG_PASSWORD =

Client flag

1
CLIENT_FOUND_ROWS =

new more secure passwords

1 << 1
CLIENT_LONG_FLAG =

Found instead of affected rows

1 << 2
CLIENT_CONNECT_WITH_DB =

Get all column flags

1 << 3
CLIENT_NO_SCHEMA =

One can specify db on connect

1 << 4
CLIENT_COMPRESS =

Don’t allow database.table.column

1 << 5
CLIENT_ODBC =

Can use compression protocol

1 << 6
CLIENT_LOCAL_FILES =

Odbc client

1 << 7
CLIENT_IGNORE_SPACE =

Can use LOAD DATA LOCAL

1 << 8
CLIENT_PROTOCOL_41 =

Ignore spaces before ‘(’

1 << 9
CLIENT_INTERACTIVE =

New 4.1 protocol

1 << 10
CLIENT_SSL =

This is an interactive client

1 << 11
CLIENT_IGNORE_SIGPIPE =

Switch to SSL after handshake

1 << 12
CLIENT_TRANSACTIONS =

IGNORE sigpipes

1 << 13
CLIENT_RESERVED =

Client knows about transactions

1 << 14
CLIENT_SECURE_CONNECTION =

Old flag for 4.1 protocol

1 << 15
CLIENT_MULTI_STATEMENTS =

New 4.1 authentication

1 << 16
CLIENT_MULTI_RESULTS =

Enable/disable multi-stmt support

1 << 17
OPT_CONNECT_TIMEOUT =

Connection Option

0
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
OPT_PROTOCOL =
9
SHARED_MEMORY_BASE_NAME =
10
OPT_READ_TIMEOUT =
11
OPT_WRITE_TIMEOUT =
12
OPT_USE_RESULT =
13
OPT_USE_REMOTE_CONNECTION =
14
OPT_USE_EMBEDDED_CONNECTION =
15
OPT_GUESS_CONNECTION =
16
SET_CLIENT_IP =
17
SECURE_AUTH =
18
REPORT_DATA_TRUNCATION =
19
OPT_RECONNECT =
20
OPT_SSL_VERIFY_SERVER_CERT =
21
OPTION_MULTI_STATEMENTS_ON =

Server Option

0
OPTION_MULTI_STATEMENTS_OFF =
1
SERVER_STATUS_IN_TRANS =

Server Status

1
SERVER_STATUS_AUTOCOMMIT =
1 << 1
SERVER_MORE_RESULTS_EXISTS =
1 << 3
SERVER_QUERY_NO_GOOD_INDEX_USED =
1 << 4
SERVER_QUERY_NO_INDEX_USED =
1 << 5
SERVER_STATUS_CURSOR_EXISTS =
1 << 6
SERVER_STATUS_LAST_ROW_SENT =
1 << 7
SERVER_STATUS_DB_DROPPED =
1 << 8
SERVER_STATUS_NO_BACKSLASH_ESCAPES =
1 << 9
REFRESH_GRANT =

Refresh parameter

1
REFRESH_LOG =
1 << 1
REFRESH_TABLES =
1 << 2
REFRESH_HOSTS =
1 << 3
REFRESH_STATUS =
1 << 4
REFRESH_THREADS =
1 << 5
REFRESH_SLAVE =
1 << 6
REFRESH_MASTER =
1 << 7
REFRESH_READ_LOCK =
1 << 14
REFRESH_FAST =
1 << 15

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMysql

Creates a new connection to a MySQL server.

Parameters:

  • host (String)

    the MySQL server

  • user (String)

    the username to login

  • passwd (String)

    the user’s password

  • db (String)

    the name of the database to use

  • port (Integer)

    the port of the server to use

  • flag (Integer)

    connection flags



244
245
246
247
248
249
250
# File 'lib/ffi-mysql/mysql.rb', line 244

def initialize
    @mysql_free = [true]
    @mysql = C::mysql_init( nil )
    @connected = false
    @query_with_result = true
    @reconnect = false
end

Instance Attribute Details

#query_with_resultObject

if true (the default), query return the first result



239
240
241
# File 'lib/ffi-mysql/mysql.rb', line 239

def query_with_result
  @query_with_result
end

Class Method Details

.client_infoString Also known as: get_client_info

Returns string containing the client’s version.

Returns:

  • (String)

    string containing the client’s version



197
198
199
# File 'lib/ffi-mysql/mysql.rb', line 197

def self.client_info
    C::mysql_get_client_info
end

.client_versionInteger Also known as: get_client_version

Returns the version of the client.

Returns:

  • (Integer)

    the version of the client



186
187
188
# File 'lib/ffi-mysql/mysql.rb', line 186

def self.client_version
    C::mysql_get_client_version
end

.debug(arg) ⇒ Object

Does a DBUG_PUSH with the given string.



208
209
210
211
# File 'lib/ffi-mysql/mysql.rb', line 208

def self.debug(arg)
    C::mysql_debug(arg)
    self
end

.escape_string(str) ⇒ Object Also known as: quote

Escape special character in MySQL.

Note

In Ruby 1.8, this is not safe for multibyte charset such as ‘SJIS’. You should use place-holder in prepared-statement.



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ffi-mysql/mysql.rb', line 217

def self.escape_string(str)
    str.gsub(/[\0\n\r\\\'\"\x1a]/) do |s|
        case s
        when "\0" then "\\0"
        when "\n" then "\\n"
        when "\r" then "\\r"
        when "\x1a" then "\\Z"
        else "\\#{s}"
        end
    end
end

.finalizer(mysql, mysql_free) ⇒ Object

internal finalizer



253
254
255
256
257
258
259
# File 'lib/ffi-mysql/mysql.rb', line 253

def self.finalizer(mysql, mysql_free)
    Proc.new do |*args|
        unless mysql_free[0]
            C::mysql_close(mysql)
        end
    end
end

.initObject

Creates a new MySQL object.



171
172
173
174
175
# File 'lib/ffi-mysql/mysql.rb', line 171

def self.init
    mysql = allocate
    mysql.send :initialize
    mysql
end

.libpath(*args) ⇒ Object

Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join.



12
13
14
# File 'lib/ffi-mysql.rb', line 12

def self.libpath( *args )
    args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
end

.new(host = nil, user = nil, passwd = nil, db = nil, port = 0, sock = nil, flag = 0) ⇒ Object Also known as: real_connect, connect

Creates a new MySQL connector and opens a connection.



178
179
180
181
182
183
# File 'lib/ffi-mysql/mysql.rb', line 178

def self.new( host = nil, user = nil, passwd = nil, db = nil, port = 0, sock = nil, flag = 0)
    mysql = allocate
    mysql.send :initialize
    mysql.real_connect( host, user, passwd, db, port, sock, flag )
    mysql
end

.path(*args) ⇒ Object

Returns the lpath for the module. If any arguments are given, they will be joined to the end of the path using File.join.



20
21
22
# File 'lib/ffi-mysql.rb', line 20

def self.path( *args )
    args.empty? ? PATH : ::File.join(PATH, args.flatten)
end

.require_all_libs_relative_to(fname, dir = nil) ⇒ Object

Utility method used to require all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in. Optionally, a specific directory name can be passed in such that the filename does not have to be equivalent to the directory.



29
30
31
32
33
34
35
# File 'lib/ffi-mysql.rb', line 29

def self.require_all_libs_relative_to( fname, dir = nil )
    dir ||= ::File.basename(fname, '.*')
    search_me = ::File.expand_path(
                                   ::File.join(::File.dirname(fname), dir, '**', '*.rb'))

    Dir.glob(search_me).sort.each {|rb| require rb}
end

.versionObject

Returns the version string for the library.



14
15
16
# File 'lib/ffi-mysql/version.rb', line 14

def self.version
    VERSION
end

Instance Method Details

#affected_rowsInteger

Returns the number of affected rows by the last query.

Returns:

  • (Integer)

    the number of affected rows by the last query



684
685
686
# File 'lib/ffi-mysql/mysql.rb', line 684

def affected_rows
     C::mysql_affected_rows(@mysql)
end

#autocommit(mode) ⇒ Object

Sets autocommit mode on if mode is true, off if mode is false.



589
590
591
592
593
594
# File 'lib/ffi-mysql/mysql.rb', line 589

def autocommit(mode)
  if C::mysql_autocommit(@mysql, mode ? 1 : 0) != 0
    raise Error.new(error, errno)
  end
  self
end

#change_user(user, password, db) ⇒ Object

Change the user and change to a different database

Raises an error when there is a problem, otherwise returns self.

Parameters:

  • user (String)

    user to change to

  • password (String)

    password of user

  • db (String)

    name of database to change to



383
384
385
386
387
388
# File 'lib/ffi-mysql/mysql.rb', line 383

def change_user(user, password, db)
    if C::mysql_change_user(@mysql, user, password, db) != 0
        raise Error.new(error, errno)
    end
    self
end

#character_set_nameString

Returns default character set name for the current connection.

Returns:

  • (String)

    default character set name for the current connection



430
431
432
# File 'lib/ffi-mysql/mysql.rb', line 430

def character_set_name
    C::mysql_character_set_name(@mysql)
end

#client_infoString Also known as: get_client_info

Returns string containing the client’s version.

Returns:

  • (String)

    string containing the client’s version



202
203
204
# File 'lib/ffi-mysql/mysql.rb', line 202

def client_info
    Mysql.client_info
end

#client_versionInteger Also known as: get_client_version

Returns the version of the client.

Returns:

  • (Integer)

    the version of the client



191
192
193
# File 'lib/ffi-mysql/mysql.rb', line 191

def client_version
    Mysql.client_version
end

#closeObject

Closes the connection to the server.



301
302
303
304
305
306
307
# File 'lib/ffi-mysql/mysql.rb', line 301

def close
    C::mysql_close(@mysql)
    @mysql = nil
    @mysql_free[0] = true
    @connected = false
    self
end

#commitObject

Commit transaction



597
598
599
600
601
602
# File 'lib/ffi-mysql/mysql.rb', line 597

def commit
    if C::mysql_commit(@mysql) != 0
        raise Error.new(error, errno)
    end
    self
end

#dump_debug_infoObject

Write debug information to the log Raises an error when there is a problem, otherwise returns self.



334
335
336
337
338
339
# File 'lib/ffi-mysql/mysql.rb', line 334

def dump_debug_info
    if C::mysql_dump_debug_info(@mysql) != 0
        raise Error.new(error, errno)
    end
    self
end

#errnoInteger

Returns error code for the most recently failed API function.

Returns:

  • (Integer)

    error code for the most recently failed API function



318
319
320
# File 'lib/ffi-mysql/mysql.rb', line 318

def errno
    C::mysql_errno(@mysql)
end

#errorString

Returns error message for the most recently failing API function.

Returns:

  • (String)

    error message for the most recently failing API function



323
324
325
# File 'lib/ffi-mysql/mysql.rb', line 323

def error
    C::mysql_error(@mysql)
end

#field_countInteger

Returns the number of columns for the most recent query.

Returns:

  • (Integer)

    the number of columns for the most recent query



689
690
691
# File 'lib/ffi-mysql/mysql.rb', line 689

def field_count
    C::mysql_field_count(@mysql)
end

#host_infoString Also known as: get_host_info

Returns string containing the host name and connection type.

Returns:

  • (String)

    string containing the host name and connection type



467
468
469
# File 'lib/ffi-mysql/mysql.rb', line 467

def host_info
    C::mysql_get_host_info(@mysql)
end

#infoString

Return information about the most recently executed statement

Returns:

  • (String)

    information about statement



419
420
421
# File 'lib/ffi-mysql/mysql.rb', line 419

def info
    C::mysql_info(@mysql)
end

#insert_idObject

Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement.



531
532
533
# File 'lib/ffi-mysql/mysql.rb', line 531

def insert_id
    C::mysql_insert_id(@mysql)
end

#kill(pid) ⇒ Object

Asks the server to kill the specified thread.

Parameters:

  • pid (Integer)

    thread id to kill



410
411
412
413
414
415
# File 'lib/ffi-mysql/mysql.rb', line 410

def kill(pid)
    if C::mysql_kill(@mysql, pid) != 0
        raise Error.new(error, errno)
    end
    self
end

#list_dbs(wild = nil) ⇒ Object

Returns a list of all databases on the server matching a pattern

Parameters:

  • wild (String, nil) (defaults to: nil)

    The pattern the databases must match



537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/ffi-mysql/mysql.rb', line 537

def list_dbs(wild = nil)
    res = C::mysql_list_dbs(@mysql, wild)
    if res.nil? or res.null?
        raise Error.new(error, errno)
    end
    res = Result.new(@mysql, res)
    dbs = []
    res.each do |row|
        dbs << row[0]
    end
    dbs
end

#list_fields(table, wild = nil) ⇒ Result

Returns a result consisting the field names in the given table matching a pattern

Parameters:

  • table (String)

    The table whose fields should be returned

  • wild (String, nil) (defaults to: nil)

    The pattern the field names must match

Returns:

  • (Result)

    A MySQL result of the field names



554
555
556
557
558
559
560
# File 'lib/ffi-mysql/mysql.rb', line 554

def list_fields(table, wild = nil)
    res = C::mysql_list_fields(@mysql, table, wild)
    if res.nil? or res.null?
        raise Error.new(error, errno)
    end
    Result.new(@mysql, res)
end

#list_processesResult

Returns a result describing the current server threads

Returns:

  • (Result)

    A MySQL result of the current server threads



564
565
566
567
568
569
570
# File 'lib/ffi-mysql/mysql.rb', line 564

def list_processes
    res = C::mysql_list_processes(@mysql)
    if res.nil? or res.null?
        raise Error.new(error, errno)
    end
    Result.new(@mysql, res)
end

#list_tables(wild = nil) ⇒ Array<String>

Returns a result consisting the table names matching a pattern

Parameters:

  • wild (String, nil) (defaults to: nil)

    The pattern the table names must match

Returns:

  • (Array<String>)

    The table names



575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/ffi-mysql/mysql.rb', line 575

def list_tables(wild = nil)
    res = C::mysql_list_tables(@mysql, wild)
    if res.nil? or res.null?
        raise Error.new(error, errno)
    end
    res = Result.new(@mysql, res)
    tables = []
    res.each do |row|
        tables << row[0]
    end
    tables
end

#more_results?true, false Also known as: more_results

Returns true if there’s another result set.

Returns:

  • (true, false)

    true if there’s another result set



678
679
680
# File 'lib/ffi-mysql/mysql.rb', line 678

def more_results?
    C::mysql_more_results(@mysql)
end

#next_resulttrue, false

Advances to the next result set.

Returns:

  • (true, false)

    true if there’s another result set



666
667
668
669
670
671
672
673
674
675
# File 'lib/ffi-mysql/mysql.rb', line 666

def next_result
    result = C::mysql_next_result(@mysql)
    if result == 0
        true
    elsif result < 0
        false
    else
        raise Error.new(error, errno)
    end
end

#options(arg, value = nil) ⇒ Object

Sets extra connection options.

Parameters:

  • option (Integer)

    the option to set

  • the (String, Integer, true, false, nil)

    value of the option to set

Raises:



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/ffi-mysql/mysql.rb', line 487

def options( arg, value = nil )
    result = if value.nil?
                 C::mysql_options( @mysql, arg, nil )
             elsif value.kind_of? Integer
                 C::mysql_options( @mysql, arg, FFI::MemoryPointer.new(:uint).write_int(value) )
             elsif value.kind_of? String
                 C::mysql_options( @mysql, arg, FFI::MemoryPointer.from_string(value) )
             elsif value == true
                 C::mysql_options( @mysql, arg, FFI::MemoryPointer.new(:uint).write_int(1) )
             elsif value == false
                 C::mysql_options( @mysql, arg, FFI::MemoryPointer.new(:uint).write_int(0) )
             else
                 raise ArgumentError, "value must one of [String, Integer, nil, true, false]"
             end
    raise Error.new(error, errno) if result != 0
    self
end

#pingObject

Check whether the connection to the server is working. Raises an error when there is a problem, otherwise returns self.



310
311
312
313
314
315
# File 'lib/ffi-mysql/mysql.rb', line 310

def ping
    if C::mysql_ping(@mysql) != 0
        raise Error.new(error, errno)
    end
    self
end

#prepare(stmt) ⇒ Stmt

Creates and prepares a new statement.

Parameters:

  • stmt (String)

    the SQL statement

Returns:

  • (Stmt)

    the new prepared statement



701
702
703
# File 'lib/ffi-mysql/mysql.rb', line 701

def prepare( stmt )
    stmt_init.prepare(stmt)
end

#proto_infoInteger Also known as: get_proto_info

Returns containing the current connection’s procol version.

Returns:

  • (Integer)

    containing the current connection’s procol version



473
474
475
# File 'lib/ffi-mysql/mysql.rb', line 473

def proto_info
    C::mysql_get_proto_info(@mysql)
end

#query(sql) {|optional, Result| ... } ⇒ Result, self Also known as: real_query

Execute a query statement.

Parameters:

  • sql (String)

    the SQL statement

Yields:

  • (optional, Result)

    calls block once per result set

Returns:

  • (Result, self)

    the first result set if no block is given, self otherwise

Raises:



618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/ffi-mysql/mysql.rb', line 618

def query(sql)
    raise Error, "Not connected" unless @connected
    if C::mysql_real_query(@mysql, sql, sql.size) != 0
        raise Error.new(error, errno)
    end

    if block_given?
        begin
            result = store_result
            yield result
        ensure
            result.free
        end while next_result
        self
    elsif query_with_result
        if field_count == 0
            nil
        else
            store_result
        end
    else
        self
    end
end

#real_connect(host = nil, user = nil, passwd = nil, db = nil, port = 0, sock = nil, flag = 0) ⇒ Object Also known as: connect

Opens a new connection to a MySQL server.

Parameters:

  • host (String) (defaults to: nil)

    the MySQL server

  • user (String) (defaults to: nil)

    the username to login

  • passwd (String) (defaults to: nil)

    the user’s password

  • db (String) (defaults to: nil)

    the name of the database to use

  • port (Integer) (defaults to: 0)

    the port of the server to use

  • flag (Integer) (defaults to: 0)

    connection flags

Raises:



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/ffi-mysql/mysql.rb', line 285

def real_connect( host = nil, user = nil, passwd = nil, db = nil, port = 0, sock = nil, flag = 0)
    raise Error, "Already connected to a MySQL server" if @connected

    ObjectSpace.define_finalizer( self, Mysql.finalizer(@mysql, @mysql_free))

    ret = C::mysql_real_connect( @mysql, host, user, passwd, db, port, sock, flag )
    if ret.nil? or ret.null?
        raise Mysql::Error, error
    end

    @connected = true
    self
end

#real_escape_string(str) ⇒ String Also known as: escape_string, quote

Returns a legal SQL string to be used in SQL statements

Parameters:

  • str (String)

    The string to be escaped

Returns:

  • (String)

    The escaped string



446
447
448
449
450
# File 'lib/ffi-mysql/mysql.rb', line 446

def real_escape_string(str)
    to = FFI::MemoryPointer.new(str.size * 2 + 1, 1, false)
    len = C::mysql_real_escape_string(@mysql, to, str, str.size)
    to.read_string(len)
end

#reconnecttrue, false

Returns the current state of the reconnect flag

Returns:

  • (true, false)

    true if auto-reconnection is enabled.



525
526
527
# File 'lib/ffi-mysql/mysql.rb', line 525

def reconnect
    @reconnect
end

#reconnect=(flag) ⇒ Object

Sets the auto-reconnect flag of mysql

Parameters:

  • The (true, false)

    new reconnect state.



518
519
520
521
# File 'lib/ffi-mysql/mysql.rb', line 518

def reconnect= flag
    options OPT_RECONNECT, flag
    @reconnect = flag
end

#refresh(options) ⇒ Object

Flush tables or caches, or reset replication server information.

Parameters:

  • options (Integer)

    bit mask of REFRESH_… values



353
354
355
356
357
358
# File 'lib/ffi-mysql/mysql.rb', line 353

def refresh(options)
    if C::mysql_refresh(@mysql, options) != 0
        raise Error.new(error, errno)
    end
    self
end

#reloadObject

Asks the MySQL server to reload the grant table.



361
362
363
364
365
366
# File 'lib/ffi-mysql/mysql.rb', line 361

def reload
    if C::mysql_reload(@mysql) != 0
        raise Error.new(error, errno)
    end
    self
end

#rollbackObject

Rollback the current transaction



605
606
607
608
609
610
# File 'lib/ffi-mysql/mysql.rb', line 605

def rollback
    if C::mysql_rollback(@mysql) != 0
        raise Error.new(error, errno)
    end
    self
end

#select_db(db) ⇒ Object

Change the current database

Raises an error when there is a problem, otherwise returns self.

Parameters:

  • db (String)

    name of database to change to



395
396
397
398
399
400
# File 'lib/ffi-mysql/mysql.rb', line 395

def select_db(db)
    if C::mysql_select_db(@mysql, db) != 0
        raise Error.new(error, errno)
    end
    self
end

#server_infoString Also known as: get_server_info

Returns string containing the server’s version.

Returns:

  • (String)

    string containing the server’s version



461
462
463
# File 'lib/ffi-mysql/mysql.rb', line 461

def server_info
    C::mysql_get_server_info(@mysql)
end

#server_versionInteger Also known as: get_server_version

Returns the version of the server.

Returns:

  • (Integer)

    the version of the server



455
456
457
# File 'lib/ffi-mysql/mysql.rb', line 455

def server_version
    C::mysql_get_server_version(@mysql)
end

#set_character_set(csname) ⇒ Object

Set default character set for the current connection.

Parameters:

  • csname (String)

    default character set to use



436
437
438
439
440
441
# File 'lib/ffi-mysql/mysql.rb', line 436

def set_character_set(csname)
    if C::mysql_set_character_set(@mysql, csname) != 0
        raise Error.new(error, errno)
    end
    self
end

#set_server_option(option) ⇒ Object

Enables or disables an options for the connection.

Parameters:

  • option (Integer)

    server option



508
509
510
511
512
513
# File 'lib/ffi-mysql/mysql.rb', line 508

def set_server_option( option )
    if C::mysql_set_server_option( @mysql, option ) != 0
        raise Error.new(error, errno)
    end
    self
end

#shutdownObject

Asks the MySQL server to shut down.



369
370
371
372
373
374
# File 'lib/ffi-mysql/mysql.rb', line 369

def shutdown
    if C::mysql_shutdown(@mysql) != 0
        raise Error.new(error, errno)
    end
    self
end

#sqlstateString

Returns the SQLSTATE error code for the most recent statement.

Returns:

  • (String)

    the SQLSTATE error code for the most recent statement



479
480
481
# File 'lib/ffi-mysql/mysql.rb', line 479

def sqlstate
    C::mysql_sqlstate(@mysql)
end

#ssl_set(key, cert, ca, capath, cipher) ⇒ Object

Set SSL settings (must be called before real_connect)

Any unused SSL parameters may be given as NULL.

Parameters:

  • key (String)

    path to the key file

  • cert (String)

    path to the certificate file

  • ca (String)

    path to the certificate authority file

  • capath (String)

    path name to a directory with SSL CA certificates in pem format

  • cipher (String)

    list of permissible ciphers to use



270
271
272
273
274
275
# File 'lib/ffi-mysql/mysql.rb', line 270

def ssl_set(key, cert, ca, capath, cipher)
  if C::mysql_ssl_set(@mysql, key, cert, ca, capath, cipher) != 0
    raise Error.new(error, errno)
  end
  self
end

#statString

Return information about the server status

Returns:

  • (String)

    string describing the server status



425
426
427
# File 'lib/ffi-mysql/mysql.rb', line 425

def stat
    C::mysql_stat(@mysql)
end

#stmt_initStmt

Returns a new statement.

Returns:

  • (Stmt)

    a new statement



694
695
696
# File 'lib/ffi-mysql/mysql.rb', line 694

def stmt_init
    Stmt.new( @mysql )
end

#store_resultResult

Stores the current result in a result set.

Returns:



646
647
648
649
650
651
652
# File 'lib/ffi-mysql/mysql.rb', line 646

def store_result
    res = C::mysql_store_result(@mysql)
    if res.nil? or res.null?
        raise Error.new(error, errno)
    end
    Result.new(@mysql, res)
end

#thread_idInteger

Returns the thread ID of the current connection

Returns:

  • (Integer)

    thread id of the current connection



404
405
406
# File 'lib/ffi-mysql/mysql.rb', line 404

def thread_id
    C::mysql_thread_id(@mysql)
end

#use_resultResult

Initialize result set retrieval but do not read data.

Returns:



656
657
658
659
660
661
662
# File 'lib/ffi-mysql/mysql.rb', line 656

def use_result
    res = C::mysql_use_result(@mysql)
    if res.nil? or res.null?
        raise Error.new(error, errno)
    end
    Result.new(@mysql, res)
end

#warning_countInteger

Returns the number of errors, warnings and notes generated during the previous SQL statement.

Returns:

  • (Integer)

    the number of errors, warnings and notes generated during the previous SQL statement



328
329
330
# File 'lib/ffi-mysql/mysql.rb', line 328

def warning_count
    C::mysql_warning_count(@mysql)
end