Skip to content

Commit

Permalink
removeIn
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Oct 1, 2023
1 parent 7759a1a commit 3244846
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 58 deletions.
2 changes: 1 addition & 1 deletion sqli-builder/src/main/java/io/xream/sqli/builder/Q.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.xream.sqli.api.Routable;
import io.xream.sqli.mapping.Mappable;
import io.xream.sqli.mapping.XHelpful;
import io.xream.sqli.mapping.SqlNormalizer;
import io.xream.sqli.mapping.XHelpful;
import io.xream.sqli.page.Paged;
import io.xream.sqli.parser.Parsed;
import io.xream.sqli.util.BeanUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
package io.xream.sqli.builder.internal;

import io.xream.sqli.builder.*;
import io.xream.sqli.exception.QSyntaxException;
import io.xream.sqli.exception.ParsingException;
import io.xream.sqli.exception.QSyntaxException;
import io.xream.sqli.exception.SqlBuildException;
import io.xream.sqli.filter.BaseTypeFilter;
import io.xream.sqli.mapping.Mappable;
import io.xream.sqli.parser.BeanElement;
import io.xream.sqli.parser.Parsed;
import io.xream.sqli.parser.Parser;
import io.xream.sqli.support.XSingleSourceSupport;
import io.xream.sqli.support.TimeSupport;
import io.xream.sqli.support.XSingleSourceSupport;
import io.xream.sqli.util.EnumUtil;
import io.xream.sqli.util.SqliJsonUtil;
import io.xream.sqli.util.SqliStringUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.xream.sqli.annotation.X;
import io.xream.sqli.builder.Q;
import io.xream.sqli.exception.NotSupportedException;
import io.xream.sqli.exception.ParsingException;
import io.xream.sqli.util.BeanUtil;
import io.xream.sqli.util.ParserUtil;
import io.xream.sqli.util.SqliExceptionUtil;
Expand Down
6 changes: 4 additions & 2 deletions sqli-core/src/main/java/io/xream/sqli/api/BaseRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ public interface BaseRepository<T> extends Typed<T> {
*/
boolean refreshUnSafe(Qr<T> qr);

boolean remove(String keyOne);
boolean remove(String id);

boolean remove(long keyOne);
boolean remove(long id);

boolean removeIn(List<? extends Object> idList);

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package io.xream.sqli.cache.internal;

import io.xream.sqli.builder.Bb;
import io.xream.sqli.builder.Q;
import io.xream.sqli.builder.KV;
import io.xream.sqli.builder.Q;
import io.xream.sqli.builder.Sort;
import io.xream.sqli.cache.CacheKeyBuildable;
import io.xream.sqli.util.SqliStringUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
package io.xream.sqli.cache.internal;

import io.xream.sqli.builder.Q;
import io.xream.sqli.builder.In;
import io.xream.sqli.builder.Q;
import io.xream.sqli.cache.QueryForCache;
import io.xream.sqli.exception.L2CacheException;
import io.xream.sqli.exception.NoResultUnderProtectionException;
Expand Down
30 changes: 30 additions & 0 deletions sqli-core/src/main/java/io/xream/sqli/core/Keys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2020 io.xream.sqli
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 io.xream.sqli.core;

import java.util.List;

/**
* @author Sim
*/
public interface Keys<T> {

List<? extends Object> list();
Class<T> getClzz();
}
37 changes: 8 additions & 29 deletions sqli-core/src/main/java/io/xream/sqli/core/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,26 @@
*/
public interface Repository extends QueryForCache {

/**
* @param clz
*/
<T> void refreshCache(Class<T> clz);
/**
* @param obj
*/

boolean create(Object obj);
boolean createOrReplace(Object obj);
/**
* @param qr
* @return true | false
*/

<T> boolean refresh(Qr<T> qr);
/**
* @param keyOne
*/

<T> boolean remove(KeyOne<T> keyOne);

<T> boolean removeIn(Keys<T> keys);


<T> T get(KeyOne<T> keyOne);
/**
* @param cond
*
*/

<T> List<T> list(Object cond);

/**
* @param q
*/
<T> Page<T> find(Q q);

/**
* @param xq
*
*/
Page<Map<String,Object>> find(Q.X xq);
/**
*
* @param xq
*
*/

List<Map<String,Object>> list(Q.X xq);

<K> List<K> listPlainValue(Class<K> clzz, Q.X xq);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
package io.xream.sqli.repository.core;


import io.xream.sqli.builder.Q;
import io.xream.sqli.builder.In;
import io.xream.sqli.builder.KV;
import io.xream.sqli.builder.Q;
import io.xream.sqli.builder.Qr;
import io.xream.sqli.core.KeyOne;
import io.xream.sqli.core.NativeSupport;
import io.xream.sqli.core.Repository;
import io.xream.sqli.core.RowHandler;
import io.xream.sqli.core.*;
import io.xream.sqli.exception.QueryException;
import io.xream.sqli.page.Page;
import io.xream.sqli.parser.Parsed;
Expand Down Expand Up @@ -175,6 +172,30 @@ public <T> boolean remove(KeyOne<T> keyOne) {
String key = String.valueOf(keyOne.get());
cacheResolver.refresh(clz, key);
}

return flag;
}

@Override
public <T> boolean removeIn(Keys<T> keys) {

Class clz = keys.getClzz();
Parsed parsed = Parser.get(clz);

if (parsed.getKey() == null)
throw new IllegalStateException("no primary key, can not call remove(id)");

boolean flag = dao.removeIn(keys);

if (!flag) return flag;

if (isCacheEnabled(parsed)) {
for (Object o : keys.list()) {
String key = String.valueOf(o);
cacheResolver.refresh(clz, key);
}
}

return flag;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
*/
package io.xream.sqli.repository.dao;

import io.xream.sqli.builder.Q;
import io.xream.sqli.builder.In;
import io.xream.sqli.builder.Q;
import io.xream.sqli.builder.Qr;
import io.xream.sqli.cache.QueryForCache;
import io.xream.sqli.core.KeyOne;
import io.xream.sqli.core.Keys;
import io.xream.sqli.core.RowHandler;
import io.xream.sqli.page.Page;

Expand All @@ -45,6 +46,8 @@ public interface Dao extends QueryForCache {

<T> boolean remove(KeyOne<T> keyOne);

<T> boolean removeIn(Keys<T> keys);

<T> boolean refreshByCondition(Qr<T> conditon);

<T> List<T> list(Object conditionObj);
Expand Down Expand Up @@ -76,4 +79,5 @@ List<Map<String,Object>> list(String sql,
void findToHandle(Q.X xq, RowHandler<Map<String, Object>> handler);

boolean exists(Q q);

}
18 changes: 16 additions & 2 deletions sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.xream.sqli.builder.internal.PageBuilderHelper;
import io.xream.sqli.converter.ObjectDataConverter;
import io.xream.sqli.core.KeyOne;
import io.xream.sqli.core.Keys;
import io.xream.sqli.core.RowHandler;
import io.xream.sqli.dialect.Dialect;
import io.xream.sqli.exception.ExceptionTranslator;
Expand Down Expand Up @@ -72,8 +73,8 @@ public Dialect getDialect(){
return this.dialect;
}

public void set2Sql(Q2Sql condToSql) {
this.q2Sql = condToSql;
public void set2Sql(Q2Sql q2Sql) {
this.q2Sql = q2Sql;
}

public void setJdbcHelper(JdbcHelper jdbcHelper) {
Expand Down Expand Up @@ -121,6 +122,19 @@ public <T> boolean remove(KeyOne<T> keyOne) {
return this.jdbcHelper.remove(sql, keyOne.get());
}

@Override
public <T> boolean removeIn(Keys<T> keys) {

Class clz = keys.getClzz();
String sql = getSql(clz, SqlInit.REMOVE_IN);

sql = sqlBuilder.buildQueryByInCondition(sql, clz, keys.list());

SqliLoggerProxy.debug(clz, sql);

return this.jdbcHelper.execute(sql);
}

@Override
public boolean create(Object obj) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ protected String buildQueryByInCondition(String sqlSegment, String mapper, BeanE
return sb.toString();
}

protected String buildQueryByInCondition(String sqlSegment, Class<?> keyType, List<? extends Object> inList) {

StringBuilder sb = new StringBuilder();
sb.append(sqlSegment);

buildIn(sb,keyType,inList);

return sb.toString();
}

protected SqlBuilt buildQueryByQ(List<Object> valueList, Q q, Q2Sql qParser, Dialect dialect) {

final SqlBuilt sqlBuilt = new SqlBuilt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ public String getRemoveSql(Class clz) {
return sql;
}

public String getRemoveInSql(Class clz) {
Parsed parsed = Parser.get(clz);
StringBuilder sb = new StringBuilder();
sb.append(dialect.getAlterTableDelete()).append(SqlScript.SPACE);
sb.append(BeanUtil.getByFirstLower(parsed.getClzName())).append(dialect.getCommandDelete());
sb.append("WHERE ");

parseKeyIns(sb, clz);

String sql = sb.toString();

sql = SqlParserUtil.mapper(sql, parsed);

getSqlMap(clz).put(REMOVE_IN, sql);

SqliLoggerProxy.debug(clz, sb);

return sql;
}

public String getOneSql(Class clz) {
Parsed parsed = Parser.get(clz);
String space = " ";
Expand Down Expand Up @@ -112,6 +132,15 @@ public void parseKey(StringBuilder sb, Class clz) {
}
}

public void parseKeyIns(StringBuilder sb, Class clz) {
Parsed parsed = Parser.get(clz);

if (parsed.getClzName() != null) {
sb.append(parsed.getKey());
sb.append(" IN");
}
}

public String getLoadSql(Class clz) {

Parsed parsed = Parser.get(clz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ public interface SqlInit extends SqlTemplate{

String getRemoveSql(Class clz);

String getRemoveInSql(Class clz);

default void tryToParse(Class clz) {

getRemoveSql(clz);
getRemoveInSql(clz);
getOneSql(clz);
getLoadSql(clz);
getCreateSql(clz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface SqlTemplate {

String CREATE = "CREATE";
String REMOVE = "REMOVE";
String REMOVE_IN = "REMOVE_IN";
String LOAD = "LOAD";
String GET_ONE = "GET_ONE";

Expand Down
Loading

0 comments on commit 3244846

Please sign in to comment.