private static JSONObject resultSetToJsonObject(final ResultSet resultSet, final boolean ifOnlyOne, final String tableName)
            throws SQLException, JSONException, RepositoryException {
        final ResultSetMetaData resultSetMetaData = resultSet.getMetaData();

        final List<FieldDefinition> definitionList = JdbcRepositories.getKeys(tableName);
        if (null == definitionList) {
            LOGGER.log(Level.ERROR, "resultSetToJsonObject: null definitionList finded for table  {0}", tableName);
            throw new RepositoryException("resultSetToJsonObject: null definitionList finded for table  " + tableName);
        }

        final Map<String, FieldDefinition> dMap = new HashMap<>();
        for (FieldDefinition fieldDefinition : definitionList) {
            if (Latkes.RuntimeDatabase.H2 == Latkes.getRuntimeDatabase() || Latkes.RuntimeDatabase.ORACLE == Latkes.getRuntimeDatabase()) {
                dMap.put(fieldDefinition.getName().toUpperCase(), fieldDefinition);
            } else {
                dMap.put(fieldDefinition.getName(), fieldDefinition);
            }
        }

        final int numColumns = resultSetMetaData.getColumnCount();

        final JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject;
        String columnName;

        while (resultSet.next()) {
            jsonObject = new JSONObject();

            for (int i = 1; i < numColumns + 1; i++) {
                columnName = resultSetMetaData.getColumnName(i);

                final FieldDefinition definition = dMap.get(columnName);

                if (null == definition) { // COUNT(OID)
                    jsonObject.put(columnName, resultSet.getObject(columnName));
                } else if ("boolean".equals(definition.getType())) {
                    jsonObject.put(definition.getName(), resultSet.getBoolean(columnName));
                } else {
                    final Object v = resultSet.getObject(columnName);
                    if (v instanceof Clob) {
                        final Clob clob = (Clob) v;
                        String str = null;
                        try {
                            str = IOUtils.toString(clob.getCharacterStream());
                        } catch (final IOException e) {
                            LOGGER.log(Level.ERROR,
                                    "Cant not read column[name=" + columnName + "] in table[name=" + tableName + "] on H2", e);
                        } finally {
                            try {
                                clob.free();
                            } catch (final Exception e) { // Some drivers dose not implement free(), for example, jtds
                                LOGGER.log(Level.ERROR, "clob.free error", e);
                            }
                        }

                        jsonObject.put(definition.getName(), str);
                    } else {
                        jsonObject.put(definition.getName(), v);
                    }
                }
            }

            if (Latkes.RuntimeDatabase.ORACLE == Latkes.getRuntimeDatabase()) {
                jsonObject.remove("R__");
                fromOracleClobEmpty(jsonObject);
            }

            jsonArray.put(jsonObject);
        }

        if (ifOnlyOne) {
            if (jsonArray.length() > 0) {
                jsonObject = jsonArray.getJSONObject(0);
                return jsonObject;
            }

            return null;
        }

        jsonObject = new JSONObject();
        jsonObject.put(Keys.RESULTS, jsonArray);

        return jsonObject;
    }
private static JSONObject resultSetToJsonObject ( final ResultSet resultSet , final boolean ifOnlyOne , final String tableName ) throws SQLException , JSONException , RepositoryException { final ResultSetMetaData resultSetMetaData = resultSet . getMetaData ( ) ; final List < FieldDefinition > definitionList = JdbcRepositories . getKeys ( tableName ) ; if ( null == definitionList ) { LOGGER . log ( Level . ERROR , "resultSetToJsonObject: null definitionList finded for table  {0}" , tableName ) ; throw new RepositoryException ( "resultSetToJsonObject: null definitionList finded for table  " + tableName ) ; } final Map < String , FieldDefinition > dMap = new HashMap < > ( ) ; for ( FieldDefinition fieldDefinition : definitionList ) { if ( Latkes . RuntimeDatabase . H2 == Latkes . getRuntimeDatabase ( ) || Latkes . RuntimeDatabase . ORACLE == Latkes . getRuntimeDatabase ( ) ) { dMap . put ( fieldDefinition . getName ( ) . toUpperCase ( ) , fieldDefinition ) ; } else { dMap . put ( fieldDefinition . getName ( ) , fieldDefinition ) ; } } final int numColumns = resultSetMetaData . getColumnCount ( ) ; final JSONArray jsonArray = new JSONArray ( ) ; JSONObject jsonObject ; String columnName ; while ( resultSet . next ( ) ) { jsonObject = new JSONObject ( ) ; for ( int i = 1 ; i < numColumns + 1 ; i ++ ) { columnName = resultSetMetaData . getColumnName ( i ) ; final FieldDefinition definition = dMap . get ( columnName ) ; if ( null == definition ) { jsonObject . put ( columnName , resultSet . getObject ( columnName ) ) ; } else if ( "boolean" . equals ( definition . getType ( ) ) ) { jsonObject . put ( definition . getName ( ) , resultSet . getBoolean ( columnName ) ) ; } else { final Object v = resultSet . getObject ( columnName ) ; if ( v instanceof Clob ) { final Clob clob = ( Clob ) v ; String str = null ; try { str = IOUtils . toString ( clob . getCharacterStream ( ) ) ; } catch ( final IOException e ) { LOGGER . log ( Level . ERROR , "Cant not read column[name=" + columnName + "] in table[name=" + tableName + "] on H2" , e ) ; } finally { try { clob . free ( ) ; } catch ( final Exception e ) { LOGGER . log ( Level . ERROR , "clob.free error" , e ) ; } } jsonObject . put ( definition . getName ( ) , str ) ; } else { jsonObject . put ( definition . getName ( ) , v ) ; } } } if ( Latkes . RuntimeDatabase . ORACLE == Latkes . getRuntimeDatabase ( ) ) { jsonObject . remove ( "R__" ) ; fromOracleClobEmpty ( jsonObject ) ; } jsonArray . put ( jsonObject ) ; } if ( ifOnlyOne ) { if ( jsonArray . length ( ) > 0 ) { jsonObject = jsonArray . getJSONObject ( 0 ) ; return jsonObject ; } return null ; } jsonObject = new JSONObject ( ) ; jsonObject . put ( Keys . RESULTS , jsonArray ) ; return jsonObject ; }
resultSetToJsonObject.

@param resultSet resultSet
@param ifOnlyOne ifOnlyOne
@param tableName tableName
@return JSONObject
@throws SQLException        SQLException
@throws JSONException       JSONException
@throws RepositoryException RepositoryException