Android Open Source - Alfred4Android D B Operation






From Project

Back to project page Alfred4Android.

License

The source code is released under:

Apache License

If you think the Android project Alfred4Android 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 com.toraleap.collimator.dal;
/*from   w ww  .j av  a2  s.  c o  m*/
import java.io.File;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.toraleap.collimator.model.BaseTag;

public class DBOperation {
  SQLiteDatabase mDb;
  
  public DBOperation(Context context) {
    mDb = new DBHelper(context).getWritableDatabase();
  }
  
  public void close() {
    mDb.close();
  }
  
  public Cursor queryFilesByTag(String tag) {
    return mDb.rawQuery(String.format("SELECT f.%s, f.%s, f.%s FROM %s f WHERE f.%s IN (SELECT DISTINCT l.%s FROM %s l WHERE l.%s IN (SELECT t.%s FROM %s t WHERE t.%s LIKE %s))",
        DBColumns.FileId, DBColumns.Name, DBColumns.Path, DBColumns.FileTable, DBColumns.FileId,
        DBColumns.FileId, DBColumns.LinkTable, DBColumns.TagId,
        DBColumns.TagId, DBColumns.TagTable, DBColumns.TagName,
        DatabaseUtils.sqlEscapeString("_" + tag + "_")), null);
  }
  
  public Cursor queryTagsByFile(String filename) {
    return mDb.rawQuery(String.format("SELECT t.%s, t.%s, t.%s FROM %s t WHERE t.%s IN (SELECT l.tagid FROM filetag l WHERE l.fileid = (SELECT f.id FROM file f WHERE f.path = %s))",
        DBColumns.TagId, DBColumns.Type, DBColumns.TagName, DBColumns.TagTable, DBColumns.TagId,
        DBColumns.TagId, DBColumns.LinkTable, DBColumns.FileId,
        DBColumns.FileId, DBColumns.FileTable, DBColumns.Path,
        DatabaseUtils.sqlEscapeString(filename)), null);
  }
  
  public boolean isFileExists(File file) {
    return mDb.rawQuery(String.format("SELECT %s FROM %s WHERE %s = %s",
        DBColumns.FileId, DBColumns.FileTable, DBColumns.Path,
        DatabaseUtils.sqlEscapeString(file.getPath())), null).getCount() > 0;
  }
  
  public boolean isFileModified(File file) {
    return mDb.rawQuery(String.format("SELECT %s FROM %s WHERE %s = %s AND %s = %s AND %s = %s",
        DBColumns.FileId, DBColumns.FileTable, DBColumns.Path,
        DatabaseUtils.sqlEscapeString(file.getPath()),
        DBColumns.Size, Long.toString(file.length()),
        DBColumns.LastModified, Long.toString(file.lastModified())), null).getCount() > 0;
  }
  
  public boolean insertFile(File file, ArrayList<BaseTag> tags) {
    Cursor cursor;
    ContentValues cv = new ContentValues();
    mDb.beginTransaction();
    // Create tags
    for (BaseTag tag : tags) {
      cv.clear();
      cv.put(DBColumns.TagName, tag.Name);
      cv.put(DBColumns.Type, tag.Type);
      mDb.insert(DBColumns.TagTable, null, cv);
      cursor = mDb.rawQuery("SELECT last_insert_rowid()", null);
      cursor.moveToFirst();
      tag.Id = cursor.getLong(0);
    }
    // Insert file
    cv.clear();
    cv.put(DBColumns.Name, file.getName());
    cv.put(DBColumns.Path, file.getPath());
    cv.put(DBColumns.Size, file.length());
    cv.put(DBColumns.LastModified, file.lastModified());
    mDb.insert(DBColumns.FileTable, null, cv);
    cursor = mDb.rawQuery("SELECT last_insert_rowid()", null);
    cursor.moveToFirst();
    long lastid = cursor.getLong(0);
    // Link file with tags
    for (BaseTag tag : tags) {
      cv.clear();
      cv.put(DBColumns.FileId, lastid);
      cv.put(DBColumns.TagId, tag.Id);
      mDb.insert(DBColumns.LinkTable, null, cv);
    }
    mDb.setTransactionSuccessful();
    mDb.endTransaction();
    return false;
  }
  
  public boolean updateFile(File file, ArrayList<BaseTag> tags) {
    return false;
  }
  
  public boolean removeFile(File file) {
    return mDb.delete(DBColumns.FileTable, String.format("%s = %s", DBColumns.Path, DatabaseUtils.sqlEscapeString(file.getPath())), null) > 0;
  }
}




Java Source Code List

android.media.MediaMetadataRetriever.java
com.toraleap.collimator.AppSearchActivity.java
com.toraleap.collimator.GlobalContext.java
com.toraleap.collimator.HelpActivity.java
com.toraleap.collimator.PrefsActivity.java
com.toraleap.collimator.SearchActivity.java
com.toraleap.collimator.ShortcutActivity.java
com.toraleap.collimator.bll.FileScannerService.java
com.toraleap.collimator.bll.TagGenerator.java
com.toraleap.collimator.dal.DBColumns.java
com.toraleap.collimator.dal.DBHelper.java
com.toraleap.collimator.dal.DBOperation.java
com.toraleap.collimator.data.Expression.java
com.toraleap.collimator.data.IndexData.java
com.toraleap.collimator.data.IndexLoader.java
com.toraleap.collimator.data.Index.java
com.toraleap.collimator.data.Match.java
com.toraleap.collimator.data.Matcher.java
com.toraleap.collimator.data.Sorter.java
com.toraleap.collimator.ext.GlobalProvider.java
com.toraleap.collimator.ext.Playlist.java
com.toraleap.collimator.model.BaseTag.java
com.toraleap.collimator.ui.FloatingDialog.java
com.toraleap.collimator.ui.MatchAdapter.java
com.toraleap.collimator.util.DigestUtil.java
com.toraleap.collimator.util.FileInfo.java
com.toraleap.collimator.util.MimeTypeMap.java
com.toraleap.collimator.util.RecursiveFileObserver.java
com.toraleap.collimator.util.ShortcutHelper.java
com.toraleap.collimator.util.SoftCache.java
com.toraleap.collimator.util.ThumbnailUtil.java
com.toraleap.collimator.util.Unicode2Alpha.java