|
| 1 | +package com.github.mzule.androidweekly.dao; |
| 2 | + |
| 3 | +import android.content.ContentValues; |
| 4 | +import android.database.Cursor; |
| 5 | +import android.database.sqlite.SQLiteDatabase; |
| 6 | +import android.database.sqlite.SQLiteOpenHelper; |
| 7 | + |
| 8 | +import com.github.mzule.androidweekly.App; |
| 9 | +import com.github.mzule.androidweekly.entity.Article; |
| 10 | +import com.github.mzule.androidweekly.entity.Favorite; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +/** |
| 16 | + * Created by CaoDongping on 4/2/16. |
| 17 | + */ |
| 18 | +public class FavoriteDao extends SQLiteOpenHelper { |
| 19 | + private static final String NAME = "favorite"; |
| 20 | + private static final int VERSION = 1; |
| 21 | + |
| 22 | + public FavoriteDao() { |
| 23 | + super(App.getInstance(), NAME, null, VERSION); |
| 24 | + } |
| 25 | + |
| 26 | + public List<Favorite> read() { |
| 27 | + List<Favorite> favorites = new ArrayList<>(); |
| 28 | + Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM FAVORITE ORDER BY TIME DESC", null); |
| 29 | + while (cursor.moveToNext()) { |
| 30 | + Article article = new Article(); |
| 31 | + article.setTitle(cursor.getString(1)); |
| 32 | + article.setBrief(cursor.getString(2)); |
| 33 | + article.setLink(cursor.getString(3)); |
| 34 | + article.setImageUrl(cursor.getString(4)); |
| 35 | + article.setDomain(cursor.getString(5)); |
| 36 | + article.setIssue(cursor.getString(6)); |
| 37 | + article.setSection(cursor.getString(7)); |
| 38 | + Favorite favorite = new Favorite(article); |
| 39 | + favorite.setTime(cursor.getLong(9)); |
| 40 | + favorites.add(favorite); |
| 41 | + } |
| 42 | + cursor.close(); |
| 43 | + return favorites; |
| 44 | + } |
| 45 | + |
| 46 | + public boolean contains(Article article) { |
| 47 | + Cursor cursor = getReadableDatabase().rawQuery("SELECT COUNT(*) FROM FAVORITE WHERE LINK=? AND ISSUE=?", new String[]{article.getLink(), article.getIssue()}); |
| 48 | + cursor.moveToNext(); |
| 49 | + int count = cursor.getInt(0); |
| 50 | + cursor.close(); |
| 51 | + return count > 0; |
| 52 | + } |
| 53 | + |
| 54 | + public void save(Article article) { |
| 55 | + if (!contains(article)) { |
| 56 | + ContentValues cv = new ContentValues(); |
| 57 | + cv.put("TITLE", article.getTitle()); |
| 58 | + cv.put("BRIEF", article.getBrief()); |
| 59 | + cv.put("LINK", article.getLink()); |
| 60 | + cv.put("IMAGE_URL", article.getImageUrl()); |
| 61 | + cv.put("DOMAIN", article.getDomain()); |
| 62 | + cv.put("ISSUE", article.getIssue()); |
| 63 | + cv.put("SECTION", article.getSection()); |
| 64 | + cv.put("TYPE", 0);// TODO add type for article |
| 65 | + cv.put("TIME", System.currentTimeMillis()); |
| 66 | + getWritableDatabase().insert("FAVORITE", null, cv); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public void delete(Article article) { |
| 71 | + getWritableDatabase().delete("FAVORITE", "LINK=? AND ISSUE=?", new String[]{article.getLink(), article.getIssue()}); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void onCreate(SQLiteDatabase db) { |
| 76 | + // 不创建一个与Article的外键引用是因为Article表是cache,可能会被清除。 |
| 77 | + db.execSQL("CREATE TABLE IF NOT EXISTS FAVORITE(" + |
| 78 | + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " + |
| 79 | + "TITLE VARCHAR(255), " + |
| 80 | + "BRIEF TEXT, " + |
| 81 | + "LINK VARCHAR(255), " + |
| 82 | + "IMAGE_URL VARCHAR(255), " + |
| 83 | + "DOMAIN VARCHAR(255), " + |
| 84 | + "ISSUE VARCHAR(255), " + |
| 85 | + "SECTION VARCHAR(255), " + |
| 86 | + "TYPE INTEGER, " + |
| 87 | + "TIME INTEGER)"); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 92 | + |
| 93 | + } |
| 94 | +} |
0 commit comments