Illegal query with FROM and tables starting with a number

50 views
Skip to first unread message

Andre Schemschat

unread,
May 4, 2015, 7:51:07 AM5/4/15
to quer...@googlegroups.com
Hi everybody,

i'm currently using QueryDSL to wrap a dynamic database access in one of our projects. Now i stumbled upon a little problem with a table named `1phoenix2` in a MS-SQL 2014 Server. The following code generates the sql-select-query:

SQLTemplates template = SQLServer2008Templates.builder().printSchema().build();
final Configuration configuration = new Configuration(template);
configuration
.setUseLiterals(true);
final SimplePath<Object> table = Expressions.path(Object.class, "1phoenix2");
final StringPath column = Expressions.stringPath(table, "aColumn");
final SQLQuery sqlQuery = new SQLQuery(configuration).from(table);
String query = sqlQuery.getSQL(column).getSQL();
System.out.println(query); => select 1phoenix2.aColumn from 1phoenix2


If i fire the generated query against my MS-SQL i get the error "Msg 102, Level 15, State 1, Line 2 - Incorrect syntax near '1'. ". If i manually escape the query (...from "1phoenix2") it works. 

I found the relevant part in the SQLTemplate-Class (at least i think so) and the simplest solution seems to be to just subclass the mssql-template and add another constraint, but i wanted to check if i missed something or there is a more elegant way first. 

class SQLTemplates {
 
....

 
protected boolean requiresQuotes(final String identifier, final boolean precededByDot) {
   
if (NON_UNDERSCORE_ALPHA_NUMERIC.matchesAnyOf(identifier)) {
       
return true;
   
} else if (precededByDot && supportsUnquotedReservedWordsAsIdentifier) {
       
return false;
   
} else {
       
return isReservedWord(identifier);
    }
}


Thanks in advance and thanks for the great library :)


Greetings,
Andre

timowest

unread,
May 5, 2015, 3:49:30 PM5/5/15
to quer...@googlegroups.com
Hi.

Could you provide the generated SQL?

Br,
Timo

Andre Schemschat

unread,
May 6, 2015, 3:46:48 AM5/6/15
to quer...@googlegroups.com
Hi,

sorry, i added the generated query after the System.out.println in the first code-block, but that was indeed a bit hidden. The generated query is

select 1phoenix2.aColumn from 1phoenix2


Bye

Andre Schemschat

unread,
May 6, 2015, 5:07:08 AM5/6/15
to quer...@googlegroups.com
And, as i also forgot that :p, i'm using the current version 3.6.3 

timowest

unread,
May 6, 2015, 4:26:06 PM5/6/15
to quer...@googlegroups.com

Andre Schemschat

unread,
May 7, 2015, 1:49:37 AM5/7/15
to quer...@googlegroups.com
Ok, thanks :)
I wil subclass my sql template for the time beeing 
Reply all
Reply to author
Forward
0 new messages