Android Open Source - colibri Database






From Project

Back to project page colibri.

License

The source code is released under:

Apache License

If you think the Android project colibri listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package labrom.colibri.data;
//  ww w  .j  a v  a 2  s.c o m
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
import static labrom.colibri.Util.TAG;

/**
 *
 * @author Romain Laboisse labrom@gmail.com
 */
public abstract class Database {
  
    protected final SQLiteOpenHelper helper;
    private final ThreadLocal<SQLiteDatabase> dbs = new ThreadLocal<SQLiteDatabase>();
    
    
    protected Database(Context ctx) {
      this.helper = createHelper(ctx);
    }
    
    protected abstract SQLiteOpenHelper createHelper(Context ctx);


    public void close() {
      SQLiteDatabase db = dbs.get();
      if(db != null && db.isOpen()) {
          try {
              db.close();
              dbs.remove();
          } catch(Exception e) {
              Log.e(TAG, "Unable to close database: " + e.getMessage());
          }
      }
    }
    
  public SQLiteDatabase ensureOpen() {
      SQLiteDatabase db = dbs.get();
      if(db == null || !db.isOpen()) {
        db = helper.getWritableDatabase();
        dbs.set(db);
      }
      return db;
  }
  
  public <T extends ActiveRecord> boolean persist(T record) {
    SQLiteDatabase db = ensureOpen();
      ContentValues v = new ContentValues();
      record.populateFull(v);
        long id = db.insert(record.getTableName(), null, v);
        if(id >= 0) {
            record.attach(this, id);
            return true;
        }
        return false;
  }
  
  
  public <T extends ActiveRecord> boolean update(T record) {
    SQLiteDatabase db = ensureOpen();
      ContentValues v = new ContentValues();
      record.populateForUpdate(v);
      if(record.getRowId() >= 0)
          return db.update(record.getTableName(), v, BaseColumns._ID + "=" + record.getRowId(), null) == 1;
      
      return false;
  }
  
  public <T extends ActiveRecord> T read(T proto, long id) {
    SQLiteDatabase db = ensureOpen();
      Cursor c = db.query(proto.getTableName(), null, BaseColumns._ID + "=" + id, null, null, null, null);
      try {
          if(c.moveToFirst()) {
              @SuppressWarnings("unchecked")
                T copy = (T)proto.copy();
                copy.hydrateFromCursor(this, c);
              return copy;
          }
      } finally {
          c.close();
      }
      return null;
  }
  
  public <T extends ActiveRecord> boolean delete(T record) {
    SQLiteDatabase db = ensureOpen();
      return db.delete(record.getTableName(), BaseColumns._ID + "=" + record.getRowId(), null) == 1;
  }
  


    public <T extends ActiveRecord> ActiveRecordList<T> query(T proto, String[] columns, String selection, String[] selectionArgs, String orderBy) {
      return query(proto, false, columns, selection, selectionArgs, null, null, orderBy, null);
    }

    public <T extends ActiveRecord> ActiveRecordList<T> query(T proto, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
      return query(proto, false, columns, selection, selectionArgs, groupBy, having, orderBy, null);
    }

    public <T extends ActiveRecord> ActiveRecordList<T> query(T proto, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
      return query(proto, false, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
    }

    public <T extends ActiveRecord> ActiveRecordList<T> query(T proto, boolean distinct, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
      SQLiteDatabase db = ensureOpen();
        Cursor c = db.query(distinct, proto.getTableName(), columns, selection, selectionArgs, groupBy, having, orderBy, limit);
        return new ActiveRecordList<T>(this, proto, c);
    }

  public <T extends ActiveRecord> ActiveRecordList<T> queryByExample(T example, String orderBy) {
    SelectionArgs selection = new SelectionArgs();
    example.populateForQueryByExample(selection);
    return query(example, null, selection.buildSelectionString(), selection.buildSelectionValues(), orderBy);
  }
  
  public <T extends ActiveRecord> T getUnique(T proto, String selection, String[] selectionArgs) {
      ActiveRecordList<T> list = query(proto, null, selection, selectionArgs, null);
      return list.getUnique();
  }
  
  public <T extends ActiveRecord> int update(T proto, ContentValues values, String selection, String[] selectionArgs) {
    SQLiteDatabase db = ensureOpen();
      return db.update(proto.getTableName(), values, selection, selectionArgs);
  }
  

}




Java Source Code List

labrom.colibri.Util.java
labrom.colibri.cache.CacheEntry.java
labrom.colibri.cache.Cache.java
labrom.colibri.cache.Entries.java
labrom.colibri.data.ActiveContentProvider.java
labrom.colibri.data.ActiveRecordList.java
labrom.colibri.data.ActiveRecord.java
labrom.colibri.data.Database.java
labrom.colibri.data.SelectionArgs.java
labrom.colibri.xml.AttributeContext.java
labrom.colibri.xml.CursorDef.java
labrom.colibri.xml.CursorException.java
labrom.colibri.xml.Cursor.java
labrom.colibri.xml.CustomMap.java
labrom.colibri.xml.ElementContext.java
labrom.colibri.xml.FieldSetter.java
labrom.colibri.xml.Item.java
labrom.colibri.xml.NodeContext.java
labrom.colibri.xml.ObjectFactory.java
labrom.colibri.xml.ParserFactory.java
labrom.colibri.xml.ParserUtil.java
labrom.colibri.xml.PullTest.java
labrom.colibri.xml.ReflectorTest.java
labrom.colibri.xml.Reflector.java
labrom.colibri.xml.RootContext.java
labrom.colibri.xml.SetterTest.java
labrom.colibri.xml.StringToDateTransformer.java
labrom.colibri.xml.StringTransformer.java
labrom.colibri.xml.maps.Cursor4Maps.java
labrom.colibri.xml.maps.CursorDef4Maps.java
labrom.colibri.xml.maps.ElementContext4Maps.java
labrom.colibri.xml.maps.RootContext4Maps.java
labrom.colibri.xml.maps.package-info.java
labrom.colibri.xml.package-info.java