Class: Tina4::Drivers::MysqlDriver
- Inherits:
-
Object
- Object
- Tina4::Drivers::MysqlDriver
- Includes:
- SchemaSplit
- Defined in:
- lib/tina4/drivers/mysql_driver.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #apply_limit(sql, limit, offset = 0) ⇒ Object
- #begin_transaction ⇒ Object
- #close ⇒ Object
- #columns(table_name) ⇒ Object
- #commit ⇒ Object
- #connect(connection_string, username: nil, password: nil) ⇒ Object
- #execute(sql, params = []) ⇒ Object
- #execute_query(sql, params = []) ⇒ Object
- #last_insert_id ⇒ Object
- #placeholder ⇒ Object
- #placeholders(count) ⇒ Object
- #rollback ⇒ Object
-
#table_exists?(name) ⇒ Boolean
v3.13.14 (#48): MySQL’s “schema” is the database.
- #tables ⇒ Object
Methods included from SchemaSplit
Instance Attribute Details
#connection ⇒ Object (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_transaction ⇒ Object
69 70 71 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 69 def begin_transaction @connection.query("START TRANSACTION") end |
#close ⇒ Object
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 |
#commit ⇒ Object
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_id ⇒ Object
53 54 55 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 53 def last_insert_id @connection.last_id end |
#placeholder ⇒ Object
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 |
#rollback ⇒ Object
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().
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 |
#tables ⇒ Object
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 |