Skip to content

Commit

Permalink
fix issue #675
Browse files Browse the repository at this point in the history
  • Loading branch information
zozoh committed Aug 12, 2014
1 parent 7ac417a commit 0bfa84b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/org/nutz/dao/Dao.java
Expand Up @@ -804,6 +804,8 @@ int updateRelation(Class<?> classOfT,
*/
int func(Class<?> classOfT, String funcName, String fieldName);

Object func2(Class<?> classOfT, String funcName, String fieldName);

/**
* 对某一个数据表字段,进行计算。
*
Expand All @@ -817,6 +819,8 @@ int updateRelation(Class<?> classOfT,
*/
int func(String tableName, String funcName, String colName);

Object func2(String tableName, String funcName, String colName);

/**
* 对某一个对象字段,进行计算。
*
Expand All @@ -832,6 +836,11 @@ int updateRelation(Class<?> classOfT,
*/
int func(Class<?> classOfT, String funcName, String fieldName, Condition cnd);

Object func2(Class<?> classOfT,
String funcName,
String fieldName,
Condition cnd);

/**
* 对某一个数据表字段,进行计算。
*
Expand All @@ -847,6 +856,11 @@ int updateRelation(Class<?> classOfT,
*/
int func(String tableName, String funcName, String colName, Condition cnd);

Object func2(String tableName,
String funcName,
String colName,
Condition cnd);

/**
* 根据数据源的类型,创建一个翻页对象
*
Expand Down
56 changes: 48 additions & 8 deletions src/org/nutz/dao/impl/NutDao.java
Expand Up @@ -36,6 +36,7 @@
import org.nutz.dao.impl.sql.pojo.PojoEachRecordCallback;
import org.nutz.dao.impl.sql.pojo.PojoFetchEntityCallback;
import org.nutz.dao.impl.sql.pojo.PojoFetchIntCallback;
import org.nutz.dao.impl.sql.pojo.PojoFetchObjectCallback;
import org.nutz.dao.impl.sql.pojo.PojoFetchRecordCallback;
import org.nutz.dao.impl.sql.pojo.PojoQueryEntityCallback;
import org.nutz.dao.impl.sql.pojo.PojoQueryRecordCallback;
Expand Down Expand Up @@ -79,6 +80,8 @@ public class NutDao extends DaoSupport implements Dao {

private PojoCallback _pojo_fetchInt;

private PojoCallback _pojo_fetchObject;

protected volatile long _selfId;

// ==========================================================
Expand All @@ -91,6 +94,7 @@ public NutDao() {
_pojo_fetchEntity = new PojoFetchEntityCallback();
_pojo_eachEntity = new PojoEachEntityCallback();
_pojo_fetchInt = new PojoFetchIntCallback();
_pojo_fetchObject = new PojoFetchObjectCallback();
_pojo_queryRecord = new PojoQueryRecordCallback();
_pojo_fetchRecord = new PojoFetchRecordCallback();
_pojo_eachRecord = new PojoEachRecordCallback();
Expand Down Expand Up @@ -136,10 +140,10 @@ public void invoke(int i, Object ele, int length) throws ExitLoop,
}

public void insert(String tableName, Chain chain) {
if (chain.isSpecial()) {
Daos.insertBySpecialChain(this, null, tableName, chain);
return;
}
if (chain.isSpecial()) {
Daos.insertBySpecialChain(this, null, tableName, chain);
return;
}
EntityOperator opt = _optBy(chain.toEntityMap(tableName));
if (null == opt)
return;
Expand All @@ -148,10 +152,10 @@ public void insert(String tableName, Chain chain) {
}

public void insert(Class<?> classOfT, Chain chain) {
if (chain.isSpecial()) {
Daos.insertBySpecialChain(this, getEntity(classOfT), null, chain);
return;
}
if (chain.isSpecial()) {
Daos.insertBySpecialChain(this, getEntity(classOfT), null, chain);
return;
}
EntityOperator opt = _opt(classOfT);
opt.myObj = chain;
opt.addInsertSelfOnly();
Expand Down Expand Up @@ -707,6 +711,42 @@ public int func(String tableName,
return pojo.getInt();
}

public Object func2(Class<?> classOfT, String func2Name, String fieldName) {
return func2(classOfT, func2Name, fieldName, null);
}

public Object func2(String tableName, String func2Name, String colName) {
return func2(tableName, func2Name, colName, null);
}

public Object func2(Class<?> classOfT,
String func2Name,
String colName,
Condition cnd) {
Entity<?> en = holder.getEntity(classOfT);
if (null != en.getField(colName))
colName = en.getField(colName).getColumnName();
DaoStatement pojo = pojoMaker.makeFunc(en.getViewName(),
func2Name,
colName)
.append(Pojos.Items.cnd(cnd))
.setAfter(_pojo_fetchObject)
.setEntity(en);
_exec(pojo);
return pojo.getResult();
}

public Object func2(String tableName,
String func2Name,
String colName,
Condition cnd) {
DaoStatement pojo = pojoMaker.makeFunc(tableName, func2Name, colName)
.append(Pojos.Items.cnd(cnd))
.setAfter(_pojo_fetchObject);
_exec(pojo);
return pojo.getResult();
}

public Pager createPager(int pageNumber, int pageSize) {
Pager pager = new Pager();
pager.setPageNumber(pageNumber);
Expand Down
20 changes: 20 additions & 0 deletions src/org/nutz/dao/impl/sql/pojo/PojoFetchObjectCallback.java
@@ -0,0 +1,20 @@
package org.nutz.dao.impl.sql.pojo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.nutz.dao.sql.Pojo;
import org.nutz.dao.sql.PojoCallback;

public class PojoFetchObjectCallback implements PojoCallback {

public Object invoke(Connection conn, ResultSet rs, Pojo pojo)
throws SQLException {
if (null != rs && rs.next()) {
return rs.getObject(1);
}
return null;
}

}
11 changes: 11 additions & 0 deletions test/org/nutz/dao/test/meta/Pet.java
Expand Up @@ -52,6 +52,17 @@ public static Pet[] create(int num) {
@Column
private Timestamp birthday;

@Column
private float price;

public float getPrice() {
return price;
}

public void setPrice(float price) {
this.price = price;
}

public int getId() {
return id;
}
Expand Down
29 changes: 25 additions & 4 deletions test/org/nutz/dao/test/normal/SimpleDaoTest.java
Expand Up @@ -27,6 +27,7 @@
import org.nutz.dao.test.meta.SimplePOJO;
import org.nutz.dao.test.meta.issue396.Issue396Master;
import org.nutz.lang.Lang;
import org.nutz.lang.random.R;

public class SimpleDaoTest extends DaoCase {

Expand All @@ -38,11 +39,29 @@ private void insertRecords(int len) {
for (int i = 0; i < len; i++) {
Pet pet = Pet.create("pet" + i);
pet.setNickName("alias_" + i);
pet.setPrice(R.random(30, 100) / 3.1415f);
dao.insert(pet);
}
}

// for issue #515 写给 mysql 一个特殊的例子
/**
* for issue #675 提供一个直接返回对象的方法
*/
@Test
public void test_dao_func() {
insertRecords(10);

int n = dao.func(Pet.class, "SUM", "price");
assertTrue(n > 0);

Object o = dao.func2(Pet.class, "SUM", "price");
assertTrue((o instanceof Double));
assertTrue(((Double) o).floatValue() > 0.0f);
}

/**
* for issue #515 写给 mysql 一个特殊的例子
*/
@Test
public void test_escape_char() {
if (dao.meta().isMySql()) {
Expand Down Expand Up @@ -275,10 +294,12 @@ public void test_insert_with() {
return;
dao.create(Issue396Master.class, true);
}

@Test
public void test_insert_special_chain() {
if (dao.meta().isMySql())
dao.insert(Pet.class, Chain.makeSpecial("birthday", "now()").add("name", "wendal"));
if (dao.meta().isMySql())
dao.insert(Pet.class,
Chain.makeSpecial("birthday", "now()").add("name",
"wendal"));
}
}

0 comments on commit 0bfa84b

Please sign in to comment.