Search in sources :

Example 51 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class OdbcRequestHandler method executeQuery.

/**
 * {@link OdbcQueryExecuteRequest} command handler.
 *
 * @param req Execute query request.
 * @return Response.
 */
private ClientListenerResponse executeQuery(OdbcQueryExecuteRequest req) {
    int cursorCnt = qryResults.size();
    if (maxCursors > 0 && cursorCnt >= maxCursors)
        return new OdbcResponse(IgniteQueryErrorCode.UNKNOWN, "Too many open cursors (either close " + "other open cursors or increase the limit through " + "ClientConnectorConfiguration.maxOpenCursorsPerConnection) [maximum=" + maxCursors + ", current=" + cursorCnt + ']');
    long qryId = QRY_ID_GEN.getAndIncrement();
    assert !cliCtx.isStream();
    try {
        String sql = OdbcEscapeUtils.parse(req.sqlQuery());
        if (log.isDebugEnabled())
            log.debug("ODBC query parsed [reqId=" + req.requestId() + ", original=" + req.sqlQuery() + ", parsed=" + sql + ']');
        SqlFieldsQuery qry = makeQuery(req.schema(), sql, req.arguments(), req.timeout(), req.autoCommit());
        List<FieldsQueryCursor<List<?>>> cursors = ctx.query().querySqlFields(null, qry, cliCtx, true, false);
        OdbcQueryResults results = new OdbcQueryResults(cursors, ver);
        Collection<OdbcColumnMeta> fieldsMeta;
        OdbcResultSet set = results.currentResultSet();
        if (set == null)
            fieldsMeta = new ArrayList<>();
        else {
            fieldsMeta = set.fieldsMeta();
            if (log.isDebugEnabled()) {
                for (OdbcColumnMeta meta : fieldsMeta) log.debug("Meta - " + meta.toString());
            }
        }
        if (!results.hasUnfetchedRows())
            results.closeAll();
        else
            qryResults.put(qryId, results);
        OdbcQueryExecuteResult res = new OdbcQueryExecuteResult(qryId, fieldsMeta, results.rowsAffected());
        return new OdbcResponse(res);
    } catch (Exception e) {
        qryResults.remove(qryId);
        U.error(log, "Failed to execute SQL query [reqId=" + req.requestId() + ", req=" + req + ']', e);
        return exceptionToResult(e);
    }
}
Also used : FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) ArrayList(java.util.ArrayList) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) BatchUpdateException(java.sql.BatchUpdateException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 52 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class CacheMvccAbstractTest method mergeSql.

/**
 * Merge account by means of SQL API.
 *
 * @param cache Cache.
 * @param key Key.
 * @param val Value.
 * @param updateCnt Update counter.
 */
protected static void mergeSql(TestCache<Integer, MvccTestAccount> cache, Integer key, Integer val, Integer updateCnt) {
    SqlFieldsQuery qry = new SqlFieldsQuery("merge into MvccTestAccount(_key, val, updateCnt) values " + " (" + key + ", " + val + ", " + updateCnt + ")");
    cache.cache.query(qry).getAll();
}
Also used : SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 53 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class CacheMvccAbstractTest method insertSql.

/**
 * Inserts account by means of SQL API.
 *
 * @param cache Cache.
 * @param key Key.
 * @param val Value.
 * @param updateCnt Update counter.
 */
private static void insertSql(TestCache<Integer, MvccTestAccount> cache, int key, Integer val, Integer updateCnt) {
    SqlFieldsQuery qry = new SqlFieldsQuery("insert into MvccTestAccount(_key, val, updateCnt) values " + " (" + key + ", " + val + ", " + updateCnt + ")");
    cache.cache.query(qry).getAll();
}
Also used : SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 54 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class CacheMvccAbstractTest method updateSql.

/**
 * Updates account by means of SQL API.
 *
 * @param cache Cache.
 * @param key Key.
 * @param val Value.
 * @param updateCnt Update counter.
 */
private static void updateSql(TestCache<Integer, MvccTestAccount> cache, Integer key, Integer val, Integer updateCnt) {
    SqlFieldsQuery qry = new SqlFieldsQuery("update MvccTestAccount set val=" + val + ", updateCnt=" + updateCnt + " where _key=" + key);
    cache.cache.query(qry).getAll();
}
Also used : SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 55 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class CacheMvccAbstractTest method getAllSql.

/**
 * Returns all accounts from cache by means of SQL.
 *
 * @param cache Cache.
 * @return All accounts
 */
protected static Map<Integer, MvccTestAccount> getAllSql(TestCache<Integer, MvccTestAccount> cache) {
    Map<Integer, MvccTestAccount> accounts = new HashMap<>();
    SqlFieldsQuery qry = new SqlFieldsQuery("select _key, val, updateCnt from MvccTestAccount");
    for (List<?> row : cache.cache.query(qry).getAll()) {
        Integer id = (Integer) row.get(0);
        Integer val = (Integer) row.get(1);
        Integer updateCnt = (Integer) row.get(2);
        MvccTestAccount old = accounts.put(id, new MvccTestAccount(val, updateCnt));
        assertNull(old);
    }
    return accounts;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Aggregations

SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)679 Test (org.junit.Test)388 List (java.util.List)373 Ignite (org.apache.ignite.Ignite)170 ArrayList (java.util.ArrayList)136 IgniteCache (org.apache.ignite.IgniteCache)123 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)115 AbstractIndexingCommonTest (org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest)94 IgniteEx (org.apache.ignite.internal.IgniteEx)90 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)83 Transaction (org.apache.ignite.transactions.Transaction)80 CacheException (javax.cache.CacheException)67 Random (java.util.Random)55 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)53 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)52 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)45 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)41 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)32 HashMap (java.util.HashMap)31 Cache (javax.cache.Cache)31