class Connection
A high-level database connection that implements the standardized connection interface. This class provides a bridge between the underlying native MariaDB interface and the DB gem's unified connection API.
Definitions
def initialize(**options)
Initialize a new database connection.
Signature
-
parameter
optionsHash Connection options passed to the native connection.
Implementation
def initialize(**options)
@native = Native::Connection.connect(**options)
super()
end
def close
Close the database connection and release resources.
Implementation
def close
@native.close
super
end
def types
Get the type mapping for database types.
Signature
-
returns
Hash The type mapping configuration.
Implementation
def types
@native.types
end
def append_string(value, buffer = String.new)
Append an escaped string value to the buffer.
Signature
-
parameter
valueString The string value to escape and append.
-
parameter
bufferString The buffer to append to.
-
returns
String The buffer with the escaped string appended.
Implementation
def append_string(value, buffer = String.new)
buffer << "'" << @native.escape(value) << "'"
return buffer
end
def append_literal(value, buffer = String.new)
Append a literal value to the buffer with appropriate formatting.
Signature
-
parameter
valueObject The value to append (supports Time, Date, Numeric, Boolean, nil, and strings).
-
parameter
bufferString The buffer to append to.
-
returns
String The buffer with the formatted value appended.
Implementation
def append_literal(value, buffer = String.new)
case value
when Time, DateTime
append_string(value.utc.strftime("%Y-%m-%d %H:%M:%S"), buffer)
when Date
append_string(value.strftime("%Y-%m-%d"), buffer)
when Numeric
buffer << value.to_s
when TrueClass
buffer << "TRUE"
when FalseClass
buffer << "FALSE"
when nil
buffer << "NULL"
else
append_string(value, buffer)
end
return buffer
end
def append_identifier(value, buffer = String.new)
Append an escaped identifier to the buffer.
Signature
-
parameter
valueString | Array(String) The identifier or array of identifiers to escape.
-
parameter
bufferString The buffer to append to.
-
returns
String The buffer with the escaped identifier appended.
Implementation
def append_identifier(value, buffer = String.new)
case value
when Array
first = true
value.each do |part|
buffer << "." unless first
first = false
buffer << escape_identifier(part)
end
else
buffer << escape_identifier(value)
end
return buffer
end
def key_column(name = "id", primary: true, null: false)
Generate a key column definition for table creation.
Signature
-
parameter
nameString The column name.
-
parameter
primaryBoolean Whether this is a primary key column.
-
parameter
nullBoolean Whether this column allows null values.
-
returns
String The column definition string.
Implementation
def key_column(name = "id", primary: true, null: false)
buffer = String.new
append_identifier(name, buffer)
buffer << " BIGINT"
if primary
buffer << " AUTO_INCREMENT PRIMARY KEY"
elsif !null
buffer << " NOT NULL"
end
return buffer
end
def status
Get the current connection status.
Signature
-
returns
String The status string from the server.
Implementation
def status
@native.status
end
def send_query(statement)
Send a query to the database server.
Signature
-
parameter
statementString The SQL statement to execute.
Implementation
def send_query(statement)
@native.discard_results
@native.send_query(statement)
end
def next_result
Get the next result set from a multi-result query.
Signature
-
returns
Native::Result | Nil The next result set, or
nilif no more results.
Implementation
def next_result
@native.next_result
end
def features
Database feature detection for migration and query building.
Signature
-
returns
DB::Features The supported database features.
Implementation
def features
FEATURES
end