firebirdsql / fbjava Goto Github PK
View Code? Open in Web Editor NEWFB/Java plugin for Firebird
FB/Java plugin for Firebird
Example:
public static int a() {
return 123;
}
public static int b() throws SQLException {
Connection con = DriverManager.getConnection("jdbc:default:connection:");
PreparedStatement pstmt = con.prepareStatement("select a() from rdb$database");
ResultSet rs = pstmt.executeQuery();
rs.next();
return rs.getInt(1);
}
create function a() returns int external name 'a()' engine java;
create function b() returns int external name 'b()' engine java;
commit;
select b() from rdb$database;
Both functions share same InternalContext, so after closing context from first function, second fails with error:
B
============
Statement failed, SQLSTATE = HY000
java.lang.NullPointerException
at org.firebirdsql.fbjava.impl.InternalContext.close(InternalContext.java:152)
at org.firebirdsql.fbjava.impl.ExternalFunction.execute(ExternalFunction.java:74)
at org.firebirdsql.fbjava.impl.FbClientLibrary$IExternalFunction$VTable$2.invoke(FbClientLibrary.java:9580)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback(CallbackReference.java:485)
at com.sun.jna.CallbackReference$DefaultCallbackProxy.callback(CallbackReference.java:515)
-At function 'B'
After line 0 in file test.sql
Instead of need to set JAVA_HOME, first place to locate the JVM should be in the .conf file.
In this case, class should be annotated specifying if instance should be created:
Context (or any of its inherited interfaces) should be the first parameter.
in DB:
create or alter procedure java_split (
dummy blob SUB_TYPE TEXT,
delimiter varchar(10)
) returns (item varchar(60))
external name 'org.firebirdsql.fbjava.examples.fbjava_example.FbSplit.split()'
engine java;
Java:
public class FbSplit {
public static ExternalResultSet split() throws Exception {
final ProcedureContext context = ProcedureContext.get();
return new ExternalResultSet() {
ValuesMetadata outMetadata = context.getOutputMetadata();
Values outValues = context.getOutputValues();
Values inputValues = context.getInputValues();
String[] items_list = null;
int item_index;
@Override
public boolean fetch() throws Exception {
if (items_list == null) {
java.lang.Object blob_object = inputValues.getObject(1);
// FBBlob blob = (FBBlob) blob_object;
java.sql.Blob blob = (java.sql.Blob) blob_object;
byte[] bytes_data = blob.getBytes(1, (int) (blob.length())); // TO SLOW
String blob_string = new String(bytes_data);
items_list = blob_string.split(inputValues.getObject(2).toString());
}
if (item_index < items_list.length) {
outValues.setObject(1, items_list[item_index]);
item_index++;
return true;
}
return false;
}
};
}
}
then call
select * from java_split(6 000 000 characters length blob, ',')
40 sec execution time
but if I call from standalone java application ~1-2 sec
env:
java 8
jaybird 2.2.10
The username and password are optional in deployer, consider making them required (or at minimum display a warning when left off). I was trying to install fbjava, and I had made a typo in the --password
option (I typed --pasword
instead) and it took me a while to notice that typo ;).
Database with single external java function can not be restored. Not sure if it engine's or plugin's problem.
/opt/firebird/bin/gbak -user sysdba -password masterkey -rep /tmp/x.fbk localhost:/tmp/x_rest.fdb /workspace/fbjava
gbak: ERROR:java.lang.RuntimeException: java.sql.SQLException: database /tmp/x_rest.fdb shutdown [SQLState:HY000, ISC error code:335544528]
at org.firebirdsql.fbjava.impl.ExternalEngine.lambda$open$3(ExternalEngine.java:1091)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
at org.firebirdsql.fbjava.impl.ExternalEngine.open(ExternalEngine.java:1084)
at org.firebirdsql.fbjava.impl.FbClientLibrary$IExternalEngine$VTable$1.invoke(FbClientLibrary.java:10228)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback(CallbackReference.java:485)
at com.sun.jna.CallbackReference$DefaultCallbackProxy.callback(CallbackReference.java:515)
Caused by: java.sql.SQLException: database /tmp/x_re...
gbak:Exiting before completion due to errors
Allow the use of embedded, local and native Jaybird URL specs.
Example:
select test(-1) from rdb$database;
TEST
=====================
-281470681743361
When data type converter adding, data type changing, but length of data not updated (int was 4 as returned from engine, but long is 8 as want plugin), so part of data in message buffer overwritten by null flag.
Installer should set files permissions and user/group owner.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.