Re: Auto-registration of Functions

77 views
Skip to first unread message

Noel Grandin

unread,
Jul 9, 2012, 10:52:41 AM7/9/12
to h2-da...@googlegroups.com, Alfred Reibenschuh
Feel free to provide a patch.

On 2012-07-09 16:40, Alfred Reibenschuh wrote:
> hi!
>
> i'd like to propose a simple but effective methodology to auto
> register additional db-functions to h2 just by having the user/admin
> making additional jar files available on the classpath.
>
> The H2-DB Engine would have to use the classloader to lookup all files
> named '/META-INF/org.h2.ext.FunctionClass' (or similar) which would be
> a simple text file containing one class-name by line to be considered.
>

Alfred Reibenschuh

unread,
Jul 10, 2012, 6:55:10 AM7/10/12
to h2-da...@googlegroups.com, Alfred Reibenschuh
hi!

i have written a class thta does all the dirty work but where should a put the call to execute it?

in org.h2.engine.Database.open() ?
----
        getLobStorage().init();
        systemSession.commit(true);
        
        FnRegUtil.registerFromClasspath(trace, this.getClass().getClassLoader(), systemSession, mainSchema, null);
        
        trace.info("opened {0}", databaseName);
        if (checkpointAllowed > 0) {
            afterWriting();
        }
    }

----
like this ? 


c
FnRegUtil.java

Noel Grandin

unread,
Jul 10, 2012, 7:05:33 AM7/10/12
to h2-da...@googlegroups.com, Alfred Reibenschuh
Cool, that looks reasonable!

Please can you supply
- a diff
- a license statement
- don't forget to update changelog and documentation

See here:
http://www.h2database.com/html/build.html#providing_patches
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To view this discussion on the web visit https://groups.google.com/d/msg/h2-database/-/17E6i7yUAnMJ.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.


Alfred Reibenschuh

unread,
Jul 10, 2012, 12:44:00 PM7/10/12
to h2-da...@googlegroups.com, Alfred Reibenschuh
OK heres the contribution:

I wrote the code, it's mine, and I'm contributing it to H2 for distribution multiple-licensed under the H2 License, version 1.0, and under the Eclipse Public License, version 1.0 (http://h2database.com/html/license.html).

Copyright 2012 H2 Group. Multiple-Licensed under the H2 License,
Version 1.0, and under the Eclipse Public License, Version 1.0
Initial Developer: Alfred Reibenschuh contributed to the H2 Group

C
h2-fnautoreg.patch

Noel Grandin

unread,
Jul 11, 2012, 3:58:04 AM7/11/12
to h2-da...@googlegroups.com, Alfred Reibenschuh
Thanks a lot!

Committed with some changes.

On 2012-07-10 18:44, Alfred Reibenschuh wrote:
> Alfred Reibenschuh c


Alfred Reibenschuh

unread,
Jul 11, 2012, 9:04:44 AM7/11/12
to h2-da...@googlegroups.com, Alfred Reibenschuh
ok, 

the class name look more reasonable.

how about an Interface "org.h2.(api or ext).FunctionRegistry" like this:

public interface FunctionRegistry 
{
public boolean register(ClassLoader cl, Session sess, Schema sch, boolean vendorPrefixed);
}

so a class could register their functions on its own and the engine could request that the registered functions be prefixed by a vendor tag (eg. MYSQL_, POSTGRES_, etc)

i also have in the queue stubs for all mysql 5.6 functions not implemented by h2.

how about that ?

Noel Grandin

unread,
Jul 11, 2012, 9:10:05 AM7/11/12
to h2-da...@googlegroups.com, Alfred Reibenschuh

On 2012-07-11 15:04, Alfred Reibenschuh wrote:
> ok,
>
> the class name look more reasonable.
>
> how about an Interface "org.h2.(api or ext).FunctionRegistry" like this:
>
> public interface FunctionRegistry
> {
> public boolean register(ClassLoader cl, Session sess, Schema sch,
> boolean vendorPrefixed);
> }
>
> so a class could register their functions on its own and the engine
> could request that the registered functions be prefixed by a vendor
> tag (eg. MYSQL_, POSTGRES_, etc)
>
At that point I think we're outside the bounds of what H2 should
provide, and you should probably just implement that yourself in your
own main() function.

However, I'm happy to make some more of the AutoRegisterFunctionAliases
methods public again, if that will help?

> i also have in the queue stubs for all mysql 5.6 functions not
> implemented by h2.
>

Unless you actually have a use-case for those, I'd not be terribly keen
on cluttering up the code with them.

Alfred Reibenschuh

unread,
Jul 11, 2012, 6:51:05 PM7/11/12
to h2-da...@googlegroups.com, Alfred Reibenschuh
At that point I think we're outside the bounds of what H2 should
provide, and you should probably just implement that yourself in your
own main() function.

However, I'm happy to make some more of the AutoRegisterFunctionAliases
methods public again, if that will help?

hmm ... ok i'm sprinting ahead --- maybe a interface like:

public interface FunctionRegistry 

public Map<String,String> getRegistryMap(); 

 
should be recognized by AutoRegisterFunctionAliases like the aggregate interface.

then the would-be implementors need not public registerFunction/registerAggregate, 
but simply return a map of aliasName/classMethodName to be registered.

hope you like the attached patch.

> i also have in the queue stubs for all mysql 5.6 functions not
> implemented by h2.
>

Unless you actually have a use-case for those, I'd not be terribly keen
on cluttering up the code with them.

no you need not fear -- this will be a separated google-code project, so users may pick what they want.

C
h2db.patch

Noel Grandin

unread,
Jul 12, 2012, 4:28:45 AM7/12/12
to h2-da...@googlegroups.com, Alfred Reibenschuh
Hi Alfred

I'm sorry but this is going to have to wait until next week. There were
some unit test failures and we want to push out a release this week, so
it had to be reverted.

Regards, Noel Grandin

Alfred Reibenschuh

unread,
Jul 12, 2012, 8:50:18 AM7/12/12
to h2-da...@googlegroups.com, Alfred Reibenschuh
ack

Thomas Mueller

unread,
Oct 29, 2012, 3:07:29 PM10/29/12
to h2-da...@googlegroups.com
Hi,

It sounds like an interesting idea! What about using the "service loader" API, similar to how Java drivers are auto-loaded in Java 6. For this, a text file META-INF/services/org.h2.Extension could be used that contains the list of extension classes.

As a staring point, the class could have a method with the signature:

    public static void register(Connection conn) throws SQLException

same as in org.h2.mode.FunctionsMySQL. Probably we want to add an interface that defines this method. Function names could be defined using an annotations, but the register method might be more flexible (allows to install triggers, create tables / views,...) so I think the register method would be nice.

For extensions that are to be auto-loaded when a certain mode is used, what about org.h2.ExtensionMySQL / ExtensionPostgreSQL / ExtensionOracle and so on.

Regards,
Thomas

then:

for each $class
{
  if $class instanceof org.h2.api.AggregateFunction 
  {
    registerFunctionClass($class)
  }
  else
  {
    scan $class for methods starting with 'FN_' 
      then registerFunctionMethod($class, $method, $fname)
  }
}

for the MODE-Support already in H2 a filename like '/META-INF/org.h2.ext.FunctionClass.$MODE' could be used.

--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To view this discussion on the web visit https://groups.google.com/d/msg/h2-database/-/NXzf0yf1S8AJ.
Reply all
Reply to author
Forward
0 new messages