Module: Olaf
- Defined in:
- lib/olaf.rb,
lib/olaf/errors.rb,
lib/olaf/drivers/fake.rb,
lib/olaf/query_definition.rb,
lib/olaf/drivers/big_query.rb,
lib/olaf/drivers/snowflake.rb,
lib/olaf/query_definition/class_methods.rb
Defined Under Namespace
Modules: QueryDefinition Classes: BigQuery, Fake, MissingArgumentsError, QueryExecutionError, Snowflake, UndefinedArgumentsError, UnknownArgumentsError, UnknownDriverError
Constant Summary collapse
- DEFAULT_DRIVER =
:default
Class Method Summary collapse
-
.configure(olaf_driver: nil, drivers: nil, default: nil, **args) ⇒ Object
Configures the drivers used to execute queries, either a single one built from the arguments given:.
-
.execute(olaf_query) ⇒ Object
Executes a query defined by Olaf::QueryDefinition with the driver the query declares, or with the default driver when it declares none.
-
.instance(name = nil) ⇒ Object
Returns an instance to execute queries when its configured.
Class Method Details
.configure(olaf_driver: nil, drivers: nil, default: nil, **args) ⇒ Object
Configures the drivers used to execute queries, either a single one built from the arguments given:
Olaf.configure(user: 'olaf') # Olaf::Snowflake, the default
Olaf.configure(olaf_driver: Olaf::Fake) # ideal for testing
or several instantiated ones, which queries pick by name with driver:
Olaf.configure(
drivers: { snowflake: Olaf::Snowflake.new(user: 'olaf'), big_query: Olaf::BigQuery.new(project: 'carwow') },
default: :snowflake
)
@return the default Olaf driver instance
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/olaf.rb', line 26 def self.configure(olaf_driver: nil, drivers: nil, default: nil, **args) @drivers, @default_driver = if drivers raise ArgumentError, 'Pass `drivers:` or a single `olaf_driver:`, not both' if olaf_driver || args.any? raise ArgumentError, 'No drivers given to configure' if drivers.empty? default_driver = default || drivers.keys.first unless drivers.key?(default_driver) raise ArgumentError, "Unknown default driver #{default_driver.inspect}, given: #{drivers.keys.inspect}" end [drivers.dup, default_driver] else [{ DEFAULT_DRIVER => (olaf_driver || Snowflake).new(**args) }, DEFAULT_DRIVER] end instance end |
.execute(olaf_query) ⇒ Object
Executes a query defined by Olaf::QueryDefinition with the driver the query declares, or with the default driver when it declares none.
@return Enumerable of results.
(i.e. Array of Hashes or `row_objects` when specified)
@raises Olaf::QueryExecutionError
@raises Olaf::UnknownDriverError
53 54 55 56 57 58 59 60 |
# File 'lib/olaf.rb', line 53 def self.execute(olaf_query) row_object = olaf_query.class.row_object row_transformer = row_object ? ->(r) { row_object.new(**r) } : Proc.new(&:itself) instance(driver_name(olaf_query)) .fetch(olaf_query) .map!(&row_transformer) end |
.instance(name = nil) ⇒ Object
Returns an instance to execute queries when its configured. Without a name,
the default driver. A single configured driver serves every name, which is
what keeps Olaf::Fake covering every query in tests.
@return Olaf driver instance
* Olaf::Fake - Ideal for testing
* Olaf::Snowflake - Sequel.odbc driver to run queries in Snowflake
* Olaf::BigQuery - google-cloud-bigquery driver to run queries in BigQuery
71 72 73 74 75 76 77 |
# File 'lib/olaf.rb', line 71 def self.instance(name = nil) drivers = @drivers || raise('You need to configure Olaf before using it!') return drivers.fetch(@default_driver) if name.nil? || drivers.size == 1 drivers.fetch(name) { raise UnknownDriverError.new(name, drivers.keys) } end |