├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── wenld
│ │ │ │ └── databaseupdate
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── MyApp.java
│ │ │ │ ├── db
│ │ │ │ ├── DBOpenHelper.java
│ │ │ │ ├── DaoSession.java
│ │ │ │ ├── DaoMaster.java
│ │ │ │ ├── FileInfoDao.java
│ │ │ │ └── ThreadInfoDao.java
│ │ │ │ └── bean
│ │ │ │ ├── FileInfo.java
│ │ │ │ └── ThreadInfo.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── wenld
│ │ │ └── databaseupdate
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── wenld
│ │ └── databaseupdate
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── greendaoupgradehelper
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── strings.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── wenld
│ │ │ └── greendaoupgradehelper
│ │ │ └── DBMigrationHelper.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── wenld
│ │ └── greendaoupgradehelper
│ │ └── ExampleUnitTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── README.md
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/greendaoupgradehelper/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':greendaoupgradehelper'
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DataBaseUpdate
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LidongWen/greenDaoUpgradeHepler/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/greendaoupgradehelper/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | GreenDaoUpgradeHelper
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LidongWen/greenDaoUpgradeHepler/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LidongWen/greenDaoUpgradeHepler/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LidongWen/greenDaoUpgradeHepler/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LidongWen/greenDaoUpgradeHepler/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LidongWen/greenDaoUpgradeHepler/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/greendaoupgradehelper/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/wenld/databaseupdate/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/greendaoupgradehelper/src/test/java/com/wenld/greendaoupgradehelper/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.wenld.greendaoupgradehelper;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Program Files (x86)\AndroidSDK/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/wenld/databaseupdate/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.widget.TextView;
6 |
7 | public class MainActivity extends AppCompatActivity {
8 |
9 | TextView tv;
10 |
11 | @Override
12 | protected void onCreate(Bundle savedInstanceState) {
13 | super.onCreate(savedInstanceState);
14 |
15 | setContentView(R.layout.activity_main);
16 | tv = (TextView) findViewById(R.id.tv);
17 | // List str = new FileInfoDB().loadAll();
18 | // if (str != null && str.size() > 0) {
19 | // tv.setText(str.get(0).getUrl());
20 | // }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/greendaoupgradehelper/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Program Files (x86)\AndroidSDK/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/wenld/databaseupdate/MyApp.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate;
2 |
3 | import android.app.Application;
4 |
5 | import com.wenld.databaseupdate.db.DBOpenHelper;
6 | import com.wenld.databaseupdate.db.DaoMaster;
7 |
8 | /**
9 | * Created by wenld on 2017/1/10.
10 | */
11 | public class MyApp extends Application {
12 | DBOpenHelper helper;
13 | private DaoMaster daoMaster;
14 | @Override
15 | public void onCreate() {
16 | super.onCreate();
17 | // AbstractDatabaseManager.initOpenHelper(this);
18 |
19 | DBOpenHelper helper = new DBOpenHelper(this, "test.db",
20 | null,DaoMaster.SCHEMA_VERSION);
21 | daoMaster = new DaoMaster(helper.getWritableDatabase());
22 |
23 | // new FileInfoDB().insert(new FileInfo("1", 0, "1", "1", "1", 0, "1", true, "1", false));
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/wenld/databaseupdate/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.wenld.databaseupdate", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/greendaoupgradehelper/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | group = 'com.github.LiDongWen'
5 | android {
6 | compileSdkVersion 23
7 | buildToolsVersion "23.0.3"
8 |
9 | defaultConfig {
10 | minSdkVersion 15
11 | targetSdkVersion 23
12 | versionCode 1
13 | versionName "1.0"
14 |
15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16 |
17 | }
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 | }
25 |
26 | dependencies {
27 | compile fileTree(dir: 'libs', include: ['*.jar'])
28 | testCompile 'junit:junit:4.12'
29 | compile 'org.greenrobot:greendao:3.2.0'
30 | compile 'com.android.support:support-annotations:23.4.0'
31 | }
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DataBaseUpdate
2 | 实现了数据库更新功能,一行代码轻松升级greenDao数据库。
3 |
4 | ## 引用
5 | ```groovy
6 | // 项目引用
7 | buildscript {
8 | repositories {
9 | mavenCentral()
10 | }
11 | dependencies {
12 | classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
13 | }
14 | }
15 | apply plugin: 'org.greenrobot.greendao'
16 |
17 | compile 'com.github.LidongWen:DataBaseUpdate:1.0.2'
18 | ```
19 | 根目录
20 | ```groovy
21 | allprojects {
22 | repositories {
23 | jcenter()
24 | maven { url "https://www.jitpack.io" }
25 | }
26 | }
27 | ```
28 | ## 使用
29 | 在数据库更新这边调用
30 |
31 | ```java
32 | public class DBOpenHelper extends SQLiteOpenHelper {
33 | .........
34 | @Override
35 | public void onCreate(SQLiteDatabase db) {
36 | ....
37 | }
38 |
39 | @Override
40 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
41 | try {
42 | DBMigrationHelper migratorHelper = new DBMigrationHelper();
43 | //判断版本, 设置需要修改得表 我这边设置一个 FileInfo
44 | if(true) {
45 | migratorHelper.onUpgrade(db, FileInfoDao.class);
46 | }
47 | } catch (ClassCastException e) {
48 | }
49 | }
50 | }
51 | ```
52 | ## thank
53 | [wragony](https://github.com/wragony)
54 | ## Contact me
55 | blog: [wenld's blog](http://www.jianshu.com/u/99f514ea81b3)
56 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | mavenCentral()
4 | }
5 | dependencies {
6 | classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
7 | }
8 | }
9 | apply plugin: 'org.greenrobot.greendao'
10 |
11 | apply plugin: 'com.android.application'
12 |
13 | android {
14 | compileSdkVersion 23
15 | buildToolsVersion "23.0.3"
16 | defaultConfig {
17 | applicationId "com.wenld.databaseupdate"
18 | minSdkVersion 15
19 | targetSdkVersion 23
20 | versionCode 1
21 | versionName "1.0"
22 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
23 | }
24 | buildTypes {
25 | release {
26 | minifyEnabled false
27 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
28 | }
29 | }
30 |
31 | }
32 |
33 | dependencies {
34 | compile fileTree(dir: 'libs', include: ['*.jar'])
35 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
36 | exclude group: 'com.android.support', module: 'support-annotations'
37 | })
38 | compile 'com.android.support:appcompat-v7:23.4.0'
39 | // compile 'com.github.LidongWen:DataBaseUpdate:1.0.0'
40 | compile project(':greendaoupgradehelper')
41 | }
42 |
43 | greendao {
44 | targetGenDir 'src/main/java'
45 | daoPackage 'com.wenld.databaseupdate.db'
46 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/wenld/databaseupdate/db/DBOpenHelper.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate.db;
2 |
3 | import android.content.Context;
4 | import android.database.DatabaseErrorHandler;
5 | import android.database.sqlite.SQLiteDatabase;
6 | import android.database.sqlite.SQLiteOpenHelper;
7 |
8 | import com.wenld.greendaoupgradehelper.DBMigrationHelper;
9 |
10 | import org.greenrobot.greendao.database.StandardDatabase;
11 |
12 |
13 | /**
14 | * Created by wenld- on 2015/12/24.
15 | */
16 | public class DBOpenHelper extends SQLiteOpenHelper {
17 | public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
18 | super(context, name, factory, version);
19 | }
20 |
21 | public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
22 | super(context, name, factory, version, errorHandler);
23 | }
24 |
25 | @Override
26 | public void onCreate(SQLiteDatabase db) {
27 | DaoMaster.createAllTables(new StandardDatabase(db), false);
28 | }
29 |
30 | @Override
31 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
32 | try {
33 | DBMigrationHelper migratorHelper = new DBMigrationHelper();
34 | //判断版本, 设置需要修改得表 我这边设置一个 FileInfo
35 | if (true) {
36 | migratorHelper.onUpgrade(db, FileInfoDao.class);
37 | }
38 | } catch (ClassCastException e) {
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/app/src/main/java/com/wenld/databaseupdate/bean/FileInfo.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate.bean;
2 |
3 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit.
4 |
5 | import org.greenrobot.greendao.annotation.Entity;
6 | import org.greenrobot.greendao.annotation.Id;
7 | import org.greenrobot.greendao.annotation.Keep;
8 | import org.greenrobot.greendao.annotation.Generated;
9 |
10 | /**
11 | * Entity mapped to table "FILE_INFO".
12 | */
13 | @Entity
14 | public class FileInfo {
15 | @Id
16 | private Long id;
17 | private Integer length;
18 | /**
19 | * Not-null value.
20 | */
21 | private String url;
22 | /**
23 | * Not-null value.
24 | */
25 | private String fileName;
26 |
27 | @Keep
28 | public FileInfo(Long id, Integer length, String url, String fileName) {
29 | this.id = id;
30 | this.length = length;
31 | this.url = url;
32 | this.fileName = fileName;
33 | }
34 |
35 | public FileInfo() {
36 | }
37 |
38 | public Long getId() {
39 | return id;
40 | }
41 |
42 | public void setId(Long id) {
43 | this.id = id;
44 | }
45 |
46 | public Integer getLength() {
47 | return length;
48 | }
49 |
50 | public void setLength(Integer length) {
51 | this.length = length;
52 | }
53 |
54 | /**
55 | * Not-null value.
56 | */
57 | public String getUrl() {
58 | return url;
59 | }
60 |
61 | /**
62 | * Not-null value; ensure this value is available before it is saved to the database.
63 | */
64 | public void setUrl(String url) {
65 | this.url = url;
66 | }
67 |
68 | /**
69 | * Not-null value.
70 | */
71 | public String getFileName() {
72 | return fileName;
73 | }
74 |
75 | /**
76 | * Not-null value; ensure this value is available before it is saved to the database.
77 | */
78 | public void setFileName(String fileName) {
79 | this.fileName = fileName;
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/app/src/main/java/com/wenld/databaseupdate/db/DaoSession.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate.db;
2 |
3 | import java.util.Map;
4 |
5 | import org.greenrobot.greendao.AbstractDao;
6 | import org.greenrobot.greendao.AbstractDaoSession;
7 | import org.greenrobot.greendao.database.Database;
8 | import org.greenrobot.greendao.identityscope.IdentityScopeType;
9 | import org.greenrobot.greendao.internal.DaoConfig;
10 |
11 | import com.wenld.databaseupdate.bean.FileInfo;
12 | import com.wenld.databaseupdate.bean.ThreadInfo;
13 |
14 | import com.wenld.databaseupdate.db.FileInfoDao;
15 | import com.wenld.databaseupdate.db.ThreadInfoDao;
16 |
17 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
18 |
19 | /**
20 | * {@inheritDoc}
21 | *
22 | * @see org.greenrobot.greendao.AbstractDaoSession
23 | */
24 | public class DaoSession extends AbstractDaoSession {
25 |
26 | private final DaoConfig fileInfoDaoConfig;
27 | private final DaoConfig threadInfoDaoConfig;
28 |
29 | private final FileInfoDao fileInfoDao;
30 | private final ThreadInfoDao threadInfoDao;
31 |
32 | public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig>
33 | daoConfigMap) {
34 | super(db);
35 |
36 | fileInfoDaoConfig = daoConfigMap.get(FileInfoDao.class).clone();
37 | fileInfoDaoConfig.initIdentityScope(type);
38 |
39 | threadInfoDaoConfig = daoConfigMap.get(ThreadInfoDao.class).clone();
40 | threadInfoDaoConfig.initIdentityScope(type);
41 |
42 | fileInfoDao = new FileInfoDao(fileInfoDaoConfig, this);
43 | threadInfoDao = new ThreadInfoDao(threadInfoDaoConfig, this);
44 |
45 | registerDao(FileInfo.class, fileInfoDao);
46 | registerDao(ThreadInfo.class, threadInfoDao);
47 | }
48 |
49 | public void clear() {
50 | fileInfoDaoConfig.clearIdentityScope();
51 | threadInfoDaoConfig.clearIdentityScope();
52 | }
53 |
54 | public FileInfoDao getFileInfoDao() {
55 | return fileInfoDao;
56 | }
57 |
58 | public ThreadInfoDao getThreadInfoDao() {
59 | return threadInfoDao;
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/wenld/databaseupdate/db/DaoMaster.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate.db;
2 |
3 | import android.content.Context;
4 | import android.database.sqlite.SQLiteDatabase;
5 | import android.database.sqlite.SQLiteDatabase.CursorFactory;
6 | import android.util.Log;
7 |
8 | import org.greenrobot.greendao.AbstractDaoMaster;
9 | import org.greenrobot.greendao.database.StandardDatabase;
10 | import org.greenrobot.greendao.database.Database;
11 | import org.greenrobot.greendao.database.DatabaseOpenHelper;
12 | import org.greenrobot.greendao.identityscope.IdentityScopeType;
13 |
14 |
15 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
16 | /**
17 | * Master of DAO (schema version 1): knows all DAOs.
18 | */
19 | public class DaoMaster extends AbstractDaoMaster {
20 | public static final int SCHEMA_VERSION = 1;
21 |
22 | /** Creates underlying database table using DAOs. */
23 | public static void createAllTables(Database db, boolean ifNotExists) {
24 | FileInfoDao.createTable(db, ifNotExists);
25 | ThreadInfoDao.createTable(db, ifNotExists);
26 | }
27 |
28 | /** Drops underlying database table using DAOs. */
29 | public static void dropAllTables(Database db, boolean ifExists) {
30 | FileInfoDao.dropTable(db, ifExists);
31 | ThreadInfoDao.dropTable(db, ifExists);
32 | }
33 |
34 | /**
35 | * WARNING: Drops all table on Upgrade! Use only during development.
36 | * Convenience method using a {@link DevOpenHelper}.
37 | */
38 | public static DaoSession newDevSession(Context context, String name) {
39 | Database db = new DevOpenHelper(context, name).getWritableDb();
40 | DaoMaster daoMaster = new DaoMaster(db);
41 | return daoMaster.newSession();
42 | }
43 |
44 | public DaoMaster(SQLiteDatabase db) {
45 | this(new StandardDatabase(db));
46 | }
47 |
48 | public DaoMaster(Database db) {
49 | super(db, SCHEMA_VERSION);
50 | registerDaoClass(FileInfoDao.class);
51 | registerDaoClass(ThreadInfoDao.class);
52 | }
53 |
54 | public DaoSession newSession() {
55 | return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
56 | }
57 |
58 | public DaoSession newSession(IdentityScopeType type) {
59 | return new DaoSession(db, type, daoConfigMap);
60 | }
61 |
62 | /**
63 | * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} -
64 | */
65 | public static abstract class OpenHelper extends DatabaseOpenHelper {
66 | public OpenHelper(Context context, String name) {
67 | super(context, name, SCHEMA_VERSION);
68 | }
69 |
70 | public OpenHelper(Context context, String name, CursorFactory factory) {
71 | super(context, name, factory, SCHEMA_VERSION);
72 | }
73 |
74 | @Override
75 | public void onCreate(Database db) {
76 | Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
77 | createAllTables(db, false);
78 | }
79 | }
80 |
81 | /** WARNING: Drops all table on Upgrade! Use only during development. */
82 | public static class DevOpenHelper extends OpenHelper {
83 | public DevOpenHelper(Context context, String name) {
84 | super(context, name);
85 | }
86 |
87 | public DevOpenHelper(Context context, String name, CursorFactory factory) {
88 | super(context, name, factory);
89 | }
90 |
91 | @Override
92 | public void onUpgrade(Database db, int oldVersion, int newVersion) {
93 | Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
94 | dropAllTables(db, true);
95 | onCreate(db);
96 | }
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/app/src/main/java/com/wenld/databaseupdate/bean/ThreadInfo.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate.bean;
2 |
3 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit.
4 |
5 | import org.greenrobot.greendao.annotation.Entity;
6 | import org.greenrobot.greendao.annotation.Id;
7 | import org.greenrobot.greendao.annotation.Keep;
8 | import org.greenrobot.greendao.annotation.Generated;
9 |
10 | /**
11 | * Entity mapped to table "THREAD_INFO".
12 | */
13 | @Entity
14 | public class ThreadInfo {
15 | @Id
16 | private String id;
17 | /**
18 | * Not-null value.
19 | */
20 | private String fileId;
21 | /**
22 | * Not-null value.
23 | */
24 | private String url;
25 | /**
26 | * Not-null value.
27 | */
28 | private String md5;
29 | private int finished;
30 | private String rate;
31 | private Boolean over;
32 | private String overtime;
33 | private Integer start;
34 | private Integer end;
35 |
36 | public ThreadInfo(String id) {
37 | this.id = id;
38 | }
39 |
40 | @Keep
41 | public ThreadInfo(String id, String fileId, String url, String md5, int finished, String rate, Boolean over, String overtime, Integer start, Integer end) {
42 | this.id = id;
43 | this.fileId = fileId;
44 | this.url = url;
45 | this.md5 = md5;
46 | this.finished = finished;
47 | this.rate = rate;
48 | this.over = over;
49 | this.overtime = overtime;
50 | this.start = start;
51 | this.end = end;
52 | }
53 |
54 | @Generated(hash = 930225280)
55 | public ThreadInfo() {
56 | }
57 |
58 | public String getId() {
59 | return id;
60 | }
61 |
62 | public void setId(String id) {
63 | this.id = id;
64 | }
65 |
66 | /**
67 | * Not-null value.
68 | */
69 | public String getFileId() {
70 | return fileId;
71 | }
72 |
73 | /**
74 | * Not-null value; ensure this value is available before it is saved to the database.
75 | */
76 | public void setFileId(String fileId) {
77 | this.fileId = fileId;
78 | }
79 |
80 | /**
81 | * Not-null value.
82 | */
83 | public String getUrl() {
84 | return url;
85 | }
86 |
87 | /**
88 | * Not-null value; ensure this value is available before it is saved to the database.
89 | */
90 | public void setUrl(String url) {
91 | this.url = url;
92 | }
93 |
94 | /**
95 | * Not-null value.
96 | */
97 | public String getMd5() {
98 | return md5;
99 | }
100 |
101 | /**
102 | * Not-null value; ensure this value is available before it is saved to the database.
103 | */
104 | public void setMd5(String md5) {
105 | this.md5 = md5;
106 | }
107 |
108 | public int getFinished() {
109 | return finished;
110 | }
111 |
112 | public void setFinished(int finished) {
113 | this.finished = finished;
114 | }
115 |
116 | public String getRate() {
117 | return rate;
118 | }
119 |
120 | public void setRate(String rate) {
121 | this.rate = rate;
122 | }
123 |
124 | public Boolean getOver() {
125 | return over;
126 | }
127 |
128 | public void setOver(Boolean over) {
129 | this.over = over;
130 | }
131 |
132 | public String getOvertime() {
133 | return overtime;
134 | }
135 |
136 | public void setOvertime(String overtime) {
137 | this.overtime = overtime;
138 | }
139 |
140 | public Integer getStart() {
141 | return start;
142 | }
143 |
144 | public void setStart(Integer start) {
145 | this.start = start;
146 | }
147 |
148 | public Integer getEnd() {
149 | return end;
150 | }
151 |
152 | public void setEnd(Integer end) {
153 | this.end = end;
154 | }
155 |
156 | }
157 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/wenld/databaseupdate/db/FileInfoDao.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate.db;
2 |
3 | import android.database.Cursor;
4 | import android.database.sqlite.SQLiteStatement;
5 |
6 | import org.greenrobot.greendao.AbstractDao;
7 | import org.greenrobot.greendao.Property;
8 | import org.greenrobot.greendao.internal.DaoConfig;
9 | import org.greenrobot.greendao.database.Database;
10 | import org.greenrobot.greendao.database.DatabaseStatement;
11 |
12 | import com.wenld.databaseupdate.bean.FileInfo;
13 |
14 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
15 | /**
16 | * DAO for table "FILE_INFO".
17 | */
18 | public class FileInfoDao extends AbstractDao {
19 |
20 | public static final String TABLENAME = "FILE_INFO";
21 |
22 | /**
23 | * Properties of entity FileInfo.
24 | * Can be used for QueryBuilder and for referencing column names.
25 | */
26 | public static class Properties {
27 | public final static Property Id = new Property(0, Long.class, "id", true, "_id");
28 | public final static Property Length = new Property(1, Integer.class, "length", false, "LENGTH");
29 | public final static Property Url = new Property(2, String.class, "url", false, "URL");
30 | public final static Property FileName = new Property(3, String.class, "fileName", false, "FILE_NAME");
31 | }
32 |
33 |
34 | public FileInfoDao(DaoConfig config) {
35 | super(config);
36 | }
37 |
38 | public FileInfoDao(DaoConfig config, DaoSession daoSession) {
39 | super(config, daoSession);
40 | }
41 |
42 | /** Creates the underlying database table. */
43 | public static void createTable(Database db, boolean ifNotExists) {
44 | String constraint = ifNotExists? "IF NOT EXISTS ": "";
45 | db.execSQL("CREATE TABLE " + constraint + "\"FILE_INFO\" (" + //
46 | "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
47 | "\"LENGTH\" INTEGER," + // 1: length
48 | "\"URL\" TEXT," + // 2: url
49 | "\"FILE_NAME\" TEXT);"); // 3: fileName
50 | }
51 |
52 | /** Drops the underlying database table. */
53 | public static void dropTable(Database db, boolean ifExists) {
54 | String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"FILE_INFO\"";
55 | db.execSQL(sql);
56 | }
57 |
58 | @Override
59 | protected final void bindValues(DatabaseStatement stmt, FileInfo entity) {
60 | stmt.clearBindings();
61 |
62 | Long id = entity.getId();
63 | if (id != null) {
64 | stmt.bindLong(1, id);
65 | }
66 |
67 | Integer length = entity.getLength();
68 | if (length != null) {
69 | stmt.bindLong(2, length);
70 | }
71 |
72 | String url = entity.getUrl();
73 | if (url != null) {
74 | stmt.bindString(3, url);
75 | }
76 |
77 | String fileName = entity.getFileName();
78 | if (fileName != null) {
79 | stmt.bindString(4, fileName);
80 | }
81 | }
82 |
83 | @Override
84 | protected final void bindValues(SQLiteStatement stmt, FileInfo entity) {
85 | stmt.clearBindings();
86 |
87 | Long id = entity.getId();
88 | if (id != null) {
89 | stmt.bindLong(1, id);
90 | }
91 |
92 | Integer length = entity.getLength();
93 | if (length != null) {
94 | stmt.bindLong(2, length);
95 | }
96 |
97 | String url = entity.getUrl();
98 | if (url != null) {
99 | stmt.bindString(3, url);
100 | }
101 |
102 | String fileName = entity.getFileName();
103 | if (fileName != null) {
104 | stmt.bindString(4, fileName);
105 | }
106 | }
107 |
108 | @Override
109 | public Long readKey(Cursor cursor, int offset) {
110 | return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
111 | }
112 |
113 | @Override
114 | public FileInfo readEntity(Cursor cursor, int offset) {
115 | FileInfo entity = new FileInfo( //
116 | cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
117 | cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1), // length
118 | cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // url
119 | cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3) // fileName
120 | );
121 | return entity;
122 | }
123 |
124 | @Override
125 | public void readEntity(Cursor cursor, FileInfo entity, int offset) {
126 | entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
127 | entity.setLength(cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1));
128 | entity.setUrl(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
129 | entity.setFileName(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
130 | }
131 |
132 | @Override
133 | protected final Long updateKeyAfterInsert(FileInfo entity, long rowId) {
134 | entity.setId(rowId);
135 | return rowId;
136 | }
137 |
138 | @Override
139 | public Long getKey(FileInfo entity) {
140 | if(entity != null) {
141 | return entity.getId();
142 | } else {
143 | return null;
144 | }
145 | }
146 |
147 | @Override
148 | public boolean hasKey(FileInfo entity) {
149 | return entity.getId() != null;
150 | }
151 |
152 | @Override
153 | protected final boolean isEntityUpdateable() {
154 | return true;
155 | }
156 |
157 | }
158 |
--------------------------------------------------------------------------------
/greendaoupgradehelper/src/main/java/com/wenld/greendaoupgradehelper/DBMigrationHelper.java:
--------------------------------------------------------------------------------
1 | package com.wenld.greendaoupgradehelper;
2 |
3 |
4 | import android.database.Cursor;
5 | import android.database.sqlite.SQLiteDatabase;
6 | import android.support.annotation.NonNull;
7 | import android.text.TextUtils;
8 | import android.util.Log;
9 |
10 | import org.greenrobot.greendao.AbstractDao;
11 | import org.greenrobot.greendao.database.Database;
12 | import org.greenrobot.greendao.database.StandardDatabase;
13 | import org.greenrobot.greendao.internal.DaoConfig;
14 |
15 | import java.lang.reflect.InvocationTargetException;
16 | import java.lang.reflect.Method;
17 | import java.util.ArrayList;
18 | import java.util.Arrays;
19 | import java.util.List;
20 |
21 |
22 | /**
23 | * Created by wenld- on 2015/12/24.
24 | */
25 | public class DBMigrationHelper {
26 | private static final String CONVERSION_CLASS_NOT_FOUND_EXCEPTION = "MIGRATION HELPER - CLASS DOESN'T MATCH WITH THE CURRENT PARAMETERS";
27 |
28 | public void onUpgrade(SQLiteDatabase db, Class extends AbstractDao, ?>>... daoClasses) {
29 | Database database = new StandardDatabase(db);
30 | generateTempTables(database,daoClasses); //备份数据(表结构改变的表)
31 | dropAllTables(database, true, daoClasses); //删除旧表(结构修改/新增)
32 | createAllTables(database, false, daoClasses); //创建 新表(结构修改/新增)
33 | restoreData(database, daoClasses); //恢复数据(结构修改)
34 | }
35 |
36 | private void dropAllTables(Database db, boolean b, Class extends AbstractDao, ?>>... daoClasses) {
37 | if (daoClasses != null) {
38 | reflectMethod(db, "dropTable", b, daoClasses);
39 | }
40 | }
41 |
42 | private void createAllTables(Database db, boolean b, Class extends AbstractDao, ?>>... daoClasses) {
43 | if (daoClasses != null) {
44 | reflectMethod(db, "createTable", b, daoClasses);
45 | }
46 | }
47 |
48 | /**
49 | * 反射出方法执行
50 | */
51 | private static void reflectMethod(Database db, String methodName, boolean isExists, @NonNull Class extends AbstractDao, ?>>... daoClasses) {
52 | if (daoClasses.length < 1) {
53 | return;
54 | }
55 | try {
56 | for (Class cls : daoClasses) {
57 | // Method method = cls.getDeclaredMethod(methodName, cls);
58 | Method method = cls.getDeclaredMethod(methodName, Database.class, boolean.class);//update by wragony on 2017-06-27
59 | method.invoke(null, db, isExists);
60 | }
61 | } catch (NoSuchMethodException e) {
62 | e.printStackTrace();
63 | } catch (InvocationTargetException e) {
64 | e.printStackTrace();
65 | } catch (IllegalAccessException e) {
66 | e.printStackTrace();
67 | }
68 | }
69 |
70 | /**
71 | * 备份数据
72 | *
73 | * @param db
74 | * @param daoClasses(表结构改变的表)
75 | */
76 | private void generateTempTables(Database db, Class extends AbstractDao, ?>>... daoClasses) {
77 | for (int i = 0; i < daoClasses.length; i++) {
78 | DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
79 | String divider = "";
80 | String tableName = daoConfig.tablename;
81 | String tempTableName = daoConfig.tablename.concat("_TEMP");
82 | ArrayList properties = new ArrayList<>();
83 | StringBuilder createTableStringBuilder = new StringBuilder();
84 | createTableStringBuilder.append("CREATE TABLE ").append(tempTableName).append(" (");
85 |
86 | for (int j = 0; j < daoConfig.properties.length; j++) {
87 | String columnName = daoConfig.properties[j].columnName;
88 | if (getColumns(db, tableName).contains(columnName)) {
89 | properties.add(columnName);
90 | String type = null;
91 | try {
92 | type = getTypeByClass(daoConfig.properties[j].type);
93 | } catch (Exception exception) {
94 | // Crashlytics.logException(exception);
95 | }
96 | createTableStringBuilder.append(divider).append(columnName).append(" ").append(type);
97 | if (daoConfig.properties[j].primaryKey) {
98 | createTableStringBuilder.append(" PRIMARY KEY");
99 | }
100 | divider = ",";
101 | }
102 | }
103 | createTableStringBuilder.append(");");
104 | db.execSQL(createTableStringBuilder.toString());
105 | StringBuilder insertTableStringBuilder = new StringBuilder();
106 | insertTableStringBuilder.append("INSERT INTO ").append(tempTableName).append(" (");
107 | insertTableStringBuilder.append(TextUtils.join(",", properties));
108 | insertTableStringBuilder.append(") SELECT ");
109 | insertTableStringBuilder.append(TextUtils.join(",", properties));
110 | insertTableStringBuilder.append(" FROM ").append(tableName).append(";");
111 | db.execSQL(insertTableStringBuilder.toString());
112 | }
113 | }
114 |
115 | /**
116 | * 恢复数据
117 | *
118 | * @param db
119 | * @param daoClasses(结构修改的表)
120 | */
121 | private void restoreData(Database db, Class extends AbstractDao, ?>>... daoClasses) {
122 | for (int i = 0; i < daoClasses.length; i++) {
123 | DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
124 |
125 | String tableName = daoConfig.tablename;
126 | String tempTableName = daoConfig.tablename.concat("_TEMP");
127 | ArrayList properties = new ArrayList();
128 |
129 | for (int j = 0; j < daoConfig.properties.length; j++) {
130 | String columnName = daoConfig.properties[j].columnName;
131 |
132 | if (getColumns(db, tempTableName).contains(columnName)) {
133 | properties.add(columnName);
134 | }
135 | }
136 |
137 | StringBuilder insertTableStringBuilder = new StringBuilder();
138 |
139 | insertTableStringBuilder.append("INSERT INTO ").append(tableName).append(" (");
140 | insertTableStringBuilder.append(TextUtils.join(",", properties));
141 | insertTableStringBuilder.append(") SELECT ");
142 | insertTableStringBuilder.append(TextUtils.join(",", properties));
143 | insertTableStringBuilder.append(" FROM ").append(tempTableName).append(";");
144 |
145 | StringBuilder dropTableStringBuilder = new StringBuilder();
146 |
147 | //dropTableStringBuilder.append("DROP TABLE ").append(tempTableName);
148 | dropTableStringBuilder.append("DROP TABLE IF EXISTS ").append(tempTableName).append(";");// update by wragony on 2017-06-27
149 |
150 | db.execSQL(insertTableStringBuilder.toString());
151 | db.execSQL(dropTableStringBuilder.toString());
152 | }
153 | }
154 |
155 | private String getTypeByClass(Class> type) throws Exception {
156 | if (type.equals(String.class)) {
157 | return "TEXT";
158 | }
159 | if (type.equals(Long.class) || type.equals(Integer.class) || type.equals(long.class)) {
160 | return "INTEGER";
161 | }
162 | if (type.equals(Boolean.class)) {
163 | return "BOOLEAN";
164 | }
165 |
166 | Exception exception = new Exception(CONVERSION_CLASS_NOT_FOUND_EXCEPTION.concat(" - Class: ").concat(type.toString()));
167 | // Crashlytics.logException(exception);
168 | throw exception;
169 | }
170 |
171 | private static List getColumns(Database db, String tableName) {
172 | List columns = new ArrayList<>();
173 | Cursor cursor = null;
174 | try {
175 | cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
176 | if (cursor != null) {
177 | columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
178 | }
179 | } catch (Exception e) {
180 | Log.v(tableName, e.getMessage(), e);
181 | e.printStackTrace();
182 | } finally {
183 | if (cursor != null)
184 | cursor.close();
185 | }
186 | return columns;
187 | }
188 |
189 | }
190 |
--------------------------------------------------------------------------------
/app/src/main/java/com/wenld/databaseupdate/db/ThreadInfoDao.java:
--------------------------------------------------------------------------------
1 | package com.wenld.databaseupdate.db;
2 |
3 | import android.database.Cursor;
4 | import android.database.sqlite.SQLiteStatement;
5 |
6 | import com.wenld.databaseupdate.bean.ThreadInfo;
7 |
8 | import org.greenrobot.greendao.AbstractDao;
9 | import org.greenrobot.greendao.Property;
10 | import org.greenrobot.greendao.database.Database;
11 | import org.greenrobot.greendao.database.DatabaseStatement;
12 | import org.greenrobot.greendao.internal.DaoConfig;
13 |
14 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
15 | /**
16 | * DAO for table "THREAD_INFO".
17 | */
18 | public class ThreadInfoDao extends AbstractDao {
19 |
20 | public static final String TABLENAME = "THREAD_INFO";
21 |
22 | /**
23 | * Properties of entity ThreadInfo.
24 | * Can be used for QueryBuilder and for referencing column names.
25 | */
26 | public static class Properties {
27 | public final static Property Id = new Property(0, String.class, "id", true, "ID");
28 | public final static Property FileId = new Property(1, String.class, "fileId", false, "FILE_ID");
29 | public final static Property Url = new Property(2, String.class, "url", false, "URL");
30 | public final static Property Md5 = new Property(3, String.class, "md5", false, "MD5");
31 | public final static Property Finished = new Property(4, int.class, "finished", false, "FINISHED");
32 | public final static Property Rate = new Property(5, String.class, "rate", false, "RATE");
33 | public final static Property Over = new Property(6, Boolean.class, "over", false, "OVER");
34 | public final static Property Overtime = new Property(7, String.class, "overtime", false, "OVERTIME");
35 | public final static Property Start = new Property(8, Integer.class, "start", false, "START");
36 | public final static Property End = new Property(9, Integer.class, "end", false, "END");
37 | }
38 |
39 |
40 | public ThreadInfoDao(DaoConfig config) {
41 | super(config);
42 | }
43 |
44 | public ThreadInfoDao(DaoConfig config, DaoSession daoSession) {
45 | super(config, daoSession);
46 | }
47 |
48 | /** Creates the underlying database table. */
49 | public static void createTable(Database db, boolean ifNotExists) {
50 | String constraint = ifNotExists? "IF NOT EXISTS ": "";
51 | db.execSQL("CREATE TABLE " + constraint + "\"THREAD_INFO\" (" + //
52 | "\"ID\" TEXT PRIMARY KEY NOT NULL ," + // 0: id
53 | "\"FILE_ID\" TEXT," + // 1: fileId
54 | "\"URL\" TEXT," + // 2: url
55 | "\"MD5\" TEXT," + // 3: md5
56 | "\"FINISHED\" INTEGER NOT NULL ," + // 4: finished
57 | "\"RATE\" TEXT," + // 5: rate
58 | "\"OVER\" INTEGER," + // 6: over
59 | "\"OVERTIME\" TEXT," + // 7: overtime
60 | "\"START\" INTEGER," + // 8: start
61 | "\"END\" INTEGER);"); // 9: end
62 | }
63 |
64 | /** Drops the underlying database table. */
65 | public static void dropTable(Database db, boolean ifExists) {
66 | String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"THREAD_INFO\"";
67 | db.execSQL(sql);
68 | }
69 |
70 | @Override
71 | protected final void bindValues(DatabaseStatement stmt, ThreadInfo entity) {
72 | stmt.clearBindings();
73 |
74 | String id = entity.getId();
75 | if (id != null) {
76 | stmt.bindString(1, id);
77 | }
78 |
79 | String fileId = entity.getFileId();
80 | if (fileId != null) {
81 | stmt.bindString(2, fileId);
82 | }
83 |
84 | String url = entity.getUrl();
85 | if (url != null) {
86 | stmt.bindString(3, url);
87 | }
88 |
89 | String md5 = entity.getMd5();
90 | if (md5 != null) {
91 | stmt.bindString(4, md5);
92 | }
93 | stmt.bindLong(5, entity.getFinished());
94 |
95 | String rate = entity.getRate();
96 | if (rate != null) {
97 | stmt.bindString(6, rate);
98 | }
99 |
100 | Boolean over = entity.getOver();
101 | if (over != null) {
102 | stmt.bindLong(7, over ? 1L: 0L);
103 | }
104 |
105 | String overtime = entity.getOvertime();
106 | if (overtime != null) {
107 | stmt.bindString(8, overtime);
108 | }
109 |
110 | Integer start = entity.getStart();
111 | if (start != null) {
112 | stmt.bindLong(9, start);
113 | }
114 |
115 | Integer end = entity.getEnd();
116 | if (end != null) {
117 | stmt.bindLong(10, end);
118 | }
119 | }
120 |
121 | @Override
122 | protected final void bindValues(SQLiteStatement stmt, ThreadInfo entity) {
123 | stmt.clearBindings();
124 |
125 | String id = entity.getId();
126 | if (id != null) {
127 | stmt.bindString(1, id);
128 | }
129 |
130 | String fileId = entity.getFileId();
131 | if (fileId != null) {
132 | stmt.bindString(2, fileId);
133 | }
134 |
135 | String url = entity.getUrl();
136 | if (url != null) {
137 | stmt.bindString(3, url);
138 | }
139 |
140 | String md5 = entity.getMd5();
141 | if (md5 != null) {
142 | stmt.bindString(4, md5);
143 | }
144 | stmt.bindLong(5, entity.getFinished());
145 |
146 | String rate = entity.getRate();
147 | if (rate != null) {
148 | stmt.bindString(6, rate);
149 | }
150 |
151 | Boolean over = entity.getOver();
152 | if (over != null) {
153 | stmt.bindLong(7, over ? 1L: 0L);
154 | }
155 |
156 | String overtime = entity.getOvertime();
157 | if (overtime != null) {
158 | stmt.bindString(8, overtime);
159 | }
160 |
161 | Integer start = entity.getStart();
162 | if (start != null) {
163 | stmt.bindLong(9, start);
164 | }
165 |
166 | Integer end = entity.getEnd();
167 | if (end != null) {
168 | stmt.bindLong(10, end);
169 | }
170 | }
171 |
172 | @Override
173 | public String readKey(Cursor cursor, int offset) {
174 | return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
175 | }
176 |
177 | @Override
178 | public ThreadInfo readEntity(Cursor cursor, int offset) {
179 | ThreadInfo entity = new ThreadInfo( //
180 | cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // id
181 | cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // fileId
182 | cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // url
183 | cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // md5
184 | cursor.getInt(offset + 4), // finished
185 | cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // rate
186 | cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0, // over
187 | cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // overtime
188 | cursor.isNull(offset + 8) ? null : cursor.getInt(offset + 8), // start
189 | cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9) // end
190 | );
191 | return entity;
192 | }
193 |
194 | @Override
195 | public void readEntity(Cursor cursor, ThreadInfo entity, int offset) {
196 | entity.setId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
197 | entity.setFileId(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
198 | entity.setUrl(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
199 | entity.setMd5(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
200 | entity.setFinished(cursor.getInt(offset + 4));
201 | entity.setRate(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
202 | entity.setOver(cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0);
203 | entity.setOvertime(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
204 | entity.setStart(cursor.isNull(offset + 8) ? null : cursor.getInt(offset + 8));
205 | entity.setEnd(cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9));
206 | }
207 |
208 | @Override
209 | protected final String updateKeyAfterInsert(ThreadInfo entity, long rowId) {
210 | return entity.getId();
211 | }
212 |
213 | @Override
214 | public String getKey(ThreadInfo entity) {
215 | if(entity != null) {
216 | return entity.getId();
217 | } else {
218 | return null;
219 | }
220 | }
221 |
222 | @Override
223 | public boolean hasKey(ThreadInfo entity) {
224 | return entity.getId() != null;
225 | }
226 |
227 | @Override
228 | protected final boolean isEntityUpdateable() {
229 | return true;
230 | }
231 |
232 | }
233 |
--------------------------------------------------------------------------------