Class: Tina4::Drivers::MysqlDriver
- Inherits:
-
Object
- Object
- Tina4::Drivers::MysqlDriver
- 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
- #tables ⇒ Object
Instance Attribute Details
#connection ⇒ Object (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
55 56 57 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 55 def apply_limit(sql, limit, offset = 0) "#{sql} LIMIT #{limit} OFFSET #{offset}" end |
#begin_transaction ⇒ Object
59 60 61 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 59 def begin_transaction @connection.query("START TRANSACTION") end |
#close ⇒ Object
20 21 22 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 20 def close @connection&.close end |
#columns(table_name) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 76 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
63 64 65 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 63 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 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 8 def connect(connection_string, username: nil, password: nil) require "mysql2" 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
34 35 36 37 38 39 40 41 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 34 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
24 25 26 27 28 29 30 31 32 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 24 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
43 44 45 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 43 def last_insert_id @connection.last_id end |
#placeholder ⇒ Object
47 48 49 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 47 def placeholder "?" end |
#placeholders(count) ⇒ Object
51 52 53 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 51 def placeholders(count) (["?"] * count).join(", ") end |
#rollback ⇒ Object
67 68 69 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 67 def rollback @connection.query("ROLLBACK") end |
#tables ⇒ Object
71 72 73 74 |
# File 'lib/tina4/drivers/mysql_driver.rb', line 71 def tables rows = execute_query("SHOW TABLES") rows.map { |r| r.values.first } end |