Skip to content

Commit

Permalink
调用ModelProperty的相应方法时得到合适的CHILD DAO,这样应用层可以只使用XXX.dao在多线程情况下安全的执行任何操作
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed May 20, 2018
1 parent 4ca9489 commit 461389f
Show file tree
Hide file tree
Showing 34 changed files with 113 additions and 87 deletions.
Expand Up @@ -101,7 +101,7 @@ public ExpressionBuilder<T> set(String propertyName, Value value) {
return this;
}

public ExpressionBuilder<T> eq(String propertyName, ModelProperty<?> p) {
public ExpressionBuilder<T> eq(String propertyName, ModelProperty<?, ?> p) {
ExpressionColumn left = model.getExpressionColumn(propertyName);
ExpressionColumn right = Model.getExpressionColumn(p);
Comparison c = new Comparison(getTable().getSession(), Comparison.EQUAL, left, right);
Expand Down
32 changes: 17 additions & 15 deletions lealone-orm/src/main/java/org/lealone/orm/Model.java
Expand Up @@ -74,7 +74,7 @@ public abstract class Model<T> {

private static final ConcurrentSkipListMap<Long, ServerSession> currentSessions = new ConcurrentSkipListMap<>();

public class PRowId extends PBaseNumber<T, Long> {
public class PRowId extends PBaseNumber<T, Long, PRowId> {

private long value;

Expand Down Expand Up @@ -178,6 +178,7 @@ public boolean equals(Object obj) {
private ArrayStack<ExpressionBuilder<T>> expressionBuilderStack;

private ArrayStack<TableFilter> tableFilterStack;
private HashMap<String, ModelProperty> modelPropertiesMap;

ModelProperty[] modelProperties;
// 0: regular model; 1: root dao; 2: child dao
Expand All @@ -198,6 +199,14 @@ public boolean isDao() {

protected void setModelProperties(ModelProperty[] modelProperties) {
this.modelProperties = modelProperties;
modelPropertiesMap = new HashMap<>(modelProperties.length);
for (ModelProperty p : modelProperties) {
modelPropertiesMap.put(p.getName(), p);
}
}

ModelProperty getModelProperty(String name) {
return modelPropertiesMap.get(name);
}

/**
Expand Down Expand Up @@ -251,13 +260,13 @@ public String getTableName() {
}

@SafeVarargs
public final T select(ModelProperty<?>... properties) {
public final T select(ModelProperty<?, ?>... properties) {
Model<T> m = maybeCopy();
if (m != this) {
return m.select(properties);
}
selectExpressions = new ArrayList<>();
for (ModelProperty<?> p : properties) {
for (ModelProperty<?, ?> p : properties) {
ExpressionColumn c = getExpressionColumn(p);
selectExpressions.add(c);
}
Expand All @@ -275,20 +284,20 @@ public T orderBy() {
}

@SafeVarargs
public final T groupBy(ModelProperty<?>... properties) {
public final T groupBy(ModelProperty<?, ?>... properties) {
Model<T> m = maybeCopy();
if (m != this) {
return m.groupBy(properties);
}
groupExpressions = new ArrayList<>();
for (ModelProperty<?> p : properties) {
for (ModelProperty<?, ?> p : properties) {
ExpressionColumn c = getExpressionColumn(p);
groupExpressions.add(c);
}
return root;
}

static ExpressionColumn getExpressionColumn(ModelProperty<?> p) {
static ExpressionColumn getExpressionColumn(ModelProperty<?, ?> p) {
return new ExpressionColumn(p.getDatabaseName(), p.getSchemaName(), p.getTableName(), p.getName());
}

Expand Down Expand Up @@ -622,21 +631,14 @@ private boolean isRootDao() {
}

@SuppressWarnings("unchecked")
private Model<T> maybeCopy() {
Model<T> maybeCopy() {
if (isRootDao()) {
Model m = newInstance(modelTable.copy(), CHILD_DAO);
return m;
return newInstance(modelTable.copy(), CHILD_DAO);
} else {
return this;
}
}

// 支持并发
public T fork() {
checkDao("for");
return maybeCopy().root;
}

private void maybeCreateWhereExpression(Table dbTable) {
// 没有指定where条件时,如果存在ROWID,则用ROWID当where条件
if (_rowid_.get() != 0) {
Expand Down
41 changes: 34 additions & 7 deletions lealone-orm/src/main/java/org/lealone/orm/ModelProperty.java
Expand Up @@ -32,7 +32,8 @@
*
* @param <R> The type of the owning root bean
*/
public abstract class ModelProperty<R> {
@SuppressWarnings("unchecked")
public abstract class ModelProperty<R, P> {

protected final String name;
protected final R root;
Expand Down Expand Up @@ -72,8 +73,12 @@ public String toString() {
return name;
}

private Model<?> getModel() {
return ((Model<?>) root);
protected Model<?> getModel() {
return ((Model<?>) root).maybeCopy();
}

protected P getModelProperty(Model<?> model) {
return (P) model.getModelProperty(name);
}

/**
Expand All @@ -87,6 +92,10 @@ protected ExpressionBuilder<?> expr() {
* Is null.
*/
public R isNull() {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).isNull();
}
expr().isNull(name);
return root;
}
Expand All @@ -95,6 +104,10 @@ public R isNull() {
* Is not null.
*/
public R isNotNull() {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).isNotNull();
}
expr().isNotNull(name);
return root;
}
Expand All @@ -103,6 +116,10 @@ public R isNotNull() {
* Order by ascending on this property.
*/
public R asc() {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).asc();
}
expr().orderBy(name, false);
return root;
}
Expand All @@ -111,6 +128,10 @@ public R asc() {
* Order by descending on this property.
*/
public R desc() {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).desc();
}
expr().orderBy(name, true);
return root;
}
Expand All @@ -122,14 +143,20 @@ public String getName() {
return name;
}

public final R eq(ModelProperty<?> p) {
public final R eq(ModelProperty<?, ?> p) {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).eq(p);
}
expr().eq(name, p);
return root;
}

// public abstract R set(Object value);

public R set(Object value) {
Model<?> model = getModel();
if (model != root) {
return ((ModelProperty<R, P>) getModelProperty(model)).set(value);
}
return root;
}

Expand All @@ -156,7 +183,7 @@ protected JsonNode getJsonNode(JsonNode node) {
/**
* Helper method to check if two objects are equal.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "rawtypes" })
protected static boolean areEqual(Object obj1, Object obj2) {
if (obj1 == null) {
return (obj2 == null);
Expand Down
Expand Up @@ -24,7 +24,7 @@
*
* @param <R> the root model bean type
*/
public class PArray<R> extends ModelProperty<R> {
public class PArray<R> extends ModelProperty<R, PArray<R>> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -24,7 +24,7 @@
* @param <T> the type of the scalar property
*/
@SuppressWarnings("rawtypes")
public abstract class PBaseComparable<R, T extends Comparable> extends PBaseValueEqual<R, T> {
public abstract class PBaseComparable<R, T extends Comparable, P> extends PBaseValueEqual<R, T, P> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -24,7 +24,7 @@
* @param <D> the date time type
*/
@SuppressWarnings("rawtypes")
public abstract class PBaseDate<R, D extends Comparable> extends PBaseComparable<R, D> {
public abstract class PBaseDate<R, D extends Comparable, P> extends PBaseComparable<R, D, P> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -24,7 +24,7 @@
* @param <T> the number type
*/
@SuppressWarnings("rawtypes")
public abstract class PBaseNumber<R, T extends Comparable> extends PBaseComparable<R, T> {
public abstract class PBaseNumber<R, T extends Comparable, P> extends PBaseComparable<R, T, P> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -27,7 +27,7 @@
* @param <R> the root model bean type
* @param <T> the number type
*/
public abstract class PBaseValueEqual<R, T> extends ModelProperty<R> {
public abstract class PBaseValueEqual<R, T, P> extends ModelProperty<R, P> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -23,7 +23,7 @@
* BigDecimal property.
* @param <R> the root model bean type
*/
public class PBigDecimal<R> extends PBaseNumber<R, BigDecimal> {
public class PBigDecimal<R> extends PBaseNumber<R, BigDecimal, PBigDecimal<R>> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -19,7 +19,7 @@

import org.lealone.orm.ModelProperty;

public class PBlob<R> extends ModelProperty<R> {
public class PBlob<R> extends ModelProperty<R, PBlob<R>> {

public PBlob(String name, R root) {
super(name, root);
Expand Down
Expand Up @@ -22,7 +22,7 @@
*
* @param <R> the root model bean type
*/
public class PBoolean<R> extends PBaseValueEqual<R, Boolean> {
public class PBoolean<R> extends PBaseValueEqual<R, Boolean, PBoolean<R>> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -22,7 +22,7 @@
*
* @param <R> the root model bean type
*/
public class PByte<R> extends PBaseNumber<R, Byte> {
public class PByte<R> extends PBaseNumber<R, Byte, PByte<R>> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -24,7 +24,7 @@
*
* @param <R> the root model bean type
*/
public class PBytes<R> extends ModelProperty<R> {
public class PBytes<R> extends ModelProperty<R, PBytes<R>> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -19,7 +19,7 @@

import org.lealone.orm.ModelProperty;

public class PClob<R> extends ModelProperty<R> {
public class PClob<R> extends ModelProperty<R, PClob<R>> {

public PClob(String name, R root) {
super(name, root);
Expand Down
Expand Up @@ -24,7 +24,7 @@
*
* @param <R> the root model bean type
*/
public class PDate<R> extends PBaseDate<R, Date> {
public class PDate<R> extends PBaseDate<R, Date, PDate<R>> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -22,7 +22,7 @@
*
* @param <R> the root model bean type
*/
public class PDouble<R> extends PBaseNumber<R, Double> {
public class PDouble<R> extends PBaseNumber<R, Double, PDouble<R>> {

/**
* Construct with a property name and root instance.
Expand Down
Expand Up @@ -22,7 +22,7 @@
*
* @param <R> the root model bean type
*/
public class PFloat<R> extends PBaseNumber<R, Float> {
public class PFloat<R> extends PBaseNumber<R, Float, PFloat<R>> {

/**
* Construct with a property name and root instance.
Expand Down
13 changes: 10 additions & 3 deletions lealone-orm/src/main/java/org/lealone/orm/property/PInteger.java
Expand Up @@ -22,6 +22,7 @@

import org.lealone.db.value.Value;
import org.lealone.db.value.ValueInt;
import org.lealone.orm.Model;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -32,7 +33,7 @@
*
* @param <R> the root model bean type
*/
public class PInteger<R> extends PBaseNumber<R, Integer> {
public class PInteger<R> extends PBaseNumber<R, Integer, PInteger<R>> {

private int value;

Expand All @@ -54,11 +55,13 @@ public PInteger(String name, R root, String prefix) {
}

public final R set(int value) {
Model<?> model = getModel();
if (model != root) {
return getModelProperty(model).set(value);
}
if (!areEqual(this.value, value)) {
this.value = value;
// if (isReady()) {
expr().set(name, ValueInt.get(value));
// }
}
return root;
}
Expand All @@ -69,6 +72,10 @@ public R set(Object value) {
}

public final int get() {
Model<?> model = getModel();
if (model != root) {
return getModelProperty(model).get();
}
return value;
}

Expand Down

0 comments on commit 461389f

Please sign in to comment.