Question

I'm at my wit's end trying to test a groovy sql query. The idea is to use groovy to execute a sql query that will pull some data from a legacy MS SQL database and return it in a format that my application can use. Writing the groovy script was simple enough but testing it is another matter. Currently I'm using hypersonic to create an in-memory db for testing. When my test runs, I create a table and attempt to populate it with some test data from an xml file. The table creation works fine, but the data insertion fails with: "user lacks privilege or object not found: GROUP".

The problem seems to be that hsql is converting all my column names to uppercase. So even though the actual column name is "Group" it is being converted to "GROUP" for the insert statement. GROUP conflicts with the group keyword so the query fails.

I have no control over the column name in the legacy DB so I need some sort of workaround. Is there any way to configure hsql to prevent the column name conversion?

Update: Here is some example code that shows what I am trying to do in groovy:

First I have an initialization method that creates a table in the test DB

Sql remoteDB;
...
remoteDB.execute(
                """create table INFO_GROUP(
"Group" varchar(2) not null primary key,
VALUE varchar(3) null,
GROUP_DATE datetime null,
COMMENT varchar(25) null
)""");

Once the table is created, I parse some data from an xml file and inject it into the table I created:

groovy.sql.DataSet set = remoteDB.dataSet(tableName);               
Node n = it;
Map<String,Object> m = n.attributes() ;
assert(set!=null);
set.add(m);

The issue I'm seeing is that last line where I call set.add(m). I get an error:

Failed to execute: insert into INFO_GROUP (Group, VALUE, GROUP_DATE, COMMENT) values (?, ?, ?, ?) because: user lacks privilege or object not found: GROUP

Was it helpful?

Solution

HSQLDB preserves the "Group" case when it is double-quoted. The INSERT statement must double-quote the "Group" column name, which it is not doing in this case.

create table INFO_GROUP("Group" varchar(2) not null primary key, VALUE varchar(3) null,
    GROUP_DATE datetime null, COMMENT varchar(25) null)

insert into INFO_GROUP ("Group", VALUE, GROUP_DATE, COMMENT) values ? , ? , ? ,?

It turned out that the word Group has to be used with double quotes "Group" in the map in Groovy in order to generate the correct INSERT statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top