Class: Taupe::Database

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Accessorized

included

Constructor Details

#initialize(&block) ⇒ Database

Constructor

Parameters:

  • block (Proc)

    A given block



26
27
28
# File 'lib/taupe/database.rb', line 26

def initialize(&block)
  instance_eval(&block)
end

Instance Attribute Details

#driverObject

Accessors



22
23
24
# File 'lib/taupe/database.rb', line 22

def driver
  @driver
end

#instanceObject

Accessors



22
23
24
# File 'lib/taupe/database.rb', line 22

def instance
  @instance
end

Class Method Details

.databaseSymbol

Get the database name

Returns:

  • (Symbol)


46
47
48
# File 'lib/taupe/database.rb', line 46

def self.database
  @instance._database
end

.driver_factoryObject

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

.dsnHash

Get the data source name

Returns:



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

Parameters:

  • str (String)

Returns:

  • (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

Parameters:

  • query (String)

    The query to execute

Returns:

  • (Object)


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

Parameters:

  • query (String)

    The query to fetch

  • single (Boolean) (defaults to: false)

    Must return one or more results?

Returns:

  • (Array, Object)


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

Parameters:

  • table (String)

    The table name

Returns:



105
106
107
# File 'lib/taupe/database.rb', line 105

def self.guess_schema(table)
  @instance.driver.guess_schema table
end

.last_idInteger

Fetch last inserted id

Returns:

  • (Integer)


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

Parameters:

  • block (Proc)

    A given block



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_defaultsObject

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.expand_path('~/.taupe.db')
  else
    fail 'Unknown database type'
  end
end

.typeSymbol

Get the database type

Returns:

  • (Symbol)


40
41
42
# File 'lib/taupe/database.rb', line 40

def self.type
  @instance._type
end