| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.value; |
| 7 |
|
| 8 |
import org.h2.engine.CastDataProvider; |
| 9 |
import org.h2.engine.SysProperties; |
| 10 |
import org.h2.util.StringUtils; |
| 11 |
|
| 12 |
/** |
| 13 |
* Implementation of the CHARACTER data type. |
| 14 |
*/ |
| 15 |
public final class ValueChar extends ValueStringBase { |
| 16 |
|
| 17 |
private ValueChar(String value) { |
| 18 |
super(value); |
| 19 |
} |
| 20 |
|
| 21 |
@Override |
| 22 |
public int getValueType() { |
| 23 |
return CHAR; |
| 24 |
} |
| 25 |
|
| 26 |
@Override |
| 27 |
public int compareTypeSafe(Value v, CompareMode mode, CastDataProvider provider) { |
| 28 |
return mode.compareString(convertToChar().getString(), v.convertToChar().getString(), false); |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 33 |
if ((sqlFlags & NO_CASTS) == 0) { |
| 34 |
int length = value.length(); |
| 35 |
return StringUtils.quoteStringSQL(builder.append("CAST("), value).append(" AS CHAR(") |
| 36 |
.append(length > 0 ? length : 1).append("))"); |
| 37 |
} |
| 38 |
return StringUtils.quoteStringSQL(builder, value); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get or create a CHAR value for the given string. |
| 43 |
* |
| 44 |
* @param s the string |
| 45 |
* @return the value |
| 46 |
*/ |
| 47 |
public static ValueChar get(String s) { |
| 48 |
ValueChar obj = new ValueChar(StringUtils.cache(s)); |
| 49 |
if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) { |
| 50 |
return obj; |
| 51 |
} |
| 52 |
return (ValueChar) Value.cache(obj); |
| 53 |
} |
| 54 |
|
| 55 |
} |