I don't understand what this code does. I really just want an explanation for the part between the
/**********************************/
/**********************************/
It was predominantly found at the following site:

http://www.ch-werner.de/javasqlite/

(the actually code is in the 'test program' link at this site).

You'll notice parts are different because I edited/added parts.

So yeah. What's this myregfun and myaggfun stuff? Around there...thanks in advance.

import SQLite.*;

public class Test implements SQLite.Callback, SQLite.Function, SQLite.Authorizer, SQLite.Trace, SQLite.ProgressHandler {
    private StringBuffer acc = new StringBuffer();

    public void columns(String col[]) {
        System.out.println("#cols = " + col.length);
        for (int i = 0; i < col.length; i++) {
            System.out.println("col" + i + ": " + col[i]);
        }
        // throw new java.lang.RuntimeException("boom");
    }

    public void types(String types[]) {
        if (types != null) {
            for (int i = 0; i < types.length; i++) {
                System.out.println("coltype" + i + ": " + types[i]);
            }
        }
    }

    public boolean newrow(String data[]) {
        for (int i = 0; i < data.length; i++) {
            System.out.println("data" + i + ": " + data[i]);
        }
        return false;
    }

    public void function(FunctionContext fc, String args[]) {
        System.out.println("function:");
        for (int i = 0; i < args.length; i++) {
            System.out.println("arg[" + i + "]=" + args[i]);
        }
        if (args.length > 0) {
            fc.set_result(args[0].toLowerCase());
        }
    }

    public void step(FunctionContext fc, String args[]) {
        System.out.println("step:");
        for (int i = 0; i < args.length; i++) {
            acc.append(args[i]);
            acc.append(" ");
        }
    }

    public void last_step(FunctionContext fc) {
        System.out.println("last_step");
        fc.set_result(acc.toString());
        acc.setLength(0);
    }

    public int authorize(int what, String arg1, String arg2, String arg3, String arg4) {
        System.out.println("AUTH: " + what + "," + arg1 + "," + arg2 + ","+ arg3 + "," + arg4);
        return Constants.SQLITE_OK;
    }

    public void trace(String stmt) {
        System.out.println("TRACE: " + stmt);
    }

    public boolean progress() {
        System.out.println("PROGRESS");
        return true;
    }

    public static void main(String args[]) {
        boolean error = true;
        System.out.println("LIB version: " + SQLite.Database.version());
        SQLite.Database db = new SQLite.Database();
        try {
/************************************************************/
            db.open("mydatabase", 0666);
            //System.out.println("DB version: " + db.dbversion());
            db.interrupt();
            db.busy_timeout(1000);
            db.busy_handler(null);
            db.create_function("myregfunc", -1, new Test());
            db.function_type("myregfunc", Constants.SQLITE_TEXT);
            db.create_aggregate("myaggfunc", 1, new Test());
            db.function_type("myaggfunc", Constants.SQLITE_TEXT);
            db.exec("PRAGMA show_datatypes = on", null);
/************************************************************/            
        } catch (java.lang.Exception e) {
            System.err.println("error: " + e);
        } finally {
            try {
                System.err.println("cleaning up ...");
                db.close();
                } catch(java.lang.Exception e) {
                    System.err.println("error: " + e);
                } finally {
                    System.err.println("done.");
            }
        }
        if (error) {
            System.exit(1);
        }
    }
}
import SQLite.*;

public class Test implements SQLite.Callback, SQLite.Function, SQLite.Authorizer, SQLite.Trace, SQLite.ProgressHandler {
    private StringBuffer acc = new StringBuffer();

    public void columns(String col[]) {
        System.out.println("#cols = " + col.length);
        for (int i = 0; i < col.length; i++) {
            System.out.println("col" + i + ": " + col[i]);
        }
        // throw new java.lang.RuntimeException("boom");
    }

    public void types(String types[]) {
        if (types != null) {
            for (int i = 0; i < types.length; i++) {
                System.out.println("coltype" + i + ": " + types[i]);
            }
        }
    }

    public boolean newrow(String data[]) {
        for (int i = 0; i < data.length; i++) {
            System.out.println("data" + i + ": " + data[i]);
        }
        return false;
    }

    public void function(FunctionContext fc, String args[]) {
        System.out.println("function:");
        for (int i = 0; i < args.length; i++) {
            System.out.println("arg[" + i + "]=" + args[i]);
        }
        if (args.length > 0) {
            fc.set_result(args[0].toLowerCase());
        }
    }

    public void step(FunctionContext fc, String args[]) {
        System.out.println("step:");
        for (int i = 0; i < args.length; i++) {
            acc.append(args[i]);
            acc.append(" ");
        }
    }

    public void last_step(FunctionContext fc) {
        System.out.println("last_step");
        fc.set_result(acc.toString());
        acc.setLength(0);
    }

    public int authorize(int what, String arg1, String arg2, String arg3, String arg4) {
        System.out.println("AUTH: " + what + "," + arg1 + "," + arg2 + ","+ arg3 + "," + arg4);
        return Constants.SQLITE_OK;
    }

    public void trace(String stmt) {
        System.out.println("TRACE: " + stmt);
    }

    public boolean progress() {
        System.out.println("PROGRESS");
        return true;
    }

    public static void main(String args[]) {
        boolean error = true;
        System.out.println("LIB version: " + SQLite.Database.version());
        SQLite.Database db = new SQLite.Database();
        try {
            /**********************************/
            db.open("mydatabase", 0666);
            //System.out.println("DB version: " + db.dbversion());
            db.interrupt();
            db.busy_timeout(1000);
            db.busy_handler(null);
            db.create_function("myregfunc", -1, new Test());
            db.function_type("myregfunc", Constants.SQLITE_TEXT);
            db.create_aggregate("myaggfunc", 1, new Test());
            db.function_type("myaggfunc", Constants.SQLITE_TEXT);
            db.exec("PRAGMA show_datatypes = on", null);
            /**********************************/
            
        } catch (java.lang.Exception e) {
            System.err.println("error: " + e);
        } finally {
            try {
                System.err.println("cleaning up ...");
                db.close();
                } catch(java.lang.Exception e) {
                    System.err.println("error: " + e);
                } finally {
                    System.err.println("done.");
            }
        }
        if (error) {
            System.exit(1);
        }
    }
}

Recommended Answers

All 3 Replies

The SQLLite driver is, at best, a Type 2 JDBC Driver. It is only a JNI wrapper around the SQLite library, and that code shown does not use JDBC functionality, at all.

P.S. "func" is "function" and "agg" is "aggregate". The first is creating a function (seemingly a db function?), and the second is creating an aggregate function (again a db function?). If you do not SQLite in and of itself, you are probably going to have a very hard time understanding this "JDBC Driver". If you are simply looking for an embedded database, use JavaDB/Derby. They are at least complete JDBC Drivers.

Okay, what does this d?? someone, please! esp. the regular functions and aggregate functions. what do they do?

#
SQLite.Database db = new SQLite.Database();
#
try {
#
/**********************************/
#
db.open("mydatabase", 0666);
#
//System.out.println("DB version: " + db.dbversion());
#
db.interrupt();
#
db.busy_timeout(1000);
#
db.busy_handler(null);
#
db.create_function("myregfunc", -1, new Test());
#
db.function_type("myregfunc", Constants.SQLITE_TEXT);
#
db.create_aggregate("myaggfunc", 1, new Test());
#
db.function_type("myaggfunc", Constants.SQLITE_TEXT);
#
db.exec("PRAGMA show_datatypes = on", null);
#
/**********************************/
#
 
#
} catch (java.lang.Exception e) { //etc..

That is an SQLite question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.