Hi,

The most flexible solution is probably ";INIT=<list of SQL
statements>". The problem is that the ";" to delimit SQL statements
can't be used (unless there is some way to escape it; currently there
isn't).

There is a workaround I didn't think about first: to use the database
event listener. The problem is that there is currently no way to pass
the user and password to it, however it does work:

        url += ";DATABASE_EVENT_LISTENER='"+ Init.class.getName() + "'";
        Connection conn = DriverManager.getConnection(url, "sa", "sa");
        Statement stat = conn.createStatement();
        stat.execute("select * from test");
        conn.close();

    public class Init implements DatabaseEventListener {

        private String databaseUrl;

        public void init(String url) {
            databaseUrl = url;
        }

        public void opened() {
            try {
                Connection conn =
DriverManager.getConnection(databaseUrl, "sa", "sa");
                Statement stat = conn.createStatement();
                stat.execute("create table if not exists test(id int)");
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

       ...

Regards,
Thomas





On Tue, Feb 2, 2010 at 9:09 PM, Kerry Sainsbury <[email protected]> wrote:
> Hi Thomas,
>
> I've been quietly implementing the "INIT=" functionality and have come to a
> decision point that I'm SURE you'll want to contribute to.
>
> The "INIT=<url>" description below is very similar to the RUNSCRIPT command,
> so I came to the conclusion that I might as well just use it directly, and
> rather than "INIT=<url>" I'd support "INIT=<command>", which would in turn
> let me support the requirement with:
>
> ";INIT=RUNSCRIPT FROM <url>" (lets call this Option 1)
>
> This also fixes problems with CHARSET issues, because RUNSCRIPT handles that
> too.
>
> If you'd prefer I could just use "RUNSCRIPT" behind the scenes, and use the
> originally suggested notation, plus support for RUNSCRIPT options:
>
> ";INIT=<url> [with support for all the optional RUNSCRIPT parameters too]"
> (lets call this Option 2)
>
> or perhaps, if it's clearer:
>
> ";RUNSCRIPT=<url> [with support for all the optional RUNSCRIPT parameters
> too]" (lets call this Option 3)
>
> So, which option do you like? Or have you a 4th suggestion?
>
> Cheers
> Kerry
>
>
>
> On Tue, Dec 15, 2009 at 9:08 AM, Thomas Mueller
> <[email protected]> wrote:
>>
>> Hi,
>>
>> > The ability to auto-create schema (based on connection string) for in-
>> > memory mode would be very helpful..
>>
>> Just creating the schema may solve your specific problem, however I
>> don't want to add it, because it sounds like a hack. I will add a
>> feature request for the following feature:
>>
>> If the database URL ends with ";INIT=<url>" then the SQL script from
>> the given file or URL is executed (the user name must have admin
>> rights). Example URL: jdbc:h2:mem:test;INIT=~/init.sql
>>
>> That would allow to create schemas, tables, and data.
>>
>> However, I will not have time to implement this feature currently.
>> Patches are always welcome of course!
>>
>> Regards,
>> Thomas
>>
>> --
>>
>> You received this message because you are subscribed to the Google Groups
>> "H2 Database" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/h2-database?hl=en.
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "H2 Database" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/h2-database?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/h2-database?hl=en.

Reply via email to