Method: SQLite3::Database#create_function

Defined in:
lib/sqlite3/database.rb

#create_function(name, arity, text_rep = Constants::TextRep::ANY, &block) ⇒ Object

Creates a new function for use in SQL statements. It will be added as name, with the given arity. (For variable arity functions, use -1 for the arity.)

The block should accept at least one parameter–the FunctionProxy instance that wraps this function invocation–and any other arguments it needs (up to its arity).

The block does not return a value directly. Instead, it will invoke the FunctionProxy#result= method on the func parameter and indicate the return value that way.

Example:

db.create_function( "maim", 1 ) do |func, value|
  if value.nil?
    func.result = nil
  else
    func.result = value.split(//).sort.join
  end
end

puts db.get_first_value( "select maim(name) from table" )


319
320
321
322
323
324
325
326
# File 'lib/sqlite3/database.rb', line 319

def create_function name, arity, text_rep=Constants::TextRep::ANY, &block
  define_function(name) do |*args|
    fp = FunctionProxy.new
    block.call(fp, *args)
    fp.result
  end
  self
end