diff --git a/app/src/main/java/io/apicurio/registry/storage/impl/sql/AbstractSqlRegistryStorage.java b/app/src/main/java/io/apicurio/registry/storage/impl/sql/AbstractSqlRegistryStorage.java index ed0aa48a5b..1d24657631 100644 --- a/app/src/main/java/io/apicurio/registry/storage/impl/sql/AbstractSqlRegistryStorage.java +++ b/app/src/main/java/io/apicurio/registry/storage/impl/sql/AbstractSqlRegistryStorage.java @@ -943,13 +943,8 @@ public ArtifactSearchResultsDto searchArtifacts(Set filters, Order return handles.withHandleNoException(handle -> { List binders = new LinkedList<>(); - StringBuilder selectTemplate = new StringBuilder(); StringBuilder where = new StringBuilder(); StringBuilder orderByQuery = new StringBuilder(); - StringBuilder limitOffset = new StringBuilder(); - - // Formulate the SELECT clause for the artifacts query - selectTemplate.append("SELECT {{selectColumns}} FROM artifacts a "); // Formulate the WHERE clause for both queries String op; @@ -1081,19 +1076,14 @@ public ArtifactSearchResultsDto searchArtifacts(Set filters, Order } orderByQuery.append(" ").append(orderDirection.name()); - // Add limit and offset to artifact query - if ("mssql".equals(sqlStatements.dbType())) { - limitOffset.append(" OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); - } else { - limitOffset.append(" LIMIT ? OFFSET ?"); - } // Query for the artifacts - String artifactsQuerySql = new StringBuilder(selectTemplate).append(where).append(orderByQuery) - .append(limitOffset).toString().replace("{{selectColumns}}", "a.*"); + String artifactsQuerySql = sqlStatements.selectTableTemplate("a.*", "artifacts", "a", + where.toString(), orderByQuery.toString()); Query artifactsQuery = handle.createQuery(artifactsQuerySql); - String countQuerySql = new StringBuilder(selectTemplate).append(where).toString() - .replace("{{selectColumns}}", "count(a.artifactId)"); + + String countQuerySql = sqlStatements.selectCountTableTemplate("a.artifactId", "artifacts", "a", + where.toString()); Query countQuery = handle.createQuery(countQuerySql); // Bind all query parameters @@ -2803,13 +2793,9 @@ public GroupSearchResultsDto searchGroups(Set filters, OrderBy ord List binders = new LinkedList<>(); String op; - StringBuilder selectTemplate = new StringBuilder(); + StringBuilder where = new StringBuilder(); StringBuilder orderByQuery = new StringBuilder(); - StringBuilder limitOffset = new StringBuilder(); - - // Formulate the SELECT clause for the artifacts query - selectTemplate.append("SELECT {{selectColumns}} FROM `groups` g "); // Formulate the WHERE clause for both queries where.append(" WHERE (1 = 1)"); @@ -2872,20 +2858,14 @@ public GroupSearchResultsDto searchGroups(Set filters, OrderBy ord } orderByQuery.append(" ").append(orderDirection.name()); - // Add limit and offset to query - if ("mssql".equals(sqlStatements.dbType())) { - limitOffset.append(" OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); - } else { - limitOffset.append(" LIMIT ? OFFSET ?"); - } // Query for the group - String groupsQuerySql = new StringBuilder(selectTemplate).append(where).append(orderByQuery) - .append(limitOffset).toString().replace("{{selectColumns}}", "*"); + String groupsQuerySql = sqlStatements.selectTableTemplate("*", "groups", "g", + where.toString(), orderByQuery.toString()); Query groupsQuery = handle.createQuery(groupsQuerySql); // Query for the total row count - String countQuerySql = new StringBuilder(selectTemplate).append(where).toString() - .replace("{{selectColumns}}", "count(g.groupId)"); + String countQuerySql = sqlStatements.selectCountTableTemplate("g.groupId", "groups", "g", + where.toString()); Query countQuery = handle.createQuery(countQuerySql); // Bind all query parameters @@ -3393,13 +3373,8 @@ public BranchSearchResultsDto getBranches(GA ga, int offset, int limit) { return handles.withHandleNoException(handle -> { List binders = new LinkedList<>(); - StringBuilder selectTemplate = new StringBuilder(); StringBuilder where = new StringBuilder(); StringBuilder orderByQuery = new StringBuilder(); - StringBuilder limitOffset = new StringBuilder(); - - // Formulate the SELECT clause for the artifacts query - selectTemplate.append("SELECT {{selectColumns}} FROM branches b "); // Formulate the WHERE clause for both queries where.append(" WHERE b.groupId = ? AND b.artifactId = ?"); @@ -3413,20 +3388,14 @@ public BranchSearchResultsDto getBranches(GA ga, int offset, int limit) { // Add order by to artifact query orderByQuery.append(" ORDER BY b.branchId ASC"); - // Add limit and offset to query - if ("mssql".equals(sqlStatements.dbType())) { - limitOffset.append(" OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); - } else { - limitOffset.append(" LIMIT ? OFFSET ?"); - } - // Query for the branc - String branchesQuerySql = new StringBuilder(selectTemplate).append(where).append(orderByQuery) - .append(limitOffset).toString().replace("{{selectColumns}}", "*"); + // Query for the artifacts + String branchesQuerySql = sqlStatements.selectTableTemplate("*", "branches", "b", + where.toString(), orderByQuery.toString()); Query branchesQuery = handle.createQuery(branchesQuerySql); - // Query for the total row count - String countQuerySql = new StringBuilder(selectTemplate).append(where).toString() - .replace("{{selectColumns}}", "count(b.branchId)"); + + String countQuerySql = sqlStatements.selectCountTableTemplate("b.branchId", "branches", "b", + where.toString()); Query countQuery = handle.createQuery(countQuerySql); // Bind all query parameters diff --git a/app/src/main/java/io/apicurio/registry/storage/impl/sql/CommonSqlStatements.java b/app/src/main/java/io/apicurio/registry/storage/impl/sql/CommonSqlStatements.java index ff28484cf5..c03f1289b5 100644 --- a/app/src/main/java/io/apicurio/registry/storage/impl/sql/CommonSqlStatements.java +++ b/app/src/main/java/io/apicurio/registry/storage/impl/sql/CommonSqlStatements.java @@ -581,7 +581,7 @@ public String selectArtifactCountById() { */ @Override public String selectGroupCountById() { - return "SELECT COUNT(g.groupId) FROM `groups` g WHERE g.groupId = ?"; + return "SELECT COUNT(g.groupId) FROM " + groupsTable() + " g WHERE g.groupId = ?"; } /** @@ -669,7 +669,7 @@ public String insertContentReference() { */ @Override public String insertGroup() { - return "INSERT INTO `groups` (groupId, description, artifactsType, owner, createdOn, modifiedBy, modifiedOn, labels) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; + return "INSERT INTO " + groupsTable() + " (groupId, description, artifactsType, owner, createdOn, modifiedBy, modifiedOn, labels) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; } /** @@ -677,7 +677,7 @@ public String insertGroup() { */ @Override public String updateGroup() { - return "UPDATE `groups` SET description = ? , modifiedBy = ? , modifiedOn = ? , labels = ? WHERE groupId = ?"; + return "UPDATE " + groupsTable() + " SET description = ? , modifiedBy = ? , modifiedOn = ? , labels = ? WHERE groupId = ?"; } /** @@ -685,7 +685,7 @@ public String updateGroup() { */ @Override public String deleteGroup() { - return "DELETE FROM `groups` WHERE groupId = ?"; + return "DELETE FROM " + groupsTable() + " WHERE groupId = ?"; } /** @@ -693,7 +693,7 @@ public String deleteGroup() { */ @Override public String deleteAllGroups() { - return "DELETE FROM `groups` "; + return "DELETE FROM " + groupsTable() + " "; } /** @@ -701,7 +701,7 @@ public String deleteAllGroups() { */ @Override public String selectGroups() { - return "SELECT g.* FROM `groups` g ORDER BY g.groupId ASC LIMIT ?"; + return "SELECT g.* FROM " + groupsTable() + " g ORDER BY g.groupId ASC LIMIT ?"; } /** @@ -709,7 +709,7 @@ public String selectGroups() { */ @Override public String selectGroupByGroupId() { - return "SELECT g.* FROM `groups` g WHERE g.groupId = ?"; + return "SELECT g.* FROM " + groupsTable() + " g WHERE g.groupId = ?"; } @Override @@ -767,7 +767,7 @@ public String exportGlobalRules() { */ @Override public String exportGroups() { - return "SELECT * FROM `groups` g "; + return "SELECT * FROM " + groupsTable() + " g "; } @Override @@ -818,7 +818,7 @@ public String importGlobalRule() { */ @Override public String importGroup() { - return "INSERT INTO `groups` (groupId, description, artifactsType, owner, createdOn, modifiedBy, modifiedOn, labels) " + return "INSERT INTO " + groupsTable() + " (groupId, description, artifactsType, owner, createdOn, modifiedBy, modifiedOn, labels) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; } @@ -1210,4 +1210,19 @@ public String createOutboxEvent() { public String deleteOutboxEvent() { return "DELETE FROM outbox WHERE id = ?"; } + + @Override + public String selectCountTableTemplate(String countBy, String tableName, String alias, String whereClause) { + return "SELECT COUNT(%s) FROM %s %s %s".formatted(countBy, tableName, alias, whereClause); + } + + @Override + public String selectTableTemplate(String columns, String tableName, String alias, String whereClause, String orderBy) { + return "SELECT %s FROM %s %s %s %s LIMIT ? OFFSET ?".formatted(columns, tableName, alias, whereClause, orderBy); + } + + + protected String groupsTable() { + return "groups"; + } } diff --git a/app/src/main/java/io/apicurio/registry/storage/impl/sql/MySQLSqlStatements.java b/app/src/main/java/io/apicurio/registry/storage/impl/sql/MySQLSqlStatements.java index 4dc659cc9c..87170bbb6c 100644 --- a/app/src/main/java/io/apicurio/registry/storage/impl/sql/MySQLSqlStatements.java +++ b/app/src/main/java/io/apicurio/registry/storage/impl/sql/MySQLSqlStatements.java @@ -69,6 +69,21 @@ INSERT INTO branches (groupId, artifactId, branchId, description, systemDefined, """; } + @Override + public String selectCountTableTemplate(String countBy, String tableName, String alias, String whereClause) { + return super.selectCountTableTemplate(countBy, "`" + tableName + "`", alias, whereClause); + } + + @Override + public String selectTableTemplate(String columns, String tableName, String alias, String whereClause, String orderBy) { + return super.selectTableTemplate(columns, "`" + tableName + "`", alias, whereClause, orderBy); + } + + @Override + public String groupsTable() { + return "`groups`"; + } + @Override public String createDataSnapshot() { throw new IllegalStateException("Snapshot creation is not supported for MySQL storage"); diff --git a/app/src/main/java/io/apicurio/registry/storage/impl/sql/SQLServerSqlStatements.java b/app/src/main/java/io/apicurio/registry/storage/impl/sql/SQLServerSqlStatements.java index d1e39213cb..4985233d67 100644 --- a/app/src/main/java/io/apicurio/registry/storage/impl/sql/SQLServerSqlStatements.java +++ b/app/src/main/java/io/apicurio/registry/storage/impl/sql/SQLServerSqlStatements.java @@ -125,6 +125,12 @@ public String selectBranchTipFilteredByState() { + "ORDER BY bv.branchOrder DESC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"; } + @Override + public String selectTableTemplate(String columns, String tableName, String alias, String whereClause, String orderBy) { + return "SELECT %s FROM %s %s %s %s OFFSET ? ROWS FETCH NEXT ? ROWS ONLY" + .formatted(columns, tableName, alias, whereClause, orderBy); + } + @Override public String deleteAllOrphanedContent() { return "DELETE FROM content WHERE NOT EXISTS (SELECT 1 FROM versions v WHERE v.contentId = contentId )"; diff --git a/app/src/main/java/io/apicurio/registry/storage/impl/sql/SqlStatements.java b/app/src/main/java/io/apicurio/registry/storage/impl/sql/SqlStatements.java index 74ac6c65ed..c8faf84e96 100644 --- a/app/src/main/java/io/apicurio/registry/storage/impl/sql/SqlStatements.java +++ b/app/src/main/java/io/apicurio/registry/storage/impl/sql/SqlStatements.java @@ -652,4 +652,7 @@ public interface SqlStatements { public String deleteOutboxEvent(); + String selectCountTableTemplate(String countBy, String tableName, String alias, String whereClause); + + String selectTableTemplate(String columns, String tableName, String alias, String whereClause, String orderBy); }