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 com.jqmobile.core.orm.ORM;
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.ModORM;
import com.jqmobile.core.server.db.orm.ModORMFactory;
import com.jqmobile.core.server.db.orm.ORMFactory;
import com.jqmobile.core.utils.plain.BeanUtils;
import com.jqmobile.core.utils.plain.FieldType;
import com.jqmobile.core.utils.plain.ModBean;
import com.jqmobile.core.utils.plain.ModColumn;
import com.jqmobile.core.utils.plain.UUIDUtils;
public class ModiBeanTest {
// @Test
public void testAdd(){
ModBean mb=new ModBean();
mb.setTableName("t_student");
List<ModColumn> list=mb.getModiColumns();
ModColumn mc=new ModColumn();
mb.addColumn(mc);
mb.addColumn(list);
}
// @Test
public void testCopyBean() throws ORMException {
TestBean2 tb=new TestBean2();
tb.setAge(1);
tb.setDate(new Date().getTime());
tb.setId(UUID.randomUUID().toString());
ModBean modiBean=BeanUtils.copy(tb, null);
System.out.println(modiBean.getModiColumns()+modiBean.getModiColumns().toString());
tb=BeanUtils.copy(modiBean,TestBean2.class);
System.out.println(tb.getAge()+"----"+tb.getDate()+"----"+tb.getId());
}
// @Test
public void testFind() throws SQLException, ORMNotDBTableException,
ORMException {
Connection connection = DBPool.getInstance().getConnection();
ORM<ModBean> orms = ORMFactory.instance(connection, null);
ModBean tb = orms.find("8b4193c8-5436-402c-a1f3-e227814f4809");
System.out.println( tb.getModiColumns().get(0).getName());
TestBean tb2=tb.getBean(TestBean.class);
System.out.println(tb2.getAge());
tb=BeanUtils.getModiBean(tb2);
System.out.println( tb.getModiColumns().get(0).getName());
connection.close();
}
// @Test
public void testInsert() throws SQLException, ORMNotDBTableException,
ORMException {
Connection connection = DBPool.getInstance().getConnection();
// 循环添加数据
ModBean modiBean=new ModBean();
ModColumn mc=new ModColumn();
mc.setName("id");
mc.setPaimaryId(true);
mc.setType(FieldType.STRING);
String value=UUID.randomUUID().toString();
mc.setValue(value);
ModColumn mc1=new ModColumn();
mc1.setName("birthday");
mc1.setType(FieldType.DATE);
mc1.setValue(new Date().getTime());
modiBean.addColumn(mc,mc1);
modiBean.setTableName("t_datetest1");
ModORM morm=ModORMFactory.instance(connection,modiBean);
// morm.createTable();
//
// for (int i = 0; i < 10; i++) {
// DateTestBean dtb=new DateTestBean();
// dtb.setId(UUID.randomUUID().toString());
// dtb.setBirthday(new Date().getTime());
// ModiBean mb =BeanUtils.getModiBean(dtb);
// mb.setTableName("t_datetest");
// morm.insert(mb);
// }
// DateTestBean dtb=new DateTestBean();
// dtb.setId(UUID.randomUUID().toString());
// dtb.setBirthday(new Date().getTime());
// ModiBean modiBean =BeanUtils.getModiBean(dtb);
// ModiBean mb=new ModiBean();
// ModiColumn mc=new ModiColumn();
// mc.setColumnName("id");
// mc.setFieldType("String");
// String value=UUID.randomUUID().toString();
// mc.setValue(value);
// ModiColumn mc1=new ModiColumn();
// mc1.setColumnName("id");
// mc1.setFieldType("String");
// mc1.setValue("sssssss");
// ModiColumn mc2=new ModiColumn();
// mc2.setColumnName("name");
// mc2.setFieldType("String");
// mc2.setValue("ssssss");
// mb.addColumn(mc,mc2);
// mb.setTableName("persion");
// mb.setPaimaryId("id");
//
//
// morm.insert(mb);
// ModiBean modiBean=new ModiBean();
// ModiColumn mc=new ModiColumn();
// mc.setName("id");
// mc.setPaimaryId(true);
// mc.setType(FieldType.STRING);
// String value=UUID.randomUUID().toString();
// mc.setValue(value);
// ModiColumn mc1=new ModiColumn();
// mc1.setName("birthday");
// mc1.setType(FieldType.DATE);
// mc1.setValue(new Date().getTime());
// ModiColumn mc2=new ModiColumn();
// modiBean.addColumn(mc,mc1);
// modiBean.setTableName("t_datetest");
// morm.update(modiBean);
// morm.delete(modiBean);
// morm.find("t_datetest", UUID.randomUUID().toString(), "id");
List<ModBean> list=morm.getAll("t_datetest");
for(ModBean mb:list){
for(ModColumn msc:mb.getModiColumns()){
if(msc.getName().equals("id")){
System.out.print(msc.getName()+"-------------"+UUIDUtils.getUUID((byte[]) msc.getValue())+"|||||||||||||");
continue;
}
System.out.print(msc.getName()+"-------------"+msc.getValue());
}
System.out.println();
}
// List<ModiBean> list=morm.queryPage("select * from user", 1, 2);
// ModiBean mb=morm.queryFirst("select * from t_test",null);
// TestBean tb=mb.getBean(TestBean.class);
// System.out.println(tb==null);
// System.out.println(tb);
// System.out.println(list.size());
//System.out.println(morm.queryRow("select count(*) from user", null));
connection.commit();
connection.close();
}
// @Test
public void testQuery() throws SQLException, ORMNotDBTableException, ORMException{
Connection connection = DBPool.getInstance().getConnection();
ORM<ModBean> orm = ORMFactory.instance(connection, null);
List<ModBean> list=orm.getAll();
int i=0;
for (ModBean mb:list) {
DateTestBean dtb=mb.getBean(DateTestBean.class);
System.out.println(dtb);
i++;
}
System.out.println(i);
connection.commit();
connection.close();
}
// @Test
public void testDelete() throws SQLException, ORMNotDBTableException, ORMException{
Connection connection = DBPool.getInstance().getConnection();
ORM<ModBean> orm = ORMFactory.instance(connection, null);
orm.delete("b7ee3f55-d646-44f1-8aed-16497026af81");
connection.commit();
connection.close();
}
// @Test
public void testFind2() throws SQLException, ORMNotDBTableException, ORMException{
Connection connection = DBPool.getInstance().getConnection();
ORM<ModBean> orm = ORMFactory.instance(connection, null);
DateTestBean dtb=orm.find("f11b21b3-acee-4342-998d-81ec6ec87110").getBean(DateTestBean.class);
System.out.println(dtb);
connection.commit();
connection.close();
}
// @Test
public void testQueryPage() throws SQLException, ORMNotDBTableException, ORMException{
Connection connection = DBPool.getInstance().getConnection();
ORM<ModBean> orm = ORMFactory.instance(connection, null);
List<ModBean> list=orm.queryPage("id =? ", 0, 2, new Object[]{UUID
.fromString("f11b21b3-acee-4342-998d-81ec6ec87110")});
for(ModBean mb:list){
System.out.println(mb.getBean(DateTestBean.class));
}
connection.commit();
connection.close();
}
// @Test
public void testQueryFirst() throws SQLException, ORMNotDBTableException, ORMException{
Connection connection = DBPool.getInstance().getConnection();
ORM<ModBean> orm = ORMFactory.instance(connection, null);
ModBean mb=orm.queryFirst("where id =? ", new Object[]{UUID
.fromString("f11b21b3-acee-4342-998d-81ec6ec87110")});
System.out.println(mb.getBean(DateTestBean.class));
connection.commit();
connection.close();
}
// @Test
public void testQueryBySql() throws SQLException, ORMNotDBTableException, ORMException{
Connection connection = DBPool.getInstance().getConnection();
ORM<ModBean> orm = ORMFactory.instance(connection, null);
List<ModBean> list=orm.query(" id = ?", new Object[]{UUID
.fromString("f11b21b3-acee-4342-998d-81ec6ec87110")});
for(ModBean mb:list){
System.out.println(mb.getBean(DateTestBean.class));
}
connection.commit();
connection.close();
}
// @Test
public void testVersion() throws SQLException, ORMNotDBTableException, ORMException{
Connection connection = DBPool.getInstance().getConnection();
ORM<ModBean> orm = ORMFactory.instance(connection,null);
DateTestBean dtb=new DateTestBean();
dtb.setBirthday(1384763259000l);
dtb.setId("c71853ad-32ac-4136-b4f2-1e016c9cfb38");
ModBean mb=BeanUtils.getModiBean(dtb);
orm.update(mb);
connection.commit();
connection.close();
}
// public static void main(String[] args) {
// byte[] b=new String(UUID.randomUUID().toString()).getBytes();
// System.out.println(Arrays.toString(b));
//// System.out.println(Arrays.toString("f11b21b3-acee-4342-998d-81ec6ec87110".getBytes()));
//// System.out.println(Arrays.toString("f11b21b3acee4342998d81ec6ec87110".getBytes()));
// System.out.println(Arrays.toString(UUIDUtils.getBytes("701e4013-441a-487f-9744-a26e5dd5c488")));
// }
}