Skip to content

Commit 3f09a2e

Browse files
committed
Updated logic used to process duplicate column names in the getJson() method in DbUtils
1 parent aac25fd commit 3f09a2e

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/javaxt/express/utils/DbUtils.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ public static boolean initSchema(Database database, String schema, String tableS
118118
try (Connection conn = database.getConnection()){
119119
conn.execute("CREATE domain IF NOT EXISTS text AS varchar");
120120
conn.execute("CREATE domain IF NOT EXISTS jsonb AS varchar");
121+
122+
/*
123+
//If H2 GIS is not present, we need something like this for
124+
//models with geometry types
125+
conn.execute("CREATE ALIAS ST_AsText AS '\n" +
126+
"String geomAsText(org.h2.value.Value value) {\n" +
127+
" return value.toString();\n" +
128+
"}\n" +
129+
"';");
130+
*/
131+
132+
121133
schemaInitialized = initSchema(arr, conn);
122134
}
123135
catch(Exception e){
@@ -1172,22 +1184,32 @@ private void executeBatch(ArrayList<String> stmts, Connection conn){
11721184
//**************************************************************************
11731185
//** getJson
11741186
//**************************************************************************
1175-
/** Returns a JSON representation of a record in a Recordset. Note that the
1176-
* column names are represented using camel case.
1187+
/** Returns a JSON representation of a record in a Recordset. Column names
1188+
* are used as keys and the corresponding value is used as the value.
1189+
* Note that the column names are represented using camel case. If there
1190+
* are duplicate column names in the Recordset (e.g. "select user.id,
1191+
* contact.id from ...") the first column name and value is used (e.g.
1192+
* "user.id" column).
11771193
*/
11781194
public static JSONObject getJson(Recordset rs){
11791195
return getJson(rs.getFields());
11801196
}
1197+
11811198
public static JSONObject getJson(javaxt.sql.Record record){
11821199
return getJson(record.getFields());
11831200
}
1201+
11841202
public static JSONObject getJson(javaxt.sql.Field[] fields){
11851203
JSONObject json = new JSONObject();
1204+
HashSet<String> fieldNames = new HashSet<>();
11861205
for (javaxt.sql.Field field : fields){
11871206

11881207
String fieldName = field.getName().toLowerCase();
11891208
fieldName = StringUtils.underscoreToCamelCase(fieldName);
11901209

1210+
if (fieldNames.contains(fieldName)) continue;
1211+
fieldNames.add(fieldName);
1212+
11911213
JSONObject f = field.toJson();
11921214
json.set(fieldName, f.get("value"));
11931215
}

0 commit comments

Comments
 (0)