开发者

Problems with escaping table and field names in Derby and Hsqldb

I'm having a problem with my ORMLite package. When I generate the schema for a table, I thought it would be a good practice to escape all entity names. This would protect some Java class or field name from being a SQL reserved word:

CREATE TABLE "footable" ("stuff" VARCHAR(255))

I'm now adding "raw" query support so that ORMLite can help users perform their own queries. However, I find that with Derby and Hsqldb, the entity names cannot be used wi开发者_高级运维thout escaping. For example, the following query:

SELECT * FROM footable

generates the following errors:

Derby: ERROR 42X05: Table/View 'FOOTABLE' does not exist.
Hsqldb: Table not found in statement [select * from footable]

It works fine if the select table is also escaped as "footable". The other databases supported by ORMLite work fine with or without the escaping: MySQL, Postgres, Microsoft SQL Server, H2, and Sqlite.

Are there better ways to escape reserved words in Derby and Hsqldb? Other ideas about how to do this in a portable manner?

Thanks.


So kudos to Bryan for leading me down the path although his answer wasn't quite right.

Turns out that because I am creating the database as "footable" then, as Bryan states, it will be create case sensitively. However, when I did the select on footable (without quotes) Derby and Hsqldb are promoting it to be all uppercase so I'm in effect doing:

SELECT * FROM FOOTABLE

It's not about being case insensitive without the quotes (which would have worked) but about promoting the entity names to be all capitals when there are no quotes and then matching by case. I'd argue there was a bug here...

In any case, I've changed my Derby and Hsqldb to capitalize all entity names in ORMLite and things are working. Ugly IMO, but working.


You just have to make sure that the case matches.

So if it's:

create table "Footable" ("Stuff" varchar (25))

Then it has to be:

insert into "Footable" ("Stuff") values 'hi mom'

If the table/column name is in double quotes, the case is preserved as is.

If the table/column name is not in double quotes, then Derby handles it insensitive to case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜