Search in sources :

Example 1 with LocalDataSourceServiceImpl

use of com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl in project nacos by alibaba.

the class ConfigOpsController method derbyOps.

/**
 * // TODO In a future release, the front page should appear operable The interface to the Derby operations query
 * can only run select statements and is a direct query to the native Derby database without any additional logic.
 *
 * @param sql The query
 * @return {@link RestResult}
 */
@GetMapping(value = "/derby")
@Secured(action = ActionTypes.READ, resource = "nacos/admin")
public RestResult<Object> derbyOps(@RequestParam(value = "sql") String sql) {
    String selectSign = "SELECT";
    String limitSign = "ROWS FETCH NEXT";
    String limit = " OFFSET 0 ROWS FETCH NEXT 1000 ROWS ONLY";
    try {
        if (!PropertyUtil.isEmbeddedStorage()) {
            return RestResultUtils.failed("The current storage mode is not Derby");
        }
        LocalDataSourceServiceImpl dataSourceService = (LocalDataSourceServiceImpl) DynamicDataSource.getInstance().getDataSource();
        if (StringUtils.startsWithIgnoreCase(sql, selectSign)) {
            if (!StringUtils.containsIgnoreCase(sql, limitSign)) {
                sql += limit;
            }
            JdbcTemplate template = dataSourceService.getJdbcTemplate();
            List<Map<String, Object>> result = template.queryForList(sql);
            return RestResultUtils.success(result);
        }
        return RestResultUtils.failed("Only query statements are allowed to be executed");
    } catch (Exception e) {
        return RestResultUtils.failed(e.getMessage());
    }
}
Also used : LocalDataSourceServiceImpl(com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 2 with LocalDataSourceServiceImpl

use of com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl in project nacos by alibaba.

the class ConfigOpsControllerTest method testDerbyOps.

@Test
public void testDerbyOps() throws Exception {
    MockedStatic propertyUtil = Mockito.mockStatic(PropertyUtil.class);
    when(PropertyUtil.isEmbeddedStorage()).thenReturn(true);
    propertyUtil.close();
    Mockito.mockStatic(DynamicDataSource.class);
    DynamicDataSource dataSource = Mockito.mock(DynamicDataSource.class);
    when(DynamicDataSource.getInstance()).thenReturn(dataSource);
    LocalDataSourceServiceImpl dataSourceService = Mockito.mock(LocalDataSourceServiceImpl.class);
    when(dataSource.getDataSource()).thenReturn(dataSourceService);
    JdbcTemplate template = Mockito.mock(JdbcTemplate.class);
    when(dataSourceService.getJdbcTemplate()).thenReturn(template);
    when(template.queryForList("SELECT * FROM TEST")).thenReturn(new ArrayList<>());
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.OPS_CONTROLLER_PATH + "/derby").param("sql", "SELECT * FROM TEST");
    String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    Assert.assertEquals("200", JacksonUtils.toObj(actualValue).get("code").toString());
}
Also used : LocalDataSourceServiceImpl(com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl) MockedStatic(org.mockito.MockedStatic) DynamicDataSource(com.alibaba.nacos.config.server.service.datasource.DynamicDataSource) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 3 with LocalDataSourceServiceImpl

use of com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl in project nacos by alibaba.

the class DerbySnapshotOperation method doDerbyRestoreFromBackup.

private void doDerbyRestoreFromBackup(Callable<Void> callable) throws Exception {
    DataSourceService sourceService = DynamicDataSource.getInstance().getDataSource();
    LocalDataSourceServiceImpl localDataSourceService = (LocalDataSourceServiceImpl) sourceService;
    localDataSourceService.restoreDerby(restoreDB, callable);
}
Also used : LocalDataSourceServiceImpl(com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl) DataSourceService(com.alibaba.nacos.config.server.service.datasource.DataSourceService)

Aggregations

LocalDataSourceServiceImpl (com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl)3 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)2 Secured (com.alibaba.nacos.auth.annotation.Secured)1 DataSourceService (com.alibaba.nacos.config.server.service.datasource.DataSourceService)1 DynamicDataSource (com.alibaba.nacos.config.server.service.datasource.DynamicDataSource)1 Map (java.util.Map)1 Test (org.junit.Test)1 MockedStatic (org.mockito.MockedStatic)1 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1