-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Optimize paginated queries based on data sources * add startup conditions * user movkEnv * update Exception Class name * Optimize paginated queries based on data sources * update Exception Class name * Revert "add startup conditions" This reverts commit 4e2c85d * Optimize paginated queries based on data sources * add startup conditions * user movkEnv * update Exception Class name * Revert "add startup conditions" This reverts commit 4e2c85d * Revert "Revert "add startup conditions"" This reverts commit 4225891. * DialectFactory add database supported * fix auth plugin Page, add dataSource plugin Dialect * description * remove dataSource plugin Dialect * detail adjustment * DefaultPageHandlerAdapter
1 parent
7803e33
commit 736948f
Showing
22 changed files
with
1,005 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...th-plugin/src/main/java/com/alibaba/nacos/plugin/auth/impl/constant/AuthPageConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 1999-2022 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.plugin.auth.impl.constant; | ||
|
||
/** | ||
* Auth plugin page constant. | ||
* | ||
* @author huangKeming | ||
**/ | ||
|
||
public class AuthPageConstant { | ||
|
||
public static final String OFFSET = "OFFSET"; | ||
|
||
public static final String OFFSET_ROWS = "OFFSET ? ROWS"; | ||
|
||
public static final String FETCH_NEXT = "FETCH NEXT ? ROWS ONLY"; | ||
|
||
public static final String LIMIT = "LIMIT"; | ||
|
||
public static final String LIMIT_SIZE = "LIMIT ?,?"; | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...auth-plugin/src/main/java/com/alibaba/nacos/plugin/auth/impl/model/OffsetFetchResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.plugin.auth.impl.model; | ||
|
||
/** | ||
* Paginated query statements and query parameters encapsulate the results. | ||
* | ||
* @author huangKeMing | ||
*/ | ||
public class OffsetFetchResult { | ||
|
||
String fetchSql; | ||
|
||
Object[] newArgs; | ||
|
||
public OffsetFetchResult() { | ||
} | ||
|
||
public OffsetFetchResult(String fetchSql, Object[] newArgs) { | ||
this.fetchSql = fetchSql; | ||
this.newArgs = newArgs; | ||
} | ||
|
||
public String getFetchSql() { | ||
return fetchSql; | ||
} | ||
|
||
public void setFetchSql(String fetchSql) { | ||
this.fetchSql = fetchSql; | ||
} | ||
|
||
public Object[] getNewArgs() { | ||
return newArgs; | ||
} | ||
|
||
public void setNewArgs(Object[] newArgs) { | ||
this.newArgs = newArgs; | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...in/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/AuthPaginationHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.plugin.auth.impl.persistence; | ||
|
||
import com.alibaba.nacos.persistence.model.Page; | ||
import com.alibaba.nacos.plugin.datasource.model.MapperResult; | ||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
|
||
/** | ||
* Auth plugin Pagination Helper. | ||
* | ||
* @param <E> Generic class | ||
* @author huangKeMing | ||
*/ | ||
@SuppressWarnings("PMD.AbstractMethodOrInterfaceMethodMustUseJavadocRule") | ||
public interface AuthPaginationHelper<E> { | ||
|
||
Page<E> fetchPage(final String sqlCountRows, final String sqlFetchRows, final Object[] args, final int pageNo, | ||
final int pageSize, final RowMapper<E> rowMapper); | ||
|
||
Page<E> fetchPage(final String sqlCountRows, final String sqlFetchRows, final Object[] args, final int pageNo, | ||
final int pageSize, final Long lastMaxId, final RowMapper<E> rowMapper); | ||
|
||
Page<E> fetchPageLimit(final String sqlCountRows, final String sqlFetchRows, final Object[] args, final int pageNo, | ||
final int pageSize, final RowMapper<E> rowMapper); | ||
|
||
Page<E> fetchPageLimit(final String sqlCountRows, final Object[] args1, final String sqlFetchRows, | ||
final Object[] args2, final int pageNo, final int pageSize, final RowMapper<E> rowMapper); | ||
|
||
Page<E> fetchPageLimit(final String sqlFetchRows, final Object[] args, final int pageNo, final int pageSize, | ||
final RowMapper<E> rowMapper); | ||
|
||
Page<E> fetchPageLimit(final MapperResult countMapperResult, final MapperResult mapperResult, final int pageNo, | ||
final int pageSize, final RowMapper<E> rowMapper); | ||
|
||
void updateLimit(final String sql, final Object[] args); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
...alibaba/nacos/plugin/auth/impl/persistence/embedded/AuthEmbeddedPaginationHelperImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.plugin.auth.impl.persistence.embedded; | ||
|
||
import com.alibaba.nacos.plugin.auth.impl.persistence.handler.PageHandlerAdapterFactory; | ||
import com.alibaba.nacos.plugin.auth.impl.model.OffsetFetchResult; | ||
import com.alibaba.nacos.persistence.model.Page; | ||
import com.alibaba.nacos.persistence.repository.embedded.EmbeddedStorageContextHolder; | ||
import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate; | ||
import com.alibaba.nacos.plugin.auth.impl.persistence.AuthPaginationHelper; | ||
import com.alibaba.nacos.plugin.auth.impl.persistence.handler.support.DerbyPageHandlerAdapter; | ||
import com.alibaba.nacos.plugin.datasource.model.MapperResult; | ||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Auth plugin Pagination Utils For Apache Derby. | ||
* | ||
* @param <E> Generic class | ||
* @author huangKeMing | ||
*/ | ||
public class AuthEmbeddedPaginationHelperImpl<E> implements AuthPaginationHelper<E> { | ||
|
||
private final DatabaseOperate databaseOperate; | ||
|
||
public AuthEmbeddedPaginationHelperImpl(DatabaseOperate databaseOperate) { | ||
this.databaseOperate = databaseOperate; | ||
} | ||
|
||
/** | ||
* Take paging. | ||
* | ||
* @param sqlCountRows Query total SQL | ||
* @param sqlFetchRows Query data sql | ||
* @param args query args | ||
* @param pageNo page number | ||
* @param pageSize page size | ||
* @param rowMapper Entity mapping | ||
* @return Paging data | ||
*/ | ||
@Override | ||
public Page<E> fetchPage(final String sqlCountRows, final String sqlFetchRows, final Object[] args, | ||
final int pageNo, final int pageSize, final RowMapper rowMapper) { | ||
return fetchPage(sqlCountRows, sqlFetchRows, args, pageNo, pageSize, null, rowMapper); | ||
} | ||
|
||
@Override | ||
public Page<E> fetchPage(final String sqlCountRows, final String sqlFetchRows, Object[] args, final int pageNo, | ||
final int pageSize, final Long lastMaxId, final RowMapper rowMapper) { | ||
if (pageNo <= 0 || pageSize <= 0) { | ||
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero"); | ||
} | ||
|
||
// Query the total number of current records | ||
Integer rowCountInt = databaseOperate.queryOne(sqlCountRows, args, Integer.class); | ||
if (rowCountInt == null) { | ||
throw new IllegalArgumentException("fetchPageLimit error"); | ||
} | ||
|
||
// Count pages | ||
int pageCount = rowCountInt / pageSize; | ||
if (rowCountInt > pageSize * pageCount) { | ||
pageCount++; | ||
} | ||
|
||
// Create Page object | ||
final Page<E> page = new Page<>(); | ||
page.setPageNumber(pageNo); | ||
page.setPagesAvailable(pageCount); | ||
page.setTotalCount(rowCountInt); | ||
|
||
if (pageNo > pageCount) { | ||
return page; | ||
} | ||
|
||
// fill the sql Page args | ||
String fetchSql = sqlFetchRows; | ||
OffsetFetchResult offsetFetchResult = addOffsetAndFetchNext(fetchSql, args, pageNo, pageSize); | ||
fetchSql = offsetFetchResult.getFetchSql(); | ||
args = offsetFetchResult.getNewArgs(); | ||
|
||
List<E> result = databaseOperate.queryMany(fetchSql, args, rowMapper); | ||
for (E item : result) { | ||
page.getPageItems().add(item); | ||
} | ||
return page; | ||
} | ||
|
||
@Override | ||
public Page<E> fetchPageLimit(final String sqlCountRows, final String sqlFetchRows, Object[] args, final int pageNo, | ||
final int pageSize, final RowMapper rowMapper) { | ||
if (pageNo <= 0 || pageSize <= 0) { | ||
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero"); | ||
} | ||
// Query the total number of current records | ||
Integer rowCountInt = databaseOperate.queryOne(sqlCountRows, Integer.class); | ||
if (rowCountInt == null) { | ||
throw new IllegalArgumentException("fetchPageLimit error"); | ||
} | ||
|
||
// Count pages | ||
int pageCount = rowCountInt / pageSize; | ||
if (rowCountInt > pageSize * pageCount) { | ||
pageCount++; | ||
} | ||
|
||
// Create Page object | ||
final Page<E> page = new Page<>(); | ||
page.setPageNumber(pageNo); | ||
page.setPagesAvailable(pageCount); | ||
page.setTotalCount(rowCountInt); | ||
|
||
if (pageNo > pageCount) { | ||
return page; | ||
} | ||
|
||
// fill the sql Page args | ||
String fetchSql = sqlFetchRows; | ||
OffsetFetchResult offsetFetchResult = addOffsetAndFetchNext(fetchSql, args, pageNo, pageSize); | ||
fetchSql = offsetFetchResult.getFetchSql(); | ||
args = offsetFetchResult.getNewArgs(); | ||
|
||
List<E> result = databaseOperate.queryMany(fetchSql, args, rowMapper); | ||
for (E item : result) { | ||
page.getPageItems().add(item); | ||
} | ||
return page; | ||
} | ||
|
||
@Override | ||
public Page<E> fetchPageLimit(final String sqlCountRows, final Object[] args1, final String sqlFetchRows, | ||
Object[] args2, final int pageNo, final int pageSize, final RowMapper rowMapper) { | ||
if (pageNo <= 0 || pageSize <= 0) { | ||
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero"); | ||
} | ||
// Query the total number of current records | ||
Integer rowCountInt = databaseOperate.queryOne(sqlCountRows, args1, Integer.class); | ||
if (rowCountInt == null) { | ||
throw new IllegalArgumentException("fetchPageLimit error"); | ||
} | ||
|
||
// Count pages | ||
int pageCount = rowCountInt / pageSize; | ||
if (rowCountInt > pageSize * pageCount) { | ||
pageCount++; | ||
} | ||
|
||
// Create Page object | ||
final Page<E> page = new Page<>(); | ||
page.setPageNumber(pageNo); | ||
page.setPagesAvailable(pageCount); | ||
page.setTotalCount(rowCountInt); | ||
|
||
if (pageNo > pageCount) { | ||
return page; | ||
} | ||
|
||
// fill the sql Page args | ||
String fetchSql = sqlFetchRows; | ||
OffsetFetchResult offsetFetchResult = addOffsetAndFetchNext(fetchSql, args2, pageNo, pageSize); | ||
fetchSql = offsetFetchResult.getFetchSql(); | ||
args2 = offsetFetchResult.getNewArgs(); | ||
|
||
List<E> result = databaseOperate.queryMany(fetchSql, args2, rowMapper); | ||
for (E item : result) { | ||
page.getPageItems().add(item); | ||
} | ||
return page; | ||
} | ||
|
||
@Override | ||
public Page<E> fetchPageLimit(final String sqlFetchRows, Object[] args, final int pageNo, final int pageSize, | ||
final RowMapper rowMapper) { | ||
if (pageNo <= 0 || pageSize <= 0) { | ||
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero"); | ||
} | ||
// Create Page object | ||
final Page<E> page = new Page<>(); | ||
|
||
// fill the sql Page args | ||
String fetchSql = sqlFetchRows; | ||
OffsetFetchResult offsetFetchResult = addOffsetAndFetchNext(fetchSql, args, pageNo, pageSize); | ||
fetchSql = offsetFetchResult.getFetchSql(); | ||
args = offsetFetchResult.getNewArgs(); | ||
|
||
List<E> result = databaseOperate.queryMany(fetchSql, args, rowMapper); | ||
for (E item : result) { | ||
page.getPageItems().add(item); | ||
} | ||
return page; | ||
} | ||
|
||
@Override | ||
public Page fetchPageLimit(MapperResult countMapperResult, MapperResult mapperResult, int pageNo, int pageSize, | ||
RowMapper rowMapper) { | ||
return fetchPageLimit(countMapperResult.getSql(), countMapperResult.getParamList().toArray(), | ||
mapperResult.getSql(), mapperResult.getParamList().toArray(), pageNo, pageSize, rowMapper); | ||
} | ||
|
||
@Override | ||
public void updateLimit(final String sql, final Object[] args) { | ||
EmbeddedStorageContextHolder.addSqlContext(sql, args); | ||
try { | ||
databaseOperate.update(EmbeddedStorageContextHolder.getCurrentSqlContext()); | ||
} finally { | ||
EmbeddedStorageContextHolder.cleanAllContext(); | ||
} | ||
} | ||
|
||
private OffsetFetchResult addOffsetAndFetchNext(String fetchSql, Object[] arg, int pageNo, int pageSize) { | ||
return PageHandlerAdapterFactory.getInstance().getHandlerAdapterMap() | ||
.get(DerbyPageHandlerAdapter.class.getName()).addOffsetAndFetchNext(fetchSql, arg, pageNo, pageSize); | ||
} | ||
|
||
} |
267 changes: 267 additions & 0 deletions
267
.../alibaba/nacos/plugin/auth/impl/persistence/extrnal/AuthExternalPaginationHelperImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.plugin.auth.impl.persistence.extrnal; | ||
|
||
import com.alibaba.nacos.persistence.model.Page; | ||
import com.alibaba.nacos.persistence.repository.embedded.EmbeddedStorageContextHolder; | ||
import com.alibaba.nacos.plugin.auth.impl.model.OffsetFetchResult; | ||
import com.alibaba.nacos.plugin.auth.impl.persistence.AuthPaginationHelper; | ||
import com.alibaba.nacos.plugin.auth.impl.persistence.handler.PageHandlerAdapter; | ||
import com.alibaba.nacos.plugin.auth.impl.persistence.handler.PageHandlerAdapterFactory; | ||
import com.alibaba.nacos.plugin.auth.impl.persistence.handler.support.DefaultPageHandlerAdapter; | ||
import com.alibaba.nacos.plugin.datasource.model.MapperResult; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Auth plugin Pagination Utils For Apache External. | ||
* | ||
* @param <E> Generic class | ||
* @author huangKeMing | ||
*/ | ||
public class AuthExternalPaginationHelperImpl<E> implements AuthPaginationHelper<E> { | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
private volatile String dataSourceType; | ||
|
||
public AuthExternalPaginationHelperImpl(JdbcTemplate jdbcTemplate, String dataSourceType) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
this.dataSourceType = dataSourceType; | ||
} | ||
|
||
/** | ||
* Take paging. | ||
* | ||
* @param sqlCountRows query total SQL | ||
* @param sqlFetchRows query data sql | ||
* @param args query parameters | ||
* @param pageNo page number | ||
* @param pageSize page size | ||
* @param rowMapper {@link RowMapper} | ||
* @return Paginated data {@code <E>} | ||
*/ | ||
@Override | ||
public Page<E> fetchPage(final String sqlCountRows, final String sqlFetchRows, final Object[] args, | ||
final int pageNo, final int pageSize, final RowMapper rowMapper) { | ||
return fetchPage(sqlCountRows, sqlFetchRows, args, pageNo, pageSize, null, rowMapper); | ||
} | ||
|
||
@Override | ||
public Page<E> fetchPage(final String sqlCountRows, final String sqlFetchRows, Object[] args, final int pageNo, | ||
final int pageSize, final Long lastMaxId, final RowMapper rowMapper) { | ||
if (pageNo <= 0 || pageSize <= 0) { | ||
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero"); | ||
} | ||
|
||
// Query the total number of current records. | ||
Integer rowCountInt = jdbcTemplate.queryForObject(sqlCountRows, args, Integer.class); | ||
if (rowCountInt == null) { | ||
throw new IllegalArgumentException("fetchPageLimit error"); | ||
} | ||
|
||
// Compute pages count | ||
int pageCount = rowCountInt / pageSize; | ||
if (rowCountInt > pageSize * pageCount) { | ||
pageCount++; | ||
} | ||
|
||
// Create Page object | ||
final Page<E> page = new Page<>(); | ||
page.setPageNumber(pageNo); | ||
page.setPagesAvailable(pageCount); | ||
page.setTotalCount(rowCountInt); | ||
|
||
if (pageNo > pageCount) { | ||
return page; | ||
} | ||
|
||
// fill the sql Page args | ||
String fetchSql = sqlFetchRows; | ||
OffsetFetchResult offsetFetchResult = addOffsetAndFetchNext(fetchSql, args, pageNo, pageSize); | ||
fetchSql = offsetFetchResult.getFetchSql(); | ||
args = offsetFetchResult.getNewArgs(); | ||
|
||
List<E> result = jdbcTemplate.query(fetchSql, args, rowMapper); | ||
for (E item : result) { | ||
page.getPageItems().add(item); | ||
} | ||
return page; | ||
} | ||
|
||
@Override | ||
public Page<E> fetchPageLimit(final String sqlCountRows, final String sqlFetchRows, Object[] args, final int pageNo, | ||
final int pageSize, final RowMapper rowMapper) { | ||
if (pageNo <= 0 || pageSize <= 0) { | ||
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero"); | ||
} | ||
// Query the total number of current records | ||
Integer rowCountInt = jdbcTemplate.queryForObject(sqlCountRows, Integer.class); | ||
if (rowCountInt == null) { | ||
throw new IllegalArgumentException("fetchPageLimit error"); | ||
} | ||
|
||
// Compute pages count | ||
int pageCount = rowCountInt / pageSize; | ||
if (rowCountInt > pageSize * pageCount) { | ||
pageCount++; | ||
} | ||
|
||
// Create Page object | ||
final Page<E> page = new Page<>(); | ||
page.setPageNumber(pageNo); | ||
page.setPagesAvailable(pageCount); | ||
page.setTotalCount(rowCountInt); | ||
|
||
if (pageNo > pageCount) { | ||
return page; | ||
} | ||
|
||
// fill the sql Page args | ||
String fetchSql = sqlFetchRows; | ||
OffsetFetchResult offsetFetchResult = addOffsetAndFetchNext(fetchSql, args, pageNo, pageSize); | ||
fetchSql = offsetFetchResult.getFetchSql(); | ||
args = offsetFetchResult.getNewArgs(); | ||
|
||
List<E> result = jdbcTemplate.query(fetchSql, args, rowMapper); | ||
for (E item : result) { | ||
page.getPageItems().add(item); | ||
} | ||
return page; | ||
} | ||
|
||
@Override | ||
public Page fetchPageLimit(MapperResult countMapperResult, MapperResult mapperResult, int pageNo, int pageSize, | ||
RowMapper rowMapper) { | ||
return fetchPageLimit(countMapperResult.getSql(), countMapperResult.getParamList().toArray(), | ||
mapperResult.getSql(), mapperResult.getParamList().toArray(), pageNo, pageSize, rowMapper); | ||
} | ||
|
||
@Override | ||
public Page<E> fetchPageLimit(final String sqlCountRows, final Object[] args1, final String sqlFetchRows, | ||
Object[] args2, final int pageNo, final int pageSize, final RowMapper rowMapper) { | ||
if (pageNo <= 0 || pageSize <= 0) { | ||
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero"); | ||
} | ||
// Query the total number of current records | ||
Integer rowCountInt = jdbcTemplate.queryForObject(sqlCountRows, args1, Integer.class); | ||
if (rowCountInt == null) { | ||
throw new IllegalArgumentException("fetchPageLimit error"); | ||
} | ||
|
||
// Compute pages count | ||
int pageCount = rowCountInt / pageSize; | ||
if (rowCountInt > pageSize * pageCount) { | ||
pageCount++; | ||
} | ||
|
||
// Create Page object | ||
final Page<E> page = new Page<>(); | ||
page.setPageNumber(pageNo); | ||
page.setPagesAvailable(pageCount); | ||
page.setTotalCount(rowCountInt); | ||
|
||
if (pageNo > pageCount) { | ||
return page; | ||
} | ||
// fill the sql Page args | ||
String fetchSql = sqlFetchRows; | ||
OffsetFetchResult offsetFetchResult = addOffsetAndFetchNext(fetchSql, args2, pageNo, pageSize); | ||
fetchSql = offsetFetchResult.getFetchSql(); | ||
args2 = offsetFetchResult.getNewArgs(); | ||
|
||
List<E> result = jdbcTemplate.query(fetchSql, args2, rowMapper); | ||
for (E item : result) { | ||
page.getPageItems().add(item); | ||
} | ||
return page; | ||
} | ||
|
||
@Override | ||
public Page<E> fetchPageLimit(final String sqlFetchRows, Object[] args, final int pageNo, final int pageSize, | ||
final RowMapper rowMapper) { | ||
if (pageNo <= 0 || pageSize <= 0) { | ||
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero"); | ||
} | ||
// Create Page object | ||
final Page<E> page = new Page<>(); | ||
|
||
// fill the sql Page args | ||
String fetchSql = sqlFetchRows; | ||
OffsetFetchResult offsetFetchResult = addOffsetAndFetchNext(fetchSql, args, pageNo, pageSize); | ||
fetchSql = offsetFetchResult.getFetchSql(); | ||
args = offsetFetchResult.getNewArgs(); | ||
|
||
List<E> result = jdbcTemplate.query(fetchSql, args, rowMapper); | ||
for (E item : result) { | ||
page.getPageItems().add(item); | ||
} | ||
return page; | ||
} | ||
|
||
@Override | ||
public void updateLimit(final String sql, final Object[] args) { | ||
try { | ||
jdbcTemplate.update(sql, args); | ||
} finally { | ||
EmbeddedStorageContextHolder.cleanAllContext(); | ||
} | ||
} | ||
|
||
/** | ||
* Update limit with response. | ||
* | ||
* @param sql sql | ||
* @param args args | ||
* @return update row count | ||
*/ | ||
public int updateLimitWithResponse(final String sql, final Object[] args) { | ||
String sqlUpdate = sql; | ||
|
||
try { | ||
return jdbcTemplate.update(sqlUpdate, args); | ||
} finally { | ||
EmbeddedStorageContextHolder.cleanAllContext(); | ||
} | ||
} | ||
|
||
private OffsetFetchResult addOffsetAndFetchNext(String fetchSql, Object[] arg, int pageNo, int pageSize) { | ||
return getHandlerAdapter(dataSourceType).addOffsetAndFetchNext(fetchSql, arg, pageNo, pageSize); | ||
} | ||
|
||
/** | ||
* Get handler adapter. | ||
* | ||
* @param dataSourceType data source type. | ||
* @return | ||
*/ | ||
protected PageHandlerAdapter getHandlerAdapter(String dataSourceType) { | ||
List<PageHandlerAdapter> handlerAdapters = PageHandlerAdapterFactory.getInstance().getHandlerAdapters(); | ||
for (PageHandlerAdapter adapter : handlerAdapters) { | ||
if (adapter.supports(dataSourceType)) { | ||
return adapter; | ||
} | ||
|
||
} | ||
|
||
return PageHandlerAdapterFactory.getInstance().getHandlerAdapterMap() | ||
.get(DefaultPageHandlerAdapter.class.getName()); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
.../main/java/com/alibaba/nacos/plugin/auth/impl/persistence/handler/PageHandlerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.plugin.auth.impl.persistence.handler; | ||
|
||
import com.alibaba.nacos.plugin.auth.impl.model.OffsetFetchResult; | ||
|
||
/** | ||
* Auth plugin page handler adapter. | ||
* | ||
* @author huangKeMing | ||
*/ | ||
public interface PageHandlerAdapter { | ||
|
||
/** | ||
* Determine whether the current data source supports paging. | ||
* | ||
* @param dataSourceType data source type | ||
* @return true if the current data source supports paging | ||
*/ | ||
boolean supports(String dataSourceType); | ||
|
||
/** | ||
* Add offset and fetch next. | ||
* | ||
* @param fetchSql fetch sql. | ||
* @param arg arguments. | ||
* @param pageNo page number. | ||
* @param pageSize page size. | ||
* @return | ||
*/ | ||
OffsetFetchResult addOffsetAndFetchNext(String fetchSql, Object[] arg, int pageNo, int pageSize); | ||
} |
83 changes: 83 additions & 0 deletions
83
...ava/com/alibaba/nacos/plugin/auth/impl/persistence/handler/PageHandlerAdapterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.plugin.auth.impl.persistence.handler; | ||
|
||
import com.alibaba.nacos.plugin.auth.impl.persistence.handler.support.DerbyPageHandlerAdapter; | ||
import com.alibaba.nacos.plugin.auth.impl.persistence.handler.support.MysqlPageHandlerAdapter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* pagination factory. | ||
* | ||
* @author huangKeMing | ||
*/ | ||
public class PageHandlerAdapterFactory { | ||
|
||
private static PageHandlerAdapterFactory instance; | ||
|
||
private List<PageHandlerAdapter> handlerAdapters; | ||
|
||
private Map<String, PageHandlerAdapter> handlerAdapterMap; | ||
|
||
public List<PageHandlerAdapter> getHandlerAdapters() { | ||
return handlerAdapters; | ||
} | ||
|
||
public Map<String, PageHandlerAdapter> getHandlerAdapterMap() { | ||
return handlerAdapterMap; | ||
} | ||
|
||
private PageHandlerAdapterFactory() { | ||
handlerAdapters = new ArrayList<>(2); | ||
handlerAdapterMap = new HashMap<>(2); | ||
initHandlerAdapters(); | ||
} | ||
|
||
public static PageHandlerAdapterFactory getInstance() { | ||
if (instance == null) { | ||
synchronized (PageHandlerAdapterFactory.class) { | ||
if (instance == null) { | ||
instance = new PageHandlerAdapterFactory(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
/** | ||
* init handler adapters. | ||
*/ | ||
private void initHandlerAdapters() { | ||
// MysqlPageHandlerAdapter | ||
addHandlerAdapter(new MysqlPageHandlerAdapter()); | ||
// DerbyPageHandlerAdapter | ||
addHandlerAdapter(new DerbyPageHandlerAdapter()); | ||
// DefaultPageHandlerAdapter | ||
addHandlerAdapter(new DerbyPageHandlerAdapter()); | ||
} | ||
|
||
private void addHandlerAdapter(PageHandlerAdapter handlerAdapter) { | ||
handlerAdapters.add(handlerAdapter); | ||
handlerAdapterMap.put(handlerAdapter.getClass().getName(), handlerAdapter); | ||
} | ||
|
||
} | ||
|
39 changes: 39 additions & 0 deletions
39
...alibaba/nacos/plugin/auth/impl/persistence/handler/support/DefaultPageHandlerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.plugin.auth.impl.persistence.handler.support; | ||
|
||
import com.alibaba.nacos.plugin.auth.impl.model.OffsetFetchResult; | ||
import com.alibaba.nacos.plugin.auth.impl.persistence.handler.PageHandlerAdapter; | ||
|
||
/** | ||
* Default page handler adapter. | ||
* | ||
* @author huangKeMing | ||
*/ | ||
public class DefaultPageHandlerAdapter implements PageHandlerAdapter { | ||
|
||
@Override | ||
public boolean supports(String dataSourceType) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public OffsetFetchResult addOffsetAndFetchNext(String fetchSql, Object[] arg, int pageNo, int pageSize) { | ||
return new OffsetFetchResult(fetchSql, arg); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...m/alibaba/nacos/plugin/auth/impl/persistence/handler/support/DerbyPageHandlerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.plugin.auth.impl.persistence.handler.support; | ||
|
||
import com.alibaba.nacos.persistence.constants.PersistenceConstant; | ||
import com.alibaba.nacos.plugin.auth.impl.constant.AuthPageConstant; | ||
import com.alibaba.nacos.plugin.auth.impl.model.OffsetFetchResult; | ||
import com.alibaba.nacos.plugin.auth.impl.persistence.handler.PageHandlerAdapter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* derby page handler adapter. | ||
* | ||
* @author huangKeMing | ||
*/ | ||
public class DerbyPageHandlerAdapter implements PageHandlerAdapter { | ||
|
||
@Override | ||
public boolean supports(String dataSourceType) { | ||
return PersistenceConstant.DERBY.equals(dataSourceType); | ||
} | ||
|
||
@Override | ||
public OffsetFetchResult addOffsetAndFetchNext(String fetchSql, Object[] arg, int pageNo, int pageSize) { | ||
if (!fetchSql.contains(AuthPageConstant.OFFSET)) { | ||
fetchSql += " " + AuthPageConstant.OFFSET_ROWS + " " + AuthPageConstant.FETCH_NEXT; | ||
|
||
List<Object> newArgsList = new ArrayList<>(Arrays.asList(arg)); | ||
newArgsList.add((pageNo - 1) * pageSize); | ||
newArgsList.add(pageSize); | ||
|
||
Object[] newArgs = newArgsList.toArray(new Object[newArgsList.size()]); | ||
|
||
return new OffsetFetchResult(fetchSql, newArgs); | ||
} | ||
|
||
return new OffsetFetchResult(fetchSql, arg); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...m/alibaba/nacos/plugin/auth/impl/persistence/handler/support/MysqlPageHandlerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.plugin.auth.impl.persistence.handler.support; | ||
|
||
import com.alibaba.nacos.persistence.constants.PersistenceConstant; | ||
import com.alibaba.nacos.plugin.auth.impl.constant.AuthPageConstant; | ||
import com.alibaba.nacos.plugin.auth.impl.model.OffsetFetchResult; | ||
import com.alibaba.nacos.plugin.auth.impl.persistence.handler.PageHandlerAdapter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* mysql page handler adapter. | ||
* | ||
* @author huangKeMing | ||
*/ | ||
public class MysqlPageHandlerAdapter implements PageHandlerAdapter { | ||
|
||
@Override | ||
public boolean supports(String dataSourceType) { | ||
return PersistenceConstant.MYSQL.equals(dataSourceType); | ||
} | ||
|
||
@Override | ||
public OffsetFetchResult addOffsetAndFetchNext(String fetchSql, Object[] arg, int pageNo, int pageSize) { | ||
if (!fetchSql.contains(AuthPageConstant.LIMIT)) { | ||
fetchSql += " " + AuthPageConstant.LIMIT_SIZE; | ||
List<Object> newArgsList = new ArrayList<>(Arrays.asList(arg)); | ||
newArgsList.add((pageNo - 1) * pageSize); | ||
newArgsList.add(pageSize); | ||
|
||
Object[] newArgs = newArgsList.toArray(new Object[newArgsList.size()]); | ||
return new OffsetFetchResult(fetchSql, newArgs); | ||
} | ||
|
||
return new OffsetFetchResult(fetchSql, arg); | ||
} | ||
|
||
} |