Skip to content

Commit cff80d8

Browse files
committed
Refactor: move method
1 parent d33e4d8 commit cff80d8

File tree

2 files changed

+83
-81
lines changed

2 files changed

+83
-81
lines changed

traindb-core/src/main/java/traindb/adapter/jdbc/TrainDBJdbcSchema.java

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.sql.DatabaseMetaData;
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
22-
import javax.annotation.Nullable;
2322
import org.apache.calcite.avatica.MetaImpl;
2423
import org.apache.calcite.avatica.SqlType;
2524
import org.apache.calcite.rel.type.RelDataType;
@@ -28,8 +27,6 @@
2827
import org.apache.calcite.schema.Table;
2928
import org.apache.calcite.sql.SqlDialect;
3029
import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
31-
import org.apache.calcite.sql.type.SqlTypeName;
32-
import org.apache.calcite.util.Util;
3330
import traindb.adapter.TrainDBSqlDialect;
3431
import traindb.common.TrainDBLogger;
3532
import traindb.schema.TrainDBSchema;
@@ -127,82 +124,4 @@ private RelDataType getProtoType(MetaImpl.MetaTable tableDef, DatabaseMetaData d
127124
return builder.build();
128125
}
129126

130-
private static RelDataType sqlType(RelDataTypeFactory typeFactory, int dataType,
131-
int precision, int scale, boolean nullable,
132-
@Nullable String typeString) {
133-
// Fall back to ANY if type is unknown
134-
final SqlTypeName sqlTypeName =
135-
Util.first(SqlTypeName.getNameForJdbcType(dataType), SqlTypeName.ANY);
136-
switch (sqlTypeName) {
137-
case ARRAY:
138-
RelDataType component = null;
139-
if (typeString != null && typeString.endsWith(" ARRAY")) {
140-
// E.g. hsqldb gives "INTEGER ARRAY", so we deduce the component type
141-
// "INTEGER".
142-
final String remaining = typeString.substring(0,
143-
typeString.length() - " ARRAY".length());
144-
component = parseTypeString(typeFactory, remaining);
145-
}
146-
if (component == null) {
147-
component = typeFactory.createTypeWithNullability(
148-
typeFactory.createSqlType(SqlTypeName.ANY), true);
149-
}
150-
return typeFactory.createArrayType(component, -1);
151-
case ANY:
152-
if (typeString.startsWith("ST_")) {
153-
return typeFactory.createTypeWithNullability(
154-
typeFactory.createSqlType(SqlTypeName.GEOMETRY), nullable);
155-
}
156-
break;
157-
default:
158-
break;
159-
}
160-
if (precision >= 0
161-
&& scale >= 0
162-
&& sqlTypeName.allowsPrecScale(true, true)) {
163-
return typeFactory.createSqlType(sqlTypeName, precision, scale);
164-
} else if (precision >= 0 && sqlTypeName.allowsPrecNoScale()) {
165-
return typeFactory.createSqlType(sqlTypeName, precision);
166-
} else {
167-
assert sqlTypeName.allowsNoPrecNoScale();
168-
return typeFactory.createSqlType(sqlTypeName);
169-
}
170-
}
171-
172-
/**
173-
* Given "INTEGER", returns BasicSqlType(INTEGER).
174-
* Given "VARCHAR(10)", returns BasicSqlType(VARCHAR, 10).
175-
* Given "NUMERIC(10, 2)", returns BasicSqlType(NUMERIC, 10, 2).
176-
*/
177-
private static RelDataType parseTypeString(RelDataTypeFactory typeFactory,
178-
String typeString) {
179-
int precision = -1;
180-
int scale = -1;
181-
int open = typeString.indexOf("(");
182-
if (open >= 0) {
183-
int close = typeString.indexOf(")", open);
184-
if (close >= 0) {
185-
String rest = typeString.substring(open + 1, close);
186-
typeString = typeString.substring(0, open);
187-
int comma = rest.indexOf(",");
188-
if (comma >= 0) {
189-
precision = Integer.parseInt(rest.substring(0, comma));
190-
scale = Integer.parseInt(rest.substring(comma));
191-
} else {
192-
precision = Integer.parseInt(rest);
193-
}
194-
}
195-
}
196-
try {
197-
final SqlTypeName typeName = SqlTypeName.valueOf(typeString);
198-
return typeName.allowsPrecScale(true, true)
199-
? typeFactory.createSqlType(typeName, precision, scale)
200-
: typeName.allowsPrecScale(true, false)
201-
? typeFactory.createSqlType(typeName, precision)
202-
: typeFactory.createSqlType(typeName);
203-
} catch (IllegalArgumentException e) {
204-
return typeFactory.createTypeWithNullability(
205-
typeFactory.createSqlType(SqlTypeName.ANY), true);
206-
}
207-
}
208127
}

traindb-core/src/main/java/traindb/schema/TrainDBSchema.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
import com.google.common.collect.ImmutableMap;
1818
import java.util.Map;
19+
import javax.annotation.Nullable;
20+
import org.apache.calcite.rel.type.RelDataType;
21+
import org.apache.calcite.rel.type.RelDataTypeFactory;
1922
import org.apache.calcite.schema.Table;
2023
import org.apache.calcite.schema.impl.AbstractSchema;
24+
import org.apache.calcite.sql.type.SqlTypeName;
25+
import org.apache.calcite.util.Util;
2126

2227
public abstract class TrainDBSchema extends AbstractSchema {
2328
private final String name;
@@ -50,4 +55,82 @@ public final void setTableMap(ImmutableMap<String, Table> tableMap) {
5055
public final TrainDBDataSource getDataSource() {
5156
return dataSource;
5257
}
58+
59+
protected static RelDataType sqlType(RelDataTypeFactory typeFactory, int dataType,
60+
int precision, int scale, boolean nullable,
61+
@Nullable String typeString) {
62+
// Fall back to ANY if type is unknown
63+
final SqlTypeName sqlTypeName =
64+
Util.first(SqlTypeName.getNameForJdbcType(dataType), SqlTypeName.ANY);
65+
switch (sqlTypeName) {
66+
case ARRAY:
67+
RelDataType component = null;
68+
if (typeString != null && typeString.endsWith(" ARRAY")) {
69+
// E.g. hsqldb gives "INTEGER ARRAY", so we deduce the component type
70+
// "INTEGER".
71+
final String remaining = typeString.substring(0,
72+
typeString.length() - " ARRAY".length());
73+
component = parseTypeString(typeFactory, remaining);
74+
}
75+
if (component == null) {
76+
component = typeFactory.createTypeWithNullability(
77+
typeFactory.createSqlType(SqlTypeName.ANY), true);
78+
}
79+
return typeFactory.createArrayType(component, -1);
80+
case ANY:
81+
if (typeString.startsWith("ST_")) {
82+
return typeFactory.createTypeWithNullability(
83+
typeFactory.createSqlType(SqlTypeName.GEOMETRY), nullable);
84+
}
85+
break;
86+
default:
87+
break;
88+
}
89+
if (precision >= 0
90+
&& scale >= 0
91+
&& sqlTypeName.allowsPrecScale(true, true)) {
92+
return typeFactory.createSqlType(sqlTypeName, precision, scale);
93+
} else if (precision >= 0 && sqlTypeName.allowsPrecNoScale()) {
94+
return typeFactory.createSqlType(sqlTypeName, precision);
95+
} else {
96+
assert sqlTypeName.allowsNoPrecNoScale();
97+
return typeFactory.createSqlType(sqlTypeName);
98+
}
99+
}
100+
101+
/**
102+
* Given "INTEGER", returns BasicSqlType(INTEGER).
103+
* Given "VARCHAR(10)", returns BasicSqlType(VARCHAR, 10).
104+
* Given "NUMERIC(10, 2)", returns BasicSqlType(NUMERIC, 10, 2).
105+
*/
106+
protected static RelDataType parseTypeString(RelDataTypeFactory typeFactory, String typeString) {
107+
int precision = -1;
108+
int scale = -1;
109+
int open = typeString.indexOf("(");
110+
if (open >= 0) {
111+
int close = typeString.indexOf(")", open);
112+
if (close >= 0) {
113+
String rest = typeString.substring(open + 1, close);
114+
typeString = typeString.substring(0, open);
115+
int comma = rest.indexOf(",");
116+
if (comma >= 0) {
117+
precision = Integer.parseInt(rest.substring(0, comma));
118+
scale = Integer.parseInt(rest.substring(comma));
119+
} else {
120+
precision = Integer.parseInt(rest);
121+
}
122+
}
123+
}
124+
try {
125+
final SqlTypeName typeName = SqlTypeName.valueOf(typeString);
126+
return typeName.allowsPrecScale(true, true)
127+
? typeFactory.createSqlType(typeName, precision, scale)
128+
: typeName.allowsPrecScale(true, false)
129+
? typeFactory.createSqlType(typeName, precision)
130+
: typeFactory.createSqlType(typeName);
131+
} catch (IllegalArgumentException e) {
132+
return typeFactory.createTypeWithNullability(
133+
typeFactory.createSqlType(SqlTypeName.ANY), true);
134+
}
135+
}
53136
}

0 commit comments

Comments
 (0)