Class: DB::MariaDB::Connection

Inherits:
Async::Pool::Resource
  • Object
show all
Defined in:
lib/db/mariadb/connection.rb

Overview

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.

Constant Summary collapse

FEATURES =
DB::Features.new(
  modify_column: true,
  conditional_operations: true,
  batch_alter_table: true,
  auto_increment: true
)

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Connection

Initialize a new database connection.



18
19
20
21
22
# File 'lib/db/mariadb/connection.rb', line 18

def initialize(**options)
  @native = Native::Connection.connect(**options)
  
  super()
end

Instance Method Details

#append_identifier(value, buffer = String.new) ⇒ Object

Append an escaped identifier to the buffer.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/db/mariadb/connection.rb', line 76

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

#append_literal(value, buffer = String.new) ⇒ Object

Append a literal value to the buffer with appropriate formatting.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/db/mariadb/connection.rb', line 51

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

#append_string(value, buffer = String.new) ⇒ Object

Append an escaped string value to the buffer.



41
42
43
44
45
# File 'lib/db/mariadb/connection.rb', line 41

def append_string(value, buffer = String.new)
  buffer << "'" << @native.escape(value) << "'"
  
  return buffer
end

#closeObject

Close the database connection and release resources.



25
26
27
28
29
# File 'lib/db/mariadb/connection.rb', line 25

def close
  @native.close
  
  super
end

#featuresObject

Database feature detection for migration and query building.



143
144
145
# File 'lib/db/mariadb/connection.rb', line 143

def features
  FEATURES
end

#key_column(name = "id", primary: true, null: false) ⇒ Object

Generate a key column definition for table creation.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/db/mariadb/connection.rb', line 98

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

#next_resultObject

Get the next result set from a multi-result query.



130
131
132
# File 'lib/db/mariadb/connection.rb', line 130

def next_result
  @native.next_result
end

#send_query(statement) ⇒ Object

Send a query to the database server.



122
123
124
125
126
# File 'lib/db/mariadb/connection.rb', line 122

def send_query(statement)
  @native.discard_results
  
  @native.send_query(statement)
end

#statusObject

Get the current connection status.



116
117
118
# File 'lib/db/mariadb/connection.rb', line 116

def status
  @native.status
end

#typesObject

Get the type mapping for database types.



33
34
35
# File 'lib/db/mariadb/connection.rb', line 33

def types
  @native.types
end