sagarswathi / h2database

Automatically exported from code.google.com/p/h2database
0 stars 1 forks source link

groovy patch for SourceCompiler (function ALIAS) #442

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I made a groovy patch for SourceCompiler, now we can write function like this:
CREATE ALIAS REVERSEG AS $$@groovy.transform.CompileStatic static String 
reverseg(String s) { return new StringBuilder(s).reverse().toString(); }$$;

Groovy function must startwith "@groovy" or "//groovy". if there is no groovy 
jar, SourceCompiler is not affected.

I've tested this in H21.3.170 (2012-11-30) and groovy-all-2.1.0.jar

BTY:if the trigger can write in sourcecodestring, then trigger is groovyable.

Original issue reported on code.google.com by wonder...@gmail.com on 19 Feb 2013 at 7:54

GoogleCodeExporter commented 8 years ago
I made a new patch, this patch support auto imports ("java.sql.Connection", 
"java.sql.Types", "java.sql.ResultSet", "groovy.sql.Sql", 
"org.h2.tools.SimpleResultSet").
This is a new sample:

create alias sysinfo as $$//groovy
    import java.lang.management.ManagementFactory
    static ResultSet sysinfo(Connection conn, String pat){
        SimpleResultSet rs = new SimpleResultSet()
        rs.addColumn('type', Types.VARCHAR, 255, 0)
        rs.addColumn('key', Types.VARCHAR, 255, 0)
        rs.addColumn('value', Types.VARCHAR, 255, 0)
        switch (pat){
            case 'h2': _h2(rs, conn); break;
            case 'groovy': _groovy(rs); break;
            case 'jvm': _jvm(rs); break;
            default: _h2(rs, conn); _groovy(rs); _jvm(rs);
        }
        rs
    }
    static void _h2(SimpleResultSet rs, Connection conn){
        rs.addRow('h2', 'BUILD_DATE', org.h2.engine.Constants.BUILD_DATE)
        rs.addRow('h2', 'BUILD_ID', org.h2.engine.Constants.BUILD_ID.toString())
        //sample of db process
        def sql = new Sql(conn)
        sql.eachRow('SELECT TABLE_TYPE, count(1) AS ct FROM INFORMATION_SCHEMA.TABLES GROUP BY TABLE_TYPE'){
            rs.addRow('h2', it['TABLE_TYPE'], it['CT'].toString())
        }
    }

    static void _groovy(SimpleResultSet rs){
        rs.addRow('groovy', 'version', groovy.lang.GroovySystem.getVersion())
    }
    static void _jvm(SimpleResultSet rs){
        def defs = [ ['os', ManagementFactory.operatingSystemMXBean, ['arch', 'name', 'version', 'availableProcessors']],
            ['runtime', ManagementFactory.runtimeMXBean, ['name', 'specName', 'specVendor', 'specVersion', 'managementSpecVersion']],
            ['classloading', ManagementFactory.classLoadingMXBean, ['loadedClassCount', 'totalLoadedClassCount', 'unloadedClassCount']],
            ['compilation', ManagementFactory.compilationMXBean, ['totalCompilationTime']],
            ['heap', ManagementFactory.memoryMXBean.heapMemoryUsage, ['committed', 'init', 'max', 'used']],
            ['noneheap', ManagementFactory.memoryMXBean.nonHeapMemoryUsage, ['committed', 'init', 'max', 'used']],
        ]
        for (ad in defs){
            def mx=ad[1]
            for (fe in ad[2]){
                rs.addRow(ad[0], fe, mx[fe].toString())
            }
        }
    }
$$;

Original comment by wonder...@gmail.com on 21 Feb 2013 at 4:43

GoogleCodeExporter commented 8 years ago
Sorry I can't apply the patch as it relies on having Groovy in the classpath. 
The code would have to be called using reflection. See the callers of 
org.h2.util.Utils.callMethod, callStaticMethod on how to do that.

Original comment by thomas.t...@gmail.com on 23 Feb 2013 at 4:41

GoogleCodeExporter commented 8 years ago
OK, you are the boss. Here is a new patch useing reflection. and a new sample 
procedure.

CREATE ALIAS tr AS $$@groovy.transform.CompileStatic
 static String tr(String str, String sourceSet, String replacementSet){
 return str.tr(sourceSet, replacementSet);
 }
$$

call tr('hello', 'a-z', 'A-Z')='HELLO'

Original comment by wonder...@gmail.com on 25 Feb 2013 at 3:36

GoogleCodeExporter commented 8 years ago

Original comment by wonder...@gmail.com on 25 Feb 2013 at 3:37

Attachments:

GoogleCodeExporter commented 8 years ago
Committed with some changes in revision 4765

Original comment by noelgrandin on 7 May 2013 at 9:44