Skip to content

Commit fbe721b

Browse files
committed
delete done
1 parent 74d3b4d commit fbe721b

File tree

8 files changed

+255
-0
lines changed

8 files changed

+255
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.naskar.fluentquery;
2+
3+
import com.naskar.fluentquery.impl.DeleteConverter;
4+
5+
public interface Delete<T> extends Whereable<T, Delete<T>> {
6+
7+
Class<T> getClazz();
8+
9+
<E> E to(DeleteConverter<E> converter);
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.naskar.fluentquery;
2+
3+
import com.naskar.fluentquery.impl.DeleteImpl;
4+
5+
public class DeleteBuilder {
6+
7+
public <T> Delete<T> entity(Class<T> clazz) {
8+
return new DeleteImpl<T>(clazz);
9+
}
10+
11+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.naskar.fluentquery.converters;
2+
3+
import java.util.List;
4+
5+
import com.naskar.fluentquery.conventions.SimpleConvention;
6+
import com.naskar.fluentquery.impl.Convention;
7+
import com.naskar.fluentquery.impl.DeleteConverter;
8+
import com.naskar.fluentquery.impl.DeleteImpl;
9+
import com.naskar.fluentquery.impl.DeleteParts;
10+
import com.naskar.fluentquery.impl.HolderInt;
11+
import com.naskar.fluentquery.impl.MethodRecordProxy;
12+
13+
public class NativeSQLDelete implements DeleteConverter<NativeSQLResult> {
14+
15+
private Convention convention;
16+
private NativeSQLWhereImpl nativeWhereImpl;
17+
18+
public NativeSQLDelete(Convention convention) {
19+
this.convention = convention;
20+
this.nativeWhereImpl = new NativeSQLWhereImpl();
21+
this.nativeWhereImpl.setConvention(convention);
22+
}
23+
24+
public NativeSQLDelete() {
25+
this(new SimpleConvention());
26+
}
27+
28+
public NativeSQLDelete setConvention(Convention convention) {
29+
this.convention = convention;
30+
this.nativeWhereImpl.setConvention(convention);
31+
return this;
32+
}
33+
34+
@Override
35+
public <T> NativeSQLResult convert(DeleteImpl<T> deleteImpl) {
36+
37+
NativeSQLResult result = new NativeSQLResult();
38+
39+
DeleteParts parts = new DeleteParts();
40+
41+
HolderInt level = new HolderInt();
42+
level.value = 0;
43+
44+
convert(deleteImpl, parts, level, result, null);
45+
46+
StringBuilder sb = new StringBuilder();
47+
48+
sb.append("delete from ");
49+
sb.append(parts.getTable());
50+
51+
if(parts.hasWhere()) {
52+
sb.append(" where ");
53+
sb.append(parts.getWhere());
54+
}
55+
56+
return result.sql(sb.toString());
57+
}
58+
59+
private <T> void convert(DeleteImpl<T> deleteImpl, DeleteParts parts,
60+
final HolderInt level, NativeSQLResult result, List<String> parents) {
61+
MethodRecordProxy<T> proxy = new MethodRecordProxy<T>(createInstance(deleteImpl.getClazz()));
62+
63+
String alias = "e" + level + ".";
64+
65+
convertTable(parts.getTable(), alias, deleteImpl.getClazz());
66+
67+
nativeWhereImpl.convertWhere(parts.getWhere(), alias, proxy, parents, deleteImpl.getPredicates(), result);
68+
}
69+
70+
private <T> T createInstance(Class<T> clazz) {
71+
try {
72+
return clazz.newInstance();
73+
} catch (Exception e) {
74+
throw new RuntimeException(e);
75+
}
76+
}
77+
78+
private <T> void convertTable(StringBuilder sb, String alias, Class<T> clazz) {
79+
sb.append(convention.getNameFromClass(clazz) + " " +
80+
alias.substring(0, alias.length()-1));
81+
}
82+
83+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.naskar.fluentquery.impl;
2+
3+
public interface DeleteConverter<E> {
4+
5+
<T> E convert(DeleteImpl<T> deleteImpl);
6+
7+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.naskar.fluentquery.impl;
2+
3+
import com.naskar.fluentquery.Delete;
4+
import com.naskar.fluentquery.converters.PredicateProvider;
5+
6+
public class DeleteImpl<T>
7+
extends WhereImpl<T, Delete<T>, DeleteImpl<T>>
8+
implements Delete<T>, PredicateProvider<T, DeleteImpl<T>> {
9+
10+
private Class<T> clazz;
11+
12+
public DeleteImpl(Class<T> clazz) {
13+
super(clazz);
14+
this.clazz = clazz;
15+
}
16+
17+
public Class<T> getClazz() {
18+
return clazz;
19+
}
20+
21+
@Override
22+
public <E> E to(DeleteConverter<E> converter) {
23+
return converter.convert(this);
24+
}
25+
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.naskar.fluentquery.impl;
2+
3+
public class DeleteParts {
4+
5+
private StringBuilder table;
6+
private StringBuilder where;
7+
8+
public DeleteParts() {
9+
this.table = new StringBuilder("");
10+
this.where = new StringBuilder("");
11+
}
12+
13+
public StringBuilder getTable() {
14+
return table;
15+
}
16+
17+
public StringBuilder getWhere() {
18+
return where;
19+
}
20+
21+
public boolean hasWhere() {
22+
return where.length() > 0;
23+
}
24+
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.naskar.fluentquery;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import com.naskar.fluentquery.converters.NativeSQLDelete;
7+
import com.naskar.fluentquery.converters.NativeSQLResult;
8+
import com.naskar.fluentquery.domain.Customer;
9+
10+
public class NativeDeleteTest {
11+
12+
@Test
13+
public void testDelete() {
14+
String expected = "delete from Customer e0 where e0.id = :p0";
15+
16+
NativeSQLResult result = new DeleteBuilder()
17+
.entity(Customer.class)
18+
.where(i -> i.getId()).eq(1L)
19+
.to(new NativeSQLDelete());
20+
21+
String actual = result.sql();
22+
23+
Assert.assertEquals(expected, actual);
24+
25+
Assert.assertEquals(result.params().get("p0"), 1L);
26+
}
27+
28+
@Test
29+
public void testDeleteWithoutWhere() {
30+
String expected = "delete from Customer e0";
31+
32+
NativeSQLResult result = new DeleteBuilder()
33+
.entity(Customer.class)
34+
.to(new NativeSQLDelete());
35+
36+
String actual = result.sql();
37+
38+
Assert.assertEquals(expected, actual);
39+
}
40+
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.naskar.fluentquery;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import com.naskar.fluentquery.conventions.MappingConvention;
8+
import com.naskar.fluentquery.converters.NativeSQLDelete;
9+
import com.naskar.fluentquery.converters.NativeSQLResult;
10+
import com.naskar.fluentquery.domain.Customer;
11+
import com.naskar.fluentquery.mapping.MappingFactory;
12+
13+
public class NativeMappingDeleteTest {
14+
15+
private MappingConvention mc;
16+
17+
@Before
18+
public void setup() {
19+
mc = new MappingFactory().create();
20+
}
21+
22+
@Test
23+
public void testDelete() {
24+
String expected = "delete from TB_CUSTOMER e0 where e0.CD_CUSTOMER = :p0";
25+
26+
NativeSQLResult result = new DeleteBuilder()
27+
.entity(Customer.class)
28+
.where(i -> i.getId()).eq(1L)
29+
.to(new NativeSQLDelete(mc));
30+
31+
String actual = result.sql();
32+
33+
Assert.assertEquals(expected, actual);
34+
35+
Assert.assertEquals(result.params().get("p0"), 1L);
36+
}
37+
38+
@Test
39+
public void testDeleteWithoutWhere() {
40+
String expected = "delete from TB_CUSTOMER e0";
41+
42+
NativeSQLResult result = new DeleteBuilder()
43+
.entity(Customer.class)
44+
.to(new NativeSQLDelete(mc));
45+
46+
String actual = result.sql();
47+
48+
Assert.assertEquals(expected, actual);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)