org.sqlite
Class Function

java.lang.Object
  extended by org.sqlite.Function
Direct Known Subclasses:
Function.Aggregate

public abstract class Function
extends Object

Provides an interface for creating SQLite user-defined functions.

A subclass of org.sqlite.Function can be registered with Function.create() and called by the name it was given. All functions must implement xFunc(), which is called when SQLite runs the custom function.

Eg.
      Class.forName("org.sqlite.JDBC");
      Connection conn = DriverManager.getConnection("jdbc:sqlite:");

      Function.create(conn, "myFunc", new Function() {
          protected void xFunc() {
              System.out.println("myFunc called!");
          }
      });

      conn.createStatement().execute("select myFunc();");
  

Arguments passed to a custom function can be accessed using the protected functions provided. args() returns the number of arguments passed, while value_<type>(int) returns the value of the specific argument. Similarly a function can return a value using the result(<type>) function.

Aggregate functions are not yet supported, but coming soon.


Nested Class Summary
static class Function.Aggregate
           
 
Constructor Summary
Function()
           
 
Method Summary
protected  int args()
          Returns the number of arguments passed to the function.
static void create(Connection conn, String name, Function f)
          Registers the given function with the Connection using the provided name.
static void destroy(Connection conn, String name)
          Removes the named function form the Connection.
protected  void error(String err)
          Called by xFunc to throw an error.
protected  void result()
          Called by xFunc to return a value.
protected  void result(byte[] value)
          Called by xFunc to return a value.
protected  void result(double value)
          Called by xFunc to return a value.
protected  void result(int value)
          Called by xFunc to return a value.
protected  void result(long value)
          Called by xFunc to return a value.
protected  void result(String value)
          Called by xFunc to return a value.
protected  byte[] value_blob(int arg)
          Called by xFunc to access the value of an argument.
protected  int value_bytes(int arg)
          Called by xFunc to access the value of an argument.
protected  double value_double(int arg)
          Called by xFunc to access the value of an argument.
protected  int value_int(int arg)
          Called by xFunc to access the value of an argument.
protected  long value_long(int arg)
          Called by xFunc to access the value of an argument.
protected  String value_text(int arg)
          Called by xFunc to access the value of an argument.
protected  int value_type(int arg)
          Called by xFunc to access the value of an argument.
protected abstract  void xFunc()
          Called by SQLite as a custom function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Function

public Function()
Method Detail

create

public static final void create(Connection conn,
                                String name,
                                Function f)
                         throws SQLException
Registers the given function with the Connection using the provided name.

Throws:
SQLException

destroy

public static final void destroy(Connection conn,
                                 String name)
                          throws SQLException
Removes the named function form the Connection.

Throws:
SQLException

xFunc

protected abstract void xFunc()
                       throws SQLException
Called by SQLite as a custom function. Should access arguments through value_*(int), return results with result(*) and throw errors with error(String).

Throws:
SQLException

args

protected final int args()
                  throws SQLException
Returns the number of arguments passed to the function. Can only be called from xFunc().

Throws:
SQLException

result

protected final void result(byte[] value)
                     throws SQLException
Called by xFunc to return a value.

Throws:
SQLException

result

protected final void result(double value)
                     throws SQLException
Called by xFunc to return a value.

Throws:
SQLException

result

protected final void result(int value)
                     throws SQLException
Called by xFunc to return a value.

Throws:
SQLException

result

protected final void result(long value)
                     throws SQLException
Called by xFunc to return a value.

Throws:
SQLException

result

protected final void result()
                     throws SQLException
Called by xFunc to return a value.

Throws:
SQLException

result

protected final void result(String value)
                     throws SQLException
Called by xFunc to return a value.

Throws:
SQLException

error

protected final void error(String err)
                    throws SQLException
Called by xFunc to throw an error.

Throws:
SQLException

value_bytes

protected final int value_bytes(int arg)
                         throws SQLException
Called by xFunc to access the value of an argument.

Throws:
SQLException

value_text

protected final String value_text(int arg)
                           throws SQLException
Called by xFunc to access the value of an argument.

Throws:
SQLException

value_blob

protected final byte[] value_blob(int arg)
                           throws SQLException
Called by xFunc to access the value of an argument.

Throws:
SQLException

value_double

protected final double value_double(int arg)
                             throws SQLException
Called by xFunc to access the value of an argument.

Throws:
SQLException

value_int

protected final int value_int(int arg)
                       throws SQLException
Called by xFunc to access the value of an argument.

Throws:
SQLException

value_long

protected final long value_long(int arg)
                         throws SQLException
Called by xFunc to access the value of an argument.

Throws:
SQLException

value_type

protected final int value_type(int arg)
                        throws SQLException
Called by xFunc to access the value of an argument.

Throws:
SQLException


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.1 Japan License.