Method: SQLite::API.create_function

Defined in:
ext/sqlite-api.c

.create_function(db, name, args, proc) ⇒ nil

Defines a new function that may be invoked from within an SQL statement. The args parameter specifies how many arguments the function expects–use -1 to specify variable arity. The proc parameter must be a proc that expects args + 1 parameters, with the first parameter being an opaque handle to the function object itself:

proc do |func, *args|
  ...
end

The function object is used when calling the #set_result and #set_result_error methods.

Returns:

  • (nil)


627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'ext/sqlite-api.c', line 627

static VALUE
static_api_create_function( VALUE module, VALUE db, VALUE name, VALUE n,
  VALUE proc )
{
  sqlite *handle;
  int     result;

  GetDB( handle, db );
  Check_Type( name, T_STRING );
  Check_Type( n, T_FIXNUM );
  if( !rb_obj_is_kind_of( proc, rb_cProc ) )
  {
    rb_raise( rb_eArgError, "handler must be a proc" );
  }

  result = sqlite_create_function( handle,
              StringValueCStr(name),
              FIX2INT(n),
              static_function_callback,
              (void*)proc );

  if( result != SQLITE_OK )
  {
    static_raise_db_error( result, "create function %s(%d)",
      StringValueCStr(name), FIX2INT(n) );
    /* "raise" does not return */
  }

  return Qnil;
}