Class: Lotus::Model::Adapters::SqlAdapter Private

Inherits:
Abstract
  • Object
show all
Includes:
Implementation
Defined in:
lib/lotus/model/adapters/sql_adapter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Adapter for SQL databases

In order to use it with a specific database, you must require the Ruby gem before of loading Lotus::Model.

See Also:

Since:

  • 0.1.0

Instance Method Summary collapse

Methods included from Implementation

#all, #find, #first, #last, #persist

Methods inherited from Abstract

#all, #find, #first, #last, #persist

Constructor Details

#initialize(mapper, uri, options = {}) ⇒ Lotus::Model::Adapters::SqlAdapter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the adapter.

Lotus::Model uses Sequel. For a complete reference of the connection URI, please see: sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html

Parameters:

  • mapper (Object)

    the database mapper

  • uri (String)

    the connection uri for the database

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

    a hash of non-mandatory adapter options

Raises:

See Also:

Since:

  • 0.1.0



45
46
47
48
49
50
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 45

def initialize(mapper, uri, options = {})
  super
  @connection = Sequel.connect(@uri, @options)
rescue Sequel::AdapterNotFound => e
  raise DatabaseAdapterNotFound.new(e.message)
end

Instance Method Details

#clear(collection) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes all the records from the given collection.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Since:

  • 0.1.0



102
103
104
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 102

def clear(collection)
  command(query(collection)).clear
end

#command(query) ⇒ Lotus::Model::Adapters::Sql::Command

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fabricates a command for the given query.

Parameters:

Returns:

See Also:

Since:

  • 0.1.0



117
118
119
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 117

def command(query)
  Sql::Command.new(query)
end

#connection_stringString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a string which can be executed to start a console suitable for the configured database, adding the necessary CLI flags, such as url, password, port number etc.

Returns:

  • (String)

Since:

  • 0.3.0



227
228
229
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 227

def connection_string
  Sql::Console.new(@uri).connection_string
end

#create(collection, entity) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a record in the database for the given entity. It assigns the ‘id` attribute, in case of success.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (#id=)

    the entity to create

Returns:

  • (Object)

    the entity

Since:

  • 0.1.0



62
63
64
65
66
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 62

def create(collection, entity)
  command(
    query(collection)
  ).create(entity)
end

#delete(collection, entity) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes a record in the database corresponding to the given entity.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (#id)

    the entity to delete

Since:

  • 0.1.0



90
91
92
93
94
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 90

def delete(collection, entity)
  command(
    _find(collection, entity.id)
  ).delete
end

#disconnectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

Since:

  • 0.5.0



273
274
275
276
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 273

def disconnect
  @connection.disconnect
  @connection = DisconnectedResource.new
end

#execute(raw) ⇒ NilClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Executes a raw SQL command

Parameters:

  • raw (String)

    the raw SQL statement to execute on the connection

Returns:

  • (NilClass)

Raises:

Since:

  • 0.3.1



240
241
242
243
244
245
246
247
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 240

def execute(raw)
  begin
    @connection.execute(raw)
    nil
  rescue Sequel::DatabaseError => e
    raise Lotus::Model::InvalidCommandError.new(e.message)
  end
end

#fetch(raw, &blk) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fetches raw result sets for the given SQL query

Parameters:

  • raw (String)

    the raw SQL query

  • blk (Proc)

    optional block that is yielded for each record

Returns:

  • (Array)

Raises:

Since:

  • 0.5.0



259
260
261
262
263
264
265
266
267
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 259

def fetch(raw, &blk)
  if block_given?
    @connection.fetch(raw, &blk)
  else
    @connection.fetch(raw).to_a
  end
rescue Sequel::DatabaseError => e
  raise Lotus::Model::InvalidQueryError.new(e.message)
end

#query(collection, context = nil, &blk) ⇒ Lotus::Model::Adapters::Sql::Query

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fabricates a query

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • blk (Proc)

    a block of code to be executed in the context of the query.

Returns:

See Also:

Since:

  • 0.1.0



133
134
135
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 133

def query(collection, context = nil, &blk)
  Sql::Query.new(_collection(collection), context, &blk)
end

#transaction(options = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wraps the given block in a transaction.

For performance reasons the block isn’t in the signature of the method, but it’s yielded at the lower level.

Examples:

Basic usage

require 'lotus/model'

class Article
  include Lotus::Entity
  attributes :title, :body
end

class ArticleRepository
  include Lotus::Repository
end

article = Article.new(title: 'Introducing transactions',
  body: 'lorem ipsum')

ArticleRepository.transaction do
  ArticleRepository.dangerous_operation!(article) # => RuntimeError
  # !!! ROLLBACK !!!
end

Policy rollback always

require 'lotus/model'

class Article
  include Lotus::Entity
  attributes :title, :body
end

class ArticleRepository
  include Lotus::Repository
end

article = Article.new(title: 'Introducing transactions',
  body: 'lorem ipsum')

ArticleRepository.transaction(rollback: :always) do
  ArticleRepository.create(article)
  # !!! ROLLBACK !!!
end

# The operation is rolled back, even in no exceptions were raised.

Policy rollback reraise

require 'lotus/model'

class Article
  include Lotus::Entity
  attributes :title, :body
end

class ArticleRepository
  include Lotus::Repository
end

article = Article.new(title: 'Introducing transactions',
  body: 'lorem ipsum')

ArticleRepository.transaction(rollback: :reraise) do
  ArticleRepository.dangerous_operation!(article) # => RuntimeError
  # !!! ROLLBACK !!!
end # => RuntimeError

# The operation is rolled back, but RuntimeError is re-raised.

Parameters:

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

    options for transaction

  • rollback (Hash)

    a customizable set of options

See Also:

Since:

  • 0.2.3



214
215
216
217
218
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 214

def transaction(options = {})
  @connection.transaction(options) do
    yield
  end
end

#update(collection, entity) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Updates a record in the database corresponding to the given entity.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (#id)

    the entity to update

Returns:

  • (Object)

    the entity

Since:

  • 0.1.0



77
78
79
80
81
# File 'lib/lotus/model/adapters/sql_adapter.rb', line 77

def update(collection, entity)
  command(
    _find(collection, entity.id)
  ).update(entity)
end