Class: Taupe::Database
- Inherits:
-
Object
- Object
- Taupe::Database
- Includes:
- Accessorized
- Defined in:
- lib/taupe/database.rb,
lib/taupe/database/mysql.rb,
lib/taupe/database/sqlite.rb,
lib/taupe/database/postgresql.rb
Overview
Database class Manage database connection and serve as query proxy
Defined Under Namespace
Classes: MysqlDriver, PostgresqlDriver, SqliteDriver
Instance Attribute Summary collapse
-
#driver ⇒ Object
Accessors.
-
#instance ⇒ Object
Accessors.
Class Method Summary collapse
-
.database ⇒ Symbol
Get the database name.
-
.driver_factory ⇒ Object
Setup the connection driver.
-
.dsn ⇒ Hash
Get the data source name.
-
.escape(str) ⇒ String
Escape a string.
-
.exec(query) ⇒ Object
Execute a single query.
-
.fetch(query, single = false) ⇒ Array, Object
Fetch objects from database.
-
.guess_schema(table) ⇒ Hash
Guess schema of a table.
-
.last_id ⇒ Integer
Fetch last inserted id.
-
.setup(&block) ⇒ Object
Setup the Database instance.
-
.setup_defaults ⇒ Object
Setup default values.
-
.type ⇒ Symbol
Get the database type.
Instance Method Summary collapse
-
#initialize(&block) ⇒ Database
constructor
Constructor.
Methods included from Accessorized
Constructor Details
#initialize(&block) ⇒ Database
Constructor
26 27 28 |
# File 'lib/taupe/database.rb', line 26 def initialize(&block) instance_eval(&block) end |
Instance Attribute Details
#driver ⇒ Object
Accessors
22 23 24 |
# File 'lib/taupe/database.rb', line 22 def driver @driver end |
#instance ⇒ Object
Accessors
22 23 24 |
# File 'lib/taupe/database.rb', line 22 def instance @instance end |
Class Method Details
.database ⇒ Symbol
Get the database name
46 47 48 |
# File 'lib/taupe/database.rb', line 46 def self.database @instance._database end |
.driver_factory ⇒ Object
Setup the connection driver
96 97 98 99 100 |
# File 'lib/taupe/database.rb', line 96 def self.driver_factory cname = "Taupe::Database::#{@instance._type.capitalize}Driver" klass = cname.split('::').reduce(Object) { |a, e| a.const_get e } @instance.driver = klass.new dsn end |
.dsn ⇒ Hash
Get the data source name
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/taupe/database.rb', line 74 def self.dsn case @instance._type when :postgresql { host: @instance._host.to_s, user: @instance._username.to_s, password: @instance._password.to_s, dbname: @instance._database.to_s } when :mysql { host: @instance._host.to_s, username: @instance._username.to_s, password: @instance._password.to_s, database: @instance._database.to_s } when :sqlite @instance._database.to_s end end |
.escape(str) ⇒ String
Escape a string
134 135 136 |
# File 'lib/taupe/database.rb', line 134 def self.escape(str) @instance.driver.escape str end |
.exec(query) ⇒ Object
Execute a single query
112 113 114 |
# File 'lib/taupe/database.rb', line 112 def self.exec(query) @instance.driver.exec query end |
.fetch(query, single = false) ⇒ Array, Object
Fetch objects from database
120 121 122 123 |
# File 'lib/taupe/database.rb', line 120 def self.fetch(query, single = false) results = @instance.driver.fetch query single ? results[0] : results end |
.guess_schema(table) ⇒ Hash
Guess schema of a table
105 106 107 |
# File 'lib/taupe/database.rb', line 105 def self.guess_schema(table) @instance.driver.guess_schema table end |
.last_id ⇒ Integer
Fetch last inserted id
127 128 129 |
# File 'lib/taupe/database.rb', line 127 def self.last_id @instance.driver.last_id end |
.setup(&block) ⇒ Object
Setup the Database instance
32 33 34 35 36 |
# File 'lib/taupe/database.rb', line 32 def self.setup(&block) @instance = new(&block) setup_defaults driver_factory end |
.setup_defaults ⇒ Object
Setup default values
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/taupe/database.rb', line 51 def self.setup_defaults case @instance._type when :pg, :pgsql, :postgres, :postgresql Taupe.require_gem 'pg', 'PostgreSQL database engine' @instance._type = :postgresql @instance._host ||= :localhost @instance._port ||= 5432 when :mysql, :mysql2 Taupe.require_gem 'mysql2', 'MySQL database engine' @instance._type = :mysql @instance._host ||= :localhost @instance._port ||= 3306 when :sqlite, :sqlite3 Taupe.require_gem 'sqlite3', 'SQLite database engine' @instance._type = :sqlite @instance._database ||= File.('~/.taupe.db') else fail 'Unknown database type' end end |
.type ⇒ Symbol
Get the database type
40 41 42 |
# File 'lib/taupe/database.rb', line 40 def self.type @instance._type end |