1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.value; |
7 | |
8 | import java.sql.PreparedStatement; |
9 | import java.sql.SQLException; |
10 | |
11 | import org.h2.engine.SysProperties; |
12 | import org.h2.util.MathUtils; |
13 | import org.h2.util.StringUtils; |
14 | |
15 | /** |
16 | * Implementation of the VARCHAR data type. |
17 | * It is also the base class for other ValueString* classes. |
18 | */ |
19 | public class ValueString extends Value { |
20 | |
21 | private static final ValueString EMPTY = new ValueString(""); |
22 | |
23 | /** |
24 | * The string data. |
25 | */ |
26 | protected final String value; |
27 | |
28 | protected ValueString(String value) { |
29 | this.value = value; |
30 | } |
31 | |
32 | @Override |
33 | public String getSQL() { |
34 | return StringUtils.quoteStringSQL(value); |
35 | } |
36 | |
37 | @Override |
38 | public boolean equals(Object other) { |
39 | return other instanceof ValueString |
40 | && value.equals(((ValueString) other).value); |
41 | } |
42 | |
43 | @Override |
44 | protected int compareSecure(Value o, CompareMode mode) { |
45 | // compatibility: the other object could be another type |
46 | ValueString v = (ValueString) o; |
47 | return mode.compareString(value, v.value, false); |
48 | } |
49 | |
50 | @Override |
51 | public String getString() { |
52 | return value; |
53 | } |
54 | |
55 | @Override |
56 | public long getPrecision() { |
57 | return value.length(); |
58 | } |
59 | |
60 | @Override |
61 | public Object getObject() { |
62 | return value; |
63 | } |
64 | |
65 | @Override |
66 | public void set(PreparedStatement prep, int parameterIndex) |
67 | throws SQLException { |
68 | prep.setString(parameterIndex, value); |
69 | } |
70 | |
71 | @Override |
72 | public int getDisplaySize() { |
73 | return value.length(); |
74 | } |
75 | |
76 | @Override |
77 | public int getMemory() { |
78 | return value.length() * 2 + 48; |
79 | } |
80 | |
81 | @Override |
82 | public Value convertPrecision(long precision, boolean force) { |
83 | if (precision == 0 || value.length() <= precision) { |
84 | return this; |
85 | } |
86 | int p = MathUtils.convertLongToInt(precision); |
87 | return getNew(value.substring(0, p)); |
88 | } |
89 | |
90 | @Override |
91 | public int hashCode() { |
92 | // TODO hash performance: could build a quicker hash |
93 | // by hashing the size and a few characters |
94 | return value.hashCode(); |
95 | |
96 | // proposed code: |
97 | // private int hash = 0; |
98 | // |
99 | // public int hashCode() { |
100 | // int h = hash; |
101 | // if (h == 0) { |
102 | // String s = value; |
103 | // int l = s.length(); |
104 | // if (l > 0) { |
105 | // if (l < 16) |
106 | // h = s.hashCode(); |
107 | // else { |
108 | // h = l; |
109 | // for (int i = 1; i <= l; i <<= 1) |
110 | // h = 31 * |
111 | // (31 * h + s.charAt(i - 1)) + |
112 | // s.charAt(l - i); |
113 | // } |
114 | // hash = h; |
115 | // } |
116 | // } |
117 | // return h; |
118 | // } |
119 | |
120 | } |
121 | |
122 | @Override |
123 | public int getType() { |
124 | return Value.STRING; |
125 | } |
126 | |
127 | /** |
128 | * Get or create a string value for the given string. |
129 | * |
130 | * @param s the string |
131 | * @return the value |
132 | */ |
133 | public static Value get(String s) { |
134 | return get(s, false); |
135 | } |
136 | |
137 | /** |
138 | * Get or create a string value for the given string. |
139 | * |
140 | * @param s the string |
141 | * @param treatEmptyStringsAsNull whether or not to treat empty strings as |
142 | * NULL |
143 | * @return the value |
144 | */ |
145 | public static Value get(String s, boolean treatEmptyStringsAsNull) { |
146 | if (s.isEmpty()) { |
147 | return treatEmptyStringsAsNull ? ValueNull.INSTANCE : EMPTY; |
148 | } |
149 | ValueString obj = new ValueString(StringUtils.cache(s)); |
150 | if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) { |
151 | return obj; |
152 | } |
153 | return Value.cache(obj); |
154 | // this saves memory, but is really slow |
155 | // return new ValueString(s.intern()); |
156 | } |
157 | |
158 | /** |
159 | * Create a new String value of the current class. |
160 | * This method is meant to be overridden by subclasses. |
161 | * |
162 | * @param s the string |
163 | * @return the value |
164 | */ |
165 | protected Value getNew(String s) { |
166 | return ValueString.get(s); |
167 | } |
168 | |
169 | } |