package com.jqmobile.core.android.db.orm.base; import java.io.InputStream; import java.math.BigDecimal; import java.sql.Blob; import java.sql.Clob; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import java.util.UUID; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.jqmobile.core.orm.ORMS; import com.jqmobile.core.orm.RsAccessor; import com.jqmobile.core.orm.exception.ORMParamNotRecognitionException; class ORMSImpl extends OImpl implements ORMS{ public ORMSImpl(SQLiteDatabase conn) { super(conn); } public <D> List<D> queryRaw(String sql, Object[] args, ICallBack<D> back) throws SQLException, InstantiationException, IllegalAccessException { Cursor rs = getConnection().rawQuery(sql, getParams(args)); try{ return getResultList(back, rs); }finally{ rs.close(); } } protected String[] getParams(Object[] args) { String[] ss = new String[args.length]; for(int i=0; i<args.length; i++){ ss[i] = getParam(args[i]); } return ss; } private <D> List<D> getResultList(ICallBack<D> back, Cursor rs) throws SQLException, InstantiationException, IllegalAccessException { List<D> list = new ArrayList<D>(); for(rs.moveToFirst();!rs.isAfterLast();rs.moveToNext()){ list.add(back.get(new RsAccessorImpl(rs))); } return list; } // protected PreparedStatement getPreparedStatement(String sql, Object... args) // throws SQLException { // PreparedStatement ps = getPrepareStatement(sql); // if(args!=null&&args.length>0){ // for(int i=0; i<args.length; i++){ // //ps.setObject(i+1, args[i]); // ps.setObject(i+1, getParam(args[i])); // } // } // return ps; // } //只能传递uuid、主键、二进制字节流,其他均报异常 protected String getParam(Object obj) { if(obj instanceof UUID){ return obj.toString();//GUIDUtils.getBytes((UUID)obj); }else if(obj.getClass().isPrimitive()){ return obj+""; }else if(obj instanceof byte[] || obj instanceof Byte[]){ return new String((byte[])obj); }else{ throw new ORMParamNotRecognitionException(obj); } } public <D> List<D> queryRaw(String sql, long startIndex, long endIndex, Object[] args, ICallBack<D> iCallBack) throws SQLException, InstantiationException, IllegalAccessException { sql += " limit "+startIndex+","+endIndex+" "; // PreparedStatement ps = getPreparedStatement(sql, args); // ps.setLong(args.length+1, startIndex); // ps.setLong(args.length+2, endIndex); Cursor rs = getConnection().rawQuery(sql, getParams(args)); try{ return getResultList(iCallBack, rs); }finally{ rs.close(); } } public <D> D queryRawFirst(String sql, Object[] args, ICallBack<D> back) throws SQLException, InstantiationException, IllegalAccessException { Cursor rs = getConnection().rawQuery(sql, getParams(args)); try{ if(rs.moveToFirst()){ return back.get(new RsAccessorImpl(rs)); } }finally{ rs.close(); } return null; } public int modifyRaw(String sql, Object... args) throws SQLException { getConnection().execSQL(sql, args); return -1; } public int deleteRaw(String sql, Object... args) throws SQLException { getConnection().execSQL(sql, args); return -1; } public PreparedStatement getPrepareStatement(String sql) throws SQLException { return null; // return getConnection().prepareStatement(sql); } // class RsAccessorImpl implements RsAccessor { private final Cursor rs; private RsAccessorImpl(Cursor rs2) { this.rs = rs2; } @Override public UUID getUUID(int columnIndex) throws SQLException { String uuid = rs.getString(columnIndex); return UUID.fromString(uuid); } @Override public String getString(int columnIndex) throws SQLException { return rs.getString(columnIndex); } @Override public boolean getBoolean(int columnIndex) throws SQLException { int i = rs.getInt(columnIndex); return 1==i; } @Override public byte getByte(int columnIndex) throws SQLException { String i = rs.getString(columnIndex); return new Byte(i); } @Override public short getShort(int columnIndex) throws SQLException { return rs.getShort(columnIndex); } @Override public int getInt(int columnIndex) throws SQLException { return rs.getInt(columnIndex); } @Override public long getLong(int columnIndex) throws SQLException { return rs.getLong(columnIndex); } @Override public float getFloat(int columnIndex) throws SQLException { return rs.getFloat(columnIndex); } @Override public double getDouble(int columnIndex) throws SQLException { return rs.getDouble(columnIndex); } @Override public byte[] getBytes(int columnIndex) throws SQLException { return rs.getBlob(columnIndex); } @Override public InputStream getAsciiStream(int columnIndex) throws SQLException { return null;//rs.getAsciiStream(columnIndex); } @Override public InputStream getBinaryStream(int columnIndex) throws SQLException { return null;//rs.getBinaryStream(columnIndex); } @Override public BigDecimal getBigDecimal(int columnIndex) throws SQLException { return null;//rs.getBigDecimal(columnIndex); } @Override public Blob getBlob(int columnIndex) throws SQLException { return null;//rs.getBlob(columnIndex); } @Override public Clob getClob(int columnIndex) throws SQLException { return null;//rs.getClob(columnIndex); } @Override public String getColumnName(int columnIndex) throws SQLException { return rs.getString(columnIndex); } @Override public Object getObject(int columnIndex) throws SQLException { return rs.getString(columnIndex);//rs.getObject(i); } @Override public Timestamp getTimestamp(int columnIndex) throws SQLException { return new Timestamp(rs.getLong(columnIndex)); } } }