Skip to content

Commit

Permalink
reorganize the type definition
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuho committed Jul 9, 2014
1 parent 0d42fd7 commit 651f8ae
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 114 deletions.
18 changes: 12 additions & 6 deletions net/examp1e/picoorm/Condition.java
Expand Up @@ -7,20 +7,27 @@


public class Condition<Row extends AbstractRow> { public class Condition<Row extends AbstractRow> {


static interface Parameter { public static interface Parameter {
public void bindTo(PreparedStatement ps, int parameterIndex) throws SQLException; public void bindTo(PreparedStatement ps, int parameterIndex) throws SQLException;
} }


public static abstract class OrderBy {
abstract String toOrderBySQL();
}

final TableDefinition<Row> tableDefinition; final TableDefinition<Row> tableDefinition;
String term; String term;
ArrayList<Parameter> params = new ArrayList<Parameter>(); ArrayList<Parameter> params = new ArrayList<Parameter>();
ArrayList<String> orderBy = new ArrayList<String>(); ArrayList<String> orderBy = new ArrayList<String>();
long limitOffset = 0; long limitOffset = 0;
long limitCount = -1; long limitCount = -1;


Condition(TableDefinition<Row> tableDefinition, String term) { public Condition(TableDefinition<Row> tableDefinition, String term, Parameter... params) {
this.tableDefinition = tableDefinition; this.tableDefinition = tableDefinition;
this.term = term; this.term = term;
for (Parameter param : params) {
this.params.add(param);
}
} }


public Condition<Row> and(Condition<Row> x) { public Condition<Row> and(Condition<Row> x) {
Expand All @@ -35,10 +42,9 @@ public Condition<Row> or(Condition<Row> x) {
return this; return this;
} }


@SuppressWarnings("varargs") public Condition<Row> orderBy(OrderBy... orders) {
public Condition<Row> orderBy(Predicate<Row>... predicates) { for (OrderBy order : orders) {
for (Predicate<Row> predicate : predicates) { orderBy.add(order.toOrderBySQL());
orderBy.add(predicate.fieldName + (predicate.orderIsAscending() ? " ASC" : " DESC"));
} }
return this; return this;
} }
Expand Down
35 changes: 0 additions & 35 deletions net/examp1e/picoorm/LongPredicate.java

This file was deleted.

42 changes: 17 additions & 25 deletions net/examp1e/picoorm/Predicate.java
@@ -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();
} }
} }
27 changes: 0 additions & 27 deletions net/examp1e/picoorm/StringPredicate.java

This file was deleted.

2 changes: 1 addition & 1 deletion net/examp1e/picoorm/example/Main.java
Expand Up @@ -30,7 +30,7 @@ public static void main(String[] args) {
Member.name.is("yappo").update(conn, new Member().setName("seiitaishogun")); Member.name.is("yappo").update(conn, new Member().setName("seiitaishogun"));


// SELECT FROM member WHERE id<? // SELECT FROM member WHERE id<?
for (Member m: Member.id.lessThan(1000).limit(1).search(conn)) { for (Member m: Member.id.lessThan(1000L).limit(1).search(conn)) {
System.out.println(Long.toString(m.getId()) + ":" + m.getName()); System.out.println(Long.toString(m.getId()) + ":" + m.getName());
} }


Expand Down
34 changes: 14 additions & 20 deletions net/examp1e/picoorm/example/Member.java
Expand Up @@ -4,8 +4,8 @@
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;

import net.examp1e.picoorm.*; import net.examp1e.picoorm.*;
import net.examp1e.picoorm.types.*;


// this class definition should be automatically generated from the DDL // this class definition should be automatically generated from the DDL
public class Member extends AbstractRow { public class Member extends AbstractRow {
Expand All @@ -18,17 +18,15 @@ public String getTableName() {
} }
public ArrayList<String> getColumnNames(Member row) { public ArrayList<String> getColumnNames(Member row) {
ArrayList<String> names = new ArrayList<String>(); ArrayList<String> names = new ArrayList<String>();
if (row == null || row._id_isset) if (row == null || row._id.isSet())
names.add("id"); names.add("id");
if (row == null || row._name_isset) if (row == null || row._name.isSet())
names.add("name"); names.add("name");
return names; return names;
} }
public int bind(Member row, PreparedStatement ps, int index) throws SQLException { public int bind(Member row, PreparedStatement ps, int index) throws SQLException {
if (row._id_isset) index = row._id.bindTo(ps, index);
setLong(ps, index++, row._id_value); index = row._name.bindTo(ps, index);
if (row._name_isset)
setString(ps, index++, row._name_value);
return index; return index;
} }
public Member deserialize(ResultSet rs) throws SQLException { public Member deserialize(ResultSet rs) throws SQLException {
Expand All @@ -42,29 +40,25 @@ protected TableDefinition<Member> _getTableDefinition() {
} }


// for query // for query
public final static LongPredicate<Member> id = new LongPredicate<Member>(TABLE_DEFINITION, "id"); public final static LongType.Predicate<Member> id = new LongType.Predicate<Member>().init(TABLE_DEFINITION, "id");
public final static StringPredicate<Member> name = new StringPredicate<Member>(TABLE_DEFINITION, "name"); public final static StringType.Predicate<Member> name = new StringType.Predicate<Member>().init(TABLE_DEFINITION, "name");


// for insert,update // for insert,update
Long _id_value; LongType _id = new LongType().init(0L);
boolean _id_isset; StringType _name = new StringType().init("");
String _name_value;
boolean _name_isset;
public Member setId(long id) { public Member setId(long id) {
_id_value = id; _id.set(id);
_id_isset = true;
return this; return this;
} }
public Long getId() { public long getId() {
return _id_value; return _id.get();
} }
public Member setName(String name) { public Member setName(String name) {
_name_value = name; _name.set(name);
_name_isset = true;
return this; return this;
} }
public String getName() { public String getName() {
return _name_value; return _name.get();
} }


} }
73 changes: 73 additions & 0 deletions net/examp1e/picoorm/types/AbstractType.java
@@ -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);
}

}
37 changes: 37 additions & 0 deletions net/examp1e/picoorm/types/LongType.java
@@ -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;
}
};
}
}

}
37 changes: 37 additions & 0 deletions net/examp1e/picoorm/types/StringType.java
@@ -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;
}
};
}
}

}

0 comments on commit 651f8ae

Please sign in to comment.