Skip to content

Commit

Permalink
Fixed issue 479
Browse files Browse the repository at this point in the history
git-svn-id: http://nutz.googlecode.com/svn/trunk@2086 423f10f2-e3a4-11dd-a6ea-a32d6b26a33d
  • Loading branch information
zozoh committed May 17, 2011
1 parent 6eeddb6 commit 4c6dbd8
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 41 deletions.
26 changes: 19 additions & 7 deletions src/org/nutz/dao/impl/EntityOperator.java
Expand Up @@ -4,12 +4,14 @@
import java.util.List;
import java.util.Map;

import org.nutz.dao.Chain;
import org.nutz.dao.Condition;
import org.nutz.dao.FieldMatcher;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.sql.DaoStatement;
import org.nutz.dao.sql.Pojo;
import org.nutz.dao.sql.PojoMaker;
import org.nutz.dao.sql.SqlType;
import org.nutz.dao.util.Pojos;
import org.nutz.lang.Each;
import org.nutz.lang.ExitLoop;
Expand Down Expand Up @@ -53,6 +55,16 @@ public Pojo addUpdate() {
return addUpdate(entity, myObj);
}

public Pojo addUpdate(Chain chain, Condition cnd) {
Pojo pojo = dao.pojoMaker.makePojo(SqlType.UPDATE);
pojo.setEntity(entity);
pojo.append(Pojos.Items.entityTableName());
pojo.append(Pojos.Items.updateFieldsBy(chain));
pojo.append(Pojos.Items.cnd(cnd));
pojoList.add(pojo);
return pojo;
}

public Pojo addUpdate(final Entity<?> en, final Object obj) {
if (null == en)
return null;
Expand All @@ -70,7 +82,7 @@ public List<Pojo> addUpdateForIgnoreNull( final Entity<?> en,

if (null == en)
return null;

final FieldMatcher newFM;
if (null == fm)
newFM = FieldMatcher.make(null, null, true);
Expand All @@ -96,7 +108,7 @@ public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException
public Pojo addUpdate(Condition cnd) {
if (null == entity)
return null;

Pojo pojo = dao.pojoMaker.makeUpdate(entity, null).append(Pojos.Items.cnd(cnd));
pojoList.add(pojo);
return pojo;
Expand All @@ -105,7 +117,7 @@ public Pojo addUpdate(Condition cnd) {
public Pojo addDeleteSelfOnly(long id) {
if (null == entity)
return null;

Pojo pojo = dao.pojoMaker.makeDelete(entity);
pojo.append(Pojos.Items.cndAuto(entity, myObj));
pojo.addParamsBy(myObj);
Expand All @@ -116,7 +128,7 @@ public Pojo addDeleteSelfOnly(long id) {
public Pojo addDeleteSelfOnly(String name) {
if (null == entity)
return null;

Pojo pojo = dao.pojoMaker.makeDelete(entity);
pojo.append(Pojos.Items.cndName(entity, name));
pojo.addParamsBy(name);
Expand All @@ -127,7 +139,7 @@ public Pojo addDeleteSelfOnly(String name) {
public Pojo addDeleteSelfOnly() {
if (null == entity)
return null;

Pojo pojo = dao.pojoMaker.makeDelete(entity);
pojo.append(Pojos.Items.cndAuto(entity, myObj));
pojo.addParamsBy(myObj);
Expand All @@ -142,7 +154,7 @@ public List<Pojo> addInsert() {
public List<Pojo> addInsert(Entity<?> en, Object obj) {
if (null == en)
return null;

int len = Lang.length(obj);
List<Pojo> re = new ArrayList<Pojo>(len);
if (len > 0) {
Expand All @@ -167,7 +179,7 @@ public Pojo addInsertSelfOnly() {
public Pojo addInsertSelfOnly(Entity<?> en, Object obj) {
if (null == entity)
return null;

Pojo pojo = dao.pojoMaker.makeInsert(en).setOperatingObject(obj);
pojoList.add(pojo);
return pojo;
Expand Down
58 changes: 34 additions & 24 deletions src/org/nutz/dao/impl/NutDao.java
Expand Up @@ -89,7 +89,7 @@ public <T> T getObject(Class<T> classOfT, ResultSet rs, FieldMatcher fm) {
}

public <T> T insert(final T obj) {
final EntityOperator opt = __opt(Lang.first(obj));
final EntityOperator opt = _optBy(Lang.first(obj));
Lang.each(obj, new Each<Object>() {
public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
opt.addInsert(opt.entity, ele);
Expand All @@ -100,24 +100,26 @@ public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException
}

public void insert(String tableName, Chain chain) {
EntityOperator opt = __opt(chain.toEntityMap(tableName));
EntityOperator opt = _optBy(chain.toEntityMap(tableName));
opt.addInsert();
opt.exec();
}

public void insert(Class<?> classOfT, Chain chain) {
insert(chain.toObject(classOfT));//TODO 这样的效率,未免太低了,需要改进
insert(chain.toObject(classOfT));// TODO 这样的效率,未免太低了,需要改进
}

public <T> T fastInsert(T obj) {
EntityOperator opt = __opt(obj);
EntityOperator opt = _optBy(obj);
opt.addInsertSelfOnly();
opt.exec();
return obj;
}

public <T> T insertWith(T obj, String regex) {//TODO 天啊,每个调用都有4个正则表达式,能快起来不?
EntityOperator opt = __opt(obj);
public <T> T insertWith(T obj, String regex) {
// TODO 天啊,每个调用都有4个正则表达式,能快起来不?
// TODO zzh: NutEntity 会缓存正则表达式计算的结果的,会很快的
EntityOperator opt = _optBy(obj);

opt.entity.visitOne(obj, regex, doInsert(opt));
opt.addInsert();
Expand All @@ -129,8 +131,10 @@ public <T> T insertWith(T obj, String regex) {//TODO 天啊,每个调用都有4
return obj;
}

public <T> T insertLinks(T obj, String regex) {//TODO 天啊,每个调用都有4个正则表达式,能快起来不?
EntityOperator opt = __opt(obj);
public <T> T insertLinks(T obj, String regex) {
// TODO 天啊,每个调用都有4个正则表达式,能快起来不?
// TODO zzh: NutEntity 会缓存正则表达式计算的结果的,会很快的
EntityOperator opt = _optBy(obj);

opt.entity.visitOne(obj, regex, doInsert(opt));
opt.entity.visitMany(obj, regex, doInsert(opt));
Expand All @@ -142,7 +146,7 @@ public <T> T insertLinks(T obj, String regex) {//TODO 天啊,每个调用都有4
}

public <T> T insertRelation(T obj, String regex) {
EntityOperator opt = __opt(obj);
EntityOperator opt = _optBy(obj);

opt.entity.visitManyMany(obj, regex, doInsertRelation(opt));
opt.exec();
Expand All @@ -151,33 +155,35 @@ public <T> T insertRelation(T obj, String regex) {
}

public int update(Object obj) {
EntityOperator opt = __opt(obj);
EntityOperator opt = _optBy(obj);
opt.addUpdate();
opt.exec();
return opt.getUpdateCount();
}

public int updateIgnoreNull(final Object obj) {
EntityOperator opt = __opt(obj);
EntityOperator opt = _optBy(obj);
opt.addUpdateForIgnoreNull(opt.entity, obj, FieldFilter.get(opt.entity.getType()));
opt.exec();
return opt.getUpdateCount();
}

public int update(String tableName, Chain chain, Condition cnd) {
EntityOperator opt = __opt(chain.toEntityMap(tableName));
EntityOperator opt = _optBy(chain.toEntityMap(tableName));
opt.addUpdate(cnd);
opt.exec();
return opt.getUpdateCount();
}

public int update(Class<?> classOfT, Chain chain, Condition cnd) {
Entity<?> en = holder.getEntity(classOfT);
return update(en.getTableName(), chain.updateBy(en), cnd);
EntityOperator opt = _opt(classOfT);
opt.addUpdate(chain, cnd);
opt.exec();
return opt.getUpdateCount();
}

public <T> T updateWith(T obj, String regex) {
EntityOperator opt = this.__opt(obj);
EntityOperator opt = this._optBy(obj);

opt.entity.visitOne(obj, regex, doUpdate(opt));
opt.addUpdate();
Expand All @@ -190,7 +196,7 @@ public <T> T updateWith(T obj, String regex) {
}

public <T> T updateLinks(T obj, String regex) {
EntityOperator opt = this.__opt(obj);
EntityOperator opt = this._optBy(obj);

opt.entity.visitOne(obj, regex, doUpdate(opt));
opt.entity.visitMany(obj, regex, doUpdate(opt));
Expand Down Expand Up @@ -235,14 +241,16 @@ public <T> int deletex(Class<T> classOfT, Object... pks) {
}

public int delete(Object obj) {
EntityOperator opt = __opt(obj);
EntityOperator opt = _optBy(obj);
opt.addDeleteSelfOnly();
opt.exec();
return opt.getUpdateCount();
}

public int deleteWith(Object obj, String regex) {//TODO 天啊,又有4个正则表达式,能快起来不?
EntityOperator opt = this.__opt(obj);
public int deleteWith(Object obj, String regex) {
// TODO 天啊,又有4个正则表达式,能快起来不?
// TODO zzh: NutEntity 会缓存正则表达式计算的结果的,会很快的
EntityOperator opt = this._optBy(obj);

opt.entity.visitMany(obj, regex, doDelete(opt));
opt.entity.visitManyMany(obj, regex, doClearRelationByLinkedField(opt));
Expand All @@ -253,8 +261,10 @@ public int deleteWith(Object obj, String regex) {//TODO 天啊,又有4个正则
return opt.exec().getUpdateCount();
}

public int deleteLinks(Object obj, String regex) {//TODO 天啊,又有4个正则表达式,能快起来不?
EntityOperator opt = this.__opt(obj);
public int deleteLinks(Object obj, String regex) {
// TODO 天啊,又有4个正则表达式,能快起来不?
// TODO zzh: NutEntity 会缓存正则表达式计算的结果的,会很快的
EntityOperator opt = this._optBy(obj);

opt.entity.visitMany(obj, regex, doDelete(opt));
opt.entity.visitManyMany(obj, regex, doClearRelationByLinkedField(opt));
Expand Down Expand Up @@ -351,7 +361,7 @@ public <T> T fetch(Class<T> classOfT) {
}

public <T> T fetchLinks(T obj, String regex) {
EntityOperator opt = this.__opt(obj);
EntityOperator opt = this._optBy(obj);
opt.entity.visitMany(obj, regex, doFetch(opt));
opt.entity.visitManyMany(obj, regex, doFetch(opt));
opt.entity.visitOne(obj, regex, doFetch(opt));
Expand Down Expand Up @@ -380,7 +390,7 @@ public int clear(String tableName) {
}

public <T> T clearLinks(T obj, String regex) {
EntityOperator opt = this.__opt(obj);
EntityOperator opt = this._optBy(obj);

opt.entity.visitMany(obj, regex, doClear(opt));
opt.entity.visitManyMany(obj, regex, doClearRelationByHostField(opt));
Expand Down Expand Up @@ -592,7 +602,7 @@ <T> EntityOperator _opt(Class<T> classOfT) {
return _opt(holder.getEntity(classOfT));
}

EntityOperator __opt(Object obj) {
EntityOperator _optBy(Object obj) {
EntityOperator re = _opt(holder.getEntityBy(obj));
re.myObj = obj.getClass().isArray() ? Lang.array2list((Object[]) obj) : obj;
return re;
Expand Down
6 changes: 4 additions & 2 deletions src/org/nutz/dao/impl/jdbc/NutPojo.java
Expand Up @@ -178,8 +178,10 @@ public Pojo clear() {
public Pojo append(PItem... itemAry) {
if (null != itemAry)
for (PItem item : itemAry) {
items.add(item);
item.setPojo(this);
if (null != item) {
items.add(item);
item.setPojo(this);
}
}
return this;
}
Expand Down
57 changes: 57 additions & 0 deletions src/org/nutz/dao/impl/sql/pojo/UpdateFieldsByChainPItem.java
@@ -0,0 +1,57 @@
package org.nutz.dao.impl.sql.pojo;

import org.nutz.dao.Chain;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.entity.MappingField;
import org.nutz.dao.jdbc.Jdbcs;
import org.nutz.dao.jdbc.ValueAdaptor;
import org.nutz.lang.Lang;

public class UpdateFieldsByChainPItem extends AbstractPItem {

private Chain chain;

public UpdateFieldsByChainPItem(Chain chain) {
this.chain = chain;
}

public void joinSql(Entity<?> en, StringBuilder sb) {
if (chain.size() > 0) {
sb.append(" SET ");
Chain c = chain.head();
while (c != null) {
sb.append(this._fmtcolnm(en, c.name()));
sb.append("=? ,");
c = c.next();
}
sb.deleteCharAt(sb.length() - 1);
sb.append(' ');
} else {
throw Lang.makeThrow("Entity chain for UPDATE '%s'", en.getType().getName());
}
}

public int joinAdaptor(Entity<?> en, ValueAdaptor[] adaptors, int off) {
Chain c = chain.head();
while (c != null) {
MappingField mf = en.getField(c.name());
adaptors[off++] = (null == mf ? Jdbcs.getAdaptorBy(c.value()) : mf.getAdaptor());
c = c.next();
}
return off;
}

public int joinParams(Entity<?> en, Object obj, Object[] params, int off) {
Chain c = chain.head();
while (c != null) {
params[off++] = c.value();
c = c.next();
}
return off;
}

public int paramCount(Entity<?> en) {
return chain.size();
}

}
13 changes: 11 additions & 2 deletions src/org/nutz/dao/util/Pojos.java
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.regex.Pattern;

import org.nutz.dao.Chain;
import org.nutz.dao.Condition;
import org.nutz.dao.FieldFilter;
import org.nutz.dao.FieldMatcher;
Expand All @@ -23,6 +24,7 @@
import org.nutz.dao.impl.sql.pojo.InsertValuesPItem;
import org.nutz.dao.impl.sql.pojo.SqlTypePItem;
import org.nutz.dao.impl.sql.pojo.StaticPItem;
import org.nutz.dao.impl.sql.pojo.UpdateFieldsByChainPItem;
import org.nutz.dao.impl.sql.pojo.UpdateFieldsPItem;
import org.nutz.dao.jdbc.JdbcExpert;
import org.nutz.dao.jdbc.ValueAdaptor;
Expand Down Expand Up @@ -72,6 +74,10 @@ public static PItem updateFields(Object refer) {
return new UpdateFieldsPItem(refer);
}

public static PItem updateFieldsBy(Chain chain) {
return new UpdateFieldsByChainPItem(chain);
}

public static PItem queryEntityFields() {
return new QueryEntityFieldsPItem();
}
Expand Down Expand Up @@ -132,9 +138,12 @@ public static PItem cndAuto(Entity<?> en, Object obj) {
public static PItem[] cnd(Condition cnd) {
List<PItem> list = new LinkedList<PItem>();
if (null == cnd) {}
if (cnd instanceof Criteria) {
// 高级条件
else if (cnd instanceof Criteria) {
list.add((Criteria) cnd);
} else {
}
// 普通条件
else {
list.add(new ConditionPItem(cnd));
}
return list.toArray(new PItem[list.size()]);
Expand Down

0 comments on commit 4c6dbd8

Please sign in to comment.