package com.jiuqi.mobile.core.service.db;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.junit.Assert;
import org.junit.Test;
import com.jqmobile.core.orm.ORM;
import com.jqmobile.core.orm.RsAccessor;
import com.jqmobile.core.orm.ORMS.ICallBack;
import com.jqmobile.core.orm.exception.ORMException;
import com.jqmobile.core.orm.exception.ORMNotDBTableException;
import com.jqmobile.core.server.db.DBPool;
import com.jqmobile.core.server.db.orm.ORMDBTable;
import com.jqmobile.core.server.db.orm.ORMFactory;
public class ORMTest<D> extends Assert{
public static void main(String[] args) throws SQLException {
Connection conn = DBPool.getInstance().getConnection();
assertTrue(null != conn);
}
//
public void testConn() throws SQLException{
Connection conn = DBPool.getInstance().getConnection();
assertTrue(null != conn);
}
//
public void testInsert() throws SQLException, ORMNotDBTableException,
ORMException {
Connection connection = DBPool.getInstance().getConnection();
// 循环添加数据
ORM<TestBean> orm = ORMFactory.instance(connection, TestBean.class);
for (int i = 0; i < 10; i++) {
TestBean t = new TestBean();
t.setAge(122 + i);
t.setDate(new Date().getTime());
t.setMaxSize(200);
t.setMemo("oooooo");
t.setSex(0);
t.setRecid(UUID.randomUUID().toString());
orm.insert(t);
}
connection.commit();
connection.close();
}
public void testQuery() throws SQLException, ORMNotDBTableException,
ORMException {
Connection connection = DBPool.getInstance().getConnection();
ORM<TestBean> orms = ORMFactory.instance(connection, TestBean.class);
List<TestBean> list = orms.query("", null);
for (TestBean tb : list) {
System.out.println(tb.getRecid() + "---" + tb.getAge()+"---"
+ tb.getDate() + "---" + tb.getMaxSize() + "---"
+ tb.getMemo() + "---" + tb.getSex());
}
connection.commit();
connection.close();
}
public void testDelete() throws SQLException, ORMNotDBTableException,
Exception {
Connection connection = DBPool.getInstance().getConnection();
ORM<TestBean> orms = ORMFactory.instance(connection, TestBean.class);
orms.delete("299869d8-5353-4da8-acc3-e64fbbc73d29");
connection.commit();
connection.close();
}
public void testUpdate() throws SQLException, ORMNotDBTableException,
Exception {
Connection connection = DBPool.getInstance().getConnection();
ORM<TestBean> orms = ORMFactory.instance(connection, TestBean.class);
TestBean t = new TestBean();
t.setAge(1000000);
t.setDate(138430595382l);
t.setMaxSize(1000);
t.setMemo("aaaaaaaaaaaaaaaa");
t.setSex(1);
t.setRecid("a357f1ff-66f7-4ff6-83d2-b3d87b930986");
orms.update(t);
connection.commit();
connection.close();
}
public void testQueryBysql() throws SQLException, ORMNotDBTableException,
ORMException {
Connection connection = DBPool.getInstance().getConnection();
ORM<TestBean> orms = ORMFactory.instance(connection, TestBean.class);
// 必须传入一个uuid
int row = orms.queryRow("where recid=?", new Object[] { UUID
.fromString("a357f1ff-66f7-4ff6-83d2-b3d87b930986") });
System.out.println("该条件下共有" + row + "条数据");
connection.close();
}
public void testFind() throws SQLException, ORMNotDBTableException,
ORMException {
Connection connection = DBPool.getInstance().getConnection();
ORM<TestBean> orms = ORMFactory.instance(connection, TestBean.class);
// **sql正常,rs异常**//*
Object object = orms.find("6d8f9dee-1694-48b6-a31c-270f5f0d5b01");
TestBean tb = (TestBean) object;
System.out.println(tb.getRecid() + "---" + tb.getAge() + tb.getDate()
+ "---" + tb.getMaxSize() + "---" + tb.getMemo() + "---"
+ tb.getSex());
connection.close();
}
public void testFindByUUID() throws SQLException, ORMNotDBTableException,
ORMException {
Connection connection = DBPool.getInstance().getConnection();
ORM<TestBean> orms = ORMFactory.instance(connection, TestBean.class);
/** 问题同上 **/
Object object = orms.find(UUID
.fromString("6d8f9dee-1694-48b6-a31c-270f5f0d5b01"));
TestBean tb = (TestBean) object;
System.out.println(tb.getRecid());
connection.close();
}
public void testGetAll() throws SQLException, ORMNotDBTableException,
ORMException {
Connection connection = DBPool.getInstance().getConnection();
ORM<DateTestBean> orms = ORMFactory.instance(connection, DateTestBean.class);
List<DateTestBean> list = orms.getAll();
System.out.println(list.get(0).getBirthday());
connection.close();
}
// 测试不会通过,因为没有实现get方法
public void testQueryWithBack() throws SQLException,
ORMNotDBTableException, ORMException, InstantiationException,
IllegalAccessException {
Connection connection = DBPool.getInstance().getConnection();
ORM<TestBean> orms = ORMFactory.instance(connection, TestBean.class);
List<D> list = orms.queryRaw("select * from t_test", null,
new ICallBack<D>() {
@Override
public D get(RsAccessor rs) throws SQLException {
// TODO Auto-generated method stub
return null;
}
});
System.out.println(list.size());
connection.close();
}
//
// // 测试不会通过,因为没有实现get方法
// public void testGetFirstWithBack() throws SQLException,
// ORMNotDBTableException, ORMException, InstantiationException,
// IllegalAccessException {
// Connection connection = DBPool.getInstance().getConnection();
// ORM<TestBean> orms = ORMFactory.instance(connection, TestBean.class);
// TestBean tb = (TestBean) orms.queryRawFirst(
// "select * from t_test where age=18", null, new ICallBack<D>() {
//
// @Override
// public D get(RsAccessor rs) throws SQLException {
// // TODO Auto-generated method stub
// return null;
// }
// });
// System.out.println(tb.getRecid() + "----" + tb.getAge() + "----"
// + tb.getDate() + "----" + tb.getMaxSize() + tb.getMemo()
// + tb.getSex());
// connection.close();
// }
public void testPage() throws SQLException, ORMNotDBTableException,
ORMException, InstantiationException, IllegalAccessException {
Connection connection = DBPool.getInstance().getConnection();
ORM<TestBean> orms = ORMFactory.instance(connection, TestBean.class);
List<TestBean> list = orms.queryPage("recid=?", 0, 2,
new Object[] { UUID
.fromString("a357f1ff-66f7-4ff6-83d2-b3d87b930986") });
System.out.println(list.size());
connection.close();
}
public void testDBTable() throws SQLException, ORMNotDBTableException, ORMException{
Connection connection = DBPool.getInstance().getConnection();
ORM<ORMDBTable> orm = ORMFactory.instance(connection, ORMDBTable.class);
ORMDBTable sdt = new ORMDBTable();
sdt.setClassUrl("ssssssssss");
sdt.setName("ssssssss");
sdt.setRecid(UUID.randomUUID());
sdt.setVersion(0);
orm.insert(sdt);
connection.close();
}
// public static void main(String[] args) {
// ORMTest o=new ORMTest();
// try {
// o.testGetAll();
// } catch (ORMNotDBTableException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (ORMException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}