Class: ROM::Yesql::Gateway

Inherits:
Gateway
  • Object
show all
Extended by:
Initializer
Defined in:
lib/rom/yesql/gateway.rb

Overview

Yesql gateway exposes access to configured SQL queries

Relations created with datasets provided by this gateway automatically expose access to gateway’s queries.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGateway

Initializes a yesql gateway

Examples:

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

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

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

Parameters:

  • uri (String)
  • options (Hash)

    @option :path [String] @option :queries [Hash] @option :query_proc [Proc]



74
75
76
77
78
79
80
# File 'lib/rom/yesql/gateway.rb', line 74

def initialize(*)
  super
  @connection = Sequel.connect(uri, options)
  @queries = @queries.merge(load_queries(path)).freeze
  Relation.query_proc(query_proc)
  Relation.load_queries(queries)
end

Instance Attribute Details

#connectionObject (readonly)



46
47
48
# File 'lib/rom/yesql/gateway.rb', line 46

def connection
  @connection
end

#pathString (readonly)

Returns a path to files with SQL queries.

Returns:

  • (String)

    a path to files with SQL queries



29
# File 'lib/rom/yesql/gateway.rb', line 29

option :path, reader: true, optional: true

#queriesHash (readonly)

Returns a hash with queries.

Returns:

  • (Hash)

    a hash with queries



33
# File 'lib/rom/yesql/gateway.rb', line 33

option :queries, default: -> { EMPTY_HASH }

#query_procProc (readonly)

This defaults to simple interpolation of the query using option hash passed to a relation

Returns:

  • (Proc)

    custom query proc for pre-processing a query string



38
39
40
41
42
# File 'lib/rom/yesql/gateway.rb', line 38

option :query_proc, reader: true, default: proc { |_gateway|
  proc do |_name, query, opts|
    query % opts
  end
}

#uriString (readonly)

Returns connection string.

Returns:

  • (String)

    connection string



25
# File 'lib/rom/yesql/gateway.rb', line 25

param :uri

Instance Method Details

#dataset(_name) ⇒ Dataset

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.

Initializes a dataset

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

Returns:



89
90
91
# File 'lib/rom/yesql/gateway.rb', line 89

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

#dataset?(_name) ⇒ True

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 if a dataset with the given name exists

This always returns true because all relations use the same dataset

Returns:

  • (True)


100
101
102
# File 'lib/rom/yesql/gateway.rb', line 100

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