Skip to content

Commit

Permalink
Revert "Merge branch '27-update-resql-to-use-project-based-dsls' into…
Browse files Browse the repository at this point in the history
… dev"

This reverts commit d54acb6, reversing
changes made to de31adc.
  • Loading branch information
RayDNoper committed May 28, 2024
1 parent d54acb6 commit 75b4f80
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 116 deletions.
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ COPY pom.xml .
COPY src src
COPY libs libs
COPY templates templates
COPY .env .env

ENV sqlms.saved-queries-dir=./templates
ENV sqlms.saved-queries-dir=/DSL
RUN mkdir -p DSL/GET DSL/POST

RUN ./mvnw install:install-file -Dfile=libs/id-log-${ID_LOG_VERSION}.jar -DgroupId=ee.ria.commons -DartifactId=id-log -Dversion=${ID_LOG_VERSION} -Dpackaging=jar -DgeneratePom=true
RUN ./mvnw install -DskipTests=true
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ services:
context: .
environment:
- sqlms.saved-queries-dir=./templates/
volumes:
- ./DSL:/DSL
ports:
- 8082:8080
networks:
- resql

networks:
resql:
driver: bridge
driver: bridge
2 changes: 1 addition & 1 deletion src/main/java/rig/sqlms/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ResponseEntity<ErrorResponseBody> handleException(Exception ex, WebReques
message = "Internal error";
}
ErrorResponseBody body = new ErrorResponseBody(simpleName, message);
log.error("Writing error: %s (%s)".formatted(body, request.getDescription(false)), ex);
log.error("Writing error: %s".formatted(body), ex);
return new ResponseEntity<>(body, BAD_REQUEST);
}

Expand Down
47 changes: 9 additions & 38 deletions src/main/java/rig/sqlms/controller/QueryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,28 @@

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.webjars.NotFoundException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import rig.sqlms.service.QueryService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequiredArgsConstructor
@Slf4j
public class QueryController {
private final QueryService queryService;

private static final Pattern urlPattern = Pattern.compile("(/.+?)(/.+)");

/**
* @return JSON array containing the results
*/
@Operation(description = "Initiates previously configured POST queries")
@PostMapping(value = "/{project}/**")
public List<Map<String, Object>> execute(@PathVariable String project,
@RequestBody(required = false) Map<String, Object> parameters,
HttpServletRequest request) {
Matcher urlMatcher = urlPattern.matcher(request.getRequestURI());
if (! urlMatcher.find())
throw new NotFoundException(request.getRequestURI() + " not found");
String name = urlMatcher.group(2);
log.debug("INPUT POST: {}", name);
return queryService.execute(project, "POST", name, parameters);
}

/**
* @return JSON array containing the results
*/
@Operation(description = "Initiates previously configured GET queries")
@GetMapping(value = "/{project}/**")
public List<Map<String, Object>> execute(@PathVariable String project,
@RequestParam(required = false) MultiValueMap<String, Object> parameters,
HttpServletRequest request) {
Matcher urlMatcher = urlPattern.matcher(request.getRequestURI());
if (! urlMatcher.find())
throw new NotFoundException(request.getRequestURI() + " not found");
String name = urlMatcher.group(2);
log.debug("INPUT GET: {}", name);
return queryService.execute(project, "GET", name, parameters.toSingleValueMap());
@Operation(description = "Initiates previously configured queries")
@PostMapping("/{name}")
public List<Map<String, Object>> execute(@PathVariable String name, @RequestBody(required = false) Map<String, Object> parameters) {
return queryService.execute(name, parameters);
}

/**
Expand All @@ -62,7 +33,7 @@ public List<Map<String, Object>> execute(@PathVariable String project,
@PostMapping("/{name}/batch")
public List<List<Map<String, Object>>> executeBatch(@PathVariable String name, @RequestBody BatchRequest batchRequest) {
return batchRequest.queries.stream()
.map(parameters -> queryService.executePost(name, parameters))
.map(parameters -> queryService.execute(name, parameters))
.toList();
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/rig/sqlms/model/SavedQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ public record SavedQuery(String query, String dataSourceName) {
Objects.requireNonNull(dataSourceName);
}

public static SavedQuery of(String absolutePath, String project) throws IOException {
return new SavedQuery(getContents(absolutePath), getDatabaseName(project));
public static SavedQuery of(String absolutePath) throws IOException {
return new SavedQuery(getContents(absolutePath), getDatabaseName(absolutePath));
}

private static String getContents(String absolutePath) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(absolutePath));
return new String(encoded, StandardCharsets.UTF_8);
}

private static String getDatabaseName(String project) {
return "byk"; // TODO: change this to project if multiple datasources are needed;
private static String getDatabaseName(String absolutePath) {
return absolutePath.substring(nthLastIndexOf(2, "/", absolutePath) + 1, absolutePath.lastIndexOf("/"));
}

private static int nthLastIndexOf(int nth, String character, String string) {
Expand Down
19 changes: 2 additions & 17 deletions src/main/java/rig/sqlms/service/QueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,12 @@ public class QueryService {
private final SavedQueryService savedQueryService;
private final ResqlJdbcTemplate resqlJdbcTemplate;

public List<Map<String, Object>> execute(String method, String queryName, Map<String, Object> parameters) {
String[] projectQuery = queryName.split("/", 1);
String project = projectQuery[0];
String query = projectQuery[1];
return execute(project, method, query, parameters);
}
public List<Map<String, Object>> execute(String project, String method, String queryName, Map<String, Object> parameters) {
log.info("Incoming "+method+" for "+project + "::" + queryName);
SavedQuery savedQuery = savedQueryService.get(project, method, queryName);
public List<Map<String, Object>> execute(String queryName, Map<String, Object> parameters) {
SavedQuery savedQuery = savedQueryService.get(queryName);
setDatabaseContext(savedQuery.dataSourceName());
return resqlJdbcTemplate.queryOrExecute(savedQuery.query(), parameters);
}

public List<Map<String, Object>> executePost(String queryName, Map<String, Object> parameters) {
return execute("POST", queryName, parameters);
}

public List<Map<String, Object>> executeGet(String queryName, Map<String, Object> parameters) {
return execute("GET", queryName, parameters);
}

private void setDatabaseContext(String dataSourceName) {
configProperties.stream()
.map(DataSourceConfigProperties::getName)
Expand Down
60 changes: 11 additions & 49 deletions src/main/java/rig/sqlms/service/SavedQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,28 @@
import rig.sqlms.model.SavedQuery;

import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SavedQueryService {

private final String[] METHODS = {"GET", "POST"};

private final Map<String, Map<String, Map<String, SavedQuery>>> savedQueries = new HashMap<>();

private final static String PATHSPLITTER = "(/.+?){3}(/.+)\\..*";
public final static Pattern pathPattern = Pattern.compile(PATHSPLITTER);
private final Map<String, SavedQuery> savedQueries = new HashMap<>();

public SavedQueryService(@Value("${sqlms.saved-queries-dir}") String savedQueriesDir) {
log.info("Initializing SavedQueryService");
File dslRoot = getConfigDir(savedQueriesDir);
for (File projectDir : dslRoot.listFiles((f) -> f.isDirectory())) {
for (String method : METHODS) {
String queriesPath = projectDir.getPath() + "/" + method + "/";
log.info("Loading queries from " + queriesPath);
loadQueries(projectDir.getName(), method,
getConfigDir(savedQueriesDir + "/"
+ projectDir.getName() + "/"
+ method + "/").listFiles());
log.info("Loaded queries: " +
savedQueries.get(projectDir.getName()).get(method).entrySet().stream().map(entry-> entry.getKey()).collect(Collectors.joining(", ")));
}
}
loadQueries(getConfigDir(savedQueriesDir).listFiles());
}

private void loadQueries(String project, String method, File[] filesList) {
if (!savedQueries.containsKey(project))
savedQueries.put(project, new HashMap<>());
if (!savedQueries.get(project).containsKey(method))
savedQueries.get(project).put(method, new HashMap<>());
private void loadQueries(File[] filesList) {
try {
if (filesList != null) {
for (File file : filesList) {
if (file.isDirectory()) {
loadQueries(project, method, file.listFiles());
loadQueries(file.listFiles());
} else {
try {
savedQueries.get(project).get(method).put(getQueryName(file, project, method), SavedQuery.of(file.getAbsolutePath(), project));
savedQueries.put(getQueryName(file), SavedQuery.of(file.getAbsolutePath()));
} catch (Throwable t) {
log.error("Failed parsing saved query file {}", file.getName(), t);
}
Expand All @@ -66,21 +40,16 @@ private void loadQueries(String project, String method, File[] filesList) {
} catch (Exception e) {
log.error("Failed loading configuration service", e);
}

log.debug("Loaded queries: "+mapDeepToString(savedQueries));
}

private String getQueryName(File file, String project, String method) {
Matcher pathmatcher = pathPattern.matcher(file.getPath());
pathmatcher.find();
return pathmatcher.group(2);
// return file.getPath().substring(project.length() + 1 + method.length(), file.getName().lastIndexOf(".")).toLowerCase();
private String getQueryName(File file) {
return file.getName().substring(0, file.getName().lastIndexOf(".")).toLowerCase();
}

@NonNull
public SavedQuery get(String project, String method, String name) {
SavedQuery query = savedQueries.get(project).get(method).get(name.trim().toLowerCase());
public SavedQuery get(String name) {
SavedQuery query = savedQueries.get(name.trim().toLowerCase());

if (query == null) {
throw new ResqlRuntimeException("Saved query '%s' does not exist".formatted(name));
}
Expand All @@ -95,15 +64,8 @@ private File getConfigDir(String path) {

configDir = new File(path);
if (!configDir.exists() && !configDir.isDirectory())
throw new InvalidDirectoryException("Saved configuration directory missing or not a directory: " + path);
throw new InvalidDirectoryException("Saved configuration directory missing or not a directory");

return configDir;
}

public static <T> String mapDeepToString(Map<String, T> map) {
return map == null ? "" : map.entrySet().stream()
.map(e ->"{ " + e.getKey() + " => " + (e.getValue() instanceof Map ? mapDeepToString((Map)e.getValue()) : e.getValue().toString()) + " }")
.collect(Collectors.joining(","));
}

}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ h2:
enabled: true

sqlms:
saved-queries-dir: "/DSL/"
saved-queries-dir: "./templates/"
datasources:
- name: test_db_1
jdbcUrl: jdbc:h2:mem:test_db_1;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1
Expand Down

0 comments on commit 75b4f80

Please sign in to comment.