Class: Taupe::Database::MysqlDriver
- Inherits:
-
Object
- Object
- Taupe::Database::MysqlDriver
- Defined in:
- lib/taupe/database/mysql.rb
Overview
Mysql database driver
Instance Attribute Summary collapse
-
#connection ⇒ Object
Accessors.
Instance Method Summary collapse
-
#escape(str) ⇒ String
Escape a string.
-
#exec(query) ⇒ Object
Execute a single query.
-
#fetch(query) ⇒ Array, Object
Fetch objects from database.
-
#guess_schema(table) ⇒ Hash
Guess schema of a table.
-
#initialize(dsn) ⇒ MysqlDriver
constructor
Constructor.
-
#last_id ⇒ Integer
Get last inserted id.
Constructor Details
#initialize(dsn) ⇒ MysqlDriver
Constructor
15 16 17 18 19 |
# File 'lib/taupe/database/mysql.rb', line 15 def initialize(dsn) dsn[:host] = '127.0.0.1' if dsn[:host].to_s == 'localhost' @connection = Mysql2::Client.new dsn @connection..merge! symbolize_keys: true end |
Instance Attribute Details
#connection ⇒ Object
Accessors
11 12 13 |
# File 'lib/taupe/database/mysql.rb', line 11 def connection @connection end |
Instance Method Details
#escape(str) ⇒ String
Escape a string
65 66 67 |
# File 'lib/taupe/database/mysql.rb', line 65 def escape(str) @connection.escape str end |
#exec(query) ⇒ Object
Execute a single query
24 25 26 |
# File 'lib/taupe/database/mysql.rb', line 24 def exec(query) @connection.query query end |
#fetch(query) ⇒ Array, Object
Fetch objects from database
31 32 33 |
# File 'lib/taupe/database/mysql.rb', line 31 def fetch(query) exec(query).to_a end |
#guess_schema(table) ⇒ Hash
Guess schema of a table
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/taupe/database/mysql.rb', line 44 def guess_schema(table) results = {} query = format('SHOW COLUMNS FROM %s', table) fetch(query).each do |values| type = Taupe::Validate.standardize_sql_type values[:Type] results[values[:Field].to_sym] = { type: type, null: values[:Null] != 'NO', primary_key: values[:Key] == 'PRI' } end results end |
#last_id ⇒ Integer
Get last inserted id
37 38 39 |
# File 'lib/taupe/database/mysql.rb', line 37 def last_id @connection.last_id.to_i end |