@Override public Views loadViews(String id) throws IOException { String q = "SELECT VIEW, LANGUAGE, TYPE, TEMPLATE, VIEWS.CREATED as CREATED, VIEWS.MODIFIED AS MODIFIED FROM VIEWS, APIS WHERE VIEWS.API = APIS.ID AND APIS.NICKNAME = ? ORDER BY VIEWS.MODIFIED DESC, VIEWS.CREATED DESC;"; Views views = new Views(); try { Class.forName("com.mysql.jdbc.Driver"); try (Connection connect = DriverManager.getConnection(jdbcUri)) { try (PreparedStatement stmt = connect.prepareStatement(q)) { stmt.setString(1, id); ResultSet rs = stmt.executeQuery(); while (rs.next()) { views.put( rs.getString("TYPE"), rs.getString("VIEW"), rs.getString("TEMPLATE"), Engine.byContentType(rs.getString("LANGUAGE"))); } } } } catch (ClassNotFoundException | SQLException e) { throw new IOException(e); } return views; }
@Override public void saveViews(String id, Views views) throws IOException { /** * FIXME This method is very inefficient... It does not harm for the moment but we should change * it. - enridaga */ try { Class.forName("com.mysql.jdbc.Driver"); try (Connection connect = DriverManager.getConnection(jdbcUri)) { connect.setAutoCommit(false); try { int dbId = _dbId(id); if (dbId == 0) { // Let's create it first dbId = _createDbId(id); } // delete views not in String d = "DELETE FROM VIEWS WHERE API = " + dbId; try (PreparedStatement stmt = connect.prepareStatement(d)) { // stmt.setInt(1, dbId); stmt.executeUpdate(); } String q = "INSERT INTO VIEWS (API, VIEW, LANGUAGE, TYPE, TEMPLATE ) VALUES (?,?,?,?,?) ON DUPLICATE " + "KEY UPDATE LANGUAGE = VALUES(LANGUAGE), TYPE = VALUES(TYPE), TEMPLATE = VALUES(TEMPLATE), MODIFIED = NOW();"; try (PreparedStatement stmt = connect.prepareStatement(q)) { for (String name : views.getNames()) { stmt.setInt(1, dbId); stmt.setString(2, name); View v = views.byName(name); stmt.setString(3, v.getEngine().getContentType()); stmt.setString(4, v.getMimeType()); stmt.setString(5, v.getTemplate()); stmt.executeUpdate(); } } connect.commit(); } catch (IOException e) { connect.rollback(); } finally { connect.setAutoCommit(true); } } } catch (ClassNotFoundException | SQLException e) { throw new IOException(e); } }