class DBI::DBD::Mysql::Database

Models the DBI::BaseDatabase API to create DBI::DatabaseHandle objects.

Constants

MYSQL_to_XOPEN

Hash to translate MySQL type names to DBI SQL type constants

Only used in mysql_type_info.

TYPE_MAP

This maps type names to DBI Types.

Public Class Methods

new(handle, attr) click to toggle source

Constructor. Attributes supported:

  • AutoCommit: Commit after each executed statement. This will raise a DBI::NotSupportedError if the backend does not support transactions.

Calls superclass method
# File lib/dbd/mysql/database.rb, line 130
def initialize(handle, attr)
    super
    # check server version to determine transaction capability
    ver_str = @handle.get_server_info
    major, minor, teeny = ver_str.split(".")
    teeny.sub!(/\D*$/, "")  # strip any non-numeric suffix if present
    server_version = major.to_i*10000 + minor.to_i*100 + teeny.to_i
    # It's not until 3.23.17 that SET AUTOCOMMIT,
    # BEGIN, COMMIT, and ROLLBACK all are available
    @have_transactions = (server_version >= 32317)
    # assume that the connection begins in AutoCommit mode
    @attr['AutoCommit'] = true
    @mutex = Mutex.new
end

Public Instance Methods

[]=(attr, value) click to toggle source

See DBI::DBD::MySQL::Database.new for supported attributes and usage.

# File lib/dbd/mysql/database.rb, line 293
def []=(attr, value)
    case attr
    when 'AutoCommit'
        if @have_transactions
            self.do("SET AUTOCOMMIT=" + (value ? "1" : "0"))
        else
            raise NotSupportedError
        end
    else
        raise NotSupportedError
    end

    @attr[attr] = value
end
__client_info() click to toggle source
# File lib/dbd/mysql/database.rb, line 393
def __client_info
    @handle.client_info
end
__client_version() click to toggle source
# File lib/dbd/mysql/database.rb, line 397
def __client_version
    @handle.client_version
end
__createdb(db) click to toggle source
# File lib/dbd/mysql/database.rb, line 353
def __createdb(db)
    @handle.create_db(db)
end
__dropdb(db) click to toggle source
# File lib/dbd/mysql/database.rb, line 357
def __dropdb(db)
    @handle.drop_db(db)
end
__host_info() click to toggle source
# File lib/dbd/mysql/database.rb, line 381
def __host_info
    @handle.host_info
end
__info() click to toggle source
# File lib/dbd/mysql/database.rb, line 377
def __info
    @handle.info
end
__insert_id() click to toggle source
# File lib/dbd/mysql/database.rb, line 369
def __insert_id
    @handle.insert_id
end
__proto_info() click to toggle source
# File lib/dbd/mysql/database.rb, line 385
def __proto_info
    @handle.proto_info
end
__reload() click to toggle source
# File lib/dbd/mysql/database.rb, line 365
def __reload
    @handle.reload
end
__server_info() click to toggle source
# File lib/dbd/mysql/database.rb, line 389
def __server_info
    @handle.server_info
end
__shutdown() click to toggle source
# File lib/dbd/mysql/database.rb, line 361
def __shutdown
    @handle.shutdown
end
__stat() click to toggle source
# File lib/dbd/mysql/database.rb, line 401
def __stat
    @handle.stat
end
__thread_id() click to toggle source
# File lib/dbd/mysql/database.rb, line 373
def __thread_id
    @handle.thread_id
end
columns(table) click to toggle source

See DBI::BaseDatabase#columns.

Extra attributes:

  • sql_type: XOPEN integer constant relating to type.

  • nullable: true if the column allows NULL as a value.

  • indexed: true if the column belongs to an index.

  • primary: true if the column is a part of a primary key.

  • unique: true if the values in this column are unique.

  • default: the default value if this column is not explicitly set.

# File lib/dbd/mysql/database.rb, line 187
def columns(table)
    dbh = DBI::DatabaseHandle.new(self)
    uniques = []
    dbh.execute("SHOW INDEX FROM #{table}") do |sth|
        sth.each do |row|
            uniques << row[4] if row[1] == 0
        end
    end

    ret = nil
    dbh.execute("SHOW FIELDS FROM #{table}") do |sth|
        ret = sth.collect do |row|
            name, type, nullable, key, default, extra = row
            #type = row[1]
            #size = type[type.index('(')+1..type.index(')')-1]
            #size = 0
            #type = type[0..type.index('(')-1]

            sqltype, type, size, decimal = mysql_type_info(row[1])
            col = Hash.new
            col['name']           = name
            col['sql_type']       = sqltype
            col['type_name']      = type
            col['nullable']       = nullable == "YES"
            col['indexed']        = key != ""
            col['primary']        = key == "PRI"
            col['unique']         = uniques.index(name) != nil
            col['precision']      = size
            col['scale']          = decimal
            col['default']        = row[4]

            case col['type_name']
            when 'timestamp'
                col['dbi_type'] = DBI::Type::Timestamp
            end

            col
        end # collect
    end # execute

    ret
end
commit() click to toggle source

MySQL has several backends, some of which may not support commits. If the backend this database uses doesn't, calling this method will raise a DBI::NotSupportedError.

# File lib/dbd/mysql/database.rb, line 250
def commit
    if @have_transactions
        self.do("COMMIT")
    else
        raise NotSupportedError
    end
rescue MyError => err
    error(err)
end
database_name() click to toggle source
# File lib/dbd/mysql/database.rb, line 152
def database_name
    sth = Statement.new(self, @handle, "select DATABASE()", @mutex)
    sth.execute
    res = sth.fetch
    sth.finish
    return res[0]
end
disconnect() click to toggle source
# File lib/dbd/mysql/database.rb, line 145
def disconnect
    self.rollback unless @attr['AutoCommit']
    @handle.close
rescue MyError => err
    error(err)
end
do(stmt, *bindvars) click to toggle source
# File lib/dbd/mysql/database.rb, line 230
def do(stmt, *bindvars)
    st = Statement.new(self, @handle, stmt, @mutex)
    st.bind_params(*bindvars)
    res = st.execute
    st.finish
    return res
rescue MyError => err
    error(err)
end
ping() click to toggle source
# File lib/dbd/mysql/database.rb, line 160
def ping
    begin
        @handle.ping
        return true
    rescue MyError
        return false
    end
end
prepare(statement) click to toggle source
# File lib/dbd/mysql/database.rb, line 241
def prepare(statement)
    Statement.new(self, @handle, statement, @mutex)
end
rollback() click to toggle source

See commit for information regarding transactionless database backends.

# File lib/dbd/mysql/database.rb, line 264
def rollback
    if @have_transactions
        self.do("ROLLBACK")
    else
        raise NotSupportedError
    end
rescue MyError => err
    error(err)
end
tables() click to toggle source
# File lib/dbd/mysql/database.rb, line 169
def tables
    @handle.list_tables
rescue MyError => err
    error(err)
end

Private Instance Methods

mysql_type_info(typedef) click to toggle source

Given a type name, weans some basic information from that and returns it in a format similar to columns.

Return is an array of sqltype, type, size, and decimal. sqltype is the XOPEN type, and type is the string with the parameters removed.

size and decimal refer to precision and scale in most cases, but not always for all types. Please consult the documentation for your MySQL version.

# File lib/dbd/mysql/database.rb, line 323
def mysql_type_info(typedef)
    sqltype, type, size, decimal = nil, nil, nil, nil

    pos = typedef.index('(')
    if not pos.nil?
        type = typedef[0..pos-1]
        size = typedef[pos+1..-2]
        pos = size.index(',')
        if not pos.nil?
            size, decimal = size.split(',', 2)
            decimal = decimal.to_i
        end
        size = size.to_i
    else
        type = typedef
    end

    type_info = MYSQL_to_XOPEN[type.upcase] || MYSQL_to_XOPEN[nil]
    sqltype = type_info[0]
    if size.nil? then size = type_info[1] end
    if decimal.nil? then decimal = type_info[2] end
    return sqltype, type, size, decimal
end