Hi Everyone!

I wanted to use the user defined functions in Java language with
sqlite-jdbc-3.8.7. (On Ubuntu Linux 14.04 LTS  64bits)
I wanted to check the arguments number in my custom function. And I
thought, that I send a message for user, when he gave too many arguments in
the function.
I wanted to use the "error()" function for this.
http://priede.bf.lu.lv/ftp/pub/DatuBazes/SQLite/SqliteJDBC/api/org/sqlite/Function.html#error%28java.lang.String%29

I inserted this function in my Java program for, when the user gave not
correct arguments number.
But I got the following exception:


*java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database
(java.lang.UnsatisfiedLinkError:
org.sqlite.core.NativeDB.result_error(JLjava/lang/String;)V)*
I checked the NativeDB.java. I found the "result_error()" function.
https://bitbucket.org/xerial/sqlite-jdbc/src/cb3185b148726e00530779014e69d1475697084c/src/main/java/org/sqlite/NativeDB.java?at=default#cl-299

But I did not find any function for "result_error()" in NativeDb.c file.
I think, I have to find "Java_org_sqlite_NativeDB_result_1error()"
function. But there is not any.
https://bitbucket.org/xerial/sqlite-jdbc/src/cb3185b148726e00530779014e69d1475697084c/src/main/java/org/sqlite/NativeDB.c?at=default

Is it a bug?
Or I wanted to use the "error()" function in wrong mode?


There are the two Java files, that I wrote for testing the user defined
functions:

*// UdfTester*
import java.sql.*;

public class UdfTester {

    static String dbPath = "/home/user/mypath/test.sqlite";

    public static void main(String args[]) {

        String sql = "SELECT * "
                + ",jReverseString(name) as rev "
                + "FROM names "
                + "WHERE "
                + "jLikeRegExp(name,'.*i.*a.*',3) "
                + ";"
                + "";
        try {
            SqliteConn db = new SqliteConn(dbPath, false);
            if (db.isConnected()) {
                System.out.println("Opened database successfully");
                ResultSet rs = db.getStatement().executeQuery(sql);
                while (rs.next()) {
                    int id = rs.getInt("id");
                    String name = rs.getString("name");
                    String rev = rs.getString("rev");
                    System.out.print("ID= " + id);
                    System.out.print("\tNAME= " + name);
                    System.out.print("\tREV= " + rev);
                    System.out.println();
                }
                rs.close();
                db.close();
                System.out.println("Operation done successfully");
            } else {
                System.err.println("The database could not open!");
            }
        } catch (Exception ex) {
            System.err.println("Exception in "
                    + "UdfTester.main() "
                    + "using query: " + sql + "\n"
                    + ex.getClass().getName() + ": " + ex.getMessage()
            );
            System.exit(0);
        }
    }
}


*// SqliteConn*
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.sqlite.Function;

public class SqliteConn {

    private Connection conn = null;
    private Statement stmt = null;
    private String[] funcNames = null;
    private int funcIndex;

    public SqliteConn(String dbPath, boolean isAutoCommit) throws
SQLException, ClassNotFoundException {
        this.funcNames = new String[]{
            "jReverseString",
            "jLikeRegExp"
        };
        this.funcIndex = 0;
        try {
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
            conn.setAutoCommit(isAutoCommit);
            stmt = conn.createStatement();
            addFunctions();
        } catch (SQLException ex) {
            destroyFunctions();
            stmt = null;
            conn = null;
            throw new SQLException(ex.getMessage() + " In SqliteConn()
constructor ");
        } catch (ClassNotFoundException ex) {
            destroyFunctions();
            stmt = null;
            conn = null;
            throw new ClassNotFoundException(ex.getMessage() + " In
SqliteConn() constructor ");
        }
    }

    public boolean isConnected() {
        return (conn != null && stmt != null);
    }

    private void addFunctions() throws SQLException {
        funcIndex = 0;

        // jReverseString(String arg0)
        Function.create(conn, funcNames[funcIndex], new Function() {
            @Override
            protected void xFunc() throws SQLException {
                int num = args();
                String ret = "";
                if (num == 1) {
                    String s = value_text(0);
                    ret = new StringBuffer(s).reverse().toString();
                }
                result(ret);
            }
        });
        ++funcIndex;

        // jLikeRegExp(String arg0, String arg1) arg0:the text   arg1: the
regexp
        Function.create(conn, funcNames[funcIndex], new Function() {
            @Override
            protected void xFunc() throws SQLException {
                int num = args();
                if (num == 2) {
                    Pattern pattern = Pattern.compile(value_text(1));
                    Matcher matcher = pattern.matcher(value_text(0));
                    result(matcher.find() ? 1 : 0);
                } else {
                    error(" Illegal arguments!!! ");
                    /*throw new SQLException(
                            " Illegal arguments by user defined function "
                            + funcNames[1] + "(). "
                            + "It must have 2 arguments (text, regexp) "
                    );*/
                }
            }
        });
        ++funcIndex;
    }

    private void destroyFunctions() throws SQLException {
        if (isConnected() && funcNames != null && funcNames.length > 0) {
            for (int i = 0; i < funcIndex; ++i) {
                try {
                    Function.destroy(conn, funcNames[i]);
                } catch (Exception ex) {
                    throw new SQLException(ex.getMessage() + " In
SqliteConn.destroyFunctions() ");
                }
            }
        }
    }

    public void close() throws SQLException {
        try {
            destroyFunctions();
            stmt.close();
            conn.close();
        } catch (SQLException ex) {
            throw new SQLException(ex.getMessage() + " In
SqliteConn.close() ");
        } finally {
            stmt = null;
            conn = null;
        }
    }

    /**
     * @return the conn
     */
    public Connection getConn() {
        return conn;
    }

    /**
     * @return the stmt
     */
    public Statement getStatement() {
        return stmt;
    }
}



Thanks!
-- 
Best Regards: Pelz
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to