@Override public boolean tableExists(Handle handle, String tableName) { return !handle.createQuery("select * from SYS.SYSTABLES where tablename = :tableName") .bind("tableName", StringUtils.toUpperCase(tableName)) .list() .isEmpty(); }
@Override public boolean tableExists(final Handle handle, final String tableName) { return !handle.createQuery( "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public' AND tablename ILIKE :tableName" ) .bind("tableName", tableName) .map(StringMapper.FIRST) .list() .isEmpty(); }
@Override public boolean tableExists(final Handle handle, final String tableName) { return !handle.createQuery("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = :tableName") .bind("tableName", tableName) .map(StringMapper.FIRST) .list() .isEmpty(); }
@Override public Optional<StatusType> withHandle(Handle handle) throws Exception { byte[] res = handle.createQuery( StringUtils.format("SELECT status_payload FROM %s WHERE id = :id", entryTable) ) .bind("id", entryId) .map(ByteArrayMapper.FIRST) .first(); return Optional.fromNullable( res == null ? null : jsonMapper.readValue(res, statusType) ); } }
@Override public Optional<EntryType> withHandle(Handle handle) throws Exception { byte[] res = handle.createQuery( StringUtils.format("SELECT payload FROM %s WHERE id = :id", entryTable) ) .bind("id", entryId) .map(ByteArrayMapper.FIRST) .first(); return Optional.fromNullable( res == null ? null : jsonMapper.readValue(res, entryType) ); } }
@Override public List<String> inTransaction(Handle handle, TransactionStatus status) { return handle.createQuery(reverseFetchQuery) .bind("val", value) .map(StringMapper.FIRST) .list(); } });
@Override public List<String> inTransaction(Handle handle, TransactionStatus status) { return handle.createQuery(fetchQuery) .bind("val", key) .map(StringMapper.FIRST) .list(); } }
@Override public List<AuditEntry> withHandle(Handle handle) { Query<Map<String, Object>> query = handle.createQuery(theQueryString); if (key != null) { query.bind("audit_key", key); } return query.bind("type", type) .setMaxRows(theLimit) .map( new ResultSetMapper<AuditEntry>() { @Override public AuditEntry map(int index, ResultSet r, StatementContext ctx) throws SQLException { try { return jsonMapper.readValue(r.getBytes("payload"), AuditEntry.class); } catch (IOException e) { throw new SQLException(e); } } } ) .list(); } }
private Query<Map<String, Object>> createActiveTaskInfoQuery(Handle handle, @Nullable String dataSource) { String sql = StringUtils.format( "SELECT " + " id, " + " status_payload, " + " payload, " + " datasource, " + " created_date " + "FROM " + " %s " + "WHERE " + getWhereClauseForActiveStatusesQuery(dataSource) + "ORDER BY created_date", entryTable ); Query<Map<String, Object>> query = handle.createQuery(sql); if (dataSource != null) { query = query.bind("ds", dataSource); } return query; }
public byte[] lookupWithHandle( final Handle handle, final String tableName, final String keyColumn, final String valueColumn, final String key ) { final String selectStatement = StringUtils.format( "SELECT %s FROM %s WHERE %s = :key", valueColumn, tableName, keyColumn ); List<byte[]> matched = handle.createQuery(selectStatement) .bind("key", key) .map(ByteArrayMapper.FIRST) .list(); if (matched.isEmpty()) { return null; } if (matched.size() > 1) { throw new ISE("Error! More than one matching entry[%d] found for [%s]?!", matched.size(), key); } return matched.get(0); }
@Override public List<Map<String, Object>> withHandle(Handle handle) { return handle.createQuery( StringUtils.format("SELECT id FROM %s WHERE id=:id", config.getSegmentsTable()) ) .bind("id", segmentId) .list(); } }
@Override @Nullable public TaskInfo<EntryType, StatusType> getTaskInfo(String entryId) { return connector.retryWithHandle(handle -> { final String query = StringUtils.format( "SELECT id, status_payload, payload, datasource, created_date FROM %s WHERE id = :id", entryTable ); return handle.createQuery(query) .bind("id", entryId) .map(taskInfoMapper) .first(); }); }
private List<SegmentIdWithShardSpec> getPendingSegmentsForIntervalWithHandle( final Handle handle, final String dataSource, final Interval interval ) throws IOException { final List<SegmentIdWithShardSpec> identifiers = new ArrayList<>(); final ResultIterator<byte[]> dbSegments = handle.createQuery( StringUtils.format( "SELECT payload FROM %1$s WHERE dataSource = :dataSource AND start <= :end and %2$send%2$s >= :start", dbTables.getPendingSegmentsTable(), connector.getQuoteString() ) ) .bind("dataSource", dataSource) .bind("start", interval.getStart().toString()) .bind("end", interval.getEnd().toString()) .map(ByteArrayMapper.FIRST) .iterator(); while (dbSegments.hasNext()) { final byte[] payload = dbSegments.next(); final SegmentIdWithShardSpec identifier = jsonMapper.readValue(payload, SegmentIdWithShardSpec.class); if (interval.overlaps(identifier.getInterval())) { identifiers.add(identifier); } } dbSegments.close(); return identifiers; }
@Override public boolean tableExists(Handle handle, String tableName) { String databaseCharset = handle .createQuery("SELECT @@character_set_database") .map(StringMapper.FIRST) .first(); if (!databaseCharset.startsWith("utf8")) { throw new ISE( "Druid requires its MySQL database to be created with an UTF8 charset, found `%1$s`. " + "The recommended charset is `utf8mb4`.", databaseCharset ); } else if (!"utf8mb4".equals(databaseCharset)) { log.warn("The current database charset `%1$s` does not match the recommended charset `utf8mb4`", databaseCharset); } return !handle.createQuery("SHOW tables LIKE :tableName") .bind("tableName", tableName) .list() .isEmpty(); }
private boolean segmentExists(final Handle handle, final DataSegment segment) { return !handle .createQuery(StringUtils.format("SELECT id FROM %s WHERE id = :identifier", dbTables.getSegmentsTable())) .bind("identifier", segment.getId().toString()) .map(StringMapper.FIRST) .list() .isEmpty(); }
@Override public List<LogType> withHandle(Handle handle) { return handle .createQuery( StringUtils.format( "SELECT log_payload FROM %1$s WHERE %2$s_id = :entryId", logTable, entryTypeName ) ) .bind("entryId", entryId) .map(ByteArrayMapper.FIRST) .fold( new ArrayList<>(), (List<LogType> list, byte[] bytes, FoldController control, StatementContext ctx) -> { try { list.add(jsonMapper.readValue(bytes, logType)); return list; } catch (IOException e) { log.makeAlert(e, "Failed to deserialize log") .addData("entryId", entryId) .addData("payload", StringUtils.fromUtf8(bytes)) .emit(); throw new SQLException(e); } } ); } }