Class: ROM::Yesql::Repository

Inherits:
Repository
  • Object
show all
Includes:
Options
Defined in:
lib/rom/yesql/repository.rb

Overview

Yesql repository exposes access to configured SQL queries

Examples:

# Load all queries from a specific path
ROM::Yesql::Repository.new(uri, path: '/path/to/my_queries')

# Provide queries explicitly using a hash
ROM::Yesql::Repository.new(uri, queries: {
  reports: {
    all_users: 'SELECT * FROM users'
  }
})

# Override default query proc handler
ROM::Yesql::Repository.new(uri, query_proc: proc { |name, query, *args|
  # do something to return an sql string
})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}) ⇒ Repository

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 new instance of Repository


53
54
55
56
57
58
59
60
# File 'lib/rom/yesql/repository.rb', line 53

def initialize(uri, options = {})
  super
  @connection = Sequel.connect(uri, options)
  initialize_queries
  queries.freeze
  Relation.query_proc(query_proc)
  Relation.load_queries(queries)
end

Instance Attribute Details

#connectionSequel::Database (readonly)

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.

Return Sequel database connection

Returns:

  • (Sequel::Database)

43
44
45
# File 'lib/rom/yesql/repository.rb', line 43

def connection
  @connection
end

#queriesHash (readonly)

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.

Hash with loaded queries indexed using dataset names

Returns:

  • (Hash)

50
51
52
# File 'lib/rom/yesql/repository.rb', line 50

def queries
  @queries
end

Instance Method Details

#dataset(_name) ⇒ 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.

Initialize a dataset

Since all relations use the same dataset we simply create one instance


67
68
69
# File 'lib/rom/yesql/repository.rb', line 67

def dataset(_name)
  @dataset ||= Dataset.new(connection)
end

#dataset?(_name) ⇒ Boolean

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:

  • (Boolean)

72
73
74
# File 'lib/rom/yesql/repository.rb', line 72

def dataset?(_name)
  ! @dataset.nil?
end