下面列出了怎么用org.hibernate.type.LiteralType的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
@SuppressWarnings( {"unchecked"})
public String getRenderText(SessionFactoryImplementor sessionFactory) {
final boolean literalValue = getValue();
if ( expectedType instanceof AttributeConverterTypeAdapter ) {
return determineConvertedValue( (AttributeConverterTypeAdapter) expectedType, literalValue );
}
else if ( expectedType instanceof LiteralType ) {
try {
return ( (LiteralType) expectedType ).objectToSQLString( getValue(), sessionFactory.getDialect() );
}
catch( Exception t ) {
throw new QueryException( "Unable to render boolean literal value using expected LiteralType", t );
}
}
return sessionFactory.getDialect().toBooleanValueString( literalValue );
}
private String quoteIdentifier(Object value, Type type) {
Type resolvedType = ( !type.getReturnedClass().equals( value.getClass() ) ) ?
typeResolver.heuristicType( value.getClass().getName() ) : type;
if ( resolvedType instanceof LiteralType ) {
LiteralType literalType = (LiteralType) resolvedType;
try {
return literalType.objectToSQLString( value, dialect );
}
catch ( Exception e ) {
throw new IllegalArgumentException( e );
}
}
return String.valueOf( value );
}
private String resolveToLiteralString(Type type) {
try {
LiteralType literalType = ( LiteralType ) type;
Dialect dialect = factory.getDialect();
return literalType.objectToSQLString( constantValue, dialect );
}
catch ( Throwable t ) {
throw new QueryException( QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + constantExpression, t );
}
}
public Insert addColumn(String columnName, Object value, LiteralType type) throws Exception {
return addColumn( columnName, type.objectToSQLString(value, dialect) );
}
public Update addColumn(String columnName, Object value, LiteralType type) throws Exception {
return addColumn( columnName, type.objectToSQLString(value, dialect) );
}
@Override
@SuppressWarnings("unchecked")
public String getRenderText(SessionFactoryImplementor sessionFactory) {
final Type type = expectedType == null
? heuristicType
: Number.class.isAssignableFrom( heuristicType.getReturnedClass() )
? heuristicType
: expectedType;
try {
if ( LiteralType.class.isInstance( type ) ) {
final LiteralType literalType = (LiteralType) type;
final Dialect dialect = factory.getDialect();
return literalType.objectToSQLString( constantValue, dialect );
}
else if ( AttributeConverterTypeAdapter.class.isInstance( type ) ) {
final AttributeConverterTypeAdapter converterType = (AttributeConverterTypeAdapter) type;
if ( !converterType.getModelType().isInstance( constantValue ) ) {
throw new QueryException(
String.format(
Locale.ENGLISH,
"Recognized query constant expression [%s] was not resolved to type [%s] expected by defined AttributeConverter [%s]",
constantExpression,
constantValue.getClass().getName(),
converterType.getModelType().getName()
)
);
}
final Object value = converterType.getAttributeConverter().toRelationalValue( constantValue );
if ( String.class.equals( converterType.getJdbcType() ) ) {
return "'" + value + "'";
}
else {
return value.toString();
}
}
else {
throw new QueryException(
String.format(
Locale.ENGLISH,
"Unrecognized Hibernate Type for handling query constant (%s); expecting LiteralType implementation or AttributeConverter",
constantExpression
)
);
}
}
catch (QueryException e) {
throw e;
}
catch (Exception t) {
throw new QueryException( QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + constantExpression, t );
}
}
private void setConstantValue(DotNode node, String text, Object value) {
if ( LOG.isDebugEnabled() ) {
LOG.debugf( "setConstantValue() %s -> %s %s", text, value, value.getClass().getName() );
}
// Chop off the rest of the tree.
node.setFirstChild( null );
if ( value instanceof String ) {
node.setType( SqlTokenTypes.QUOTED_STRING );
}
else if ( value instanceof Character ) {
node.setType( SqlTokenTypes.QUOTED_STRING );
}
else if ( value instanceof Byte ) {
node.setType( SqlTokenTypes.NUM_INT );
}
else if ( value instanceof Short ) {
node.setType( SqlTokenTypes.NUM_INT );
}
else if ( value instanceof Integer ) {
node.setType( SqlTokenTypes.NUM_INT );
}
else if ( value instanceof Long ) {
node.setType( SqlTokenTypes.NUM_LONG );
}
else if ( value instanceof Double ) {
node.setType( SqlTokenTypes.NUM_DOUBLE );
}
else if ( value instanceof Float ) {
node.setType( SqlTokenTypes.NUM_FLOAT );
}
else {
node.setType( SqlTokenTypes.CONSTANT );
}
Type type;
try {
type = walker.getSessionFactoryHelper().getFactory().getTypeResolver().heuristicType(
value.getClass().getName()
);
}
catch (MappingException me) {
throw new QueryException( me );
}
if ( type == null ) {
throw new QueryException( QueryTranslator.ERROR_CANNOT_DETERMINE_TYPE + node.getText() );
}
try {
LiteralType literalType = (LiteralType) type;
Dialect dialect = walker.getSessionFactoryHelper().getFactory().getDialect();
//noinspection unchecked
node.setText( literalType.objectToSQLString( value, dialect ) );
}
catch (Exception e) {
throw new QueryException( QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + node.getText(), e );
}
node.setDataType( type );
node.setResolvedConstant( text );
}
public Insert addColumn(String columnName, Object value, LiteralType type) throws Exception {
return addColumn( columnName, type.objectToSQLString(value, dialect) );
}
public Update addColumn(String columnName, Object value, LiteralType type) throws Exception {
return addColumn( columnName, type.objectToSQLString(value, dialect) );
}
private void setConstantValue(DotNode node, String text, Object value) {
if ( log.isDebugEnabled() ) {
log.debug( "setConstantValue() " + text + " -> " + value + " " + value.getClass().getName() );
}
node.setFirstChild( null ); // Chop off the rest of the tree.
if ( value instanceof String ) {
node.setType( SqlTokenTypes.QUOTED_STRING );
}
else if ( value instanceof Character ) {
node.setType( SqlTokenTypes.QUOTED_STRING );
}
else if ( value instanceof Byte ) {
node.setType( SqlTokenTypes.NUM_INT );
}
else if ( value instanceof Short ) {
node.setType( SqlTokenTypes.NUM_INT );
}
else if ( value instanceof Integer ) {
node.setType( SqlTokenTypes.NUM_INT );
}
else if ( value instanceof Long ) {
node.setType( SqlTokenTypes.NUM_LONG );
}
else if ( value instanceof Double ) {
node.setType( SqlTokenTypes.NUM_DOUBLE );
}
else if ( value instanceof Float ) {
node.setType( SqlTokenTypes.NUM_FLOAT );
}
else {
node.setType( SqlTokenTypes.CONSTANT );
}
Type type;
try {
type = TypeFactory.heuristicType( value.getClass().getName() );
}
catch ( MappingException me ) {
throw new QueryException( me );
}
if ( type == null ) {
throw new QueryException( QueryTranslator.ERROR_CANNOT_DETERMINE_TYPE + node.getText() );
}
try {
LiteralType literalType = ( LiteralType ) type;
Dialect dialect = walker.getSessionFactoryHelper().getFactory().getDialect();
node.setText( literalType.objectToSQLString( value, dialect ) );
}
catch ( Exception e ) {
throw new QueryException( QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + node.getText(), e );
}
node.setDataType( type );
node.setResolvedConstant( text );
}
private void doToken(String token, QueryTranslatorImpl q) throws QueryException {
if ( q.isName( StringHelper.root( token ) ) ) { //path expression
doPathExpression( q.unalias( token ), q );
}
else if ( token.startsWith( ParserHelper.HQL_VARIABLE_PREFIX ) ) { //named query parameter
q.addNamedParameter( token.substring( 1 ) );
appendToken( q, "?" );
}
else {
Queryable persister = q.getEntityPersisterUsingImports( token );
if ( persister != null ) { // the name of a class
final String discrim = persister.getDiscriminatorSQLValue();
if ( InFragment.NULL.equals(discrim) || InFragment.NOT_NULL.equals(discrim) ) {
throw new QueryException( "subclass test not allowed for null or not null discriminator" );
}
else {
appendToken( q, discrim );
}
}
else {
Object constant;
if (
token.indexOf( '.' ) > -1 &&
( constant = ReflectHelper.getConstantValue( token ) ) != null
) {
Type type;
try {
type = TypeFactory.heuristicType( constant.getClass().getName() );
}
catch ( MappingException me ) {
throw new QueryException( me );
}
if ( type == null ) throw new QueryException( QueryTranslator.ERROR_CANNOT_DETERMINE_TYPE + token );
try {
appendToken( q, ( ( LiteralType ) type ).objectToSQLString( constant, q.getFactory().getDialect() ) );
}
catch ( Exception e ) {
throw new QueryException( QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + token, e );
}
}
else { //anything else
String negatedToken = negated ? ( String ) NEGATIONS.get( token.toLowerCase() ) : null;
if ( negatedToken != null && ( !betweenSpecialCase || !"or".equals( negatedToken ) ) ) {
appendToken( q, negatedToken );
}
else {
appendToken( q, token );
}
}
}
}
}