|
|
|
|
@ -1,13 +1,13 @@
|
|
|
|
|
//指定了当前类BookContentBeanDao所在的包路径,即com.monke.monkeybook.dao
|
|
|
|
|
package com.monke.monkeybook.dao;
|
|
|
|
|
//导入必要的类,包含GreenDAO、SQLite和Android数据库操作的相关类
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.database.sqlite.SQLiteStatement;
|
|
|
|
|
import org.greenrobot.greendao.AbstractDao;
|
|
|
|
|
import org.greenrobot.greendao.Property;
|
|
|
|
|
import org.greenrobot.greendao.internal.DaoConfig;
|
|
|
|
|
import org.greenrobot.greendao.database.Database;
|
|
|
|
|
import org.greenrobot.greendao.database.DatabaseStatement;
|
|
|
|
|
import android.database.Cursor;// 导入Cursor类,用于数据库查询结果
|
|
|
|
|
import android.database.sqlite.SQLiteStatement;// 导入SQLiteStatement类,用于预编译SQL语句
|
|
|
|
|
import org.greenrobot.greendao.AbstractDao;// 导入AbstractDao类,这是GreenDAO的基类,用于实现DAO功能
|
|
|
|
|
import org.greenrobot.greendao.Property;// 导入Property类,用于属性映射
|
|
|
|
|
import org.greenrobot.greendao.internal.DaoConfig;// 导入DaoConfig类,包含DAO的配置
|
|
|
|
|
import org.greenrobot.greendao.database.Database;// 导入Database类,代表数据库操作
|
|
|
|
|
import org.greenrobot.greendao.database.DatabaseStatement;// 导入DatabaseStatement类,用于执行数据库语句
|
|
|
|
|
// 导入实体类 BookContentBean
|
|
|
|
|
import com.monke.monkeybook.bean.BookContentBean;
|
|
|
|
|
|
|
|
|
|
@ -18,7 +18,7 @@ import com.monke.monkeybook.bean.BookContentBean;
|
|
|
|
|
//继承自GreenDAO的AbstractDao类,BookContentBean为实体类,String为主键类型
|
|
|
|
|
public class BookContentBeanDao extends AbstractDao<BookContentBean, String> {
|
|
|
|
|
//定义常量TABLENAME,表示该表的名称
|
|
|
|
|
public static final String TABLENAME = "BOOK_CONTENT_BEAN";
|
|
|
|
|
public static final String TABLENAME = "BOOK_CONTENT_BEAN";// 表名常量
|
|
|
|
|
/**
|
|
|
|
|
* Properties of entity BookContentBean.<br/>
|
|
|
|
|
* Can be used for QueryBuilder and for referencing column names.
|
|
|
|
|
@ -26,10 +26,10 @@ public class BookContentBeanDao extends AbstractDao<BookContentBean, String> {
|
|
|
|
|
*/
|
|
|
|
|
// 定义实体类属性映射:对应数据库表字段名的属性
|
|
|
|
|
public static class Properties {
|
|
|
|
|
public final static Property DurChapterUrl = new Property(0, String.class, "durChapterUrl", true, "DUR_CHAPTER_URL");
|
|
|
|
|
public final static Property DurChapterIndex = new Property(1, int.class, "durChapterIndex", false, "DUR_CHAPTER_INDEX");
|
|
|
|
|
public final static Property DurCapterContent = new Property(2, String.class, "durCapterContent", false, "DUR_CAPTER_CONTENT");
|
|
|
|
|
public final static Property Tag = new Property(3, String.class, "tag", false, "TAG");
|
|
|
|
|
public final static Property DurChapterUrl = new Property(0, String.class, "durChapterUrl", true, "DUR_CHAPTER_URL");// 章节URL属性
|
|
|
|
|
public final static Property DurChapterIndex = new Property(1, int.class, "durChapterIndex", false, "DUR_CHAPTER_INDEX");// 章节索引属性
|
|
|
|
|
public final static Property DurCapterContent = new Property(2, String.class, "durCapterContent", false, "DUR_CAPTER_CONTENT");// 章节内容属性
|
|
|
|
|
public final static Property Tag = new Property(3, String.class, "tag", false, "TAG");// 标签属性
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//构造函数,传入DaoConfig,用于初始化DAO
|
|
|
|
|
@ -51,10 +51,10 @@ public class BookContentBeanDao extends AbstractDao<BookContentBean, String> {
|
|
|
|
|
String constraint = ifNotExists? "IF NOT EXISTS ": "";
|
|
|
|
|
//使用SQL执行表创建语句
|
|
|
|
|
db.execSQL("CREATE TABLE " + constraint + "\"BOOK_CONTENT_BEAN\" (" + //
|
|
|
|
|
"\"DUR_CHAPTER_URL\" TEXT PRIMARY KEY NOT NULL ," + // 0: durChapterUrl
|
|
|
|
|
"\"DUR_CHAPTER_INDEX\" INTEGER NOT NULL ," + // 1: durChapterIndex
|
|
|
|
|
"\"DUR_CAPTER_CONTENT\" TEXT," + // 2: durCapterContent
|
|
|
|
|
"\"TAG\" TEXT);"); // 3: tag
|
|
|
|
|
"\"DUR_CHAPTER_URL\" TEXT PRIMARY KEY NOT NULL ," + // 0: durChapterUrl, 主键列
|
|
|
|
|
"\"DUR_CHAPTER_INDEX\" INTEGER NOT NULL ," + // 1: durChapterIndex, 整数列
|
|
|
|
|
"\"DUR_CAPTER_CONTENT\" TEXT," + // 2: durCapterContent,章节内容列
|
|
|
|
|
"\"TAG\" TEXT);"); // 3: tag, 标签列
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Drops the underlying database table.
|
|
|
|
|
@ -62,73 +62,73 @@ public class BookContentBeanDao extends AbstractDao<BookContentBean, String> {
|
|
|
|
|
*/
|
|
|
|
|
public static void dropTable(Database db, boolean ifExists) {
|
|
|
|
|
//使用SQL删除表,确保表存在时删除
|
|
|
|
|
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BOOK_CONTENT_BEAN\"";
|
|
|
|
|
db.execSQL(sql);
|
|
|
|
|
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BOOK_CONTENT_BEAN\"";// 生成删除表的SQL语句
|
|
|
|
|
db.execSQL(sql);// 执行删除表操作
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected final void bindValues(DatabaseStatement stmt, BookContentBean entity) {
|
|
|
|
|
//清除绑定的值,确保每次绑定时都是干净的
|
|
|
|
|
stmt.clearBindings();
|
|
|
|
|
stmt.clearBindings();// 清除先前绑定的参数
|
|
|
|
|
|
|
|
|
|
//绑定durChapterUrl字段到SQL语句的第一个位置
|
|
|
|
|
String durChapterUrl = entity.getDurChapterUrl();
|
|
|
|
|
String durChapterUrl = entity.getDurChapterUrl();// 获取durChapterUrl属性值
|
|
|
|
|
if (durChapterUrl != null) {
|
|
|
|
|
//将章节URL字符串绑定到SQL语句的第一个位置
|
|
|
|
|
stmt.bindString(1, durChapterUrl);
|
|
|
|
|
stmt.bindString(1, durChapterUrl);// 将durChapterUrl绑定到SQL语句的第一个参数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//绑定durChapterIndex字段到SQL语句的第二个位置
|
|
|
|
|
stmt.bindLong(2, entity.getDurChapterIndex());//将章节索引绑定到SQL语句的第二个位置
|
|
|
|
|
|
|
|
|
|
//绑定durCapterContent字段到SQL语句的第三个位置
|
|
|
|
|
String durCapterContent = entity.getDurCapterContent();
|
|
|
|
|
String durCapterContent = entity.getDurCapterContent();// 获取durCapterContent属性值
|
|
|
|
|
if (durCapterContent != null) {
|
|
|
|
|
//将章节内容绑定到SQL语句的第三个位置
|
|
|
|
|
stmt.bindString(3, durCapterContent);
|
|
|
|
|
stmt.bindString(3, durCapterContent);// 将durCapterContent绑定到SQL语句的第三个参数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//绑定tag字段到SQL语句的第四个位置
|
|
|
|
|
String tag = entity.getTag();
|
|
|
|
|
String tag = entity.getTag();// 获取tag属性值
|
|
|
|
|
if (tag != null) {
|
|
|
|
|
//将标签绑定到SQL语句的第四个位置
|
|
|
|
|
stmt.bindString(4, tag);
|
|
|
|
|
stmt.bindString(4, tag);// 将tag绑定到SQL语句的第四个参数
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected final void bindValues(SQLiteStatement stmt, BookContentBean entity) {
|
|
|
|
|
//清除绑定的值,确保每次绑定时都是干净的
|
|
|
|
|
stmt.clearBindings();
|
|
|
|
|
stmt.clearBindings();// 清除先前绑定的参数
|
|
|
|
|
|
|
|
|
|
//绑定durChapterUrl字段到SQL语句的第一个位置
|
|
|
|
|
String durChapterUrl = entity.getDurChapterUrl();
|
|
|
|
|
String durChapterUrl = entity.getDurChapterUrl();// 获取durChapterUrl属性值
|
|
|
|
|
if (durChapterUrl != null) {
|
|
|
|
|
//将章节URL字符串绑定到SQL语句的第一个位置
|
|
|
|
|
stmt.bindString(1, durChapterUrl);
|
|
|
|
|
stmt.bindString(1, durChapterUrl);// 将durChapterUrl绑定到SQL语句的第一个参数
|
|
|
|
|
}
|
|
|
|
|
//绑定durChapterIndex字段到SQL语句的第二个位置
|
|
|
|
|
stmt.bindLong(2, entity.getDurChapterIndex()); //将章节索引绑定到SQL语句的第二个位置
|
|
|
|
|
|
|
|
|
|
//绑定durCapterContent字段到SQL语句的第三个位置
|
|
|
|
|
String durCapterContent = entity.getDurCapterContent();
|
|
|
|
|
String durCapterContent = entity.getDurCapterContent();// 获取durCapterContent属性值
|
|
|
|
|
if (durCapterContent != null) {
|
|
|
|
|
//将章节内容绑定到SQL语句的第三个位置
|
|
|
|
|
stmt.bindString(3, durCapterContent);
|
|
|
|
|
stmt.bindString(3, durCapterContent);// 将durCapterContent绑定到SQL语句的第三个参数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//绑定tag字段到SQL语句的第四个位置
|
|
|
|
|
String tag = entity.getTag();
|
|
|
|
|
String tag = entity.getTag();// 获取tag属性值
|
|
|
|
|
if (tag != null) {
|
|
|
|
|
//将标签绑定到SQL语句的第四个位置
|
|
|
|
|
stmt.bindString(4, tag);
|
|
|
|
|
stmt.bindString(4, tag);// 将tag绑定到SQL语句的第四个参数
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String readKey(Cursor cursor, int offset) {
|
|
|
|
|
//从Cursor中读取主键(durChapterUrl),若为null则返回null
|
|
|
|
|
return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
|
|
|
|
|
return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);// 根据游标读取主键值
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ -140,38 +140,38 @@ public class BookContentBeanDao extends AbstractDao<BookContentBean, String> {
|
|
|
|
|
cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // durCapterContent
|
|
|
|
|
cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3) // tag
|
|
|
|
|
);
|
|
|
|
|
return entity;
|
|
|
|
|
return entity;// 返回创建的实体对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void readEntity(Cursor cursor, BookContentBean entity, int offset) {
|
|
|
|
|
//从Cursor中读取值并设置到BookContentBean对象的相应属性中
|
|
|
|
|
entity.setDurChapterUrl(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
|
|
|
|
|
entity.setDurChapterIndex(cursor.getInt(offset + 1));
|
|
|
|
|
entity.setDurCapterContent(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
|
|
|
|
|
entity.setTag(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
|
|
|
|
|
entity.setDurChapterUrl(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));// 设置durChapterUrl属性
|
|
|
|
|
entity.setDurChapterIndex(cursor.getInt(offset + 1));// 设置durChapterIndex属性
|
|
|
|
|
entity.setDurCapterContent(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));// 设置durCapterContent属性
|
|
|
|
|
entity.setTag(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));// 设置tag属性
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected final String updateKeyAfterInsert(BookContentBean entity, long rowId) {
|
|
|
|
|
//返回更新时需要的主键字段,这里是durChapterUrl
|
|
|
|
|
return entity.getDurChapterUrl();
|
|
|
|
|
return entity.getDurChapterUrl();// 返回用于更新的主键
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getKey(BookContentBean entity) {
|
|
|
|
|
//获取实体对象的主键值,返回durChapterUrl
|
|
|
|
|
if(entity != null) {
|
|
|
|
|
return entity.getDurChapterUrl();
|
|
|
|
|
return entity.getDurChapterUrl();// 返回实体的主键值
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
return null;// 如果实体为空,返回null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected final boolean isEntityUpdateable() {
|
|
|
|
|
//指示实体是否可以被更新,这里返回true,表示支持更新
|
|
|
|
|
return true;
|
|
|
|
|
return true;// 表示该实体可被更新
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|