Search in sources :

Example 46 with KvPair

use of doitincloud.rdbcache.models.KvPair in project rdbcache by rdbcache.

the class Query method executeDelete.

public boolean executeDelete() {
    boolean allOk = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        String table = keyInfo.getTable();
        if (!keyInfo.getIsNew() && !keyInfo.hasParams() && keyInfo.ifJustCreated()) {
            waitForParamsUpdate(pair.getIdType(), keyInfo);
        }
        if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
            String msg = "executeDelete failed when calling prepareStandardClauseParams for " + pair.getId();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
            continue;
        }
        params = keyInfo.getParams();
        String clause = keyInfo.getClause();
        sql = "delete from " + table + " where " + clause + " limit 1";
        LOGGER.trace("sql: " + sql);
        LOGGER.trace("params: " + params.toString());
        StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.delete");
        try {
            if (jdbcTemplate.update(sql, params.toArray()) > 0) {
                if (stopWatch != null)
                    stopWatch.stopNow();
                LOGGER.trace("delete " + pair.getId() + " from " + table);
                continue;
            } else {
                if (stopWatch != null)
                    stopWatch.stopNow();
                allOk = false;
            }
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            allOk = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
        keyInfo.setQueryKey(null);
    }
    return allOk;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) SQLException(java.sql.SQLException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 47 with KvPair

use of doitincloud.rdbcache.models.KvPair in project rdbcache by rdbcache.

the class Query method ifSelectOk.

public boolean ifSelectOk() {
    Assert.isTrue(anyKey.size() == 1, "anyKey.size() = " + anyKey.size() + ", select only supports anyKey size == 1");
    KeyInfo keyInfo = anyKey.getKeyInfo();
    String table = keyInfo.getTable();
    if (table == null) {
        return false;
    }
    String queryKey = keyInfo.getQueryKey();
    if (queryKey == null) {
        return false;
    }
    String clause = keyInfo.getClause();
    params = keyInfo.getParams();
    if (clause == null || clause.length() == 0 || params == null || params.size() == 0) {
        boolean ready = false;
        KvPair pair = pairs.getPair();
        if (Parser.prepareQueryClauseParams(context, pair, keyInfo)) {
            ready = true;
        } else if (keyInfo.getQueryLimit() != null) {
            ready = true;
        } else if (pairs.size() > 0) {
            ready = true;
        } else {
            if (!keyInfo.getIsNew() && !keyInfo.hasParams() && keyInfo.ifJustCreated()) {
                waitForParamsUpdate(pair.getIdType(), keyInfo);
            }
            if (Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
                ready = true;
            }
        }
        if (!ready) {
            return false;
        }
        clause = keyInfo.getClause();
        params = keyInfo.getParams();
    }
    int limit = getLimit();
    sql = "select * from " + table;
    if (clause != null && clause.length() > 0) {
        sql += " where " + clause;
    }
    if (limit != 0) {
        sql += " limit " + limit;
    }
    QueryInfo queryInfo = keyInfo.getQuery();
    if (queryInfo != null) {
        keyInfo.setQuery(null);
        Utils.getExcutorService().submit(() -> {
            Thread.yield();
            AppCtx.getDbaseOps().saveQuery(context, queryInfo);
        });
    }
    return true;
}
Also used : KeyInfo(doitincloud.rdbcache.models.KeyInfo) KvPair(doitincloud.rdbcache.models.KvPair)

Example 48 with KvPair

use of doitincloud.rdbcache.models.KvPair in project rdbcache by rdbcache.

the class Query method executeUpdate.

public boolean executeUpdate() {
    params = new ArrayList<>();
    boolean allOk = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        String table = keyInfo.getTable();
        if (!keyInfo.getIsNew() && !keyInfo.hasParams() && keyInfo.ifJustCreated()) {
            waitForParamsUpdate(pair.getIdType(), keyInfo);
        }
        // 
        if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
            allOk = false;
            String msg = "executeUpdate failed when calling prepareStandardClauseParams for " + pair.getId();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
            continue;
        }
        Map<String, Object> map = pair.getData();
        params.clear();
        String updates = "";
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            params.add(entry.getValue());
            if (updates.length() != 0)
                updates += ", ";
            updates += entry.getKey() + " = ?";
        }
        params.addAll(keyInfo.getParams());
        String clause = keyInfo.getClause();
        sql = "update " + table + " set " + updates + " where " + clause + " limit 1";
        LOGGER.trace("sql: " + sql);
        LOGGER.trace("params: " + params.toString());
        StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.update");
        try {
            if (jdbcTemplate.update(sql, params.toArray()) > 0) {
                if (stopWatch != null)
                    stopWatch.stopNow();
                LOGGER.trace("update " + pair.getId() + " from " + table);
                continue;
            } else {
                if (stopWatch != null)
                    stopWatch.stopNow();
                allOk = false;
            }
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            allOk = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
        keyInfo.setQueryKey(null);
    }
    return allOk;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) SQLException(java.sql.SQLException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch) KeyInfo(doitincloud.rdbcache.models.KeyInfo) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException)

Example 49 with KvPair

use of doitincloud.rdbcache.models.KvPair in project rdbcache by rdbcache.

the class KvPairsTest method getPair.

@Test
public void getPair() {
    KvPairs pairs;
    KvPair pair;
    pairs = new KvPairs();
    pair = pairs.getPair();
    assertNull(pair);
    pair = new KvPair("key", "data", "value");
    pairs = new KvPairs(pair);
    assertTrue(pair == pairs.get(0));
    KvPair pair2 = pairs.getPair();
    assertNotNull(pair2);
    assertTrue(pair == pair2);
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs) Test(org.junit.Test)

Example 50 with KvPair

use of doitincloud.rdbcache.models.KvPair in project rdbcache by rdbcache.

the class KvPairsTest method listConstructor2.

@Test
public void listConstructor2() {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("k00", Utils.toMap("{\"kk00\": 0}"));
    map.put("k01", Utils.toMap("{\"kk01\": 1}"));
    map.put("k02", Utils.toMap("{\"kk02\": 2}"));
    list.add(map);
    map = new LinkedHashMap<>();
    map.put("k10", Utils.toMap("{\"kk10\": 0}"));
    map.put("k11", Utils.toMap("{\"kk11\": 1}"));
    map.put("k12", Utils.toMap("{\"kk12\": 2}"));
    list.add(map);
    map = new LinkedHashMap<>();
    map.put("k20", Utils.toMap("{\"kk20\": 0}"));
    map.put("k21", Utils.toMap("{\"kk21\": 1}"));
    map.put("k22", Utils.toMap("{\"kk22\": 2}"));
    list.add(map);
    KvPairs pairs = new KvPairs(list);
    assertEquals(3, pairs.size());
    for (int i = 0; i < 3; i++) {
        KvPair pair = pairs.get(i);
        map = pair.getData();
        assertTrue(pair.isNewUuid());
        assertEquals(3, map.size());
    }
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) ArrayList(java.util.ArrayList) KvPairs(doitincloud.rdbcache.supports.KvPairs) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

KvPair (doitincloud.rdbcache.models.KvPair)51 KeyInfo (doitincloud.rdbcache.models.KeyInfo)29 KvPairs (doitincloud.rdbcache.supports.KvPairs)22 Context (doitincloud.rdbcache.supports.Context)15 Test (org.junit.Test)15 AnyKey (doitincloud.rdbcache.supports.AnyKey)14 StopWatch (doitincloud.rdbcache.models.StopWatch)13 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)9 BadRequestException (doitincloud.commons.exceptions.BadRequestException)7 KvIdType (doitincloud.rdbcache.models.KvIdType)6 SQLException (java.sql.SQLException)4 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)4 ResultActions (org.springframework.test.web.servlet.ResultActions)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 NotFoundException (doitincloud.commons.exceptions.NotFoundException)1