forked from p81075629/datagear
[persistence]完善类设计
This commit is contained in:
parent
64d0a6087a
commit
aea4003031
|
@ -2,9 +2,7 @@
|
|||
* Copyright 2018 datagear.tech. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package org.datagear.persistence.support;
|
||||
|
||||
import org.datagear.persistence.PersistenceException;
|
||||
package org.datagear.persistence;
|
||||
|
||||
/**
|
||||
* 非唯一结果异常。
|
|
@ -157,27 +157,26 @@ public interface PersistenceManager
|
|||
* @param table
|
||||
* @param param
|
||||
* @return
|
||||
* @throws NonUniqueResultException
|
||||
* @throws PersistenceException
|
||||
*/
|
||||
Row get(Connection cn, Table table, Row param) throws PersistenceException;
|
||||
Row get(Connection cn, Table table, Row param) throws NonUniqueResultException, PersistenceException;
|
||||
|
||||
/**
|
||||
* 获取行对象。
|
||||
*
|
||||
* @param cn
|
||||
* @param dialect
|
||||
* 允许为{@code null}
|
||||
* @param dialect 允许为{@code null}
|
||||
* @param table
|
||||
* @param param
|
||||
* @param sqlParamValueMapper
|
||||
* 允许为{@code null}
|
||||
* @param rowMapper
|
||||
* 允许为{@code null}
|
||||
* @param sqlParamValueMapper 允许为{@code null}
|
||||
* @param rowMapper 允许为{@code null}
|
||||
* @return
|
||||
* @throws NonUniqueResultException
|
||||
* @throws PersistenceException
|
||||
*/
|
||||
Row get(Connection cn, Dialect dialect, Table table, Row param, SqlParamValueMapper sqlParamValueMapper,
|
||||
RowMapper rowMapper) throws PersistenceException;
|
||||
RowMapper rowMapper) throws NonUniqueResultException, PersistenceException;
|
||||
|
||||
/**
|
||||
* 查询。
|
||||
|
|
|
@ -6,8 +6,6 @@ package org.datagear.persistence;
|
|||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 查询。
|
||||
|
@ -28,9 +26,6 @@ public class Query implements Serializable
|
|||
/** 排序方式 */
|
||||
private Order[] orders;
|
||||
|
||||
/** 查询参数 */
|
||||
private Map<String, Object> params = new HashMap<>();
|
||||
|
||||
/** 针对keyword,是否使用“NOT LIKE”而非“LIKE” */
|
||||
private boolean notLike = false;
|
||||
|
||||
|
@ -105,20 +100,10 @@ public class Query implements Serializable
|
|||
this.orders = orders;
|
||||
}
|
||||
|
||||
public Map<String, Object> getParams()
|
||||
{
|
||||
return params;
|
||||
}
|
||||
|
||||
public void setParam(String name, Object value)
|
||||
{
|
||||
this.params.put(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return getClass().getSimpleName() + " [keyword=" + keyword + ", condition=" + condition + ", orders="
|
||||
+ Arrays.toString(orders) + ", params=" + params + ", notLike=" + notLike + "]";
|
||||
+ Arrays.toString(orders) + ", notLike=" + notLike + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,9 +51,6 @@ public abstract class AbstractRowMapper extends PersistenceSupport implements Ro
|
|||
|
||||
/**
|
||||
* 映射列值。
|
||||
* <p>
|
||||
* 子类应该重写此方法实现映射列值。
|
||||
* </p>
|
||||
*
|
||||
* @param cn
|
||||
* @param table
|
||||
|
@ -64,9 +61,6 @@ public abstract class AbstractRowMapper extends PersistenceSupport implements Ro
|
|||
* @throws SQLException
|
||||
* @throws RowMapperException
|
||||
*/
|
||||
protected Object mapColumn(Connection cn, Table table, ResultSet rs, int rowIndex, Column column)
|
||||
throws SQLException, RowMapperException
|
||||
{
|
||||
throw new UnsupportedOperationException("Not implement");
|
||||
}
|
||||
protected abstract Object mapColumn(Connection cn, Table table, ResultSet rs, int rowIndex, Column column)
|
||||
throws SQLException, RowMapperException;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ import org.datagear.meta.Column;
|
|||
import org.datagear.meta.Table;
|
||||
import org.datagear.persistence.Dialect;
|
||||
import org.datagear.persistence.DialectSource;
|
||||
import org.datagear.persistence.NonUniqueResultException;
|
||||
import org.datagear.persistence.PagingData;
|
||||
import org.datagear.persistence.PagingQuery;
|
||||
import org.datagear.persistence.PersistenceException;
|
||||
|
@ -256,14 +257,14 @@ public class DefaultPersistenceManager extends PersistenceSupport implements Per
|
|||
}
|
||||
|
||||
@Override
|
||||
public Row get(Connection cn, Table table, Row param) throws PersistenceException
|
||||
public Row get(Connection cn, Table table, Row param) throws NonUniqueResultException, PersistenceException
|
||||
{
|
||||
return get(cn, null, table, param, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Row get(Connection cn, Dialect dialect, Table table, Row param, SqlParamValueMapper sqlParamValueMapper,
|
||||
RowMapper rowMapper) throws PersistenceException
|
||||
RowMapper rowMapper) throws NonUniqueResultException, PersistenceException
|
||||
{
|
||||
dialect = getDialect(cn, dialect);
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -76,13 +77,16 @@ public class DefaultPersistenceManagerTest extends DBTestSupport
|
|||
|
||||
try
|
||||
{
|
||||
this.defaultPersistenceManager.delete(connection, table, row);
|
||||
|
||||
this.defaultPersistenceManager.insert(connection, null, table, row, new ConversionSqlParamValueMapper());
|
||||
|
||||
Row getRow = this.defaultPersistenceManager.get(connection, table, row);
|
||||
|
||||
assertEquals(id, ((Number) getRow.get("ID")).intValue());
|
||||
assertEquals(name, getRow.get("NAME"));
|
||||
assertArrayEquals(new byte[] { 0x09 }, toBytes(getRow.get("HEAD_IMG")));
|
||||
assertArrayEquals(new byte[] { 0x09 }, columnValueToBytes(getRow.get("HEAD_IMG")));
|
||||
assertEquals(INTRODUCTION, columnValueToString(getRow.get("INTRODUCTION")));
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -102,7 +106,7 @@ public class DefaultPersistenceManagerTest extends DBTestSupport
|
|||
assertTrue(rows.size() <= 1);
|
||||
}
|
||||
|
||||
protected byte[] toBytes(Object o) throws Exception
|
||||
protected byte[] columnValueToBytes(Object o) throws Exception
|
||||
{
|
||||
if (o instanceof byte[])
|
||||
return (byte[]) o;
|
||||
|
@ -111,4 +115,19 @@ public class DefaultPersistenceManagerTest extends DBTestSupport
|
|||
else
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
protected String columnValueToString(Object o) throws Exception
|
||||
{
|
||||
if (o instanceof String)
|
||||
return (String) o;
|
||||
else if (o instanceof Reader)
|
||||
return IOUtil.readString((Reader) o, true);
|
||||
else if (o instanceof InputStream)
|
||||
{
|
||||
Reader reader = IOUtil.getReader((InputStream) o, null);
|
||||
return IOUtil.readString(reader, true);
|
||||
}
|
||||
else
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue