Debian Codegraph @

src/main/org/h2/value/ValueVarchar.java

raw
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 VARYING data type.
14 */
15 public final class ValueVarchar extends ValueStringBase {
16
17 /**
18 * Empty string. Should not be used in places where empty string can be
19 * treated as {@code NULL} depending on database mode.
20 */
21 public static final ValueVarchar EMPTY = new ValueVarchar("");
22
23 private ValueVarchar(String value) {
24 super(value);
25 }
26
27 @Override
28 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
29 return StringUtils.quoteStringSQL(builder, value);
30 }
31
32 @Override
33 public int getValueType() {
34 return VARCHAR;
35 }
36
37 /**
38 * Get or create a VARCHAR value for the given string.
39 *
40 * @param s the string
41 * @return the value
42 */
43 public static Value get(String s) {
44 return get(s, null);
45 }
46
47 /**
48 * Get or create a VARCHAR value for the given string.
49 *
50 * @param s the string
51 * @param provider the cast information provider, or {@code null}
52 * @return the value
53 */
54 public static Value get(String s, CastDataProvider provider) {
55 if (s.isEmpty()) {
56 return provider != null && provider.getMode().treatEmptyStringsAsNull ? ValueNull.INSTANCE : EMPTY;
57 }
58 ValueVarchar obj = new ValueVarchar(StringUtils.cache(s));
59 if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
60 return obj;
61 }
62 return Value.cache(obj);
63 // this saves memory, but is really slow
64 // return new ValueString(s.intern());
65 }
66
67 }