示例#1
0
 @Override
 public SourceFormat getSourceFormat(Identifier identifier) throws IOException {
   try {
     // JdbcResolver.function.media_type may contain a JavaScript
     // function or null.
     String functionResult = executeGetMediaType(identifier);
     MediaType mediaType = null;
     if (functionResult != null) {
       // the function result may be a media type, or an SQL
       // statement to look it up.
       if (functionResult.toUpperCase().contains("SELECT")
           && functionResult.toUpperCase().contains("FROM")) {
         logger.debug(functionResult);
         try (Connection connection = getConnection()) {
           PreparedStatement statement = connection.prepareStatement(functionResult);
           statement.setString(1, executeGetDatabaseIdentifier(identifier));
           ResultSet resultSet = statement.executeQuery();
           if (resultSet.next()) {
             mediaType = new MediaType(resultSet.getString(1));
           }
         }
       } else {
         mediaType = new MediaType(functionResult);
       }
     } else {
       mediaType = SourceFormat.getSourceFormat(identifier).getPreferredMediaType();
     }
     return SourceFormat.getSourceFormat(mediaType);
   } catch (ScriptException | SQLException e) {
     throw new IOException(e.getMessage(), e);
   }
 }
示例#2
0
  @Override
  public StreamSource getStreamSource(Identifier identifier) throws IOException {
    try (Connection connection = getConnection()) {
      Configuration config = Application.getConfiguration();
      String sql = config.getString(LOOKUP_SQL_CONFIG_KEY);
      if (!sql.contains("?")) {
        throw new IOException(LOOKUP_SQL_CONFIG_KEY + " does not support prepared statements");
      }
      logger.debug(sql);

      PreparedStatement statement = connection.prepareStatement(sql);
      statement.setString(1, executeGetDatabaseIdentifier(identifier));
      ResultSet result = statement.executeQuery();
      if (result.next()) {
        return new JdbcStreamSource(result, 1);
      }
    } catch (ScriptException | SQLException e) {
      throw new IOException(e.getMessage(), e);
    }
    throw new FileNotFoundException();
  }