add refresh database function

dev
floxx2112 2 years ago
parent 2dbfd6d667
commit dbd113590f
  1. 15
      pom.xml
  2. 26
      src/main/java/com/secondtest/secondtest/SecondtestApplication.java
  3. 6
      src/main/java/com/secondtest/secondtest/controller/InfoColumnController.java
  4. 10
      src/main/java/com/secondtest/secondtest/controller/InfoTableController.java
  5. 5
      src/main/java/com/secondtest/secondtest/controller/LinkInfoController.java
  6. 25
      src/main/java/com/secondtest/secondtest/controller/ResetDataController.java
  7. 9
      src/main/java/com/secondtest/secondtest/repository/InfoColumnRepository.java
  8. 7
      src/main/java/com/secondtest/secondtest/repository/InfoTableRepository.java
  9. 7
      src/main/java/com/secondtest/secondtest/repository/LinkInfoRepository.java
  10. 14
      src/main/java/com/secondtest/secondtest/service/InfoColumnService.java
  11. 7
      src/main/java/com/secondtest/secondtest/service/InfoTableService.java
  12. 10
      src/main/java/com/secondtest/secondtest/service/LinkInfoService.java
  13. 171
      src/main/java/com/secondtest/secondtest/service/ResetDataService.java
  14. 1
      src/main/resources/tableSchema.json
  15. 2216
      src/main/resources/testTable.json
  16. 4
      src/test/java/com/secondtest/secondtest/SecondtestApplicationTests.java

@ -46,6 +46,21 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -1,17 +1,41 @@
package com.secondtest.secondtest; package com.secondtest.secondtest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.secondtest.secondtest.controller.InfoColumnController;
import com.secondtest.secondtest.model.Employee;
import com.secondtest.secondtest.model.InfoColumn;
import com.secondtest.secondtest.model.InfoTable;
import com.secondtest.secondtest.service.InfoColumnService;
import com.secondtest.secondtest.service.InfoTableService;
import com.secondtest.secondtest.service.LinkInfoService;
import org.apache.tomcat.util.json.JSONParser;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter; import org.springframework.web.filter.CorsFilter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
@SpringBootApplication @SpringBootApplication
public class SecondtestApplication { public class SecondtestApplication {
public static void main(String[] args) { public static void main(String[] args) throws IOException {
SpringApplication.run(SecondtestApplication.class, args); SpringApplication.run(SecondtestApplication.class, args);
} }
@Bean @Bean

@ -63,5 +63,11 @@ public class InfoColumnController {
return infoColumnService.getColumnsForJoinTwo(tablesList, schemasList); return infoColumnService.getColumnsForJoinTwo(tablesList, schemasList);
} }
@DeleteMapping("/columns/deleteAll")
public void deleteAllColumns() {
infoColumnService.deleteAllColumn();
}
} }

@ -5,10 +5,7 @@ import com.secondtest.secondtest.model.InfoTable;
import com.secondtest.secondtest.repository.InfoTableRepository; import com.secondtest.secondtest.repository.InfoTableRepository;
import com.secondtest.secondtest.service.InfoTableService; import com.secondtest.secondtest.service.InfoTableService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional; import java.util.Optional;
@ -61,4 +58,9 @@ public class InfoTableController {
return infoTableService.getTablesBySchemaName(schema); return infoTableService.getTablesBySchemaName(schema);
} }
@DeleteMapping("/tables/deleteAll")
public void deleteAllTables() {
infoTableService.deleteAllTable();
}
} }

@ -3,6 +3,7 @@ package com.secondtest.secondtest.controller;
import com.secondtest.secondtest.model.LinkInfo; import com.secondtest.secondtest.model.LinkInfo;
import com.secondtest.secondtest.service.LinkInfoService; import com.secondtest.secondtest.service.LinkInfoService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -38,4 +39,8 @@ public class LinkInfoController {
return linkInfoService.getAllLinksInfos(); return linkInfoService.getAllLinksInfos();
} }
@DeleteMapping("/links/deleteAll")
public void deleteAllLinks() {
linkInfoService.deleteAllLinks();
}
} }

@ -0,0 +1,25 @@
package com.secondtest.secondtest.controller;
import java.io.IOException;
import java.util.Optional;
import com.secondtest.secondtest.service.InfoColumnService;
import com.secondtest.secondtest.service.InfoTableService;
import com.secondtest.secondtest.service.LinkInfoService;
import com.secondtest.secondtest.service.ResetDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class ResetDataController {
@Autowired
private ResetDataService resetDataService;
@PostMapping("/reset")
public void resetData() throws IOException {
resetDataService.deleteEverything();
resetDataService.insertEverything();
resetDataService.checkAndInsertLinks();
}
}

@ -1,10 +1,12 @@
package com.secondtest.secondtest.repository; package com.secondtest.secondtest.repository;
import com.secondtest.secondtest.model.InfoColumn; import com.secondtest.secondtest.model.InfoColumn;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import java.util.List; import java.util.List;
@ -23,4 +25,11 @@ public interface InfoColumnRepository extends CrudRepository<InfoColumn, Long> {
"(SELECT column_id FROM link_informations WHERE name_table IN :tables AND name_schema IN :schemas GROUP BY column_id HAVING COUNT(column_id) > 1)", nativeQuery = true) "(SELECT column_id FROM link_informations WHERE name_table IN :tables AND name_schema IN :schemas GROUP BY column_id HAVING COUNT(column_id) > 1)", nativeQuery = true)
Iterable<InfoColumn> getColumnsForJoinTwo(@RequestParam("tables") List<String> tables, @RequestParam("schemas") List<String> schemas); Iterable<InfoColumn> getColumnsForJoinTwo(@RequestParam("tables") List<String> tables, @RequestParam("schemas") List<String> schemas);
@Query(value = "SELECT * FROM informations_column WHERE name_column = :namecol AND data_type = :datatype AND length_column = :lengthcol AND column_text = :columntext", nativeQuery = true)
InfoColumn getSpecColumn(@Param("namecol") String nameCol, @Param("datatype") String dataType, @Param("lengthcol") int lengthCol, @Param("columntext") String columnText);
@Modifying
@Transactional
@Query(value = "TRUNCATE TABLE informations_column", nativeQuery = true)
void truncateMyColumn();
} }

@ -2,10 +2,12 @@ package com.secondtest.secondtest.repository;
import com.secondtest.secondtest.model.InfoTable; import com.secondtest.secondtest.model.InfoTable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository @Repository
public interface InfoTableRepository extends CrudRepository<InfoTable, Long> { public interface InfoTableRepository extends CrudRepository<InfoTable, Long> {
@ -18,4 +20,9 @@ public interface InfoTableRepository extends CrudRepository<InfoTable, Long> {
value = "SELECT * FROM informations_table WHERE name_schema=:schema", value = "SELECT * FROM informations_table WHERE name_schema=:schema",
nativeQuery = true) nativeQuery = true)
Iterable<InfoTable> getTablesBySchemaName(@Param("schema") String schema); Iterable<InfoTable> getTablesBySchemaName(@Param("schema") String schema);
@Modifying
@Transactional
@Query(value = "TRUNCATE TABLE informations_table", nativeQuery = true)
void truncateMyTable();
} }

@ -1,10 +1,17 @@
package com.secondtest.secondtest.repository; package com.secondtest.secondtest.repository;
import com.secondtest.secondtest.model.LinkInfo; import com.secondtest.secondtest.model.LinkInfo;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository @Repository
public interface LinkInfoRepository extends CrudRepository<LinkInfo, Long>{ public interface LinkInfoRepository extends CrudRepository<LinkInfo, Long>{
@Modifying
@Transactional
@Query(value = "TRUNCATE TABLE link_informations", nativeQuery = true)
void truncateMyLink();
} }

@ -39,8 +39,20 @@ public class InfoColumnService {
infoColumnRepository.deleteById(id); infoColumnRepository.deleteById(id);
} }
public InfoColumn addOrUpdateTable(InfoColumn infoColumn) { public void deleteAllColumn() {
infoColumnRepository.deleteAll();
}
public InfoColumn addOrUpdateColumn(InfoColumn infoColumn) {
InfoColumn savedInfoColumn = infoColumnRepository.save(infoColumn); InfoColumn savedInfoColumn = infoColumnRepository.save(infoColumn);
return savedInfoColumn; return savedInfoColumn;
} }
public InfoColumn getSpecCol(String nameCol, String dataType, int lengthCol, String columnText){
return infoColumnRepository.getSpecColumn(nameCol, dataType, lengthCol, columnText);
}
public void truncateMyColumn(){
infoColumnRepository.truncateMyColumn();
}
} }

@ -32,10 +32,17 @@ public class InfoTableService {
infoTableRepository.deleteById(id); infoTableRepository.deleteById(id);
} }
public void deleteAllTable() {
infoTableRepository.deleteAll();
}
public InfoTable addOrUpdateTable(InfoTable infoTable) { public InfoTable addOrUpdateTable(InfoTable infoTable) {
InfoTable savedInfoTable = infoTableRepository.save(infoTable); InfoTable savedInfoTable = infoTableRepository.save(infoTable);
return savedInfoTable; return savedInfoTable;
} }
public void truncateMyTable(){
infoTableRepository.truncateMyTable();
}
} }

@ -27,8 +27,16 @@ public class LinkInfoService {
linkInfoRepository.deleteById(id); linkInfoRepository.deleteById(id);
} }
public LinkInfo addOrUpdateEmployee(LinkInfo linkInfo) { public void deleteAllLinks() {
linkInfoRepository.deleteAll();
}
public LinkInfo addOrUpdateLink(LinkInfo linkInfo) {
LinkInfo savedLinkInfo = linkInfoRepository.save(linkInfo); LinkInfo savedLinkInfo = linkInfoRepository.save(linkInfo);
return savedLinkInfo; return savedLinkInfo;
} }
public void truncateMyLink(){
linkInfoRepository.truncateMyLink();
}
} }

@ -0,0 +1,171 @@
package com.secondtest.secondtest.service;
import com.secondtest.secondtest.model.InfoColumn;
import com.secondtest.secondtest.model.InfoTable;
import com.secondtest.secondtest.model.LinkInfo;
import lombok.Data;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
@Data
@Service
public class ResetDataService {
@Autowired
private InfoTableService infoTableService;
@Autowired
private InfoColumnService infoColumnService;
@Autowired
private LinkInfoService linkInfoService;
public void deleteEverything(){
infoColumnService.truncateMyColumn();
infoTableService.truncateMyTable();
linkInfoService.truncateMyLink();
}
public void insertEverything() throws IOException {
String result = new String(Files.readAllBytes(Paths.get("src/main/resources/testTable.json")));
JSONArray jo = new JSONArray(result);
System.out.println("ca marche");
for (int i=0; i<jo.length(); i++){
JSONObject schema = jo.getJSONObject(i);
String schemaName = schema.getString("schema");
JSONArray data = schema.getJSONArray("data");
for (int ia = 0; ia<data.length(); ia++){
JSONObject apra = data.getJSONObject(ia);
String tableText, tableName;
if(apra.has("table_text")){
tableText = apra.getString("table_text");
} else {
tableText = "";
}
if(apra.has("table")){
tableName = apra.getString("table");
} else {
tableName = "";
}
JSONArray col = apra.getJSONArray("columns");
InfoTable infoTable = new InfoTable();
infoTable.setNameTable(tableName);
infoTable.setNameSchema(schemaName);
infoTable.setTableText(tableText);
infoTableService.addOrUpdateTable(infoTable);
for (int ib =0; ib<col.length(); ib++){
JSONObject colData = col.getJSONObject(ib);
String nameCol, dataType, columnText;
int lengthCol;
if(colData.has("name")){
nameCol = colData.getString("name");
} else {
nameCol = "";
}
if (colData.has("data_type")){
dataType = colData.getString("data_type");
} else {
dataType = "";
}
if (colData.has("length")){
lengthCol = colData.getInt("length");
} else {
lengthCol = 0;
}
if(colData.has("column_text")) {
columnText = colData.getString("column_text");
} else {
columnText = "";
}
InfoColumn infoColumn = new InfoColumn();
infoColumn.setNameColumn(nameCol);
infoColumn.setDataType(dataType);
infoColumn.setLengthColumn(lengthCol);
infoColumn.setColumnText(columnText);
InfoColumn findCol = infoColumnService.getSpecCol(nameCol, dataType, lengthCol, columnText);
if(findCol == null){
infoColumnService.addOrUpdateColumn(infoColumn);
}
}
}
}
}
public void checkAndInsertLinks() throws IOException {
String result = new String(Files.readAllBytes(Paths.get("src/main/resources/testTable.json")));
JSONArray jo = new JSONArray(result);
for (int i=0; i<jo.length(); i++){
JSONObject schema = jo.getJSONObject(i);
String schemaName = schema.getString("schema");
JSONArray data = schema.getJSONArray("data");
for (int ia = 0; ia<data.length(); ia++){
JSONObject apra = data.getJSONObject(ia);
String tableText, tableName;
if(apra.has("table_text")){
tableText = apra.getString("table_text");
} else {
tableText = "";
}
if(apra.has("table")){
tableName = apra.getString("table");
} else {
tableName = "";
}
JSONArray col = apra.getJSONArray("columns");
for (int ib =0; ib<col.length(); ib++){
JSONObject colData = col.getJSONObject(ib);
String nameCol, dataType, columnText;
int lengthCol;
if(colData.has("name")){
nameCol = colData.getString("name");
} else {
nameCol = "";
}
if (colData.has("data_type")){
dataType = colData.getString("data_type");
} else {
dataType = "";
}
if (colData.has("length")){
lengthCol = colData.getInt("length");
} else {
lengthCol = 0;
}
if(colData.has("column_text")) {
columnText = colData.getString("column_text");
} else {
columnText = "";
}
InfoColumn findCol = infoColumnService.getSpecCol(nameCol, dataType, lengthCol, columnText);
Long idCol = findCol.getId();
int intId = Math.toIntExact(idCol);
if (findCol != null){
LinkInfo linkInfo = new LinkInfo();
linkInfo.setNameSchema(schemaName);
linkInfo.setNameTable(tableName);
linkInfo.setColumnId(intId);
linkInfoService.addOrUpdateLink(linkInfo);
}
}
}
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

@ -19,11 +19,11 @@ class SecondtestApplicationTests {
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@Test /*@Test
public void testGetEmployees() throws Exception { public void testGetEmployees() throws Exception {
mockMvc.perform(get("/api/employee/all")) mockMvc.perform(get("/api/employee/all"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$[0].firstName", is("Laurent"))); .andExpect(jsonPath("$[0].firstName", is("Laurent")));
} }*/
} }

Loading…
Cancel
Save