-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb84569
commit a547351
Showing
15 changed files
with
368 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.naskar.fluentquery.binder; | ||
|
||
public interface Binder<T, R> { | ||
|
||
R bind(T t); | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/naskar/fluentquery/binder/BinderSQL.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,15 @@ | ||
package com.naskar.fluentquery.binder; | ||
|
||
import java.util.function.Function; | ||
|
||
import com.naskar.fluentquery.converters.NativeSQLResult; | ||
|
||
public interface BinderSQL<T> extends Binder<T, NativeSQLResult> { | ||
|
||
void configure(NativeSQLResult result); | ||
|
||
<R> R get(Function<T, R> getter); | ||
|
||
NativeSQLResult bind(T t); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/naskar/fluentquery/binder/BinderSQLBuilder.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,9 @@ | ||
package com.naskar.fluentquery.binder; | ||
|
||
public class BinderSQLBuilder { | ||
|
||
public <T> BinderSQL<T> from(Class<T> clazz) { | ||
return new BinderSQLImpl<T>(clazz); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/com/naskar/fluentquery/binder/BinderSQLImpl.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,81 @@ | ||
package com.naskar.fluentquery.binder; | ||
|
||
import java.util.IdentityHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.function.Function; | ||
|
||
import com.naskar.fluentquery.converters.NativeSQLResult; | ||
import com.naskar.fluentquery.impl.MethodRecordProxy; | ||
|
||
public class BinderSQLImpl<T> implements BinderSQL<T> { | ||
|
||
private NativeSQLResult result; | ||
|
||
private Map<Object, Function<T, ?>> maps; | ||
private MethodRecordProxy<T> proxy; | ||
|
||
public BinderSQLImpl(Class<T> clazz) { | ||
this.maps = new IdentityHashMap<Object, Function<T, ?>>(); | ||
this.proxy = new MethodRecordProxy<T>(clazz); | ||
this.proxy.setExecute(false); | ||
} | ||
|
||
@Override | ||
public void configure(NativeSQLResult result) { | ||
this.result = result; | ||
} | ||
|
||
@Override | ||
public <R> R get(Function<T, R> getter) { | ||
proxy.clear(); | ||
getter.apply(proxy.getProxy()); | ||
|
||
R r = createInstance(proxy.getCalledMethod().getReturnType()); | ||
|
||
maps.put(r, getter); | ||
|
||
return r; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private <R, E> R createInstance(Class<E> returnType) { | ||
if(Long.class.equals(returnType)) { | ||
return (R) new Long(0L); | ||
|
||
} else if(Double.class.equals(returnType)) { | ||
return (R) new Double(0L); | ||
|
||
} else { | ||
return (R)MethodRecordProxy.createInstance(returnType); | ||
} | ||
} | ||
|
||
@Override | ||
public NativeSQLResult bind(T t) { | ||
|
||
NativeSQLResult newResult = new NativeSQLResult(); | ||
newResult.sql(result.sql()); | ||
|
||
Map<String, Object> params = newResult.params(); | ||
List<Object> values = newResult.values(); | ||
|
||
for(Entry<String, Object> e : result.params().entrySet()) { | ||
|
||
Object v = e.getValue(); | ||
|
||
Function<T, ?> f = maps.get(e.getValue()); | ||
if(f != null) { | ||
v = f.apply(t); | ||
|
||
} | ||
|
||
params.put(e.getKey(), v); | ||
values.add(v); | ||
} | ||
|
||
return newResult; | ||
} | ||
|
||
} |
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
Oops, something went wrong.