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());
}
}
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());
}
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);
}
Aggregations