Module: SqliteExt
- Defined in:
- lib/sqlite_ext.rb,
lib/sqlite_ext/version.rb,
lib/sqlite_ext/init_injection.rb,
lib/sqlite_ext.rb
Defined Under Namespace
Modules: InitInjection
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.enhance_db_session(db) ⇒ Object
Creates all of the registered functions on an instance of ‘SQLite3::Database`.
-
.purge_function_registrations ⇒ Object
Removes all function registrations.
-
.register_create_function(name, arity, *other_args, &block) ⇒ Object
Registers a #create_function call to be invoked on every new instance of ‘SQLite3::Database` immidately after it is instantiated and before it is returned from the call to `.new` and before the invocation of a block that is passed to `.new`.
-
.registered_function_names ⇒ Object
Returns an array of the names of all currently registered functions.
Class Method Details
.enhance_db_session(db) ⇒ Object
Creates all of the registered functions on an instance of ‘SQLite3::Database`.
This is normally called automatically for each new instance, but can also be used to add the functions to an instance that was created before the functions were registered.
70 71 72 73 74 |
# File 'lib/sqlite_ext.rb', line 70 def enhance_db_session(db) registered_function_creations.each_value do |(args,block)| db.create_function *args, &block end end |
.purge_function_registrations ⇒ Object
Removes all function registrations. Has no effect on existing instances of ‘SQLite3::Database`.
59 60 61 |
# File 'lib/sqlite_ext.rb', line 59 def purge_function_registrations registered_function_creations.clear end |
.register_create_function(name, arity, *other_args, &block) ⇒ Object
Registers a #create_function call to be invoked on every new instance of ‘SQLite3::Database` immidately after it is instantiated and before it is returned from the call to `.new` and before the invocation of a block that is passed to `.new`.
Note that this only affects instances of ‘SQLite3::Database` that are subsequently created and has no effect on previously created instances.
Example:
SqliteExt.register_create_function 'sqrt', 1 do |fn,x|
fn.result =
case x
when nil then nil
else Math.sqrt(x)
end
end
SQLite3::Database.new 'data.db' do |db|
puts db.execute("SELECT sqrt(25)")[0][0]
end
# Output: 5.0
43 44 45 46 47 48 49 |
# File 'lib/sqlite_ext.rb', line 43 def register_create_function(name, arity, *other_args, &block) name = "#{name}" registered_function_creations[name] = [ [name, arity, *other_args], block ] end |
.registered_function_names ⇒ Object
Returns an array of the names of all currently registered functions.
53 54 55 |
# File 'lib/sqlite_ext.rb', line 53 def registered_function_names registered_function_creations.keys end |