class ROM::Yesql::Gateway
Yesql
gateway exposes access to configured SQL queries
Relations created with datasets provided by this gateway automatically expose access to gateway's queries.
@api public
Attributes
connection[R]
@!attribute [r] connection
@return [Sequel::Database] Sequel database connection
Public Class Methods
new(*)
click to toggle source
Initializes a yesql gateway
@example
# 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 })
@param uri [String]
@param options [Hash]
@option :path [String] @option :queries [Hash] @option :query_proc [Proc]
@api public
Calls superclass method
# 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
Public Instance Methods
dataset(_name)
click to toggle source
Initializes a dataset
Since all relations use the same dataset we simply create one instance
@return [Dataset]
@api private
# File lib/rom/yesql/gateway.rb, line 89 def dataset(_name) @dataset ||= Dataset.new(connection) end
dataset?(_name)
click to toggle source
Returns if a dataset with the given name exists
This always returns true because all relations use the same dataset
@return [True]
@api private
# File lib/rom/yesql/gateway.rb, line 100 def dataset?(_name) !@dataset.nil? end
Private Instance Methods
load_queries(path)
click to toggle source
Loads queries from filesystem if :path was provided
@api private
# File lib/rom/yesql/gateway.rb, line 109 def load_queries(path) if path.nil? {} else Dir["#{path}/*"].each_with_object({}) do |dir, fs_queries| dataset = File.basename(dir).to_sym fs_queries[dataset] = Dir["#{dir}/**/*.sql"].each_with_object({}) do |file, ds_queries| query_name = File.basename(file, ".*").to_sym sql = File.read(file).strip ds_queries[query_name] = sql end end end end