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



86
87
88
# File 'lib/tina4/drivers/mysql_driver.rb', line 86

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

#begin_transactionObject



90
91
92
# File 'lib/tina4/drivers/mysql_driver.rb', line 90

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

#closeObject



40
41
42
# File 'lib/tina4/drivers/mysql_driver.rb', line 40

def close
  @connection&.close
end

#columns(table_name) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/tina4/drivers/mysql_driver.rb', line 120

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



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

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
29
30
31
32
33
34
35
36
37
38
# 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)
  # libmysqlclient connects over a UNIX socket whenever host is "localhost"
  # (its historical convention) and silently ignores the port. A URL that
  # names a port clearly intends TCP, so rewrite "localhost" to "127.0.0.1"
  # in that case to force the TCP path — without it a Docker/TCP-only MySQL
  # fails with "Can't connect ... through socket '/tmp/mysql.sock'". A
  # port-less "localhost" keeps the socket path so socket deployments still
  # work. Parity with PHP's MySQLAdapter::rewriteHostForTcp (mysqli has the
  # identical socket trap).
  host = uri.host || "127.0.0.1"
  host = "127.0.0.1" if host == "localhost" && uri.port
  @connection = Mysql2::Client.new(
    host: host,
    port: uri.port || 3306,
    username: username || uri.user,
    password: password || uri.password,
    database: uri.path&.sub("/", "")
  )
end

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tina4/drivers/mysql_driver.rb', line 54

def execute(sql, params = [])
  result =
    if params.empty?
      @connection.query(sql)
    else
      stmt = @connection.prepare(sql)
      stmt.execute(*params)
    end
  # Capture the generated id AT WRITE TIME — mirrors the Python master
  # (mysql.py execute(): `last_id = cursor.lastrowid` is read straight after
  # the statement, never re-read later). mysql2's @connection.last_id reflects
  # the LAST statement on this connection, so a follow-up autocommit COMMIT
  # (a separate query) clobbers it to 0 — that is exactly why db.get_last_id
  # returned 0 after an insert (issue #262). Snapshot it for every INSERT so
  # last_insert_id keeps the id of the last insert regardless of any
  # subsequent COMMIT / SELECT on the connection.
  @last_insert_id = @connection.last_id if sql.to_s.lstrip[0, 6].casecmp?("INSERT")
  result
end

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



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

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



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

def last_insert_id
  @last_insert_id
end

#placeholderObject



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

def placeholder
  "?"
end

#placeholders(count) ⇒ Object



82
83
84
# File 'lib/tina4/drivers/mysql_driver.rb', line 82

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

#rollbackObject



98
99
100
# File 'lib/tina4/drivers/mysql_driver.rb', line 98

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)


105
106
107
108
109
110
111
112
113
# File 'lib/tina4/drivers/mysql_driver.rb', line 105

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



115
116
117
118
# File 'lib/tina4/drivers/mysql_driver.rb', line 115

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