Class: Tina4::Drivers::MysqlDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/drivers/mysql_driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/tina4/drivers/mysql_driver.rb', line 6

def connection
  @connection
end

Instance Method Details

#apply_limit(sql, limit, offset = 0) ⇒ Object



62
63
64
# File 'lib/tina4/drivers/mysql_driver.rb', line 62

def apply_limit(sql, limit, offset = 0)
  "#{sql} LIMIT #{limit} OFFSET #{offset}"
end

#begin_transactionObject



66
67
68
# File 'lib/tina4/drivers/mysql_driver.rb', line 66

def begin_transaction
  @connection.query("START TRANSACTION")
end

#closeObject



27
28
29
# File 'lib/tina4/drivers/mysql_driver.rb', line 27

def close
  @connection&.close
end

#columns(table_name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tina4/drivers/mysql_driver.rb', line 83

def columns(table_name)
  rows = execute_query("DESCRIBE #{table_name}")
  rows.map do |r|
    {
      name: r[:Field],
      type: r[:Type],
      nullable: r[:Null] == "YES",
      default: r[:Default],
      primary_key: r[:Key] == "PRI"
    }
  end
end

#commitObject



70
71
72
# File 'lib/tina4/drivers/mysql_driver.rb', line 70

def commit
  @connection.query("COMMIT")
end

#connect(connection_string, username: nil, password: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tina4/drivers/mysql_driver.rb', line 8

def connect(connection_string, username: nil, password: nil)
  begin
    require "mysql2"
  rescue LoadError
    raise LoadError,
          "The 'mysql2' gem is required for MySQL connections. Install one of:\n" \
          "    bundle add mysql2     # if your project uses Bundler\n" \
          "    gem install mysql2    # bare driver"
  end
  uri = URI.parse(connection_string)
  @connection = Mysql2::Client.new(
    host: uri.host || "localhost",
    port: uri.port || 3306,
    username: username || uri.user,
    password: password || uri.password,
    database: uri.path&.sub("/", "")
  )
end

#execute(sql, params = []) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/tina4/drivers/mysql_driver.rb', line 41

def execute(sql, params = [])
  if params.empty?
    @connection.query(sql)
  else
    stmt = @connection.prepare(sql)
    stmt.execute(*params)
  end
end

#execute_query(sql, params = []) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/tina4/drivers/mysql_driver.rb', line 31

def execute_query(sql, params = [])
  if params.empty?
    results = @connection.query(sql, symbolize_keys: true)
  else
    stmt = @connection.prepare(sql)
    results = stmt.execute(*params, symbolize_keys: true)
  end
  results.to_a
end

#last_insert_idObject



50
51
52
# File 'lib/tina4/drivers/mysql_driver.rb', line 50

def last_insert_id
  @connection.last_id
end

#placeholderObject



54
55
56
# File 'lib/tina4/drivers/mysql_driver.rb', line 54

def placeholder
  "?"
end

#placeholders(count) ⇒ Object



58
59
60
# File 'lib/tina4/drivers/mysql_driver.rb', line 58

def placeholders(count)
  (["?"] * count).join(", ")
end

#rollbackObject



74
75
76
# File 'lib/tina4/drivers/mysql_driver.rb', line 74

def rollback
  @connection.query("ROLLBACK")
end

#tablesObject



78
79
80
81
# File 'lib/tina4/drivers/mysql_driver.rb', line 78

def tables
  rows = execute_query("SHOW TABLES")
  rows.map { |r| r.values.first }
end