Method: Libsql::Database#define_function

Defined in:
lib/libsql/database.rb

#define_function(name, callable = nil, &block) ⇒ Object Also known as: function

call-seq:

db.define_function( "name", MyDBFunction.new )
db.define_function( "my_func", callable )
db.define_function( "my_func" ) do |x,y|
  .... 
  return result
end

register a callback to be exposed as an SQL function. There are multiple ways to register this function:

  1. db.define_function( “name” ) { |a| … }

    • pass define_function a name and a block.

    • The SQL function name taking arity parameters will be registered, where arity is the arity of the block.

    • The return value of the block is the return value of the registred SQL function

  2. db.define_function( “name”, callable )

    • pass function a name and something that responds_to?( :to_proc )

    • The SQL function name is registered taking arity parameters is registered where arity is the arity of callable.to_proc.call

    • The return value of the callable.to_proc.call is the return value of the SQL function

See also ::Libsql::Function

Raises:



659
660
661
662
663
664
665
666
# File 'lib/libsql/database.rb', line 659

def define_function( name, callable = nil, &block ) 
  p = ( callable || block ).to_proc
  raise FunctionError, "Use only mandatory or arbitrary parameters in an SQL Function, not both" if p.arity < -1
  db_function = ::Libsql::SQLite3::Database::Function.new( name, p )
  @api.define_function( db_function.name, db_function )
  @functions[db_function.signature] = db_function
  nil
end