Debian Codegraph @

src/main/org/h2/result/ResultWithPaddedStrings.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.result;
7
8 import java.util.Arrays;
9 import org.h2.engine.Session;
10 import org.h2.util.MathUtils;
11 import org.h2.value.TypeInfo;
12 import org.h2.value.Value;
13 import org.h2.value.ValueVarchar;
14
15 /**
16 * Result with padded fixed length strings.
17 */
18 public class ResultWithPaddedStrings implements ResultInterface {
19
20 private final ResultInterface source;
21
22 /**
23 * Returns wrapped result if necessary, or original result if it does not
24 * contain visible CHAR columns.
25 *
26 * @param source
27 * source result
28 * @return wrapped result or original result
29 */
30 public static ResultInterface get(ResultInterface source) {
31 int count = source.getVisibleColumnCount();
32 for (int i = 0; i < count; i++) {
33 if (source.getColumnType(i).getValueType() == Value.CHAR) {
34 return new ResultWithPaddedStrings(source);
35 }
36 }
37 return source;
38 }
39
40 /**
41 * Creates new instance of result.
42 *
43 * @param source
44 * the source result
45 */
46 private ResultWithPaddedStrings(ResultInterface source) {
47 this.source = source;
48 }
49
50 @Override
51 public void reset() {
52 source.reset();
53 }
54
55 @Override
56 public Value[] currentRow() {
57 int count = source.getVisibleColumnCount();
58 Value[] row = Arrays.copyOf(source.currentRow(), count);
59 for (int i = 0; i < count; i++) {
60 TypeInfo type = source.getColumnType(i);
61 if (type.getValueType() == Value.CHAR) {
62 long precision = type.getPrecision();
63 if (precision == Integer.MAX_VALUE) {
64 // CHAR is CHAR(1)
65 precision = 1;
66 }
67 String s = row[i].getString();
68 if (s != null && s.length() < precision) {
69 /*
70 * Use ValueString to avoid truncation of spaces. There is
71 * no difference between ValueStringFixed and ValueString
72 * for JDBC layer anyway.
73 */
74 row[i] = ValueVarchar.get(rightPadWithSpaces(s, MathUtils.convertLongToInt(precision)));
75 }
76 }
77 }
78 return row;
79 }
80
81 private static String rightPadWithSpaces(String s, int length) {
82 int used = s.length();
83 if (length <= used) {
84 return s;
85 }
86 char[] res = new char[length];
87 s.getChars(0, used, res, 0);
88 Arrays.fill(res, used, length, ' ');
89 return new String(res);
90 }
91
92 @Override
93 public boolean next() {
94 return source.next();
95 }
96
97 @Override
98 public long getRowId() {
99 return source.getRowId();
100 }
101
102 @Override
103 public boolean isAfterLast() {
104 return source.isAfterLast();
105 }
106
107 @Override
108 public int getVisibleColumnCount() {
109 return source.getVisibleColumnCount();
110 }
111
112 @Override
113 public long getRowCount() {
114 return source.getRowCount();
115 }
116
117 @Override
118 public boolean hasNext() {
119 return source.hasNext();
120 }
121
122 @Override
123 public boolean needToClose() {
124 return source.needToClose();
125 }
126
127 @Override
128 public void close() {
129 source.close();
130 }
131
132 @Override
133 public String getAlias(int i) {
134 return source.getAlias(i);
135 }
136
137 @Override
138 public String getSchemaName(int i) {
139 return source.getSchemaName(i);
140 }
141
142 @Override
143 public String getTableName(int i) {
144 return source.getTableName(i);
145 }
146
147 @Override
148 public String getColumnName(int i) {
149 return source.getColumnName(i);
150 }
151
152 @Override
153 public TypeInfo getColumnType(int i) {
154 return source.getColumnType(i);
155 }
156
157 @Override
158 public boolean isIdentity(int i) {
159 return source.isIdentity(i);
160 }
161
162 @Override
163 public int getNullable(int i) {
164 return source.getNullable(i);
165 }
166
167 @Override
168 public void setFetchSize(int fetchSize) {
169 source.setFetchSize(fetchSize);
170 }
171
172 @Override
173 public int getFetchSize() {
174 return source.getFetchSize();
175 }
176
177 @Override
178 public boolean isLazy() {
179 return source.isLazy();
180 }
181
182 @Override
183 public boolean isClosed() {
184 return source.isClosed();
185 }
186
187 @Override
188 public ResultInterface createShallowCopy(Session targetSession) {
189 ResultInterface copy = source.createShallowCopy(targetSession);
190 return copy != null ? new ResultWithPaddedStrings(copy) : null;
191 }
192
193 }