我在Hsqldb中进行集成测试。我的生产数据库是Oracle数据库。
Hibernate:4.1.3.final。
Hsqldb:2.3.3。 (我不能使用2.3.4,因为它无法一键完成所有的junit测试。)
要创建我的测试数据库,我对此tutorial进行了少量修改。除了使用Oracle的TO_CHAR(整数)函数的方法外,一切正常。对于这些方法,我得到了unexpected token: )
这是导致异常的代码
select ="select p.name, to_char(p.id) "
+ " from t_player p " +
"inner join t_job job on j.id=p.id_job ";
这是我的单元测试的一部分
public void testFindPlayer() throws ClassNotFoundException, SQLException {
Class.forName("org.hsqldb.jdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:DB", "sa", "");
String syntax_ora = "SET DATABASE SQL SYNTAX ORA TRUE";
PreparedStatement ps_ora = connection.prepareStatement(syntax_ora);
ps_ora.execute();
ps_ora.close();
connection.close();
List<String[]> actual_player = Player.findPlayer("Name");
List<String[]> expected_player = new ArrayList<String[]>();
//etc..
这是例外
Caused by: org.hsqldb.HsqlException: unexpected token: )
at org.hsqldb.error.Error.parseError(Unknown Source)
at org.hsqldb.ParserBase.unexpectedToken(Unknown Source)
at org.hsqldb.ParserDQL.readExpression(Unknown Source)
at org.hsqldb.ParserDQL.readSQLFunction(Unknown Source)
at org.hsqldb.ParserDQL.readColumnOrFunctionExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadSimpleValueExpressionPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesValueExpressionPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesFactor(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesTerm(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesCommonValueExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadSelect(Unknown Source)
at org.hsqldb.ParserDQL.XreadQuerySpecification(Unknown Source)
at org.hsqldb.ParserDQL.XreadSimpleTable(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryTerm(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryExpressionBody(Unknown Source)
at org.hsqldb.ParserDQL.XreadQueryExpression(Unknown Source)
at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 41 more
1)我尝试使用Oracle的另一个函数to_char(number,'format')
。
基本上,我用以下内容更改选择部分:
select ="select p.name, to_char(p.id,'999999') "
+ " from t_player p " +
"inner join t_job job on j.id=p.id_job ";
但是,我得到了这个例外:incompatible data type in operation
2)我将SET DATABASE SQL OSYNTAX
部分放入评论
提出了相同的例外情况。
您对如何解决此问题有所了解吗?
感谢您的回复。
答案 0 :(得分:4)
HSQLDB的内置TO_CHAR函数仅支持日期和时间戳参数。
您可以为数值创建用户定义的TO_CHAR函数。例如:
CREATE FUNCTION TO_CHAR(param DECIMAL) RETURNS VARCHAR(20)
RETURN CAST(param AS VARCHAR(20))
CREATE FUNCTION TO_CHAR(param DECIMAL, format VARCHAR(20)) RETURNS VARCHAR(20)
RETURN CAST(param AS VARCHAR(20))
现在TO_CHAR(p.id)会将id作为字符串返回,而两个arge版本也会返回相同的内容。
答案 1 :(得分:0)
我使用CAST
函数有同样的错误,如下所示:
cast(id as VARCHAR)
但正确的是:
cast(id as VARCHAR(20))