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) ⇒ 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

Raises:

See Also:

Since:

  • 0.1.0



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

def initialize(mapper, uri)
  super
  @connection = Sequel.connect(@uri)
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



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

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



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

def command(query)
  Sql::Command.new(query)
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



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

def create(collection, entity)
  entity.id = command(
                query(collection)
              ).create(entity)
  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



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

def delete(collection, entity)
  command(
    _find(collection, entity.id)
  ).delete
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



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

def query(collection, context = nil, &blk)
  Sql::Query.new(_collection(collection), context, &blk)
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



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

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