Class: Tina4::Database
- Inherits:
-
Object
- Object
- Tina4::Database
- Defined in:
- lib/tina4/database.rb
Constant Summary collapse
- DRIVERS =
{ "sqlite" => "Tina4::Drivers::SqliteDriver", "sqlite3" => "Tina4::Drivers::SqliteDriver", "postgres" => "Tina4::Drivers::PostgresDriver", "postgresql" => "Tina4::Drivers::PostgresDriver", "mysql" => "Tina4::Drivers::MysqlDriver", "mysql2" => "Tina4::Drivers::MysqlDriver", "mssql" => "Tina4::Drivers::MssqlDriver", "sqlserver" => "Tina4::Drivers::MssqlDriver", "firebird" => "Tina4::Drivers::FirebirdDriver" }.freeze
Instance Attribute Summary collapse
-
#connected ⇒ Object
readonly
Returns the value of attribute connected.
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#driver_name ⇒ Object
readonly
Returns the value of attribute driver_name.
Instance Method Summary collapse
- #close ⇒ Object
- #columns(table_name) ⇒ Object
- #connect ⇒ Object
- #delete(table, filter = {}) ⇒ Object
- #execute(sql, params = []) ⇒ Object
- #fetch(sql, params = [], limit: nil, skip: nil) ⇒ Object
- #fetch_one(sql, params = []) ⇒ Object
-
#initialize(connection_string, driver_name: nil) ⇒ Database
constructor
A new instance of Database.
- #insert(table, data) ⇒ Object
- #table_exists?(table_name) ⇒ Boolean
- #tables ⇒ Object
- #transaction ⇒ Object
- #update(table, data, filter = {}) ⇒ Object
Constructor Details
#initialize(connection_string, driver_name: nil) ⇒ Database
Returns a new instance of Database.
20 21 22 23 24 25 26 |
# File 'lib/tina4/database.rb', line 20 def initialize(connection_string, driver_name: nil) @connection_string = connection_string @driver_name = driver_name || detect_driver(connection_string) @driver = create_driver @connected = false connect end |
Instance Attribute Details
#connected ⇒ Object (readonly)
Returns the value of attribute connected.
6 7 8 |
# File 'lib/tina4/database.rb', line 6 def connected @connected end |
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
6 7 8 |
# File 'lib/tina4/database.rb', line 6 def driver @driver end |
#driver_name ⇒ Object (readonly)
Returns the value of attribute driver_name.
6 7 8 |
# File 'lib/tina4/database.rb', line 6 def driver_name @driver_name end |
Instance Method Details
#close ⇒ Object
37 38 39 40 |
# File 'lib/tina4/database.rb', line 37 def close @driver.close if @connected @connected = false end |
#columns(table_name) ⇒ Object
99 100 101 |
# File 'lib/tina4/database.rb', line 99 def columns(table_name) @driver.columns(table_name) end |
#connect ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/tina4/database.rb', line 28 def connect @driver.connect(@connection_string) @connected = true Tina4::Debug.info("Database connected: #{@driver_name}") rescue => e Tina4::Debug.error("Database connection failed: #{e.}") @connected = false end |
#delete(table, filter = {}) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/tina4/database.rb', line 74 def delete(table, filter = {}) where_parts = filter.keys.map { |k| "#{k} = #{@driver.placeholder}" } sql = "DELETE FROM #{table}" sql += " WHERE #{where_parts.join(' AND ')}" unless filter.empty? @driver.execute(sql, filter.values) { success: true } end |
#execute(sql, params = []) ⇒ Object
82 83 84 |
# File 'lib/tina4/database.rb', line 82 def execute(sql, params = []) @driver.execute(sql, params) end |
#fetch(sql, params = [], limit: nil, skip: nil) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/tina4/database.rb', line 42 def fetch(sql, params = [], limit: nil, skip: nil) effective_sql = sql if limit effective_sql = @driver.apply_limit(effective_sql, limit, skip || 0) end rows = @driver.execute_query(effective_sql, params) Tina4::DatabaseResult.new(rows, sql: effective_sql) end |
#fetch_one(sql, params = []) ⇒ Object
51 52 53 54 |
# File 'lib/tina4/database.rb', line 51 def fetch_one(sql, params = []) result = fetch(sql, params, limit: 1) result.first end |
#insert(table, data) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/tina4/database.rb', line 56 def insert(table, data) columns = data.keys.map(&:to_s) placeholders = @driver.placeholders(columns.length) sql = "INSERT INTO #{table} (#{columns.join(', ')}) VALUES (#{placeholders})" @driver.execute(sql, data.values) { success: true, last_id: @driver.last_insert_id } end |
#table_exists?(table_name) ⇒ Boolean
103 104 105 |
# File 'lib/tina4/database.rb', line 103 def table_exists?(table_name) tables.any? { |t| t.downcase == table_name.to_s.downcase } end |
#tables ⇒ Object
95 96 97 |
# File 'lib/tina4/database.rb', line 95 def tables @driver.tables end |
#transaction ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/tina4/database.rb', line 86 def transaction @driver.begin_transaction yield self @driver.commit rescue => e @driver.rollback raise e end |
#update(table, data, filter = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/tina4/database.rb', line 64 def update(table, data, filter = {}) set_parts = data.keys.map { |k| "#{k} = #{@driver.placeholder}" } where_parts = filter.keys.map { |k| "#{k} = #{@driver.placeholder}" } sql = "UPDATE #{table} SET #{set_parts.join(', ')}" sql += " WHERE #{where_parts.join(' AND ')}" unless filter.empty? values = data.values + filter.values @driver.execute(sql, values) { success: true } end |