Advertisement
almozavr

ORMan :: Cursor fix code

Sep 15th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1.  
  2. package org.orman.dbms.sqliteandroid;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.orman.dbms.QueryExecutionContainer;
  8. import org.orman.dbms.ResultList;
  9. import org.orman.dbms.exception.QueryExecutionException;
  10. import org.orman.sql.Query;
  11. import org.orman.util.logging.Log;
  12.  
  13. import android.database.Cursor;
  14. import android.database.sqlite.SQLiteDatabase;
  15. import android.database.sqlite.SQLiteException;
  16.  
  17. public class QueryExecutionContainerImpl implements QueryExecutionContainer {
  18.    
  19.     private SQLiteDatabase db;
  20.    
  21.     /**
  22.      * Initialize database, create db file if not exists.
  23.      * @param db
  24.      */
  25.     public QueryExecutionContainerImpl(SQLiteDatabase db){
  26.         this.db = db;
  27.     }
  28.    
  29.     private void throwError(SQLiteException e){
  30.         throw new QueryExecutionException("SQLiteAndroid error:" + e.toString());
  31.     }
  32.    
  33.  
  34.     @Override
  35.     public void executeOnly(Query q) {     
  36.         Log.trace("Executing: " + q);
  37.        
  38.         try {
  39.             db.execSQL(q.getExecutableSql());
  40.         } catch (SQLiteException e) {
  41.             throwError(e);
  42.         }
  43.     }
  44.  
  45.     @Override
  46.     public ResultList executeForResultList(Query q) {
  47.         Log.trace("Executing: " + q);
  48.        
  49.         try {
  50.             Cursor cur = db.rawQuery(q.getExecutableSql(), null);
  51.            
  52.             int columnCount = cur.getColumnCount();
  53.             String[] colNames = cur.getColumnNames();
  54.            
  55.             List<Object[]> result = new ArrayList<Object[]>();
  56.                
  57.             int rowIndex = 0;
  58.            
  59.             boolean hasRecord = cur.moveToFirst();
  60.            
  61.             while(hasRecord){
  62.                 Object[] row = new Object[columnCount];
  63.                
  64.                 for(int j = 0; j < columnCount; j++){ // for each column
  65.                     row[j] = cur.getString(j);
  66.                 }
  67.                 result.add(row);
  68.                 ++rowIndex;
  69.                
  70.                 hasRecord = cur.moveToNext();
  71.             }
  72.  
  73.             cur.close();
  74.  
  75.             if(result.size() > 0){
  76.                 Object[][] resultArr = new Object[result.size()][columnCount];
  77.                 int i = 0;
  78.                 for(Object[] row : result)
  79.                     resultArr[i++] = row;
  80.                
  81.                 return new ResultList(colNames, resultArr);
  82.             }
  83.         } catch (SQLiteException ex) {
  84.             throwError(ex);
  85.         }
  86.         return null;   
  87.     }
  88.  
  89.    
  90.     @Override
  91.     public Object executeForSingleValue(Query q) {
  92.         Log.trace("Executing: " + q);
  93.        
  94.             Cursor cur = null;
  95.         try {
  96.             cur = db.rawQuery(q.getExecutableSql(), null);
  97.             if (!cur.moveToNext()){
  98.                 return null;
  99.             } else {
  100.                 return cur.getString(0);
  101.             }
  102.         } catch (SQLiteException e) {
  103.             throwError(e);
  104.             return null;
  105.         }
  106.         finally{           
  107.             if (cur != null) cur.close();
  108.         }
  109.     }
  110.  
  111.     @Override
  112.     public Object getLastInsertId() {
  113.             Cursor cur = null;
  114.         try {
  115.             cur = db.rawQuery("SELECT last_insert_rowid()", null);
  116.             boolean status = cur.moveToFirst();
  117.             if (!status){
  118.                 return null;
  119.             } else {
  120.                 return cur.getLong(0); // TODO returns long. test for int and String behavior.
  121.             }
  122.         } catch (SQLiteException e) {
  123.             throwError(e);
  124.             return null;
  125.         }
  126.         finally{           
  127.             if (cur != null) cur.close();
  128.         }
  129.     }
  130.  
  131.     @Override
  132.     public <T> Object getLastInsertId(Class<T> ofType) {
  133.         Object val = getLastInsertId();
  134.        
  135.         if (val == null) {
  136.             Log.warn("last_insert_rowid() returned null from query. Propagating upwards as null, may cause anomalies.");
  137.             return null;
  138.         }
  139.        
  140.         if(ofType.equals(String.class)){
  141.             return new String(val.toString());
  142.         } else if(ofType.equals(Integer.class) || ofType.equals(Integer.TYPE)){
  143.             return new Integer(val.toString()); // TODO inefficient?
  144.         } else if(ofType.equals(Long.class) || ofType.equals(Long.TYPE)){
  145.             return new Long(val.toString()); // TODO inefficient?
  146.         }  
  147.         return val;
  148.     }
  149.    
  150.     @Override
  151.     public void close() {
  152.         db.close();
  153.     }
  154.  
  155.     @Override
  156.     public boolean open(long cookie) {
  157.         return true;
  158.     }
  159.  
  160.     @Override
  161.     public boolean isAlive() {
  162.         if (db == null)
  163.             return false;
  164.         return db.isOpen();
  165.     }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement