Class: DB::MariaDB::Connection

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

Overview

This implements the interface between the underyling native interface interface and “standardised” connection interface.

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

Returns a new instance of Connection.



15
16
17
18
19
# File 'lib/db/mariadb/connection.rb', line 15

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

Instance Method Details

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/db/mariadb/connection.rb', line 58

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/db/mariadb/connection.rb', line 37

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



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

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

#closeObject



21
22
23
24
25
# File 'lib/db/mariadb/connection.rb', line 21

def close
  @native.close
  
  super
end

#featuresObject

Database feature detection for migration and query building.



113
114
115
# File 'lib/db/mariadb/connection.rb', line 113

def features
  FEATURES
end

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



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

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



101
102
103
# File 'lib/db/mariadb/connection.rb', line 101

def next_result
  @native.next_result
end

#send_query(statement) ⇒ Object



95
96
97
98
99
# File 'lib/db/mariadb/connection.rb', line 95

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

#statusObject



91
92
93
# File 'lib/db/mariadb/connection.rb', line 91

def status
  @native.status
end

#typesObject



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

def types
  @native.types
end