Method: SQLite3::Database#create_function
- Defined in:
- lib/sqlite3/database.rb
#create_function(name, arity, text_rep = Constants::TextRep::UTF8, &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" )
390 391 392 393 394 395 396 397 |
# File 'lib/sqlite3/database.rb', line 390 def create_function name, arity, text_rep=Constants::TextRep::UTF8, &block define_function_with_flags(name, text_rep) do |*args| fp = FunctionProxy.new block.call(fp, *args) fp.result end self end |