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
Showing
9 changed files
with
191 additions
and
114 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 was deleted.
Oops, something went wrong.
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,40 +1,32 @@ | |||
package net.examp1e.picoorm; | package net.examp1e.picoorm; | ||
|
|
||
public abstract class Predicate<Row extends AbstractRow> { | public abstract class Predicate<Row extends AbstractRow> extends Condition.OrderBy { | ||
|
|
||
public static class OrderPredicate<Row extends AbstractRow> extends Predicate<Row> { | public static class OrderPredicate<Row extends AbstractRow> extends Condition.OrderBy { | ||
boolean isAsc = true; | Predicate<Row> predicate; | ||
OrderPredicate(Predicate<Row> src, boolean isAsc) { | boolean isAsc; | ||
super(src); | OrderPredicate(Predicate<Row> predicate, boolean isAsc) { | ||
this.predicate = predicate; | |||
this.isAsc = isAsc; | this.isAsc = isAsc; | ||
} | } | ||
@Override | @Override | ||
boolean orderIsAscending() { | String toOrderBySQL() { | ||
return this.isAsc; | return this.predicate.fieldName + (isAsc ? " ASC" : " DESC"); | ||
} | } | ||
} | } | ||
|
|
||
final TableDefinition<Row> tableDefinition; | protected TableDefinition<Row> tableDefinition; | ||
String fieldName; | protected String fieldName; | ||
|
public final OrderPredicate<Row> asc = new OrderPredicate<Row>(this, true); | ||
public final OrderPredicate<Row> asc; | public final OrderPredicate<Row> desc = new OrderPredicate<Row>(this, false); | ||
public final OrderPredicate<Row> desc; | |||
|
|
||
protected Predicate(TableDefinition<Row> tableDefinition, String fieldName) { | protected void _init(TableDefinition<Row> tableDefinition, String fieldName) { | ||
this.tableDefinition = tableDefinition; | this.tableDefinition = tableDefinition; | ||
this.fieldName = fieldName; | this.fieldName = fieldName; | ||
this.asc = new OrderPredicate<Row>(this, true); | |||
this.desc = new OrderPredicate<Row>(this, false); | |||
} | |||
|
|||
protected Predicate(Predicate<Row> src) { | |||
this.tableDefinition = src.tableDefinition; | |||
this.fieldName = src.fieldName; | |||
this.asc = src.asc; | |||
this.desc = src.desc; | |||
} | } | ||
|
|
||
boolean orderIsAscending() { | @Override | ||
return true; | String toOrderBySQL() { | ||
return this.asc.toOrderBySQL(); | |||
} | } | ||
} | } |
This file was deleted.
Oops, something went wrong.
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,73 @@ | |||
package net.examp1e.picoorm.types; | |||
|
|||
import java.sql.PreparedStatement; | |||
import java.sql.SQLException; | |||
|
|||
import net.examp1e.picoorm.*; | |||
|
|||
public abstract class AbstractType<ThisType, ValueType> { | |||
|
|||
public static interface Binder<T> { | |||
void bind(PreparedStatement ps, int parameterIndex, T value) throws SQLException; | |||
} | |||
|
|||
ValueType value; | |||
boolean isSet; | |||
|
|||
public boolean isSet() { | |||
return isSet; | |||
} | |||
|
|||
public ValueType get() { | |||
return value; | |||
} | |||
|
|||
public void set(ValueType value) { | |||
this.value = value; | |||
this.isSet = true; | |||
} | |||
|
|||
@SuppressWarnings("unchecked") | |||
public ThisType init(ValueType defaultValue) { | |||
this.value = defaultValue; | |||
return (ThisType)this; | |||
} | |||
|
|||
public int bindTo(PreparedStatement ps, int parameterIndex) throws SQLException { | |||
if (isSet()) | |||
getBinder().bind(ps, parameterIndex, value); | |||
return parameterIndex; | |||
} | |||
|
|||
protected abstract Binder<ValueType> getBinder(); | |||
|
|||
static abstract class Parameter<ValueType> implements Condition.Parameter { | |||
ValueType value; | |||
Parameter(ValueType value) { | |||
this.value = value; | |||
} | |||
public void bindTo(PreparedStatement ps, int parameterIndex) throws SQLException { | |||
getBinder().bind(ps, parameterIndex, value); | |||
} | |||
protected abstract Binder<ValueType> getBinder(); | |||
} | |||
|
|||
public static abstract class Predicate<ThisType, Row extends AbstractRow, ValueType> extends net.examp1e.picoorm.Predicate<Row> { | |||
@SuppressWarnings("unchecked") | |||
public ThisType init(TableDefinition<Row> tableDefinition, String fieldName) { | |||
_init(tableDefinition, fieldName); | |||
return (ThisType)this; | |||
} | |||
public Condition<Row> is(ValueType x) { | |||
return _buildBinaryOp("=", x); | |||
} | |||
public Condition<Row> lessThan(ValueType x) { | |||
return _buildBinaryOp("<", x); | |||
} | |||
private Condition<Row> _buildBinaryOp(String op, ValueType value) { | |||
return new Condition<Row>(this.tableDefinition, this.fieldName + op + "?", createParameter(value)); | |||
} | |||
protected abstract Parameter<ValueType> createParameter(ValueType x); | |||
} | |||
|
|||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
package net.examp1e.picoorm.types; | |||
|
|||
import java.sql.PreparedStatement; | |||
import java.sql.SQLException; | |||
import java.sql.Types; | |||
|
|||
import net.examp1e.picoorm.*; | |||
|
|||
public class LongType extends AbstractType<LongType, Long> { | |||
|
|||
final static Binder<Long> BINDER = new Binder<Long>() { | |||
@Override | |||
public void bind(PreparedStatement ps, int parameterIndex, Long value) throws SQLException { | |||
if (value != null) | |||
ps.setLong(parameterIndex, value); | |||
else | |||
ps.setNull(parameterIndex, Types.BIGINT); | |||
} | |||
}; | |||
|
|||
@Override | |||
protected Binder<Long> getBinder() { | |||
return BINDER; | |||
} | |||
|
|||
public static class Predicate<Row extends AbstractRow> extends AbstractType.Predicate<Predicate<Row>, Row, Long> { | |||
@Override | |||
protected Parameter<Long> createParameter(Long value) { | |||
return new Parameter<Long>(value) { | |||
protected Binder<Long> getBinder() { | |||
return BINDER; | |||
} | |||
}; | |||
} | |||
} | |||
|
|||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
package net.examp1e.picoorm.types; | |||
|
|||
import java.sql.PreparedStatement; | |||
import java.sql.SQLException; | |||
import java.sql.Types; | |||
|
|||
import net.examp1e.picoorm.*; | |||
|
|||
public class StringType extends AbstractType<StringType, String> { | |||
|
|||
final static Binder<String> BINDER = new Binder<String>() { | |||
@Override | |||
public void bind(PreparedStatement ps, int parameterIndex, String value) throws SQLException { | |||
if (value != null) | |||
ps.setString(parameterIndex, value); | |||
else | |||
ps.setNull(parameterIndex, Types.VARCHAR); | |||
} | |||
}; | |||
|
|||
@Override | |||
protected Binder<String> getBinder() { | |||
return BINDER; | |||
} | |||
|
|||
public static class Predicate<Row extends AbstractRow> extends AbstractType.Predicate<Predicate<Row>, Row, String> { | |||
@Override | |||
protected Parameter<String> createParameter(String value) { | |||
return new Parameter<String>(value) { | |||
protected Binder<String> getBinder() { | |||
return BINDER; | |||
} | |||
}; | |||
} | |||
} | |||
|
|||
} |