Search in sources :

Example 1 with Query

use of org.skife.jdbi.v2.Query in project druid by druid-io.

the class JDBCExtractionNamespaceCacheFactory method lastUpdates.

private Long lastUpdates(CacheScheduler.EntryImpl<JDBCExtractionNamespace> id, JDBCExtractionNamespace namespace) {
    final DBI dbi = ensureDBI(id, namespace);
    final String table = namespace.getTable();
    final String tsColumn = namespace.getTsColumn();
    if (tsColumn == null) {
        return null;
    }
    final Timestamp update = dbi.withHandle(new HandleCallback<Timestamp>() {

        @Override
        public Timestamp withHandle(Handle handle) throws Exception {
            final String query = String.format("SELECT MAX(%s) FROM %s", tsColumn, table);
            return handle.createQuery(query).map(TimestampMapper.FIRST).first();
        }
    });
    return update.getTime();
}
Also used : DBI(org.skife.jdbi.v2.DBI) Timestamp(java.sql.Timestamp) SQLException(java.sql.SQLException) Handle(org.skife.jdbi.v2.Handle)

Example 2 with Query

use of org.skife.jdbi.v2.Query in project druid by druid-io.

the class SQLMetadataSegmentManager method poll.

@Override
public void poll() {
    try {
        if (!started) {
            return;
        }
        ConcurrentHashMap<String, DruidDataSource> newDataSources = new ConcurrentHashMap<String, DruidDataSource>();
        log.debug("Starting polling of segment table");
        // some databases such as PostgreSQL require auto-commit turned off
        // to stream results back, enabling transactions disables auto-commit
        //
        // setting connection to read-only will allow some database such as MySQL
        // to automatically use read-only transaction mode, further optimizing the query
        final List<DataSegment> segments = connector.inReadOnlyTransaction(new TransactionCallback<List<DataSegment>>() {

            @Override
            public List<DataSegment> inTransaction(Handle handle, TransactionStatus status) throws Exception {
                return handle.createQuery(String.format("SELECT payload FROM %s WHERE used=true", getSegmentsTable())).setFetchSize(connector.getStreamingFetchSize()).map(new ResultSetMapper<DataSegment>() {

                    @Override
                    public DataSegment map(int index, ResultSet r, StatementContext ctx) throws SQLException {
                        try {
                            return DATA_SEGMENT_INTERNER.intern(jsonMapper.readValue(r.getBytes("payload"), DataSegment.class));
                        } catch (IOException e) {
                            log.makeAlert(e, "Failed to read segment from db.");
                            return null;
                        }
                    }
                }).list();
            }
        });
        if (segments == null || segments.isEmpty()) {
            log.warn("No segments found in the database!");
            return;
        }
        final Collection<DataSegment> segmentsFinal = Collections2.filter(segments, Predicates.notNull());
        log.info("Polled and found %,d segments in the database", segments.size());
        for (final DataSegment segment : segmentsFinal) {
            String datasourceName = segment.getDataSource();
            DruidDataSource dataSource = newDataSources.get(datasourceName);
            if (dataSource == null) {
                dataSource = new DruidDataSource(datasourceName, ImmutableMap.of("created", new DateTime().toString()));
                Object shouldBeNull = newDataSources.put(datasourceName, dataSource);
                if (shouldBeNull != null) {
                    log.warn("Just put key[%s] into dataSources and what was there wasn't null!?  It was[%s]", datasourceName, shouldBeNull);
                }
            }
            if (!dataSource.getSegments().contains(segment)) {
                dataSource.addSegment(segment.getIdentifier(), segment);
            }
        }
        synchronized (lock) {
            if (started) {
                dataSources.set(newDataSources);
            }
        }
    } catch (Exception e) {
        log.makeAlert(e, "Problem polling DB.").emit();
    }
}
Also used : SQLException(java.sql.SQLException) TransactionStatus(org.skife.jdbi.v2.TransactionStatus) IOException(java.io.IOException) DruidDataSource(io.druid.client.DruidDataSource) DataSegment(io.druid.timeline.DataSegment) SQLException(java.sql.SQLException) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) Handle(org.skife.jdbi.v2.Handle) StatementContext(org.skife.jdbi.v2.StatementContext) ResultSet(java.sql.ResultSet) List(java.util.List) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with Query

use of org.skife.jdbi.v2.Query in project irontest by zheng-wang.

the class DBTeststepRunner method run.

protected BasicTeststepRun run(Teststep teststep) throws Exception {
    BasicTeststepRun basicTeststepRun = new BasicTeststepRun();
    DBAPIResponse response = new DBAPIResponse();
    String request = (String) teststep.getRequest();
    Endpoint endpoint = teststep.getEndpoint();
    DBI jdbi = new DBI(endpoint.getUrl(), endpoint.getUsername(), getDecryptedEndpointPassword());
    // get SQL statements (trimmed and without comments) and JDBI script object
    List<String> statements = IronTestUtils.getStatements(request);
    sanityCheckTheStatements(statements);
    Handle handle = jdbi.open();
    if (SQLStatementType.isSelectStatement(statements.get(0))) {
        // the request is a select statement
        RetainingColumnOrderResultSetMapper resultSetMapper = new RetainingColumnOrderResultSetMapper();
        // use statements.get(0) instead of the raw request, as Oracle does not support trailing semicolon in select statement
        Query<Map<String, Object>> query = handle.createQuery(statements.get(0)).map(resultSetMapper);
        // obtain columnNames in case the query returns no row
        final List<String> columnNames = new ArrayList<String>();
        query.addStatementCustomizer(new BaseStatementCustomizer() {

            public void afterExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException {
                ResultSetMetaData metaData = stmt.getMetaData();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    columnNames.add(metaData.getColumnLabel(i).toLowerCase());
                }
            }
        });
        // limit the number of returned rows
        List<Map<String, Object>> rows = query.list(5000);
        response.setColumnNames(columnNames);
        response.setRowsJSON(jacksonObjectMapper.writeValueAsString(rows));
    } else {
        // the request is one or more non-select statements
        Script script = handle.createScript(request);
        int[] returnValues = script.execute();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < returnValues.length; i++) {
            String statementType = SQLStatementType.getByStatement(statements.get(i)).toString();
            sb.append(returnValues[i]).append(" row(s) ").append(statementType.toLowerCase()).append(statementType.endsWith("E") ? "d" : "ed").append("\n");
            response.setStatementExecutionResults(sb.toString());
        }
    }
    handle.close();
    basicTeststepRun.setResponse(response);
    return basicTeststepRun;
}
Also used : BaseStatementCustomizer(org.skife.jdbi.v2.tweak.BaseStatementCustomizer) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Endpoint(io.irontest.models.endpoint.Endpoint) ResultSetMetaData(java.sql.ResultSetMetaData) Endpoint(io.irontest.models.endpoint.Endpoint) Map(java.util.Map)

Example 4 with Query

use of org.skife.jdbi.v2.Query in project pac4j by pac4j.

the class DbProfileService method execute.

protected void execute(final String query, final Object... args) {
    Handle h = null;
    try {
        h = dbi.open();
        logger.debug("Execute query: {} and values: {}", query, args);
        h.execute(query, args);
    } finally {
        if (h != null) {
            h.close();
        }
    }
}
Also used : Handle(org.skife.jdbi.v2.Handle)

Example 5 with Query

use of org.skife.jdbi.v2.Query in project packages-jpl by SWI-Prolog.

the class FamilyMT method run.

public void run() {
    Map<String, Term> solution;
    // Variable X = new Variable("X");
    // 
    Query q2 = new Query("child_of(joe,ralf)");
    System.err.println("child_of(joe,ralf) is " + (q2.hasSolution() ? "provable" : "not provable"));
    new Query("sleep(?)", new Term[] { new org.jpl7.Integer(delay) }).hasSolution();
    // 
    Query q3 = new Query("descendent_of(steve,ralf)");
    System.err.println("descendent_of(steve,ralf) is " + (q3.hasSolution() ? "provable" : "not provable"));
    delay();
    // 
    Query q4 = new Query("descendent_of(X, ralf)");
    solution = q4.oneSolution();
    System.err.println("first solution of descendent_of(X, ralf)");
    System.err.println("X = " + solution.get("X"));
    delay();
    // 
    Map<String, Term>[] solutions = q4.allSolutions();
    System.err.println("all solutions of descendent_of(X, ralf)");
    for (int i = 0; i < solutions.length; i++) {
        System.err.println("X = " + solutions[i].get("X"));
    }
    delay();
    // 
    System.err.println("each solution of descendent_of(X, ralf)");
    while (q4.hasMoreSolutions()) {
        solution = q4.nextSolution();
        System.err.println("X = " + solution.get("X"));
    }
    delay();
    // 
    Query q5 = new Query("descendent_of(X, Y)");
    System.err.println(id + ": each solution of descendent_of(X, Y)");
    while (q5.hasMoreSolutions()) {
        solution = q5.nextSolution();
        System.err.println(id + ": X = " + solution.get("X") + ", Y = " + solution.get("Y"));
        delay();
    }
}
Also used : Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map)

Aggregations

Query (org.jpl7.Query)56 Term (org.jpl7.Term)33 Variable (org.jpl7.Variable)23 Map (java.util.Map)18 Atom (org.jpl7.Atom)16 Compound (org.jpl7.Compound)16 Query (com.google.datastore.v1.Query)12 Handle (org.skife.jdbi.v2.Handle)12 Test (org.junit.Test)10 GqlQuery (com.google.datastore.v1.GqlQuery)7 Integer (org.jpl7.Integer)6 SQLException (java.sql.SQLException)5 ArrayList (java.util.ArrayList)5 Datastore (com.google.datastore.v1.client.Datastore)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 SplitQueryFn (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.SplitQueryFn)4 RunQueryRequest (com.google.datastore.v1.RunQueryRequest)3 Nullable (javax.annotation.Nullable)3