/** {@inheritDoc} */ @Override public DbStatement createStatement(DbPrepared dbPrepared) { return new JdbcStatement(this, dbPrepared); }
@Override public void close() { closePreparedSth(); if (sth == null) return; try { if (sth!=null && !sth.isClosed()) { sth.close(); } } catch (Exception e) { log().i(e); } sth = null; }
/** * Return the result of an select query. * * @param attributes * @return x * @throws Exception */ @Override public DbResult executeQuery(Map<String, Object> attributes) throws Exception { validateSth(); String query = this.query.execute(attributes); log().t(query); preparedSth = prepareStatement(attributes, sth, query); long start = System.currentTimeMillis(); try { ResultSet result = preparedSth == null ? sth.executeQuery(query) : preparedSth.executeQuery(); SqlAnalytics.trace(getConnection().getInstanceId(), original, query, start, null); return new JdbcResult(this, result ); } catch (Throwable t) { SqlAnalytics.trace(getConnection().getInstanceId(), original, query, start, t); log().e(query); throw t; } }
protected void closePreparedSth() { if (preparedSth != null) { xquery = null; try { preparedSth.close(); } catch (SQLException e) { log().t(e); } preparedSth = null; } }
protected PreparedStatement prepareStatement(Map<String, Object> attributes, Statement sth, String query ) throws SQLException { // recycle prepared query - should not differ ! if (xquery != null & preparedSth != null && xquery.equals(query)) return preparedSth; // if differ close last prepared query closePreparedSth(); // checkout new if (attributes != null && attributes.containsKey( RETURN_BINARY_KEY + "0")) { PreparedStatement psth = dbCon.getConnection().prepareStatement(query); xquery = query; for (int nr = 0; attributes.containsKey(RETURN_BINARY_KEY + nr); nr++) { psth.setBinaryStream(nr+1, (InputStream) attributes.get(RETURN_BINARY_KEY + nr)); attributes.remove(RETURN_BINARY_KEY + nr); } return psth; } return null; }
/** * Return the result of an update query. In the attributes InputStreams are allowed (blobs). * * @param attributes * @return x * @throws Exception */ @Override public int executeUpdate(Map<String, Object> attributes) throws Exception { validateSth(); String query = this.query.execute(attributes); log().t(query); preparedSth = prepareStatement(attributes, sth, query); long start = System.currentTimeMillis(); try { int result = preparedSth == null ? sth.executeUpdate(query) : preparedSth.executeUpdate(); SqlAnalytics.trace(getConnection().getInstanceId(), original, query, start, null); return result; } catch (Throwable t) { SqlAnalytics.trace(getConnection().getInstanceId(), original, query, start, t); log().e(query); throw t; } }
/** * Executes the given SQL statement, which may return multiple results. In this statement * InputStream as attribute values are allowed. * * @See Statement.execute * @param attributes * @return x * @throws Exception */ @Override public boolean execute(Map<String, Object> attributes) throws Exception { validateSth(); String query = this.query.execute(attributes); log().t(query); long start = System.currentTimeMillis(); try { preparedSth = prepareStatement(attributes, sth, query); boolean result = preparedSth == null ? sth.execute(query) : preparedSth.execute(); SqlAnalytics.trace(getConnection().getInstanceId(), original, query, start, null); return result; } catch (Throwable e) { SqlAnalytics.trace(getConnection().getInstanceId(), original, query, start, e); log().e(query); throw e; } }
/** {@inheritDoc} */ @Override public DbStatement createStatement(String sql, String language) throws MException { synchronized (this) { if (closed) throw new MException("Connection not valid"); return new JdbcStatement(this, sql, language); } }
/** {@inheritDoc} */ @Override public DbStatement getStatement(String name) throws MException { synchronized (this) { if (closed) throw new MException("Connection not valid"); String[] query = provider.getQuery(name); if (query == null) return null; return new JdbcStatement(this, query[1],query[0]); } }