package com.jqmobile.core.android.db.orm.modi; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.UUID; import android.database.sqlite.SQLiteDatabase; import com.jqmobile.core.android.db.orm.table.TableUtilFactory; import com.jqmobile.core.orm.TableUtil; import com.jqmobile.core.orm.exception.ORMException; import com.jqmobile.core.utils.plain.ModBean; class ModiORMImpl implements ModiORM { // 数据库连接对象 private SQLiteDatabase connection = null; /** * 初始化 * * @param conn */ public ModiORMImpl(SQLiteDatabase conn) { this.connection = conn; } /** * 自动验证表 * * @throws ORMException */ private boolean autoValiTable(String tableName) throws ORMException { TableUtil tableUtil = TableUtilFactory.instance(connection); return tableUtil.valiTableExist(tableName); } // /* // * (non-Javadoc) // * // * @see com.jiuqi.mobile.orm.base.ORMModi#insert(T) // */ // @Override // public int insert(ModiBean modiBean) throws ORMException { // int row = 0; // if (autoValiTable(modiBean.getTableName())) { // try { // String sql = insertSql(modiBean); // // // PreparedStatement ps = connection.prepareStatement(sql); // try { // row = ps.executeUpdate(); // } finally { // ps.close(); // } // } catch (SQLException e) { // throw new ORMException(e); // } // } else { // throw new ORMException("表不存在"); // } // return row; // } // // /** // * 拼装insert语句 insert into user ( id ,name,pwd) values(4,'d','d'); // * // * @param modiBean // * @return // */ // private String insertSql(ModiBean modiBean) { // StringBuilder sb = new StringBuilder(" insert into " // + modiBean.getTableName() + " ( "); // List<ModiColumn> mcs = modiBean.getModiColumns(); // for (ModiColumn mc : mcs) { // sb.append(mc.getColumnName() + ", "); // } // sb.deleteCharAt(sb.length() - 2); // sb.append(" ) values("); // for (ModiColumn mc : mcs) { // if (mc.getFieldType().contains("String")&&mc.getColumnName().equals(modiBean.getPaimaryId())) { // sb.append(((String)mc.getValue()).getBytes() + ", "); // continue; // }else if(mc.getFieldType().contains("String")){ // sb.append("'"+mc.getValue()+"',"); // continue; // } // sb.append(mc.getValue() + ", "); // } // String sql=sb.substring(0, sb.toString().length()-1)+" ); "; // return sql; // } // // /* // * @see com.jiuqi.mobile.orm.base.ORMModi#update(T) // */ // @Override // public int update(ModiBean modiBean) throws ORMException { // if (autoValiTable(modiBean.getTableName())) { // try { // String sql = updateSql(modiBean); // PreparedStatement ps = connection.prepareStatement(sql); // try { // return ps.executeUpdate(); // } finally { // ps.close(); // } // } catch (SQLException e) { // throw new ORMException(e); // } // } else { // throw new ORMException("表不存在"); // } // } // // /** // * 更新语句 update user set name='sssss' where id=1 // * // * @param modiBean // * @return // */ // private String updateSql(ModiBean modiBean) { // StringBuilder sb = new StringBuilder(" update " // + modiBean.getTableName() + " set "); // List<ModiColumn> mcs = modiBean.getModiColumns(); // for (ModiColumn mc : mcs) { // if (mc.getColumnName().equals(modiBean.getPaimaryId())) { // continue; // } // sb.append(mc.getColumnName() + " = " + mc.getValue()); // } // sb.append(" where " // + modiBean.getPaimaryId() // + " = " // + modiBean.queryColumn(modiBean.getPaimaryId(), true) // .getValue()); // return sb.toString(); // } // // @Override // public int delete(ModiBean modiBean) throws ORMException { // if (autoValiTable(modiBean.getTableName())) { // try { // String sql = deleteSql(modiBean); // System.out.println(sql); // PreparedStatement ps = connection.prepareStatement(sql); // try { // return ps.executeUpdate(); // } finally { // ps.close(); // } // } catch (SQLException e) { // throw new ORMException(e); // } // } else { // throw new ORMException("表不存在"); // } // } // // /** // * 删除sql delete from user where id=4 // * // * @param modiBean // * @return // */ // private String deleteSql(ModiBean modiBean) { // StringBuilder sb = new StringBuilder(" delete from " // + modiBean.getTableName() + " where "); // List<ModiColumn> mcs = modiBean.getModiColumns(); // for (ModiColumn mc : mcs) { // if (mc.getColumnName().equals(modiBean.getPaimaryId())) { // sb.append(modiBean.getPaimaryId() // + " = " // + modiBean.queryColumn(modiBean.getPaimaryId(), true) // .getValue()); // break; // } // } // return sb.toString(); // } // // @Override // public ModiBean find(String tableName, String recid, String paimaryId) // throws ORMException { // return find(tableName, UUID.fromString(recid), paimaryId); // } // // /** // * @see com.jiuqi.mobile.orm.base.ORMModi#find(java.lang.String, // * java.util.UUID, java.lang.String) // */ // @Override // public ModiBean find(String tableName, UUID recid, String paimaryId) // throws ORMException { // if (autoValiTable(tableName)) { // try { // String sql = findByIdSql(tableName, recid, paimaryId); // System.out.println(sql); // PreparedStatement ps = connection.prepareStatement(sql); // try { // ResultSet rs = ps.executeQuery(); // } finally { // ps.close(); // } // } catch (SQLException e) { // throw new ORMException(e); // } // } else { // throw new ORMException("表不存在"); // } // return null; // } // // /** // * 单条查询 // * // * @param tableName // * @param recid // * @param paimaryId // * @return // */ // private String findByIdSql(String tableName, UUID recid, String paimaryId) { // StringBuilder sb = new StringBuilder(" select * from " + tableName // + " where "); // sb.append(paimaryId + " = " + GUIDUtils.getBytes(recid)); // return sb.toString(); // } // // /** // * @see com.jiuqi.mobile.orm.base.ORMModi#getAll() // */ // @Override // public List<ModiBean> getAll(String tableName) throws ORMException { // String sql = "select * from " + tableName; // return query(sql); // } // // @Override // public List<ModiBean> query(String sql, Object... args) throws ORMException { // try { // PreparedStatement ps = connection.prepareStatement(sql); // try { // ResultSet rs = ps.executeQuery(); // try { // return getList(rs); // } catch (Exception e) { // throw new ORMException(e); // } finally { // rs.close(); // } // } finally { // ps.close(); // } // } catch (SQLException e) { // throw new ORMException(e); // } // } // // private List<ModiBean> getList(ResultSet rs) throws SQLException, // ClassNotFoundException { // ResultSetMetaData rsmd = rs.getMetaData(); // List<ModiBean> list = new ArrayList<ModiBean>(); // while (rs.next()) { // ModiBean modiBean = new ModiBean(); // for (int i = 0; i < rsmd.getColumnCount(); i++) { // ModiColumn mc = new ModiColumn(); // mc.setColumnName(rsmd.getColumnName(i + 1)); // mc.setFieldName(rsmd.getColumnName(i + 1)); // mc.setFieldType(rsmd.getColumnTypeName(i + 1)); // mc.setValue(rs.getObject(i + 1)); // modiBean.addColumn(mc); // } // list.add(modiBean); // } // return list; // } // // @Override // public List<ModiBean> queryPage(String sql, long startIndex, long endIndex, // Object... args) throws ORMException { // try { // sql = sql + " limit ?,?"; // PreparedStatement ps = connection.prepareStatement(sql); // ps.setLong(args.length + 1, startIndex); // ps.setLong(args.length + 2, endIndex); // try { // ResultSet rs = ps.executeQuery(); // try { // return getList(rs); // } catch (Exception e) { // throw new ORMException(e); // } finally { // rs.close(); // } // } finally { // ps.close(); // } // } catch (SQLException e) { // throw new ORMException(e); // } // } // // @Override // public ModiBean queryFirst(String sql, Object... args) throws ORMException { // try { // PreparedStatement ps = connection.prepareStatement(sql); // if (args != null && args.length > 0) { // for (int i = 1; i < args.length + 1; i++) { // ps.setObject(i, args[i - 1]); // } // } // try { // ResultSet rs = ps.executeQuery(); // try { // return getList(rs).get(0); // } catch (Exception e) { // throw new ORMException(e); // } finally { // rs.close(); // } // } finally { // ps.close(); // } // } catch (SQLException e) { // throw new ORMException(e); // } // } // // @Override // public int queryRow(String sql, Object... args) throws ORMException { // try { // PreparedStatement ps = connection.prepareStatement(sql); // if (args != null && args.length > 0) { // for (int i = 1; i < args.length + 1; i++) { // ps.setObject(i, args[i - 1]); // } // } // try { // ResultSet rs = ps.executeQuery(); // try { // if (rs.next()) // return rs.getInt(1); // return 0; // } catch (Exception e) { // throw new ORMException(e); // } finally { // rs.close(); // } // } finally { // ps.close(); // } // } catch (SQLException e) { // throw new ORMException(e); // } // } // // @Override // public int update(String sql, Object... args) throws ORMException { // try { // PreparedStatement ps = connection.prepareStatement(sql); // if (args != null && args.length > 0) { // for (int i = 1; i < args.length + 1; i++) { // ps.setObject(i, args[i - 1]); // } // } // try { // return ps.executeUpdate(); // } finally { // ps.close(); // } // } catch (SQLException e) { // throw new ORMException(e); // } // } // // @Override // public int delete(String sql, Object... args) throws ORMException { // try { // PreparedStatement ps = connection.prepareStatement(sql); // if (args != null && args.length > 0) { // for (int i = 1; i < args.length + 1; i++) { // ps.setObject(i, args[i - 1]); // } // } // try { // return ps.executeUpdate(); // } finally { // ps.close(); // } // } catch (SQLException e) { // throw new ORMException(e); // } // } @Override public int delete(String recid) throws ORMException { // TODO Auto-generated method stub return 0; } @Override public ModBean find(String recid) { // TODO Auto-generated method stub return null; } @Override public int delete(UUID recid) throws ORMException { // TODO Auto-generated method stub return 0; } @Override public ModBean find(UUID recid) { // TODO Auto-generated method stub return null; } @Override public List<ModBean> getAll() throws ORMException { // TODO Auto-generated method stub return null; } @Override public <D> List<D> queryRaw(String sql, Object[] args, ICallBack<D> back) throws SQLException, InstantiationException, IllegalAccessException { // TODO Auto-generated method stub return null; } @Override public <D> List<D> queryRaw(String sql, long startIndex, long endIndex, Object[] args, ICallBack<D> iCallBack) throws SQLException, InstantiationException, IllegalAccessException { // TODO Auto-generated method stub return null; } @Override public <D> D queryRawFirst(String sql, Object[] args, ICallBack<D> back) throws SQLException, InstantiationException, IllegalAccessException { // TODO Auto-generated method stub return null; } @Override public int modifyRaw(String sql, Object... args) throws SQLException { // TODO Auto-generated method stub return 0; } @Override public int deleteRaw(String sql, Object... args) throws SQLException { // TODO Auto-generated method stub return 0; } @Override public PreparedStatement getPrepareStatement(String sql) throws SQLException { // TODO Auto-generated method stub return null; } @Override public int insert(ModBean t) throws ORMException { // TODO Auto-generated method stub return 0; } @Override public int update(ModBean t) throws ORMException { // TODO Auto-generated method stub return 0; } @Override public List<ModBean> query(String where, Object... args) throws ORMException { // TODO Auto-generated method stub return null; } @Override public List<ModBean> queryPage(String where, long startIndex, long endIndex, Object... args) throws ORMException { // TODO Auto-generated method stub return null; } @Override public ModBean queryFirst(String where, Object... args) throws ORMException { // TODO Auto-generated method stub return null; } @Override public int queryRow(String where, Object... args) throws ORMException { // TODO Auto-generated method stub return 0; } @Override public int update(String set, Object... objs) throws ORMException { // TODO Auto-generated method stub return 0; } @Override public int delete(String where, Object... args) throws ORMException { // TODO Auto-generated method stub return 0; } }