Module: Swift

Defined in:
lib/swift.rb,
lib/swift/type.rb,
lib/swift/header.rb,
lib/swift/record.rb,
lib/swift/result.rb,
lib/swift/adapter.rb,
lib/swift/version.rb,
lib/swift/attribute.rb,
lib/swift/statement.rb,
lib/swift/migrations.rb,
lib/swift/adapter/sql.rb,
lib/swift/validations.rb,
lib/swift/identity_map.rb,
lib/swift/adapter/mysql.rb,
lib/swift/adapter/sqlite3.rb,
lib/swift/adapter/em/mysql.rb,
lib/swift/adapter/postgres.rb,
lib/swift/adapter/synchrony.rb,
lib/swift/adapter/em/postgres.rb,
lib/swift/adapter/eventmachine.rb,
lib/swift/fiber_connection_pool.rb,
lib/swift/adapter/synchrony/mysql.rb,
lib/swift/adapter/synchrony/postgres.rb

Overview

Based on EM::Synchrony::ConnectionPool

Defined Under Namespace

Modules: Migrations, Type Classes: Adapter, Attribute, Error, Errors, FiberConnectionPool, Header, IdentityMap, Record, Result, RuntimeError, Statement

Constant Summary collapse

VERSION =
'1.2.3'

Class Method Summary collapse

Class Method Details

.db(name = nil, &block) ⇒ Swift::Adapter

Fetch or scope a block to a specific DB by name.

Examples:

Swift.db :other do |other|
  # Inside this block all these are the same:
  # other
  # Swift.db
  # Swift.db :other

  other_users = User.prepare('select * from users where age > ?')
  other_users.execute(32)
end

Parameters:

  • name (Symbol) (defaults to: nil)

    Adapter name.

  • block (Proc)

    Scope this block to the named adapter instead of :default.

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/swift.rb', line 98

def db name = nil, &block
  repository = name || scopes.size < 1 ? repositories[name ||= :default] : scopes.last
  repository or raise "Unknown db '#{name}', did you forget to #setup ?"

  if block_given?
    begin
      scopes.push(repository)
      block.call(repository)
    ensure
      scopes.pop
    end
  end
  repository
end

.migrate!(name = nil) ⇒ Object

Migrations



47
48
49
# File 'lib/swift/migrations.rb', line 47

def self.migrate! name = nil
  schema.each {|record| record.migrate!(db(name))}
end

.repositoriesObject



133
134
135
# File 'lib/swift.rb', line 133

def repositories
  @repositories ||= {}
end

.schemaArray<Swift::Schema>

List of known Swift::Schema classes.

Handy if you are brewing stuff like migrations and need a list of defined schema subclasses.

Returns:

  • (Array<Swift::Schema>)


118
119
120
# File 'lib/swift.rb', line 118

def schema
  @schema ||= []
end

.scopesObject



138
139
140
# File 'lib/swift.rb', line 138

def scopes
  Thread.current[:swift_db] ||= []
end

.setup(name, type = nil, options = {}) ⇒ Swift::Adapter

Setup a new DB connection.

You almost certainly want to setup a :default named adapter. The :default scope will be used for unscoped calls to Swift.db.

Examples:

Swift.setup :default, Swift::DB::Postgres, db: 'db1'
Swift.setup :other,   Swift::DB::Postgres, db: 'db2'

Parameters:

  • name (Symbol)

    Adapter name.

  • type (Swift::Adapter) (defaults to: nil)

    Concrete adapter subclass. See Swift::DB

  • options (Hash) (defaults to: {})

    Connection options

Options Hash (options):

  • :db (String)

    Name.

  • :user (String) — default: *nix login user
  • :password (String) — default: ''
  • :host (String) — default: 'localhost'
  • :port (Integer) — default: DB default

Returns:

See Also:



71
72
73
74
75
76
77
78
79
80
# File 'lib/swift.rb', line 71

def setup name, type = nil, options = {}
  if block_given?
    repositories[name] = yield
  else
    unless type.kind_of?(Class) && type < Swift::Adapter
      raise TypeError, "Expected +type+ Swift::Adapter subclass but got #{type.inspect}"
    end
    repositories[name] = type.new(options)
  end
end

.trace(io = $stdout, &block) ⇒ Object

Trace the command execution in currently scoped adapter

Examples:

Swift.trace { Swift.db.execute("select * from users") }

See Also:



128
129
130
# File 'lib/swift.rb', line 128

def trace io = $stdout, &block
  Swift.db.trace(io, &block)
end