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]



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

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)



44
45
46
# File 'lib/rom/yesql/gateway.rb', line 44

def connection
  @connection
end

#pathString (readonly)

Returns a path to files with SQL queries.

Returns:

  • (String)

    a path to files with SQL queries



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

option :path, reader: true, optional: true

#queriesHash (readonly)

Returns a hash with queries.

Returns:

  • (Hash)

    a hash with queries



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

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



36
37
38
39
40
# File 'lib/rom/yesql/gateway.rb', line 36

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



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

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:



87
88
89
# File 'lib/rom/yesql/gateway.rb', line 87

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)


98
99
100
# File 'lib/rom/yesql/gateway.rb', line 98

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