Class: Tina4::Drivers::MysqlDriver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SchemaSplit

#split_schema

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/tina4/drivers/mysql_driver.rb', line 9

def connection
  @connection
end

Instance Method Details

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



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

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

#begin_transactionObject



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

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

#closeObject



30
31
32
# File 'lib/tina4/drivers/mysql_driver.rb', line 30

def close
  @connection&.close
end

#columns(table_name) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tina4/drivers/mysql_driver.rb', line 99

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



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

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

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



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

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



44
45
46
47
48
49
50
51
# File 'lib/tina4/drivers/mysql_driver.rb', line 44

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



34
35
36
37
38
39
40
41
42
# File 'lib/tina4/drivers/mysql_driver.rb', line 34

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



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

def last_insert_id
  @connection.last_id
end

#placeholderObject



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

def placeholder
  "?"
end

#placeholders(count) ⇒ Object



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

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

#rollbackObject



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

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

#table_exists?(name) ⇒ Boolean

v3.13.14 (#48): MySQL’s “schema” is the database. A qualified name (“otherdb.table”) checks that catalog; a bare name defaults to the connection’s current database via DATABASE().

Returns:

  • (Boolean)


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

def table_exists?(name)
  schema, tbl = split_schema(name)
  rows = execute_query(
    "SELECT 1 FROM information_schema.tables " \
    "WHERE table_schema = COALESCE(?, DATABASE()) AND table_name = ?",
    [schema, tbl]
  )
  !rows.empty?
end

#tablesObject



94
95
96
97
# File 'lib/tina4/drivers/mysql_driver.rb', line 94

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