├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── raizlabs │ │ └── android │ │ └── databasecomparison │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── ic_launcher-web.png │ ├── java │ └── com │ │ └── raizlabs │ │ └── android │ │ └── databasecomparison │ │ ├── Generator.java │ │ ├── Loader.java │ │ ├── MainActivity.java │ │ ├── MainApplication.java │ │ ├── Saver.java │ │ ├── activeandroid │ │ ├── AATester.java │ │ ├── AddressBook.java │ │ ├── AddressItem.java │ │ ├── Contact.java │ │ └── SimpleAddressItem.java │ │ ├── dbflow │ │ ├── AddressBook.java │ │ ├── AddressItem.java │ │ ├── Contact.java │ │ ├── DBFlowDatabase.java │ │ ├── DBFlowTester.java │ │ └── SimpleAddressItem.java │ │ ├── events │ │ ├── LogTestDataEvent.java │ │ └── TrialCompletedEvent.java │ │ ├── greendao │ │ ├── GreenDaoGenerator.java │ │ ├── GreenDaoTester.java │ │ ├── SimpleAddressItem.java │ │ └── gen │ │ │ ├── AddressBook.java │ │ │ ├── AddressBookDao.java │ │ │ ├── AddressItem.java │ │ │ ├── AddressItemDao.java │ │ │ ├── Contact.java │ │ │ ├── ContactDao.java │ │ │ ├── DaoMaster.java │ │ │ ├── DaoSession.java │ │ │ ├── SimpleAddressItem.java │ │ │ └── SimpleAddressItemDao.java │ │ ├── interfaces │ │ ├── IAddressBook.java │ │ ├── IAddressItem.java │ │ ├── IContact.java │ │ └── ISaveable.java │ │ ├── ollie │ │ ├── AddressBook.java │ │ ├── AddressItem.java │ │ ├── Contact.java │ │ ├── OllieTester.java │ │ └── SimpleAddressItem.java │ │ ├── ormlite │ │ ├── AddressBook.java │ │ ├── AddressItem.java │ │ ├── Contact.java │ │ ├── DatabaseHelper.java │ │ ├── OrmLiteTester.java │ │ └── SimpleAddressItem.java │ │ ├── realm │ │ ├── AddressBook.java │ │ ├── AddressItem.java │ │ ├── Contact.java │ │ ├── RealmTester.java │ │ └── SimpleAddressItem.java │ │ ├── sprinkles │ │ ├── AddressBook.java │ │ ├── AddressItem.java │ │ ├── Contact.java │ │ ├── SimpleAddressItem.java │ │ └── SprinklesTester.java │ │ └── sugar │ │ ├── AddressBook.java │ │ ├── AddressItem.java │ │ ├── Contact.java │ │ ├── SimpleAddressItem.java │ │ └── SugarTester.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── menu │ └── menu_main.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 │ ├── raw │ └── ormlite_config.txt │ ├── values-v21 │ └── styles.xml │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── greendaogenerator ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── raizlabs │ └── Generator.java ├── images ├── AddressBook.png ├── SimpleAddressItem.png ├── complextrial.png ├── complextrialv1.png ├── simpletrial.png └── simpletrialv1.png ├── ormlitegenerator ├── build.gradle ├── res │ └── raw │ │ └── ormlite_config.txt └── src │ └── main │ └── java │ └── com │ └── raizlabs │ ├── DatabaseConfigUtil.java │ └── android │ └── databasecomparison │ ├── interfaces │ ├── IAddressBook.java │ ├── IAddressItem.java │ ├── IContact.java │ └── ISaveable.java │ └── ormlite │ ├── AddressBook.java │ ├── AddressItem.java │ ├── Contact.java │ └── SimpleAddressItem.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Eclipse project files 23 | .classpath 24 | .project 25 | 26 | # Proguard folder generated by Eclipse 27 | proguard/ 28 | 29 | # Intellij project files 30 | *.iml 31 | *.ipr 32 | *.iws 33 | .idea/ 34 | crashlytics-build.properties 35 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "Libraries/DBFlow"] 2 | path = Libraries/DBFlow 3 | url = https://github.com/Raizlabs/DBFlow.git 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Raizlabs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidDatabaseLibraryComparison 2 | A test between a few of the popular libraries running a speed test on how fast they load and save data. 3 | 4 | Note: SugarORM and ActiveAndroid and Sprinkles were removed because they're too slow. If you're curious how well they run, you can uncomment the lines in MainActivity. SugarORM was a lot slower on load/save. ActiveAndroid and Sprinkles were a lot slower than the remaining comparisons on save. 5 | 6 | ## Benchmark Description 7 | 8 | There are two benchmarks. The Simple trial uses a flat schema for an address book so each row is composed of name, address, city, state, and phone columns. The Simple trial does 25000 copies of the AddressItem. 9 | 10 | ![Simple Address Item Schema](images/SimpleAddressItem.png "Simple Address Item Schema") 11 | 12 | The Complex trial is hierarchical and has support for multiple address books where each address book has contacts and addresses. The Complex trial does 50 copies of the AddressBook. 13 | 14 | ![Address Book Schema](images/AddressBook.png "Address Book Schema") 15 | 16 | ## Results 17 | 18 | These are the results for the Simple trial: 19 | 20 | ![Simple Trial](images/simpletrial.png "Simple Trial") 21 | 22 | And these are the results for the Complex trial: 23 | 24 | ![Complex Trial](images/complextrial.png "Complex Trial") 25 | 26 | ## Older Results 27 | 28 | These are the results for the Simple trial with the DBs that are no longer included currently: 29 | 30 | ![Simple Trial](images/simpletrialv1.png "Simple Trial") 31 | 32 | And these are the results for the Complex trial: 33 | 34 | ![Complex Trial](images/complextrialv1.png "Complex Trial") 35 | 36 | ## Blog Post: 37 | The original blog post can be [found here](http://www.raizlabs.com/dev/2015/02/go-dbflow-fastest-android-orm-database-library/) -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.neenbedankt.android-apt' 3 | apply plugin: 'realm-android' 4 | 5 | android { 6 | compileSdkVersion 22 7 | buildToolsVersion "23.0.3" 8 | 9 | defaultConfig { 10 | applicationId "com.raizlabs.android.databasecomparison" 11 | minSdkVersion 14 12 | targetSdkVersion 22 13 | versionCode 1 14 | versionName "1.0" 15 | } 16 | 17 | } 18 | 19 | dependencies { 20 | 21 | apt 'com.raizlabs.android:DBFlow-Compiler:2.1.0' 22 | compile "com.raizlabs.android:DBFlow-Core:2.1.0" 23 | compile "com.raizlabs.android:DBFlow:2.1.0" 24 | 25 | // This switches to a local copy of the DBFlow repo 26 | //apt project(':Libraries:DBFlow:compiler') 27 | //compile project(":Libraries:DBFlow:library")//compile project(":Libraries:DBFlow:flowcore") 28 | 29 | compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT' 30 | compile 'com.michaelpardo:ollie:0.3.2-SNAPSHOT' 31 | apt 'com.michaelpardo:ollie-compiler:0.3.2-SNAPSHOT' 32 | compile 'se.emilsjolander:sprinkles:1.3.1' 33 | compile 'com.github.satyan:sugar:1.3' 34 | compile 'de.greenrobot:greendao:1.3.7' 35 | compile 'com.j256.ormlite:ormlite-android:4.48' 36 | 37 | compile 'com.github.PhilJay:MPAndroidChart:v2.1.0' 38 | compile 'de.greenrobot:eventbus:2.4.0' 39 | } 40 | -------------------------------------------------------------------------------- /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 /Users/andrewgrosner/Documents/sdk/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/androidTest/java/com/raizlabs/android/databasecomparison/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 13 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /app/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rightpoint/AndroidDatabaseLibraryComparison/47a22ca32c9f9214bf2795ecf57be358044f75a2/app/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/Generator.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison; 2 | 3 | import android.annotation.TargetApi; 4 | import android.os.Build; 5 | 6 | import com.raizlabs.android.databasecomparison.interfaces.IAddressBook; 7 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 8 | import com.raizlabs.android.databasecomparison.interfaces.IContact; 9 | 10 | import java.util.ArrayList; 11 | import java.util.Collection; 12 | 13 | /** 14 | * Description: 15 | */ 16 | public class Generator { 17 | 18 | public static Collection getAddresses(Class itemClass, int count) { 19 | return getAddresses(itemClass, count, null); 20 | } 21 | 22 | @TargetApi(Build.VERSION_CODES.KITKAT) 23 | public static > Collection 24 | getAddresses(Class itemClass, int count, AddressBook addressBook) { 25 | Collection addressItemCollection = new ArrayList<>(); 26 | for (int i = 0; i < count; i++) { 27 | AddressItem addressItem = null; 28 | try { 29 | addressItem = itemClass.newInstance(); 30 | addressItem.setName("Test"); 31 | addressItem.setAddress("5486 Memory Lane"); 32 | addressItem.setCity("Bronx"); 33 | addressItem.setState("NY"); 34 | addressItem.setPhone(7185555555l); 35 | if (addressBook != null) { 36 | addressItem.setAddressBook(addressBook); 37 | } 38 | addressItemCollection.add(addressItem); 39 | } catch (InstantiationException | IllegalAccessException e) { 40 | e.printStackTrace(); 41 | } 42 | } 43 | return addressItemCollection; 44 | } 45 | 46 | @TargetApi(Build.VERSION_CODES.KITKAT) 47 | public static Collection 48 | getContacts(Class contactClass, int count, AddressBook addressBook) { 49 | Collection contactModelList = new ArrayList<>(); 50 | for (int i = 0; i < count; i++) { 51 | Contact contactModel = null; 52 | try { 53 | contactModel = contactClass.newInstance(); 54 | contactModel.setName("Test"); 55 | contactModel.setEmail("abgrosner@gmail.com"); 56 | if (addressBook != null) { 57 | contactModel.setAddressBook(addressBook); 58 | } 59 | contactModelList.add(contactModel); 60 | } catch (InstantiationException | IllegalAccessException e) { 61 | e.printStackTrace(); 62 | } 63 | } 64 | return contactModelList; 65 | } 66 | 67 | @TargetApi(Build.VERSION_CODES.KITKAT) 68 | public static > Collection 71 | createAddressBooks(Class addressBookClass, 72 | Class contactClass, 73 | Class addressItemClass, 74 | int count) { 75 | Collection addressBooks = new ArrayList<>(); 76 | for (int i = 0; i < count; i++) { 77 | AddressBook addressBook = null; 78 | try { 79 | addressBook = addressBookClass.newInstance(); 80 | addressBook.setId(i); 81 | addressBook.setName("Test"); 82 | addressBook.setAuthor("Andrew Grosner"); 83 | addressBook.setAddresses(getAddresses(addressItemClass, count, addressBook)); 84 | addressBook.setContacts(getContacts(contactClass, count, addressBook)); 85 | addressBooks.add(addressBook); 86 | } catch (InstantiationException | IllegalAccessException e) { 87 | e.printStackTrace(); 88 | } 89 | 90 | } 91 | return addressBooks; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/Loader.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressBook; 4 | import com.raizlabs.android.databasecomparison.interfaces.IContact; 5 | 6 | import java.util.Collection; 7 | 8 | /** 9 | * Description: 10 | */ 11 | public class Loader { 12 | 13 | /** 14 | * Trigger a load of all inner data to truly test speed of loading 15 | * 16 | * @param addressBooks 17 | */ 18 | public static void loadAllInnerData(Collection addressBooks) { 19 | for (IAddressBook addressBook : addressBooks) { 20 | addressBook.getAddresses(); 21 | 22 | Collection contacts = addressBook.getContacts(); 23 | 24 | for (IContact contact : contacts) { 25 | contact.getAddressBookField(); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.graphics.Color; 6 | import android.os.Bundle; 7 | import android.util.Log; 8 | import android.view.View; 9 | import android.widget.Button; 10 | import android.widget.ProgressBar; 11 | import android.widget.ScrollView; 12 | import android.widget.TextView; 13 | 14 | import com.github.mikephil.charting.charts.BarChart; 15 | import com.github.mikephil.charting.data.BarData; 16 | import com.github.mikephil.charting.data.BarDataSet; 17 | import com.github.mikephil.charting.data.BarEntry; 18 | import com.raizlabs.android.databasecomparison.activeandroid.AATester; 19 | import com.raizlabs.android.databasecomparison.dbflow.DBFlowTester; 20 | import com.raizlabs.android.databasecomparison.events.LogTestDataEvent; 21 | import com.raizlabs.android.databasecomparison.events.TrialCompletedEvent; 22 | import com.raizlabs.android.databasecomparison.greendao.GreenDaoTester; 23 | import com.raizlabs.android.databasecomparison.ollie.OllieTester; 24 | import com.raizlabs.android.databasecomparison.ormlite.OrmLiteTester; 25 | import com.raizlabs.android.databasecomparison.realm.RealmTester; 26 | import com.raizlabs.android.databasecomparison.sprinkles.SprinklesTester; 27 | import com.raizlabs.android.databasecomparison.sugar.SugarTester; 28 | 29 | import java.util.ArrayList; 30 | import java.util.LinkedHashMap; 31 | 32 | import de.greenrobot.event.EventBus; 33 | 34 | 35 | public class MainActivity extends Activity { 36 | public static final String LOAD_TIME = "Load"; 37 | public static final String SAVE_TIME = "Save"; 38 | 39 | public static final int LOOP_COUNT = 25000; 40 | 41 | public static final int ADDRESS_BOOK_COUNT = 50; 42 | 43 | private static final String STATE_MAPDATA = "mapData"; 44 | private static final String STATE_RUNNING_TESTS = "runningTests"; 45 | private static final String STATE_TEST_NAME = "testName"; 46 | 47 | private Button simpleTrialButton; 48 | private Button complexTrialButton; 49 | private TextView resultsLabel; 50 | private ScrollView resultsContainer; 51 | private TextView resultsTextView; 52 | private static StringBuilder resultsStringBuilder = new StringBuilder(); 53 | private static ProgressBar progressBar; 54 | 55 | private BarChart chartView; 56 | private LinkedHashMap> chartEntrySets = new LinkedHashMap<>(); 57 | private boolean runningTests = false; 58 | private String runningTestName; 59 | private Thread runTestThread; 60 | 61 | @Override 62 | protected void onCreate(Bundle savedInstanceState) { 63 | super.onCreate(savedInstanceState); 64 | setContentView(R.layout.activity_main); 65 | 66 | simpleTrialButton = (Button) findViewById(R.id.simple); 67 | complexTrialButton = (Button) findViewById(R.id.complex); 68 | resultsLabel = (TextView) findViewById(R.id.resultsLabel); 69 | resultsContainer = (ScrollView) findViewById(R.id.resultsContainer); 70 | resultsTextView = (TextView) findViewById(R.id.results); 71 | progressBar = (ProgressBar) findViewById(R.id.progress); 72 | progressBar.setIndeterminate(true); 73 | chartView = (BarChart) findViewById(R.id.chart); 74 | 75 | if (savedInstanceState != null) { 76 | runningTests = savedInstanceState.getBoolean(STATE_RUNNING_TESTS); 77 | runningTestName = savedInstanceState.getString(STATE_TEST_NAME); 78 | chartEntrySets = (LinkedHashMap>) savedInstanceState.getSerializable(STATE_MAPDATA); 79 | 80 | setBusyUI(runningTests, runningTestName); 81 | if (!runningTests && (chartEntrySets.size() > 0)) { 82 | // graph existing data 83 | initChart(); 84 | } 85 | } 86 | } 87 | 88 | @Override 89 | public void onSaveInstanceState(Bundle savedInstanceState) { 90 | savedInstanceState.putBoolean(STATE_RUNNING_TESTS, runningTests); 91 | savedInstanceState.putString(STATE_TEST_NAME, runningTestName); 92 | savedInstanceState.putSerializable(STATE_MAPDATA, chartEntrySets); 93 | 94 | super.onSaveInstanceState(savedInstanceState); 95 | } 96 | 97 | 98 | @Override 99 | public void onStart() { 100 | super.onStart(); 101 | EventBus.getDefault().register(this); 102 | } 103 | 104 | @Override 105 | public void onStop() { 106 | EventBus.getDefault().unregister(this); 107 | super.onStop(); 108 | } 109 | 110 | // handle data collection event 111 | public void onEvent(LogTestDataEvent event) { 112 | logTime(event.getStartTime(), event.getFramework(), event.getEventName()); 113 | } 114 | 115 | // handle graphing event 116 | public void onEventMainThread(TrialCompletedEvent event){ 117 | initChart(); 118 | runningTests = false; 119 | setBusyUI(false, event.getTrialName()); 120 | } 121 | 122 | /** 123 | * Logs msec between start time and now 124 | * @param startTime relative to start time in msec; use -1 to set elapsed time to zero 125 | * @param framework framework logging event 126 | * @param name string to log for event 127 | */ 128 | public void logTime(long startTime, String framework, String name) { 129 | Log.e(MainActivity.class.getSimpleName(), name + " took: " + (System.currentTimeMillis() - startTime)); 130 | long elapsedMsec = (startTime == -1) ? 0 : System.currentTimeMillis() - startTime; 131 | resultsStringBuilder.append(framework).append(' ').append(name) 132 | .append(" took: ") 133 | .append(elapsedMsec) 134 | .append(" msec\n"); 135 | runOnUiThread(new Runnable() { 136 | @Override 137 | public void run() { 138 | resultsTextView.setText(resultsStringBuilder.toString()); 139 | } 140 | }); 141 | // update chart data 142 | addChartData(framework, name, elapsedMsec); 143 | } 144 | 145 | private void setBusyUI(boolean enabled, String testName) { 146 | runningTestName = testName; 147 | if (enabled) { 148 | runningTests = true; 149 | resultsStringBuilder.setLength(0); 150 | resultsContainer.setVisibility(View.VISIBLE); 151 | chartView.setVisibility(View.GONE); 152 | enableButtons(false); 153 | progressBar.setVisibility(View.VISIBLE); 154 | } else { 155 | runningTests = false; 156 | resultsContainer.setVisibility(View.GONE); 157 | if (runningTestName != null) { 158 | chartView.setVisibility(View.VISIBLE); 159 | } 160 | enableButtons(true); 161 | progressBar.setVisibility(View.GONE); 162 | } 163 | if (runningTestName != null) { 164 | resultsLabel.setText(getResources().getString(R.string.results, testName)); 165 | resultsLabel.setVisibility(View.VISIBLE); 166 | } 167 | } 168 | 169 | private void initChart() { 170 | ArrayList dataSets = new ArrayList<>(); 171 | // note that we show save first because that's how we initialize the DB 172 | for (String frameworkName : chartEntrySets.keySet()) { 173 | ArrayList entrySet = chartEntrySets.get(frameworkName); 174 | BarDataSet dataSet = new BarDataSet(entrySet, frameworkName); 175 | dataSet.setColor(getFrameworkColor(frameworkName)); 176 | dataSets.add(dataSet); 177 | } 178 | // load data and animate it 179 | ArrayList xAxisLabels = new ArrayList<>(); 180 | xAxisLabels.add("Save (msec)"); 181 | xAxisLabels.add("Load (msec)"); 182 | BarData data = new BarData(xAxisLabels, dataSets); 183 | chartView.setData(data); 184 | chartView.setDescription(null); // this takes up too much space, so clear it 185 | chartView.animateXY(2000, 2000); 186 | chartView.invalidate(); 187 | } 188 | private void resetChart() { 189 | chartEntrySets.clear(); 190 | // the order you add these in is the order they're displayed in 191 | chartEntrySets.put(DBFlowTester.FRAMEWORK_NAME, new ArrayList()); 192 | chartEntrySets.put(GreenDaoTester.FRAMEWORK_NAME, new ArrayList()); 193 | chartEntrySets.put(OrmLiteTester.FRAMEWORK_NAME, new ArrayList()); 194 | chartEntrySets.put(OllieTester.FRAMEWORK_NAME, new ArrayList()); 195 | chartEntrySets.put(RealmTester.FRAMEWORK_NAME, new ArrayList()); 196 | //chartEntrySets.put(SugarTester.FRAMEWORK_NAME, new ArrayList()); 197 | //chartEntrySets.put(AATester.FRAMEWORK_NAME, new ArrayList()); 198 | //chartEntrySets.put(SprinklesTester.FRAMEWORK_NAME, new ArrayList()); 199 | } 200 | private int getFrameworkColor(String framework) { 201 | // using the 300 line colors from http://www.google.com/design/spec/style/color.html#color-color-palette 202 | switch (framework) { 203 | case DBFlowTester.FRAMEWORK_NAME: 204 | return Color.rgb(0xE5,0x73,0x73); // red 205 | case AATester.FRAMEWORK_NAME: 206 | return Color.rgb(0xF0, 0x62, 0x92); // pink 207 | case OllieTester.FRAMEWORK_NAME: 208 | return Color.rgb(0xFF, 0xA5, 0x00); // orange 209 | case GreenDaoTester.FRAMEWORK_NAME: 210 | return Color.rgb(0xBA, 0x68, 0xC8); // purple 211 | case OrmLiteTester.FRAMEWORK_NAME: 212 | return Color.rgb(0x4D, 0xB6, 0xAC); // teal 213 | case SprinklesTester.FRAMEWORK_NAME: 214 | return Color.rgb(0x79, 0x86, 0xCB); // indigo 215 | case SugarTester.FRAMEWORK_NAME: 216 | return Color.rgb(0x64, 0xB5, 0XF6); // blue 217 | case RealmTester.FRAMEWORK_NAME: 218 | return Color.rgb(0xAE, 0xD5, 0X81); // light green 219 | default: 220 | return Color.WHITE; 221 | } 222 | } 223 | private void addChartData(String framework, String category, long value) { 224 | BarEntry entry = new BarEntry(value, category.equals(SAVE_TIME) ? 0 : 1); 225 | chartEntrySets.get(framework).add(entry); 226 | } 227 | 228 | private void enableButtons(boolean enabled) { 229 | simpleTrialButton.setEnabled(enabled); 230 | complexTrialButton.setEnabled(enabled); 231 | } 232 | 233 | /** 234 | * runs simple benchmarks (onClick from R.id.simple) 235 | * @param v button view 236 | */ 237 | public void runSimpleTrial(View v) { 238 | setBusyUI(true, getResources().getString(R.string.simple)); 239 | resetChart(); 240 | runTestThread = new Thread(new Runnable() { 241 | @Override 242 | public void run() { 243 | runningTests = true; 244 | Context applicationContext = MainActivity.this.getApplicationContext(); 245 | OrmLiteTester.testAddressItems(applicationContext); 246 | GreenDaoTester.testAddressItems(applicationContext); 247 | DBFlowTester.testAddressItems(applicationContext); 248 | OllieTester.testAddressItems(applicationContext); 249 | RealmTester.testAddressItems(applicationContext); 250 | //SprinklesTester.testAddressItems(applicationContext); 251 | //AATester.testAddressItems(applicationContext); 252 | //SugarTester.testAddressItems(applicationContext); 253 | EventBus.getDefault().post(new TrialCompletedEvent(getResources().getString(R.string.simple))); 254 | } 255 | }); 256 | runTestThread.start(); 257 | } 258 | 259 | /** 260 | * runs complex benchmarks (onClick from R.id.complex) 261 | * @param v button view 262 | */ 263 | public void runComplexTrial(View v) { 264 | setBusyUI(true, getResources().getString(R.string.complex)); 265 | resetChart(); 266 | new Thread(new Runnable() { 267 | @Override 268 | public void run() { 269 | runningTests = true; 270 | Context applicationContext = MainActivity.this.getApplicationContext(); 271 | OrmLiteTester.testAddressBooks(applicationContext); 272 | GreenDaoTester.testAddressBooks(applicationContext); 273 | DBFlowTester.testAddressBooks(applicationContext); 274 | OllieTester.testAddressBooks(applicationContext); 275 | RealmTester.testAddressBooks(applicationContext); 276 | //SprinklesTester.testAddressBooks(applicationContext); 277 | //AATester.testAddressBooks(applicationContext); 278 | //SugarTester.testAddressBooks(applicationContext); 279 | EventBus.getDefault().post(new TrialCompletedEvent(getResources().getString(R.string.complex))); 280 | } 281 | }).start(); 282 | } 283 | } 284 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/MainApplication.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison; 2 | 3 | import com.activeandroid.ActiveAndroid; 4 | import com.activeandroid.Configuration; 5 | import com.orm.Database; 6 | import com.orm.SugarApp; 7 | import com.raizlabs.android.databasecomparison.activeandroid.AddressBook; 8 | import com.raizlabs.android.databasecomparison.activeandroid.AddressItem; 9 | import com.raizlabs.android.databasecomparison.activeandroid.Contact; 10 | import com.raizlabs.android.databasecomparison.activeandroid.SimpleAddressItem; 11 | import com.raizlabs.android.dbflow.config.FlowManager; 12 | 13 | import io.realm.Realm; 14 | import io.realm.RealmConfiguration; 15 | import ollie.Ollie; 16 | import se.emilsjolander.sprinkles.Sprinkles; 17 | 18 | /** 19 | * Description: 20 | */ 21 | public class MainApplication extends SugarApp { 22 | 23 | private static Database mDatabase; 24 | 25 | @Override 26 | public void onCreate() { 27 | super.onCreate(); 28 | 29 | ActiveAndroid.initialize(new Configuration.Builder(this) 30 | .setDatabaseName("activeandroid") 31 | .setDatabaseVersion(1) 32 | .setModelClasses(SimpleAddressItem.class, AddressItem.class, 33 | AddressBook.class, Contact.class).create()); 34 | 35 | Ollie.with(this) 36 | .setName("ollie") 37 | .setVersion(1) 38 | .setLogLevel(Ollie.LogLevel.FULL) 39 | .init(); 40 | 41 | FlowManager.init(this); 42 | 43 | Sprinkles.init(this, "sprinkles.db", 2); 44 | 45 | RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build(); 46 | Realm.setDefaultConfiguration(realmConfig); 47 | 48 | mDatabase = getDatabase(); 49 | } 50 | 51 | public static Database getSugarDatabase() { 52 | return mDatabase; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/Saver.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.ISaveable; 4 | 5 | import java.util.Collection; 6 | 7 | /** 8 | * Description: 9 | */ 10 | public class Saver { 11 | 12 | public static void saveAll(Collection saveables) { 13 | for(ISaveable saveable: saveables) { 14 | saveable.saveAll(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/activeandroid/AATester.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.activeandroid; 2 | 3 | import android.content.Context; 4 | 5 | import com.activeandroid.ActiveAndroid; 6 | import com.activeandroid.query.Delete; 7 | import com.activeandroid.query.Select; 8 | import com.raizlabs.android.databasecomparison.Generator; 9 | import com.raizlabs.android.databasecomparison.Loader; 10 | import com.raizlabs.android.databasecomparison.MainActivity; 11 | import com.raizlabs.android.databasecomparison.Saver; 12 | import com.raizlabs.android.databasecomparison.events.LogTestDataEvent; 13 | import com.raizlabs.android.dbflow.runtime.TransactionManager; 14 | 15 | import java.util.Collection; 16 | 17 | import de.greenrobot.event.EventBus; 18 | 19 | /** 20 | * Description: 21 | */ 22 | public class AATester { 23 | public static final String FRAMEWORK_NAME = "ActiveAndroid"; 24 | 25 | public static void testAddressBooks(Context context) { 26 | new Delete().from(AddressItem.class).execute(); 27 | new Delete().from(Contact.class).execute(); 28 | new Delete().from(AddressBook.class).execute(); 29 | 30 | Collection addressBooks = 31 | Generator.createAddressBooks(AddressBook.class, 32 | Contact.class, 33 | AddressItem.class, 34 | MainActivity.ADDRESS_BOOK_COUNT); 35 | long startTime = System.currentTimeMillis(); 36 | final Collection finalAddressBooks = addressBooks; 37 | TransactionManager.transact(ActiveAndroid.getDatabase(), new Runnable() { 38 | @Override 39 | public void run() { 40 | Saver.saveAll(finalAddressBooks); 41 | } 42 | }); 43 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 44 | 45 | startTime = System.currentTimeMillis(); 46 | addressBooks = new Select().from(AddressBook.class).execute(); 47 | Loader.loadAllInnerData(addressBooks); 48 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 49 | 50 | new Delete().from(AddressItem.class).execute(); 51 | new Delete().from(Contact.class).execute(); 52 | new Delete().from(AddressBook.class).execute(); 53 | } 54 | 55 | public static void testAddressItems(Context context) { 56 | new Delete().from(SimpleAddressItem.class).execute(); 57 | 58 | final Collection activeAndroidModels = 59 | Generator.getAddresses(SimpleAddressItem.class, MainActivity.LOOP_COUNT); 60 | 61 | long startTime = System.currentTimeMillis(); 62 | // Reuse method so we don't have to write 63 | TransactionManager.transact(ActiveAndroid.getDatabase(), new Runnable() { 64 | @Override 65 | public void run() { 66 | Saver.saveAll(activeAndroidModels); 67 | } 68 | }); 69 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 70 | 71 | startTime = System.currentTimeMillis(); 72 | Collection activeAndroidModelLoad = 73 | new Select().from(SimpleAddressItem.class).execute(); 74 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 75 | 76 | new Delete().from(SimpleAddressItem.class).execute(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/activeandroid/AddressBook.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.activeandroid; 2 | 3 | import com.activeandroid.Model; 4 | import com.activeandroid.annotation.Column; 5 | import com.activeandroid.annotation.Table; 6 | import com.activeandroid.query.Select; 7 | import com.raizlabs.android.databasecomparison.interfaces.IAddressBook; 8 | 9 | import java.util.Collection; 10 | 11 | /** 12 | * Description: 13 | */ 14 | @Table(name = "AddressBook") 15 | public class AddressBook extends Model implements IAddressBook { 16 | @Column(name = "name") 17 | private String name; 18 | 19 | @Column(name = "author") 20 | private String author; 21 | 22 | Collection addresses; 23 | 24 | Collection contacts; 25 | 26 | @Override 27 | public void setId(long id) { 28 | // no need....AA creates an autoincrementing hidden id field 29 | } 30 | 31 | @Override 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | @Override 37 | public void setAuthor(String author) { 38 | this.author = author; 39 | } 40 | 41 | @Override 42 | public void setAddresses(Collection addresses) { 43 | this.addresses = addresses; 44 | } 45 | 46 | @Override 47 | public Collection getAddresses() { 48 | if (addresses == null) { 49 | addresses = new Select().from(AddressItem.class).where("addressBook = ?", getId()).execute(); 50 | } 51 | return addresses; 52 | } 53 | 54 | @Override 55 | public Collection getContacts() { 56 | if (contacts == null) { 57 | contacts = new Select().from(Contact.class).where("addressBook = ?", getId()).execute(); 58 | } 59 | return contacts; 60 | } 61 | 62 | public void setContacts(Collection contacts) { 63 | this.contacts = contacts; 64 | } 65 | 66 | @Override 67 | public void saveAll() { 68 | super.save(); 69 | for (AddressItem addressItem : addresses) { 70 | addressItem.saveAll(); 71 | } 72 | for (Contact contact : contacts) { 73 | contact.saveAll(); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/activeandroid/AddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.activeandroid; 2 | 3 | import com.activeandroid.annotation.Column; 4 | import com.activeandroid.annotation.Table; 5 | 6 | /** 7 | * Description: 8 | */ 9 | @Table(name = "AddressItem") 10 | public class AddressItem extends SimpleAddressItem { 11 | 12 | @Column(name = "addressBook") 13 | private AddressBook addressBook; 14 | 15 | @Override 16 | public void setAddressBook(AddressBook addressBook) { 17 | this.addressBook = addressBook; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/activeandroid/Contact.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.activeandroid; 2 | 3 | import com.activeandroid.Model; 4 | import com.activeandroid.annotation.Column; 5 | import com.activeandroid.annotation.Table; 6 | import com.raizlabs.android.databasecomparison.interfaces.IContact; 7 | 8 | /** 9 | * Description: 10 | */ 11 | @Table(name = "contact") 12 | public class Contact extends Model implements IContact { 13 | 14 | @Column(name = "name") 15 | private String name; 16 | 17 | @Column(name = "email") 18 | private String email; 19 | 20 | @Column(name = "addressBook") 21 | private AddressBook addressBook; 22 | 23 | @Override 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | @Override 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String getEmail() { 35 | return email; 36 | } 37 | 38 | @Override 39 | public void setEmail(String email) { 40 | this.email = email; 41 | } 42 | 43 | @Override 44 | public AddressBook getAddressBookField() { 45 | return addressBook; 46 | } 47 | 48 | @Override 49 | public void setAddressBook(AddressBook addressBook) { 50 | this.addressBook = addressBook; 51 | } 52 | 53 | @Override 54 | public void saveAll() { 55 | super.save(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/activeandroid/SimpleAddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.activeandroid; 2 | 3 | import com.activeandroid.Model; 4 | import com.activeandroid.annotation.Column; 5 | import com.activeandroid.annotation.Table; 6 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 7 | 8 | /** 9 | * Description: 10 | */ 11 | @Table(name = "SimpleAddressItem") 12 | public class SimpleAddressItem extends Model implements IAddressItem { 13 | 14 | @Column(name = "name") 15 | private String name; 16 | 17 | @Column(name = "address") 18 | private String address; 19 | 20 | @Column(name = "city") 21 | private String city; 22 | 23 | @Column(name = "state") 24 | private String state; 25 | 26 | @Column(name = "phone") 27 | private long phone; 28 | 29 | 30 | @Override 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | @Override 36 | public void setAddress(String address) { 37 | this.address = address; 38 | } 39 | 40 | @Override 41 | public void setCity(String city) { 42 | this.city = city; 43 | } 44 | 45 | @Override 46 | public void setState(String state) { 47 | this.state = state; 48 | } 49 | 50 | @Override 51 | public void setPhone(long phone) { 52 | this.phone = phone; 53 | } 54 | 55 | @Override 56 | public void setAddressBook(AddressBook addressBook) { 57 | 58 | } 59 | 60 | @Override 61 | public void saveAll() { 62 | super.save(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/dbflow/AddressBook.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.dbflow; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressBook; 4 | import com.raizlabs.android.databasecomparison.MainActivity; 5 | import com.raizlabs.android.dbflow.annotation.Column; 6 | import com.raizlabs.android.dbflow.annotation.ModelContainer; 7 | import com.raizlabs.android.dbflow.annotation.PrimaryKey; 8 | import com.raizlabs.android.dbflow.annotation.Table; 9 | import com.raizlabs.android.dbflow.sql.builder.Condition; 10 | import com.raizlabs.android.dbflow.sql.language.Select; 11 | import com.raizlabs.android.dbflow.structure.cache.BaseCacheableModel; 12 | 13 | import java.util.Collection; 14 | 15 | /** 16 | * Description: 17 | */ 18 | @Table(tableName = "AddressBook", databaseName = DBFlowDatabase.NAME) 19 | @ModelContainer 20 | public class AddressBook extends BaseCacheableModel implements IAddressBook { 21 | 22 | @PrimaryKey(autoincrement = true) 23 | @Column 24 | long id; 25 | 26 | @Column(name = "name") 27 | String name; 28 | 29 | @Column(name = "author") 30 | String author; 31 | 32 | Collection addresses; 33 | 34 | Collection contacts; 35 | 36 | @Override 37 | public void setId(long id) { 38 | // not needed because we have autoincrementing keys 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public void setAuthor(String author) { 46 | this.author = author; 47 | } 48 | 49 | public void setAddresses(Collection addresses) { 50 | this.addresses = addresses; 51 | } 52 | 53 | public Collection getAddresses() { 54 | if (addresses == null) { 55 | addresses = new Select().from(AddressItem.class).where(Condition.column(AddressItem$Table.ADDRESSBOOK_ADDRESSBOOK).is(id)).queryList(); 56 | } 57 | return addresses; 58 | } 59 | 60 | public Collection getContacts() { 61 | if (contacts == null) { 62 | contacts = new Select().from(Contact.class).where(Condition.column(Contact$Table.ADDRESSBOOK_ADDRESSBOOK).is(id)).queryList(); 63 | } 64 | return contacts; 65 | } 66 | 67 | public void setContacts(Collection contacts) { 68 | this.contacts = contacts; 69 | } 70 | 71 | @Override 72 | public void saveAll() { 73 | super.insert(); 74 | for (AddressItem addressItem : addresses) { 75 | addressItem.saveAll(); 76 | } 77 | for (Contact contact : contacts) { 78 | contact.saveAll(); 79 | } 80 | } 81 | 82 | @Override 83 | public int getCacheSize() { 84 | return MainActivity.ADDRESS_BOOK_COUNT; 85 | } 86 | 87 | } -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/dbflow/AddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.dbflow; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 4 | import com.raizlabs.android.dbflow.annotation.Column; 5 | import com.raizlabs.android.dbflow.annotation.ForeignKey; 6 | import com.raizlabs.android.dbflow.annotation.ForeignKeyReference; 7 | import com.raizlabs.android.dbflow.annotation.PrimaryKey; 8 | import com.raizlabs.android.dbflow.annotation.Table; 9 | import com.raizlabs.android.dbflow.structure.BaseModel; 10 | import com.raizlabs.android.dbflow.structure.container.ForeignKeyContainer; 11 | 12 | import java.util.LinkedHashMap; 13 | import java.util.Map; 14 | 15 | /** 16 | * Description: 17 | */ 18 | @Table(databaseName = DBFlowDatabase.NAME) 19 | public class AddressItem extends BaseModel implements IAddressItem { 20 | 21 | @PrimaryKey(autoincrement = true) 22 | @Column 23 | long id; 24 | 25 | @Column(name = "name") 26 | String name; 27 | 28 | @Column(name = "address") 29 | String address; 30 | 31 | @Column(name = "city") 32 | String city; 33 | 34 | @Column(name = "state") 35 | String state; 36 | 37 | @Column(name = "phone") 38 | long phone; 39 | 40 | 41 | @Override 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | 46 | @Override 47 | public void setAddress(String address) { 48 | this.address = address; 49 | } 50 | 51 | @Override 52 | public void setCity(String city) { 53 | this.city = city; 54 | } 55 | 56 | @Override 57 | public void setState(String state) { 58 | this.state = state; 59 | } 60 | 61 | @Override 62 | public void setPhone(long phone) { 63 | this.phone = phone; 64 | } 65 | 66 | @Override 67 | public void saveAll() { 68 | super.insert(); 69 | } 70 | 71 | @ForeignKey( 72 | references = {@ForeignKeyReference(columnName = "addressBook", columnType = long.class, foreignColumnName = "id")}, 73 | saveForeignKeyModel = false) 74 | @Column 75 | ForeignKeyContainer addressBook; 76 | 77 | 78 | @Override 79 | public void setAddressBook(AddressBook addressBook) { 80 | this.addressBook = new ForeignKeyContainer<>(AddressBook.class); 81 | Map keys = new LinkedHashMap<>(); 82 | keys.put(AddressBook$Table.ID, addressBook.id); 83 | this.addressBook.setData(keys); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/dbflow/Contact.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.dbflow; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IContact; 4 | import com.raizlabs.android.dbflow.annotation.Column; 5 | import com.raizlabs.android.dbflow.annotation.ForeignKey; 6 | import com.raizlabs.android.dbflow.annotation.ForeignKeyReference; 7 | import com.raizlabs.android.dbflow.annotation.PrimaryKey; 8 | import com.raizlabs.android.dbflow.annotation.Table; 9 | import com.raizlabs.android.dbflow.structure.BaseModel; 10 | import com.raizlabs.android.dbflow.structure.container.ForeignKeyContainer; 11 | 12 | import java.util.LinkedHashMap; 13 | import java.util.Map; 14 | 15 | /** 16 | * Description: 17 | */ 18 | @Table(tableName = "contact", databaseName = DBFlowDatabase.NAME) 19 | public class Contact extends BaseModel implements IContact { 20 | 21 | @PrimaryKey(autoincrement = true) 22 | @Column 23 | long id; 24 | 25 | @Column(name = "name") 26 | String name; 27 | 28 | @Column(name = "email") 29 | String email; 30 | 31 | @ForeignKey(references = {@ForeignKeyReference(columnName = "addressBook", 32 | foreignColumnName = "id", columnType = long.class)}, 33 | saveForeignKeyModel = false) 34 | @Column 35 | ForeignKeyContainer addressBook; 36 | 37 | @Override 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | @Override 43 | public void setName(String name) { 44 | this.name = name; 45 | } 46 | 47 | @Override 48 | public String getEmail() { 49 | return email; 50 | } 51 | 52 | @Override 53 | public void setEmail(String email) { 54 | this.email = email; 55 | } 56 | 57 | @Override 58 | public AddressBook getAddressBookField() { 59 | return addressBook.toModel(); 60 | } 61 | 62 | @Override 63 | public void setAddressBook(AddressBook addressBook) { 64 | this.addressBook = new ForeignKeyContainer<>(AddressBook.class); 65 | Map keys = new LinkedHashMap<>(); 66 | keys.put(AddressBook$Table.ID, addressBook.id); 67 | this.addressBook.setData(keys); 68 | this.addressBook.setModel(addressBook); 69 | } 70 | 71 | @Override 72 | public void saveAll() { 73 | super.insert(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/dbflow/DBFlowDatabase.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.dbflow; 2 | 3 | import com.raizlabs.android.dbflow.annotation.Database; 4 | 5 | /** 6 | * Description: 7 | */ 8 | @Database(name = DBFlowDatabase.NAME, version = DBFlowDatabase.VERSION) 9 | public class DBFlowDatabase { 10 | 11 | public static final String NAME = "dbflow"; 12 | 13 | public static final int VERSION = 1; 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/dbflow/DBFlowTester.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.dbflow; 2 | 3 | import android.content.Context; 4 | 5 | import com.raizlabs.android.databasecomparison.Generator; 6 | import com.raizlabs.android.databasecomparison.Loader; 7 | import com.raizlabs.android.databasecomparison.MainActivity; 8 | import com.raizlabs.android.databasecomparison.Saver; 9 | import com.raizlabs.android.databasecomparison.events.LogTestDataEvent; 10 | import com.raizlabs.android.dbflow.runtime.TransactionManager; 11 | import com.raizlabs.android.dbflow.sql.language.Select; 12 | 13 | import java.util.Collection; 14 | 15 | import de.greenrobot.event.EventBus; 16 | 17 | /** 18 | * Description: 19 | */ 20 | public class DBFlowTester { 21 | public static final String FRAMEWORK_NAME = "DBFlow"; 22 | 23 | public static void testAddressBooks(Context context) { 24 | com.raizlabs.android.dbflow.sql.language.Delete.tables(AddressItem.class, 25 | Contact.class, AddressBook.class); 26 | 27 | Collection addressBooks = Generator.createAddressBooks(AddressBook.class, 28 | Contact.class, AddressItem.class, 29 | MainActivity.ADDRESS_BOOK_COUNT); 30 | 31 | long startTime = System.currentTimeMillis(); 32 | final Collection finalAddressBooks = addressBooks; 33 | TransactionManager.transact(DBFlowDatabase.NAME, new Runnable() { 34 | @Override 35 | public void run() { 36 | Saver.saveAll(finalAddressBooks); 37 | } 38 | }); 39 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 40 | 41 | startTime = System.currentTimeMillis(); 42 | addressBooks = new Select().from(AddressBook.class).queryList(); 43 | Loader.loadAllInnerData(addressBooks); 44 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 45 | 46 | 47 | com.raizlabs.android.dbflow.sql.language.Delete.tables(AddressItem.class, 48 | Contact.class, AddressBook.class); 49 | } 50 | 51 | public static void testAddressItems(Context context) { 52 | com.raizlabs.android.dbflow.sql.language.Delete.table(SimpleAddressItem.class); 53 | Collection dbFlowModels = 54 | Generator.getAddresses(SimpleAddressItem.class, MainActivity.LOOP_COUNT); 55 | long startTime = System.currentTimeMillis(); 56 | final Collection finalDbFlowModels = dbFlowModels; 57 | TransactionManager.transact(DBFlowDatabase.NAME, new Runnable() { 58 | @Override 59 | public void run() { 60 | Saver.saveAll(finalDbFlowModels); 61 | } 62 | }); 63 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 64 | 65 | startTime = System.currentTimeMillis(); 66 | dbFlowModels = new Select().from(SimpleAddressItem.class).queryList(); 67 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 68 | 69 | com.raizlabs.android.dbflow.sql.language.Delete.table(SimpleAddressItem.class); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/dbflow/SimpleAddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.dbflow; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 4 | import com.raizlabs.android.databasecomparison.MainActivity; 5 | import com.raizlabs.android.dbflow.annotation.Column; 6 | import com.raizlabs.android.dbflow.annotation.PrimaryKey; 7 | import com.raizlabs.android.dbflow.annotation.Table; 8 | import com.raizlabs.android.dbflow.structure.cache.BaseCacheableModel; 9 | 10 | /** 11 | * Description: 12 | */ 13 | @Table(databaseName = DBFlowDatabase.NAME) 14 | public class SimpleAddressItem extends BaseCacheableModel implements IAddressItem { 15 | 16 | @PrimaryKey(autoincrement = true) 17 | @Column 18 | long id; 19 | 20 | @Column(name = "name") 21 | String name; 22 | 23 | @Column(name = "address") 24 | String address; 25 | 26 | @Column(name = "city") 27 | String city; 28 | 29 | @Column(name = "state") 30 | String state; 31 | 32 | @Column(name = "phone") 33 | long phone; 34 | 35 | @Override 36 | public void setName(String name) { 37 | this.name = name; 38 | } 39 | 40 | @Override 41 | public void setAddress(String address) { 42 | this.address = address; 43 | } 44 | 45 | @Override 46 | public void setCity(String city) { 47 | this.city = city; 48 | } 49 | 50 | @Override 51 | public void setState(String state) { 52 | this.state = state; 53 | } 54 | 55 | @Override 56 | public void setPhone(long phone) { 57 | this.phone = phone; 58 | } 59 | 60 | @Override 61 | public void setAddressBook(AddressBook addressBook) { 62 | 63 | } 64 | 65 | @Override 66 | public void saveAll() { 67 | super.insert(); 68 | } 69 | 70 | @Override 71 | public int getCacheSize() { 72 | return MainActivity.LOOP_COUNT; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/events/LogTestDataEvent.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.events; 2 | 3 | /** 4 | * Event to log data for a benchmark trial 5 | */ 6 | public class LogTestDataEvent { 7 | private long startTime; 8 | private String framework; 9 | private String eventName; 10 | 11 | public LogTestDataEvent(long startTime, String framework, String eventName) { 12 | this.startTime = startTime; 13 | this.framework = framework; 14 | this.eventName = eventName; 15 | } 16 | 17 | public long getStartTime() { 18 | return startTime; 19 | } 20 | 21 | public String getFramework() { 22 | return framework; 23 | } 24 | 25 | public String getEventName() { 26 | return eventName; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/events/TrialCompletedEvent.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.events; 2 | 3 | /** 4 | * Event to indicate that a trial has been completed 5 | */ 6 | public class TrialCompletedEvent { 7 | private final String trialName; 8 | 9 | public TrialCompletedEvent(String testName) { 10 | this.trialName = testName; 11 | } 12 | 13 | public String getTrialName() { 14 | return trialName; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/GreenDaoGenerator.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao; 2 | 3 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressBook; 4 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressItem; 5 | import com.raizlabs.android.databasecomparison.greendao.gen.Contact; 6 | import com.raizlabs.android.databasecomparison.greendao.gen.SimpleAddressItem; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | /** 12 | * Description: 13 | */ 14 | public class GreenDaoGenerator { 15 | 16 | public static List getSimpleAddressItems(int count) { 17 | final List addressItemList = new ArrayList<>(); 18 | for (int i = 0; i < count; i++) { 19 | com.raizlabs.android.databasecomparison.greendao.gen.SimpleAddressItem simpleAddressItem = new com.raizlabs.android.databasecomparison.greendao.gen.SimpleAddressItem(); 20 | simpleAddressItem.setName("Test"); 21 | simpleAddressItem.setAddress("5486 Memory Lane"); 22 | simpleAddressItem.setCity("Bronx"); 23 | simpleAddressItem.setState("NY"); 24 | simpleAddressItem.setPhone(7185555555l); 25 | addressItemList.add(simpleAddressItem); 26 | } 27 | return addressItemList; 28 | } 29 | 30 | public static List getAddressItems(int count, AddressBook addressBook) { 31 | final List addressItemList = new ArrayList<>(); 32 | for (int i = 0; i < count; i++) { 33 | AddressItem simpleAddressItem = new AddressItem(); 34 | simpleAddressItem.setName("Test"); 35 | simpleAddressItem.setAddress("5486 Memory Lane"); 36 | simpleAddressItem.setCity("Bronx"); 37 | simpleAddressItem.setState("NY"); 38 | simpleAddressItem.setPhone(7185555555l); 39 | simpleAddressItem.setAddressBook(addressBook); 40 | addressItemList.add(simpleAddressItem); 41 | } 42 | return addressItemList; 43 | } 44 | 45 | public static List getContacts(int count, AddressBook addressBook) { 46 | List sugarModelList = new ArrayList<>(); 47 | for (int i = 0; i < count; i++) { 48 | Contact contact = new Contact(); 49 | contact.setName("Test"); 50 | contact.setEmail("abgrosner@gmail.com"); 51 | if (addressBook != null) { 52 | contact.setAddressBook(addressBook); 53 | } 54 | sugarModelList.add(contact); 55 | } 56 | return sugarModelList; 57 | } 58 | 59 | 60 | public static List createAddressBooks(int count) { 61 | List addressBooks = new ArrayList<>(); 62 | for (int i = 0; i < count; i++) { 63 | AddressBook addressBook = new AddressBook(); 64 | addressBook.setName("Test"); 65 | addressBook.setAuthor("Andrew Grosner"); 66 | addressBook.setAddressItemList(getAddressItems(count, addressBook)); 67 | addressBook.setContactList(getContacts(count, addressBook)); 68 | addressBooks.add(addressBook); 69 | } 70 | return addressBooks; 71 | } 72 | 73 | 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/GreenDaoTester.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao; 2 | 3 | import android.content.Context; 4 | 5 | import com.raizlabs.android.databasecomparison.MainActivity; 6 | import com.raizlabs.android.databasecomparison.events.LogTestDataEvent; 7 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressBook; 8 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressBookDao; 9 | import com.raizlabs.android.databasecomparison.greendao.gen.Contact; 10 | import com.raizlabs.android.databasecomparison.greendao.gen.DaoMaster; 11 | import com.raizlabs.android.databasecomparison.greendao.gen.DaoSession; 12 | import com.raizlabs.android.databasecomparison.greendao.gen.SimpleAddressItem; 13 | import com.raizlabs.android.databasecomparison.greendao.gen.SimpleAddressItemDao; 14 | 15 | import java.util.Collection; 16 | import java.util.List; 17 | 18 | import de.greenrobot.event.EventBus; 19 | 20 | /** 21 | * Description: 22 | */ 23 | public class GreenDaoTester { 24 | public static final String FRAMEWORK_NAME = "GreenDAO"; 25 | 26 | public static void testAddressItems(Context context) { 27 | DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "notes-db", null); 28 | DaoMaster daoMaster = new DaoMaster(helper.getWritableDatabase()); 29 | DaoSession daoSession = daoMaster.newSession(); 30 | final SimpleAddressItemDao simpleAddressItemDao = daoSession.getSimpleAddressItemDao(); 31 | simpleAddressItemDao.deleteAll(); 32 | 33 | final List addressItemList = GreenDaoGenerator.getSimpleAddressItems(MainActivity.LOOP_COUNT); 34 | 35 | long startTime = System.currentTimeMillis(); 36 | simpleAddressItemDao.insertOrReplaceInTx(addressItemList); 37 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 38 | 39 | startTime = System.currentTimeMillis(); 40 | simpleAddressItemDao.loadAll(); 41 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 42 | 43 | simpleAddressItemDao.deleteAll(); 44 | } 45 | 46 | public static void testAddressBooks(Context context) { 47 | DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "notes-db", null); 48 | DaoMaster daoMaster = new DaoMaster(helper.getWritableDatabase()); 49 | final DaoSession daoSession = daoMaster.newSession(); 50 | final AddressBookDao addressBookDao = daoSession.getAddressBookDao(); 51 | addressBookDao.deleteAll(); 52 | daoSession.getAddressItemDao().deleteAll(); 53 | daoSession.getContactDao().deleteAll(); 54 | 55 | List addressBooks = GreenDaoGenerator.createAddressBooks(MainActivity.ADDRESS_BOOK_COUNT); 56 | 57 | long startTime = System.currentTimeMillis(); 58 | final List finalAddressBooks = addressBooks; 59 | daoSession.runInTx(new Runnable() { 60 | @Override 61 | public void run() { 62 | addressBookDao.insertInTx(finalAddressBooks); 63 | for (AddressBook addressBook : finalAddressBooks) { 64 | daoSession.getContactDao().insertInTx(addressBook.getContactList()); 65 | daoSession.getAddressItemDao().insertInTx(addressBook.getAddressItemList()); 66 | } 67 | } 68 | }); 69 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 70 | 71 | startTime = System.currentTimeMillis(); 72 | addressBooks = addressBookDao.loadAll(); 73 | for (AddressBook addressBook : addressBooks) { 74 | addressBook.getAddressItemList(); 75 | Collection contactList = addressBook.getContactList(); 76 | for(Contact contact: contactList) { 77 | contact.getAddressBook(); 78 | } 79 | } 80 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 81 | 82 | addressBookDao.deleteAll(); 83 | daoSession.getAddressItemDao().deleteAll(); 84 | daoSession.getContactDao().deleteAll(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/SimpleAddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 4 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressBook; 5 | 6 | /** 7 | * Description: 8 | */ 9 | public class SimpleAddressItem extends com.raizlabs.android.databasecomparison.greendao.gen.SimpleAddressItem implements IAddressItem { 10 | @Override 11 | public void setPhone(long phone) { 12 | setPhone(phone); 13 | } 14 | 15 | @Override 16 | public void setAddressBook(AddressBook addressBook) { 17 | 18 | } 19 | 20 | @Override 21 | public void saveAll() { 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/gen/AddressBook.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao.gen; 2 | 3 | import java.util.Collection; 4 | import java.util.List; 5 | 6 | import com.raizlabs.android.databasecomparison.interfaces.IAddressBook; 7 | 8 | import de.greenrobot.dao.DaoException; 9 | 10 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit. 11 | /** 12 | * Entity mapped to table ADDRESS_BOOK. 13 | */ 14 | public class AddressBook implements IAddressBook { 15 | 16 | private Long id; 17 | private String name; 18 | private String author; 19 | 20 | /** Used to resolve relations */ 21 | private transient DaoSession daoSession; 22 | 23 | /** Used for active entity operations. */ 24 | private transient AddressBookDao myDao; 25 | 26 | protected Collection addressItemList; 27 | protected Collection contactList; 28 | 29 | public AddressBook() { 30 | } 31 | 32 | public AddressBook(Long id) { 33 | this.id = id; 34 | } 35 | 36 | public AddressBook(Long id, String name, String author) { 37 | this.id = id; 38 | this.name = name; 39 | this.author = author; 40 | } 41 | 42 | /** called by internal mechanisms, do not call yourself. */ 43 | public void __setDaoSession(DaoSession daoSession) { 44 | this.daoSession = daoSession; 45 | myDao = daoSession != null ? daoSession.getAddressBookDao() : null; 46 | } 47 | 48 | public Long getId() { 49 | return id; 50 | } 51 | 52 | public void setId(Long id) { 53 | this.id = id; 54 | } 55 | 56 | public String getName() { 57 | return name; 58 | } 59 | 60 | @Override 61 | public void setId(long id) { 62 | this.id = id; 63 | } 64 | 65 | public void setName(String name) { 66 | this.name = name; 67 | } 68 | 69 | public String getAuthor() { 70 | return author; 71 | } 72 | 73 | public void setAuthor(String author) { 74 | this.author = author; 75 | } 76 | 77 | @Override 78 | public void setAddresses(Collection addresses) { 79 | addressItemList = addresses; 80 | } 81 | 82 | @Override 83 | public Collection getAddresses() { 84 | return addressItemList; 85 | } 86 | 87 | @Override 88 | public Collection getContacts() { 89 | return contactList; 90 | } 91 | 92 | @Override 93 | public void setContacts(Collection contacts) { 94 | contactList = contacts; 95 | } 96 | 97 | /** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */ 98 | public Collection getAddressItemList() { 99 | if (addressItemList == null) { 100 | if (daoSession == null) { 101 | throw new DaoException("Entity is detached from DAO context"); 102 | } 103 | AddressItemDao targetDao = daoSession.getAddressItemDao(); 104 | List addressItemListNew = targetDao._queryAddressBook_AddressItemList(id); 105 | synchronized (this) { 106 | if(addressItemList == null) { 107 | addressItemList = addressItemListNew; 108 | } 109 | } 110 | } 111 | return addressItemList; 112 | } 113 | 114 | public void setAddressItemList(List addressItemList) { 115 | this.addressItemList = addressItemList; 116 | } 117 | 118 | /** Resets a to-many relationship, making the next get call to query for a fresh result. */ 119 | public synchronized void resetAddressItemList() { 120 | addressItemList = null; 121 | } 122 | 123 | /** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */ 124 | public Collection getContactList() { 125 | if (contactList == null) { 126 | if (daoSession == null) { 127 | throw new DaoException("Entity is detached from DAO context"); 128 | } 129 | ContactDao targetDao = daoSession.getContactDao(); 130 | List contactListNew = targetDao._queryAddressBook_ContactList(id); 131 | synchronized (this) { 132 | if(contactList == null) { 133 | contactList = contactListNew; 134 | } 135 | } 136 | } 137 | return contactList; 138 | } 139 | 140 | public void setContactList(List contactList) { 141 | this.contactList = contactList; 142 | } 143 | 144 | /** Resets a to-many relationship, making the next get call to query for a fresh result. */ 145 | public synchronized void resetContactList() { 146 | contactList = null; 147 | } 148 | 149 | /** Convenient call for {@link AbstractDao#delete(Object)}. Entity must attached to an entity context. */ 150 | public void delete() { 151 | if (myDao == null) { 152 | throw new DaoException("Entity is detached from DAO context"); 153 | } 154 | myDao.delete(this); 155 | } 156 | 157 | /** Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity context. */ 158 | public void update() { 159 | if (myDao == null) { 160 | throw new DaoException("Entity is detached from DAO context"); 161 | } 162 | myDao.update(this); 163 | } 164 | 165 | /** Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity context. */ 166 | public void refresh() { 167 | if (myDao == null) { 168 | throw new DaoException("Entity is detached from DAO context"); 169 | } 170 | myDao.refresh(this); 171 | } 172 | 173 | @Override 174 | public void saveAll() { 175 | 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/gen/AddressBookDao.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao.gen; 2 | 3 | import android.database.Cursor; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.database.sqlite.SQLiteStatement; 6 | 7 | import de.greenrobot.dao.AbstractDao; 8 | import de.greenrobot.dao.Property; 9 | import de.greenrobot.dao.internal.DaoConfig; 10 | 11 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressBook; 12 | 13 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. 14 | /** 15 | * DAO for table ADDRESS_BOOK. 16 | */ 17 | public class AddressBookDao extends AbstractDao { 18 | 19 | public static final String TABLENAME = "ADDRESS_BOOK"; 20 | 21 | /** 22 | * Properties of entity AddressBook.
23 | * Can be used for QueryBuilder and for referencing column names. 24 | */ 25 | public static class Properties { 26 | public final static Property Id = new Property(0, Long.class, "id", true, "_id"); 27 | public final static Property Name = new Property(1, String.class, "name", false, "NAME"); 28 | public final static Property Author = new Property(2, String.class, "author", false, "AUTHOR"); 29 | }; 30 | 31 | private DaoSession daoSession; 32 | 33 | 34 | public AddressBookDao(DaoConfig config) { 35 | super(config); 36 | } 37 | 38 | public AddressBookDao(DaoConfig config, DaoSession daoSession) { 39 | super(config, daoSession); 40 | this.daoSession = daoSession; 41 | } 42 | 43 | /** Creates the underlying database table. */ 44 | public static void createTable(SQLiteDatabase db, boolean ifNotExists) { 45 | String constraint = ifNotExists? "IF NOT EXISTS ": ""; 46 | db.execSQL("CREATE TABLE " + constraint + "'ADDRESS_BOOK' (" + // 47 | "'_id' INTEGER PRIMARY KEY ," + // 0: id 48 | "'NAME' TEXT," + // 1: name 49 | "'AUTHOR' TEXT);"); // 2: author 50 | } 51 | 52 | /** Drops the underlying database table. */ 53 | public static void dropTable(SQLiteDatabase db, boolean ifExists) { 54 | String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'ADDRESS_BOOK'"; 55 | db.execSQL(sql); 56 | } 57 | 58 | /** @inheritdoc */ 59 | @Override 60 | protected void bindValues(SQLiteStatement stmt, AddressBook entity) { 61 | stmt.clearBindings(); 62 | 63 | Long id = entity.getId(); 64 | if (id != null) { 65 | stmt.bindLong(1, id); 66 | } 67 | 68 | String name = entity.getName(); 69 | if (name != null) { 70 | stmt.bindString(2, name); 71 | } 72 | 73 | String author = entity.getAuthor(); 74 | if (author != null) { 75 | stmt.bindString(3, author); 76 | } 77 | } 78 | 79 | @Override 80 | protected void attachEntity(AddressBook entity) { 81 | super.attachEntity(entity); 82 | entity.__setDaoSession(daoSession); 83 | } 84 | 85 | /** @inheritdoc */ 86 | @Override 87 | public Long readKey(Cursor cursor, int offset) { 88 | return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); 89 | } 90 | 91 | /** @inheritdoc */ 92 | @Override 93 | public AddressBook readEntity(Cursor cursor, int offset) { 94 | AddressBook entity = new AddressBook( // 95 | cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id 96 | cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // name 97 | cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2) // author 98 | ); 99 | return entity; 100 | } 101 | 102 | /** @inheritdoc */ 103 | @Override 104 | public void readEntity(Cursor cursor, AddressBook entity, int offset) { 105 | entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); 106 | entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1)); 107 | entity.setAuthor(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); 108 | } 109 | 110 | /** @inheritdoc */ 111 | @Override 112 | protected Long updateKeyAfterInsert(AddressBook entity, long rowId) { 113 | entity.setId(rowId); 114 | return rowId; 115 | } 116 | 117 | /** @inheritdoc */ 118 | @Override 119 | public Long getKey(AddressBook entity) { 120 | if(entity != null) { 121 | return entity.getId(); 122 | } else { 123 | return null; 124 | } 125 | } 126 | 127 | /** @inheritdoc */ 128 | @Override 129 | protected boolean isEntityUpdateable() { 130 | return true; 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/gen/AddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao.gen; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 4 | 5 | import de.greenrobot.dao.DaoException; 6 | 7 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit. 8 | /** 9 | * Entity mapped to table ADDRESS_ITEM. 10 | */ 11 | public class AddressItem implements IAddressItem { 12 | 13 | private Long id; 14 | private String name; 15 | private String address; 16 | private String city; 17 | private String state; 18 | private Long phone; 19 | 20 | /** Used to resolve relations */ 21 | private transient DaoSession daoSession; 22 | 23 | /** Used for active entity operations. */ 24 | private transient AddressItemDao myDao; 25 | 26 | private AddressBook addressBook; 27 | private Long addressBook__resolvedKey; 28 | 29 | 30 | public AddressItem() { 31 | } 32 | 33 | public AddressItem(Long id) { 34 | this.id = id; 35 | } 36 | 37 | public AddressItem(Long id, String name, String address, String city, String state, Long phone) { 38 | this.id = id; 39 | this.name = name; 40 | this.address = address; 41 | this.city = city; 42 | this.state = state; 43 | this.phone = phone; 44 | } 45 | 46 | /** called by internal mechanisms, do not call yourself. */ 47 | public void __setDaoSession(DaoSession daoSession) { 48 | this.daoSession = daoSession; 49 | myDao = daoSession != null ? daoSession.getAddressItemDao() : null; 50 | } 51 | 52 | public Long getId() { 53 | return id; 54 | } 55 | 56 | public void setId(Long id) { 57 | this.id = id; 58 | } 59 | 60 | public String getName() { 61 | return name; 62 | } 63 | 64 | public void setName(String name) { 65 | this.name = name; 66 | } 67 | 68 | public String getAddress() { 69 | return address; 70 | } 71 | 72 | public void setAddress(String address) { 73 | this.address = address; 74 | } 75 | 76 | public String getCity() { 77 | return city; 78 | } 79 | 80 | public void setCity(String city) { 81 | this.city = city; 82 | } 83 | 84 | public String getState() { 85 | return state; 86 | } 87 | 88 | public void setState(String state) { 89 | this.state = state; 90 | } 91 | 92 | @Override 93 | public void setPhone(long phone) { 94 | setPhone((Long)phone); 95 | } 96 | 97 | public Long getPhone() { 98 | return phone; 99 | } 100 | 101 | public void setPhone(Long phone) { 102 | this.phone = phone; 103 | } 104 | 105 | /** To-one relationship, resolved on first access. */ 106 | public AddressBook getAddressBook() { 107 | Long __key = this.id; 108 | if (addressBook__resolvedKey == null || !addressBook__resolvedKey.equals(__key)) { 109 | if (daoSession == null) { 110 | throw new DaoException("Entity is detached from DAO context"); 111 | } 112 | AddressBookDao targetDao = daoSession.getAddressBookDao(); 113 | AddressBook addressBookNew = targetDao.load(__key); 114 | synchronized (this) { 115 | addressBook = addressBookNew; 116 | addressBook__resolvedKey = __key; 117 | } 118 | } 119 | return addressBook; 120 | } 121 | 122 | public void setAddressBook(AddressBook addressBook) { 123 | synchronized (this) { 124 | this.addressBook = addressBook; 125 | id = addressBook == null ? null : addressBook.getId(); 126 | addressBook__resolvedKey = id; 127 | } 128 | } 129 | 130 | /** Convenient call for {@link AbstractDao#delete(Object)}. Entity must attached to an entity context. */ 131 | public void delete() { 132 | if (myDao == null) { 133 | throw new DaoException("Entity is detached from DAO context"); 134 | } 135 | myDao.delete(this); 136 | } 137 | 138 | /** Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity context. */ 139 | public void update() { 140 | if (myDao == null) { 141 | throw new DaoException("Entity is detached from DAO context"); 142 | } 143 | myDao.update(this); 144 | } 145 | 146 | /** Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity context. */ 147 | public void refresh() { 148 | if (myDao == null) { 149 | throw new DaoException("Entity is detached from DAO context"); 150 | } 151 | myDao.refresh(this); 152 | } 153 | 154 | @Override 155 | public void saveAll() { 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/gen/AddressItemDao.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao.gen; 2 | 3 | import java.util.List; 4 | import java.util.ArrayList; 5 | import android.database.Cursor; 6 | import android.database.sqlite.SQLiteDatabase; 7 | import android.database.sqlite.SQLiteStatement; 8 | 9 | import de.greenrobot.dao.AbstractDao; 10 | import de.greenrobot.dao.Property; 11 | import de.greenrobot.dao.internal.SqlUtils; 12 | import de.greenrobot.dao.internal.DaoConfig; 13 | import de.greenrobot.dao.query.Query; 14 | import de.greenrobot.dao.query.QueryBuilder; 15 | 16 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressItem; 17 | 18 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. 19 | /** 20 | * DAO for table ADDRESS_ITEM. 21 | */ 22 | public class AddressItemDao extends AbstractDao { 23 | 24 | public static final String TABLENAME = "ADDRESS_ITEM"; 25 | 26 | /** 27 | * Properties of entity AddressItem.
28 | * Can be used for QueryBuilder and for referencing column names. 29 | */ 30 | public static class Properties { 31 | public final static Property Id = new Property(0, Long.class, "id", true, "_id"); 32 | public final static Property Name = new Property(1, String.class, "name", false, "NAME"); 33 | public final static Property Address = new Property(2, String.class, "address", false, "ADDRESS"); 34 | public final static Property City = new Property(3, String.class, "city", false, "CITY"); 35 | public final static Property State = new Property(4, String.class, "state", false, "STATE"); 36 | public final static Property Phone = new Property(5, Long.class, "phone", false, "PHONE"); 37 | }; 38 | 39 | private DaoSession daoSession; 40 | 41 | private Query addressBook_AddressItemListQuery; 42 | 43 | public AddressItemDao(DaoConfig config) { 44 | super(config); 45 | } 46 | 47 | public AddressItemDao(DaoConfig config, DaoSession daoSession) { 48 | super(config, daoSession); 49 | this.daoSession = daoSession; 50 | } 51 | 52 | /** Creates the underlying database table. */ 53 | public static void createTable(SQLiteDatabase db, boolean ifNotExists) { 54 | String constraint = ifNotExists? "IF NOT EXISTS ": ""; 55 | db.execSQL("CREATE TABLE " + constraint + "'ADDRESS_ITEM' (" + // 56 | "'_id' INTEGER PRIMARY KEY ," + // 0: id 57 | "'NAME' TEXT," + // 1: name 58 | "'ADDRESS' TEXT," + // 2: address 59 | "'CITY' TEXT," + // 3: city 60 | "'STATE' TEXT," + // 4: state 61 | "'PHONE' INTEGER);"); // 5: phone 62 | } 63 | 64 | /** Drops the underlying database table. */ 65 | public static void dropTable(SQLiteDatabase db, boolean ifExists) { 66 | String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'ADDRESS_ITEM'"; 67 | db.execSQL(sql); 68 | } 69 | 70 | /** @inheritdoc */ 71 | @Override 72 | protected void bindValues(SQLiteStatement stmt, AddressItem entity) { 73 | stmt.clearBindings(); 74 | 75 | Long id = entity.getId(); 76 | if (id != null) { 77 | stmt.bindLong(1, id); 78 | } 79 | 80 | String name = entity.getName(); 81 | if (name != null) { 82 | stmt.bindString(2, name); 83 | } 84 | 85 | String address = entity.getAddress(); 86 | if (address != null) { 87 | stmt.bindString(3, address); 88 | } 89 | 90 | String city = entity.getCity(); 91 | if (city != null) { 92 | stmt.bindString(4, city); 93 | } 94 | 95 | String state = entity.getState(); 96 | if (state != null) { 97 | stmt.bindString(5, state); 98 | } 99 | 100 | Long phone = entity.getPhone(); 101 | if (phone != null) { 102 | stmt.bindLong(6, phone); 103 | } 104 | } 105 | 106 | @Override 107 | protected void attachEntity(AddressItem entity) { 108 | super.attachEntity(entity); 109 | entity.__setDaoSession(daoSession); 110 | } 111 | 112 | /** @inheritdoc */ 113 | @Override 114 | public Long readKey(Cursor cursor, int offset) { 115 | return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); 116 | } 117 | 118 | /** @inheritdoc */ 119 | @Override 120 | public AddressItem readEntity(Cursor cursor, int offset) { 121 | AddressItem entity = new AddressItem( // 122 | cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id 123 | cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // name 124 | cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // address 125 | cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // city 126 | cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // state 127 | cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5) // phone 128 | ); 129 | return entity; 130 | } 131 | 132 | /** @inheritdoc */ 133 | @Override 134 | public void readEntity(Cursor cursor, AddressItem entity, int offset) { 135 | entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); 136 | entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1)); 137 | entity.setAddress(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); 138 | entity.setCity(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); 139 | entity.setState(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); 140 | entity.setPhone(cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5)); 141 | } 142 | 143 | /** @inheritdoc */ 144 | @Override 145 | protected Long updateKeyAfterInsert(AddressItem entity, long rowId) { 146 | entity.setId(rowId); 147 | return rowId; 148 | } 149 | 150 | /** @inheritdoc */ 151 | @Override 152 | public Long getKey(AddressItem entity) { 153 | if(entity != null) { 154 | return entity.getId(); 155 | } else { 156 | return null; 157 | } 158 | } 159 | 160 | /** @inheritdoc */ 161 | @Override 162 | protected boolean isEntityUpdateable() { 163 | return true; 164 | } 165 | 166 | /** Internal query to resolve the "addressItemList" to-many relationship of AddressBook. */ 167 | public List _queryAddressBook_AddressItemList(Long id) { 168 | synchronized (this) { 169 | if (addressBook_AddressItemListQuery == null) { 170 | QueryBuilder queryBuilder = queryBuilder(); 171 | queryBuilder.where(Properties.Id.eq(null)); 172 | addressBook_AddressItemListQuery = queryBuilder.build(); 173 | } 174 | } 175 | Query query = addressBook_AddressItemListQuery.forCurrentThread(); 176 | query.setParameter(0, id); 177 | return query.list(); 178 | } 179 | 180 | private String selectDeep; 181 | 182 | protected String getSelectDeep() { 183 | if (selectDeep == null) { 184 | StringBuilder builder = new StringBuilder("SELECT "); 185 | SqlUtils.appendColumns(builder, "T", getAllColumns()); 186 | builder.append(','); 187 | SqlUtils.appendColumns(builder, "T0", daoSession.getAddressBookDao().getAllColumns()); 188 | builder.append(" FROM ADDRESS_ITEM T"); 189 | builder.append(" LEFT JOIN ADDRESS_BOOK T0 ON T.'_id'=T0.'_id'"); 190 | builder.append(' '); 191 | selectDeep = builder.toString(); 192 | } 193 | return selectDeep; 194 | } 195 | 196 | protected AddressItem loadCurrentDeep(Cursor cursor, boolean lock) { 197 | AddressItem entity = loadCurrent(cursor, 0, lock); 198 | int offset = getAllColumns().length; 199 | 200 | AddressBook addressBook = loadCurrentOther(daoSession.getAddressBookDao(), cursor, offset); 201 | entity.setAddressBook(addressBook); 202 | 203 | return entity; 204 | } 205 | 206 | public AddressItem loadDeep(Long key) { 207 | assertSinglePk(); 208 | if (key == null) { 209 | return null; 210 | } 211 | 212 | StringBuilder builder = new StringBuilder(getSelectDeep()); 213 | builder.append("WHERE "); 214 | SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns()); 215 | String sql = builder.toString(); 216 | 217 | String[] keyArray = new String[] { key.toString() }; 218 | Cursor cursor = db.rawQuery(sql, keyArray); 219 | 220 | try { 221 | boolean available = cursor.moveToFirst(); 222 | if (!available) { 223 | return null; 224 | } else if (!cursor.isLast()) { 225 | throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount()); 226 | } 227 | return loadCurrentDeep(cursor, true); 228 | } finally { 229 | cursor.close(); 230 | } 231 | } 232 | 233 | /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */ 234 | public List loadAllDeepFromCursor(Cursor cursor) { 235 | int count = cursor.getCount(); 236 | List list = new ArrayList(count); 237 | 238 | if (cursor.moveToFirst()) { 239 | if (identityScope != null) { 240 | identityScope.lock(); 241 | identityScope.reserveRoom(count); 242 | } 243 | try { 244 | do { 245 | list.add(loadCurrentDeep(cursor, false)); 246 | } while (cursor.moveToNext()); 247 | } finally { 248 | if (identityScope != null) { 249 | identityScope.unlock(); 250 | } 251 | } 252 | } 253 | return list; 254 | } 255 | 256 | protected List loadDeepAllAndCloseCursor(Cursor cursor) { 257 | try { 258 | return loadAllDeepFromCursor(cursor); 259 | } finally { 260 | cursor.close(); 261 | } 262 | } 263 | 264 | 265 | /** A raw-style query where you can pass any WHERE clause and arguments. */ 266 | public List queryDeep(String where, String... selectionArg) { 267 | Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg); 268 | return loadDeepAllAndCloseCursor(cursor); 269 | } 270 | 271 | } 272 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/gen/Contact.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao.gen; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IContact; 4 | 5 | import de.greenrobot.dao.DaoException; 6 | 7 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit. 8 | /** 9 | * Entity mapped to table CONTACT. 10 | */ 11 | public class Contact implements IContact { 12 | 13 | private Long id; 14 | private String name; 15 | private String email; 16 | 17 | /** Used to resolve relations */ 18 | private transient DaoSession daoSession; 19 | 20 | /** Used for active entity operations. */ 21 | private transient ContactDao myDao; 22 | 23 | private AddressBook addressBook; 24 | private Long addressBook__resolvedKey; 25 | 26 | 27 | public Contact() { 28 | } 29 | 30 | public Contact(Long id) { 31 | this.id = id; 32 | } 33 | 34 | public Contact(Long id, String name, String email) { 35 | this.id = id; 36 | this.name = name; 37 | this.email = email; 38 | } 39 | 40 | /** called by internal mechanisms, do not call yourself. */ 41 | public void __setDaoSession(DaoSession daoSession) { 42 | this.daoSession = daoSession; 43 | myDao = daoSession != null ? daoSession.getContactDao() : null; 44 | } 45 | 46 | public Long getId() { 47 | return id; 48 | } 49 | 50 | public void setId(Long id) { 51 | this.id = id; 52 | } 53 | 54 | public String getName() { 55 | return name; 56 | } 57 | 58 | public void setName(String name) { 59 | this.name = name; 60 | } 61 | 62 | public String getEmail() { 63 | return email; 64 | } 65 | 66 | public void setEmail(String email) { 67 | this.email = email; 68 | } 69 | 70 | @Override 71 | public AddressBook getAddressBookField() { 72 | return addressBook; 73 | } 74 | 75 | /** To-one relationship, resolved on first access. */ 76 | public AddressBook getAddressBook() { 77 | Long __key = this.id; 78 | if (addressBook__resolvedKey == null || !addressBook__resolvedKey.equals(__key)) { 79 | if (daoSession == null) { 80 | throw new DaoException("Entity is detached from DAO context"); 81 | } 82 | AddressBookDao targetDao = daoSession.getAddressBookDao(); 83 | AddressBook addressBookNew = targetDao.load(__key); 84 | synchronized (this) { 85 | addressBook = addressBookNew; 86 | addressBook__resolvedKey = __key; 87 | } 88 | } 89 | return addressBook; 90 | } 91 | 92 | public void setAddressBook(AddressBook addressBook) { 93 | synchronized (this) { 94 | this.addressBook = addressBook; 95 | id = addressBook == null ? null : addressBook.getId(); 96 | addressBook__resolvedKey = id; 97 | } 98 | } 99 | 100 | /** Convenient call for {@link AbstractDao#delete(Object)}. Entity must attached to an entity context. */ 101 | public void delete() { 102 | if (myDao == null) { 103 | throw new DaoException("Entity is detached from DAO context"); 104 | } 105 | myDao.delete(this); 106 | } 107 | 108 | /** Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity context. */ 109 | public void update() { 110 | if (myDao == null) { 111 | throw new DaoException("Entity is detached from DAO context"); 112 | } 113 | myDao.update(this); 114 | } 115 | 116 | /** Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity context. */ 117 | public void refresh() { 118 | if (myDao == null) { 119 | throw new DaoException("Entity is detached from DAO context"); 120 | } 121 | myDao.refresh(this); 122 | } 123 | 124 | @Override 125 | public void saveAll() { 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/gen/ContactDao.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao.gen; 2 | 3 | import java.util.List; 4 | import java.util.ArrayList; 5 | import android.database.Cursor; 6 | import android.database.sqlite.SQLiteDatabase; 7 | import android.database.sqlite.SQLiteStatement; 8 | 9 | import de.greenrobot.dao.AbstractDao; 10 | import de.greenrobot.dao.Property; 11 | import de.greenrobot.dao.internal.SqlUtils; 12 | import de.greenrobot.dao.internal.DaoConfig; 13 | import de.greenrobot.dao.query.Query; 14 | import de.greenrobot.dao.query.QueryBuilder; 15 | 16 | import com.raizlabs.android.databasecomparison.greendao.gen.Contact; 17 | 18 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. 19 | /** 20 | * DAO for table CONTACT. 21 | */ 22 | public class ContactDao extends AbstractDao { 23 | 24 | public static final String TABLENAME = "CONTACT"; 25 | 26 | /** 27 | * Properties of entity Contact.
28 | * Can be used for QueryBuilder and for referencing column names. 29 | */ 30 | public static class Properties { 31 | public final static Property Id = new Property(0, Long.class, "id", true, "_id"); 32 | public final static Property Name = new Property(1, String.class, "name", false, "NAME"); 33 | public final static Property Email = new Property(2, String.class, "email", false, "EMAIL"); 34 | }; 35 | 36 | private DaoSession daoSession; 37 | 38 | private Query addressBook_ContactListQuery; 39 | 40 | public ContactDao(DaoConfig config) { 41 | super(config); 42 | } 43 | 44 | public ContactDao(DaoConfig config, DaoSession daoSession) { 45 | super(config, daoSession); 46 | this.daoSession = daoSession; 47 | } 48 | 49 | /** Creates the underlying database table. */ 50 | public static void createTable(SQLiteDatabase db, boolean ifNotExists) { 51 | String constraint = ifNotExists? "IF NOT EXISTS ": ""; 52 | db.execSQL("CREATE TABLE " + constraint + "'CONTACT' (" + // 53 | "'_id' INTEGER PRIMARY KEY ," + // 0: id 54 | "'NAME' TEXT," + // 1: name 55 | "'EMAIL' TEXT);"); // 2: email 56 | } 57 | 58 | /** Drops the underlying database table. */ 59 | public static void dropTable(SQLiteDatabase db, boolean ifExists) { 60 | String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'CONTACT'"; 61 | db.execSQL(sql); 62 | } 63 | 64 | /** @inheritdoc */ 65 | @Override 66 | protected void bindValues(SQLiteStatement stmt, Contact entity) { 67 | stmt.clearBindings(); 68 | 69 | Long id = entity.getId(); 70 | if (id != null) { 71 | stmt.bindLong(1, id); 72 | } 73 | 74 | String name = entity.getName(); 75 | if (name != null) { 76 | stmt.bindString(2, name); 77 | } 78 | 79 | String email = entity.getEmail(); 80 | if (email != null) { 81 | stmt.bindString(3, email); 82 | } 83 | } 84 | 85 | @Override 86 | protected void attachEntity(Contact entity) { 87 | super.attachEntity(entity); 88 | entity.__setDaoSession(daoSession); 89 | } 90 | 91 | /** @inheritdoc */ 92 | @Override 93 | public Long readKey(Cursor cursor, int offset) { 94 | return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); 95 | } 96 | 97 | /** @inheritdoc */ 98 | @Override 99 | public Contact readEntity(Cursor cursor, int offset) { 100 | Contact entity = new Contact( // 101 | cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id 102 | cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // name 103 | cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2) // email 104 | ); 105 | return entity; 106 | } 107 | 108 | /** @inheritdoc */ 109 | @Override 110 | public void readEntity(Cursor cursor, Contact entity, int offset) { 111 | entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); 112 | entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1)); 113 | entity.setEmail(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); 114 | } 115 | 116 | /** @inheritdoc */ 117 | @Override 118 | protected Long updateKeyAfterInsert(Contact entity, long rowId) { 119 | entity.setId(rowId); 120 | return rowId; 121 | } 122 | 123 | /** @inheritdoc */ 124 | @Override 125 | public Long getKey(Contact entity) { 126 | if(entity != null) { 127 | return entity.getId(); 128 | } else { 129 | return null; 130 | } 131 | } 132 | 133 | /** @inheritdoc */ 134 | @Override 135 | protected boolean isEntityUpdateable() { 136 | return true; 137 | } 138 | 139 | /** Internal query to resolve the "contactList" to-many relationship of AddressBook. */ 140 | public List _queryAddressBook_ContactList(Long id) { 141 | synchronized (this) { 142 | if (addressBook_ContactListQuery == null) { 143 | QueryBuilder queryBuilder = queryBuilder(); 144 | queryBuilder.where(Properties.Id.eq(null)); 145 | addressBook_ContactListQuery = queryBuilder.build(); 146 | } 147 | } 148 | Query query = addressBook_ContactListQuery.forCurrentThread(); 149 | query.setParameter(0, id); 150 | return query.list(); 151 | } 152 | 153 | private String selectDeep; 154 | 155 | protected String getSelectDeep() { 156 | if (selectDeep == null) { 157 | StringBuilder builder = new StringBuilder("SELECT "); 158 | SqlUtils.appendColumns(builder, "T", getAllColumns()); 159 | builder.append(','); 160 | SqlUtils.appendColumns(builder, "T0", daoSession.getAddressBookDao().getAllColumns()); 161 | builder.append(" FROM CONTACT T"); 162 | builder.append(" LEFT JOIN ADDRESS_BOOK T0 ON T.'_id'=T0.'_id'"); 163 | builder.append(' '); 164 | selectDeep = builder.toString(); 165 | } 166 | return selectDeep; 167 | } 168 | 169 | protected Contact loadCurrentDeep(Cursor cursor, boolean lock) { 170 | Contact entity = loadCurrent(cursor, 0, lock); 171 | int offset = getAllColumns().length; 172 | 173 | AddressBook addressBook = loadCurrentOther(daoSession.getAddressBookDao(), cursor, offset); 174 | entity.setAddressBook(addressBook); 175 | 176 | return entity; 177 | } 178 | 179 | public Contact loadDeep(Long key) { 180 | assertSinglePk(); 181 | if (key == null) { 182 | return null; 183 | } 184 | 185 | StringBuilder builder = new StringBuilder(getSelectDeep()); 186 | builder.append("WHERE "); 187 | SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns()); 188 | String sql = builder.toString(); 189 | 190 | String[] keyArray = new String[] { key.toString() }; 191 | Cursor cursor = db.rawQuery(sql, keyArray); 192 | 193 | try { 194 | boolean available = cursor.moveToFirst(); 195 | if (!available) { 196 | return null; 197 | } else if (!cursor.isLast()) { 198 | throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount()); 199 | } 200 | return loadCurrentDeep(cursor, true); 201 | } finally { 202 | cursor.close(); 203 | } 204 | } 205 | 206 | /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */ 207 | public List loadAllDeepFromCursor(Cursor cursor) { 208 | int count = cursor.getCount(); 209 | List list = new ArrayList(count); 210 | 211 | if (cursor.moveToFirst()) { 212 | if (identityScope != null) { 213 | identityScope.lock(); 214 | identityScope.reserveRoom(count); 215 | } 216 | try { 217 | do { 218 | list.add(loadCurrentDeep(cursor, false)); 219 | } while (cursor.moveToNext()); 220 | } finally { 221 | if (identityScope != null) { 222 | identityScope.unlock(); 223 | } 224 | } 225 | } 226 | return list; 227 | } 228 | 229 | protected List loadDeepAllAndCloseCursor(Cursor cursor) { 230 | try { 231 | return loadAllDeepFromCursor(cursor); 232 | } finally { 233 | cursor.close(); 234 | } 235 | } 236 | 237 | 238 | /** A raw-style query where you can pass any WHERE clause and arguments. */ 239 | public List queryDeep(String where, String... selectionArg) { 240 | Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg); 241 | return loadDeepAllAndCloseCursor(cursor); 242 | } 243 | 244 | } 245 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/gen/DaoMaster.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao.gen; 2 | 3 | import android.content.Context; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.database.sqlite.SQLiteDatabase.CursorFactory; 6 | import android.database.sqlite.SQLiteOpenHelper; 7 | import android.util.Log; 8 | import de.greenrobot.dao.AbstractDaoMaster; 9 | import de.greenrobot.dao.identityscope.IdentityScopeType; 10 | 11 | import com.raizlabs.android.databasecomparison.greendao.gen.SimpleAddressItemDao; 12 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressItemDao; 13 | import com.raizlabs.android.databasecomparison.greendao.gen.ContactDao; 14 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressBookDao; 15 | 16 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. 17 | /** 18 | * Master of DAO (schema version 1): knows all DAOs. 19 | */ 20 | public class DaoMaster extends AbstractDaoMaster { 21 | public static final int SCHEMA_VERSION = 1; 22 | 23 | /** Creates underlying database table using DAOs. */ 24 | public static void createAllTables(SQLiteDatabase db, boolean ifNotExists) { 25 | SimpleAddressItemDao.createTable(db, ifNotExists); 26 | AddressItemDao.createTable(db, ifNotExists); 27 | ContactDao.createTable(db, ifNotExists); 28 | AddressBookDao.createTable(db, ifNotExists); 29 | } 30 | 31 | /** Drops underlying database table using DAOs. */ 32 | public static void dropAllTables(SQLiteDatabase db, boolean ifExists) { 33 | SimpleAddressItemDao.dropTable(db, ifExists); 34 | AddressItemDao.dropTable(db, ifExists); 35 | ContactDao.dropTable(db, ifExists); 36 | AddressBookDao.dropTable(db, ifExists); 37 | } 38 | 39 | public static abstract class OpenHelper extends SQLiteOpenHelper { 40 | 41 | public OpenHelper(Context context, String name, CursorFactory factory) { 42 | super(context, name, factory, SCHEMA_VERSION); 43 | } 44 | 45 | @Override 46 | public void onCreate(SQLiteDatabase db) { 47 | Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); 48 | createAllTables(db, false); 49 | } 50 | } 51 | 52 | /** WARNING: Drops all table on Upgrade! Use only during development. */ 53 | public static class DevOpenHelper extends OpenHelper { 54 | public DevOpenHelper(Context context, String name, CursorFactory factory) { 55 | super(context, name, factory); 56 | } 57 | 58 | @Override 59 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 60 | Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); 61 | dropAllTables(db, true); 62 | onCreate(db); 63 | } 64 | } 65 | 66 | public DaoMaster(SQLiteDatabase db) { 67 | super(db, SCHEMA_VERSION); 68 | registerDaoClass(SimpleAddressItemDao.class); 69 | registerDaoClass(AddressItemDao.class); 70 | registerDaoClass(ContactDao.class); 71 | registerDaoClass(AddressBookDao.class); 72 | } 73 | 74 | public DaoSession newSession() { 75 | return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); 76 | } 77 | 78 | public DaoSession newSession(IdentityScopeType type) { 79 | return new DaoSession(db, type, daoConfigMap); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/gen/DaoSession.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao.gen; 2 | 3 | import android.database.sqlite.SQLiteDatabase; 4 | 5 | import java.util.Map; 6 | 7 | import de.greenrobot.dao.AbstractDao; 8 | import de.greenrobot.dao.AbstractDaoSession; 9 | import de.greenrobot.dao.identityscope.IdentityScopeType; 10 | import de.greenrobot.dao.internal.DaoConfig; 11 | 12 | import com.raizlabs.android.databasecomparison.greendao.gen.SimpleAddressItem; 13 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressItem; 14 | import com.raizlabs.android.databasecomparison.greendao.gen.Contact; 15 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressBook; 16 | 17 | import com.raizlabs.android.databasecomparison.greendao.gen.SimpleAddressItemDao; 18 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressItemDao; 19 | import com.raizlabs.android.databasecomparison.greendao.gen.ContactDao; 20 | import com.raizlabs.android.databasecomparison.greendao.gen.AddressBookDao; 21 | 22 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. 23 | 24 | /** 25 | * {@inheritDoc} 26 | * 27 | * @see de.greenrobot.dao.AbstractDaoSession 28 | */ 29 | public class DaoSession extends AbstractDaoSession { 30 | 31 | private final DaoConfig simpleAddressItemDaoConfig; 32 | private final DaoConfig addressItemDaoConfig; 33 | private final DaoConfig contactDaoConfig; 34 | private final DaoConfig addressBookDaoConfig; 35 | 36 | private final SimpleAddressItemDao simpleAddressItemDao; 37 | private final AddressItemDao addressItemDao; 38 | private final ContactDao contactDao; 39 | private final AddressBookDao addressBookDao; 40 | 41 | public DaoSession(SQLiteDatabase db, IdentityScopeType type, Map>, DaoConfig> 42 | daoConfigMap) { 43 | super(db); 44 | 45 | simpleAddressItemDaoConfig = daoConfigMap.get(SimpleAddressItemDao.class).clone(); 46 | simpleAddressItemDaoConfig.initIdentityScope(type); 47 | 48 | addressItemDaoConfig = daoConfigMap.get(AddressItemDao.class).clone(); 49 | addressItemDaoConfig.initIdentityScope(type); 50 | 51 | contactDaoConfig = daoConfigMap.get(ContactDao.class).clone(); 52 | contactDaoConfig.initIdentityScope(type); 53 | 54 | addressBookDaoConfig = daoConfigMap.get(AddressBookDao.class).clone(); 55 | addressBookDaoConfig.initIdentityScope(type); 56 | 57 | simpleAddressItemDao = new SimpleAddressItemDao(simpleAddressItemDaoConfig, this); 58 | addressItemDao = new AddressItemDao(addressItemDaoConfig, this); 59 | contactDao = new ContactDao(contactDaoConfig, this); 60 | addressBookDao = new AddressBookDao(addressBookDaoConfig, this); 61 | 62 | registerDao(SimpleAddressItem.class, simpleAddressItemDao); 63 | registerDao(AddressItem.class, addressItemDao); 64 | registerDao(Contact.class, contactDao); 65 | registerDao(AddressBook.class, addressBookDao); 66 | } 67 | 68 | public void clear() { 69 | simpleAddressItemDaoConfig.getIdentityScope().clear(); 70 | addressItemDaoConfig.getIdentityScope().clear(); 71 | contactDaoConfig.getIdentityScope().clear(); 72 | addressBookDaoConfig.getIdentityScope().clear(); 73 | } 74 | 75 | public SimpleAddressItemDao getSimpleAddressItemDao() { 76 | return simpleAddressItemDao; 77 | } 78 | 79 | public AddressItemDao getAddressItemDao() { 80 | return addressItemDao; 81 | } 82 | 83 | public ContactDao getContactDao() { 84 | return contactDao; 85 | } 86 | 87 | public AddressBookDao getAddressBookDao() { 88 | return addressBookDao; 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/gen/SimpleAddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao.gen; 2 | 3 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit. 4 | /** 5 | * Entity mapped to table SIMPLE_ADDRESS_ITEM. 6 | */ 7 | public class SimpleAddressItem { 8 | 9 | private Long id; 10 | private String name; 11 | private String address; 12 | private String city; 13 | private String state; 14 | private Long phone; 15 | 16 | public SimpleAddressItem() { 17 | } 18 | 19 | public SimpleAddressItem(Long id) { 20 | this.id = id; 21 | } 22 | 23 | public SimpleAddressItem(Long id, String name, String address, String city, String state, Long phone) { 24 | this.id = id; 25 | this.name = name; 26 | this.address = address; 27 | this.city = city; 28 | this.state = state; 29 | this.phone = phone; 30 | } 31 | 32 | public Long getId() { 33 | return id; 34 | } 35 | 36 | public void setId(Long id) { 37 | this.id = id; 38 | } 39 | 40 | public String getName() { 41 | return name; 42 | } 43 | 44 | public void setName(String name) { 45 | this.name = name; 46 | } 47 | 48 | public String getAddress() { 49 | return address; 50 | } 51 | 52 | public void setAddress(String address) { 53 | this.address = address; 54 | } 55 | 56 | public String getCity() { 57 | return city; 58 | } 59 | 60 | public void setCity(String city) { 61 | this.city = city; 62 | } 63 | 64 | public String getState() { 65 | return state; 66 | } 67 | 68 | public void setState(String state) { 69 | this.state = state; 70 | } 71 | 72 | public Long getPhone() { 73 | return phone; 74 | } 75 | 76 | public void setPhone(Long phone) { 77 | this.phone = phone; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/greendao/gen/SimpleAddressItemDao.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.greendao.gen; 2 | 3 | import android.database.Cursor; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.database.sqlite.SQLiteStatement; 6 | 7 | import de.greenrobot.dao.AbstractDao; 8 | import de.greenrobot.dao.Property; 9 | import de.greenrobot.dao.internal.DaoConfig; 10 | 11 | import com.raizlabs.android.databasecomparison.greendao.gen.SimpleAddressItem; 12 | 13 | // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. 14 | /** 15 | * DAO for table SIMPLE_ADDRESS_ITEM. 16 | */ 17 | public class SimpleAddressItemDao extends AbstractDao { 18 | 19 | public static final String TABLENAME = "SIMPLE_ADDRESS_ITEM"; 20 | 21 | /** 22 | * Properties of entity SimpleAddressItem.
23 | * Can be used for QueryBuilder and for referencing column names. 24 | */ 25 | public static class Properties { 26 | public final static Property Id = new Property(0, Long.class, "id", true, "_id"); 27 | public final static Property Name = new Property(1, String.class, "name", false, "NAME"); 28 | public final static Property Address = new Property(2, String.class, "address", false, "ADDRESS"); 29 | public final static Property City = new Property(3, String.class, "city", false, "CITY"); 30 | public final static Property State = new Property(4, String.class, "state", false, "STATE"); 31 | public final static Property Phone = new Property(5, Long.class, "phone", false, "PHONE"); 32 | }; 33 | 34 | 35 | public SimpleAddressItemDao(DaoConfig config) { 36 | super(config); 37 | } 38 | 39 | public SimpleAddressItemDao(DaoConfig config, DaoSession daoSession) { 40 | super(config, daoSession); 41 | } 42 | 43 | /** Creates the underlying database table. */ 44 | public static void createTable(SQLiteDatabase db, boolean ifNotExists) { 45 | String constraint = ifNotExists? "IF NOT EXISTS ": ""; 46 | db.execSQL("CREATE TABLE " + constraint + "'SIMPLE_ADDRESS_ITEM' (" + // 47 | "'_id' INTEGER PRIMARY KEY ," + // 0: id 48 | "'NAME' TEXT," + // 1: name 49 | "'ADDRESS' TEXT," + // 2: address 50 | "'CITY' TEXT," + // 3: city 51 | "'STATE' TEXT," + // 4: state 52 | "'PHONE' INTEGER);"); // 5: phone 53 | } 54 | 55 | /** Drops the underlying database table. */ 56 | public static void dropTable(SQLiteDatabase db, boolean ifExists) { 57 | String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'SIMPLE_ADDRESS_ITEM'"; 58 | db.execSQL(sql); 59 | } 60 | 61 | /** @inheritdoc */ 62 | @Override 63 | protected void bindValues(SQLiteStatement stmt, SimpleAddressItem entity) { 64 | stmt.clearBindings(); 65 | 66 | Long id = entity.getId(); 67 | if (id != null) { 68 | stmt.bindLong(1, id); 69 | } 70 | 71 | String name = entity.getName(); 72 | if (name != null) { 73 | stmt.bindString(2, name); 74 | } 75 | 76 | String address = entity.getAddress(); 77 | if (address != null) { 78 | stmt.bindString(3, address); 79 | } 80 | 81 | String city = entity.getCity(); 82 | if (city != null) { 83 | stmt.bindString(4, city); 84 | } 85 | 86 | String state = entity.getState(); 87 | if (state != null) { 88 | stmt.bindString(5, state); 89 | } 90 | 91 | Long phone = entity.getPhone(); 92 | if (phone != null) { 93 | stmt.bindLong(6, phone); 94 | } 95 | } 96 | 97 | /** @inheritdoc */ 98 | @Override 99 | public Long readKey(Cursor cursor, int offset) { 100 | return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); 101 | } 102 | 103 | /** @inheritdoc */ 104 | @Override 105 | public SimpleAddressItem readEntity(Cursor cursor, int offset) { 106 | SimpleAddressItem entity = new SimpleAddressItem( // 107 | cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id 108 | cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // name 109 | cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // address 110 | cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // city 111 | cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // state 112 | cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5) // phone 113 | ); 114 | return entity; 115 | } 116 | 117 | /** @inheritdoc */ 118 | @Override 119 | public void readEntity(Cursor cursor, SimpleAddressItem entity, int offset) { 120 | entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); 121 | entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1)); 122 | entity.setAddress(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); 123 | entity.setCity(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); 124 | entity.setState(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); 125 | entity.setPhone(cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5)); 126 | } 127 | 128 | /** @inheritdoc */ 129 | @Override 130 | protected Long updateKeyAfterInsert(SimpleAddressItem entity, long rowId) { 131 | entity.setId(rowId); 132 | return rowId; 133 | } 134 | 135 | /** @inheritdoc */ 136 | @Override 137 | public Long getKey(SimpleAddressItem entity) { 138 | if(entity != null) { 139 | return entity.getId(); 140 | } else { 141 | return null; 142 | } 143 | } 144 | 145 | /** @inheritdoc */ 146 | @Override 147 | protected boolean isEntityUpdateable() { 148 | return true; 149 | } 150 | 151 | } 152 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/interfaces/IAddressBook.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.interfaces; 2 | 3 | import java.util.Collection; 4 | 5 | /** 6 | * Description: interface for address book objects 7 | */ 8 | public interface IAddressBook extends ISaveable { 9 | 10 | public void setId(long id); 11 | 12 | public void setName(String name); 13 | 14 | public void setAuthor(String author); 15 | 16 | public void setAddresses(Collection addresses); 17 | 18 | public Collection getAddresses(); 19 | 20 | public Collection getContacts(); 21 | 22 | public void setContacts(Collection contacts); 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/interfaces/IAddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.interfaces; 2 | 3 | /** 4 | * Description: interface for address book items objects in address book 5 | */ 6 | public interface IAddressItem extends ISaveable { 7 | 8 | public void setName(String name); 9 | 10 | public void setAddress(String address); 11 | 12 | public void setCity(String city); 13 | 14 | public void setState(String state); 15 | 16 | public void setPhone(long phone); 17 | 18 | public void setAddressBook(AddressBook addressBook); 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/interfaces/IContact.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.interfaces; 2 | 3 | /** 4 | * Description: interface for contact objects in address book 5 | */ 6 | public interface IContact extends ISaveable { 7 | 8 | public String getName(); 9 | 10 | public void setName(String name); 11 | 12 | public String getEmail(); 13 | 14 | public void setEmail(String email); 15 | 16 | public AddressBook getAddressBookField(); 17 | 18 | public void setAddressBook(AddressBook addressBook); 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/interfaces/ISaveable.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.interfaces; 2 | 3 | /** 4 | * Description: interface for address book objects for saving themself and their child objects 5 | */ 6 | public interface ISaveable { 7 | 8 | public void saveAll(); 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ollie/AddressBook.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ollie; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressBook; 4 | 5 | import java.util.Collection; 6 | 7 | import ollie.Model; 8 | import ollie.annotation.AutoIncrement; 9 | import ollie.annotation.Column; 10 | import ollie.annotation.PrimaryKey; 11 | import ollie.annotation.Table; 12 | import ollie.query.Select; 13 | 14 | /** 15 | * Description: 16 | */ 17 | @Table("AddressBook") 18 | public class AddressBook extends Model implements IAddressBook { 19 | @PrimaryKey 20 | @AutoIncrement 21 | public Long id; 22 | 23 | @Column("name") 24 | public String name; 25 | 26 | @Column("author") 27 | public String author; 28 | 29 | Collection addresses; 30 | 31 | Collection contacts; 32 | 33 | @Override 34 | public void setId(long id) { 35 | // not needed...we have autoincrementing keys 36 | } 37 | 38 | @Override 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | @Override 44 | public void setAuthor(String author) { 45 | this.author = author; 46 | } 47 | 48 | @Override 49 | public void setAddresses(Collection addresses) { 50 | this.addresses = addresses; 51 | } 52 | 53 | @Override 54 | public Collection getAddresses() { 55 | if (addresses == null) { 56 | addresses = Select.from(AddressItem.class).where("addressBook = ?", id).fetch(); 57 | } 58 | return addresses; 59 | } 60 | 61 | @Override 62 | public Collection getContacts() { 63 | if (contacts == null) { 64 | contacts = Select.from(Contact.class).where("addressBook = ?", id).fetch(); 65 | } 66 | return contacts; 67 | } 68 | 69 | public void setContacts(Collection contacts) { 70 | this.contacts = contacts; 71 | } 72 | 73 | @Override 74 | public void saveAll() { 75 | super.save(); 76 | for (AddressItem addressItem : addresses) { 77 | addressItem.saveAll(); 78 | } 79 | for (Contact contact : contacts) { 80 | contact.saveAll(); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ollie/AddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ollie; 2 | 3 | 4 | import ollie.annotation.Column; 5 | import ollie.annotation.Table; 6 | 7 | /** 8 | * Description: 9 | */ 10 | @Table("AddressItem") 11 | public class AddressItem extends SimpleAddressItem 12 | { 13 | 14 | @Column("addressBook") 15 | public AddressBook addressBook; 16 | 17 | @Override 18 | public void setAddressBook(AddressBook addressBook) { 19 | this.addressBook = addressBook; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ollie/Contact.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ollie; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IContact; 4 | 5 | import ollie.Model; 6 | import ollie.annotation.Column; 7 | import ollie.annotation.Table; 8 | 9 | /** 10 | * Description: 11 | */ 12 | @Table("contact") 13 | public class Contact extends Model implements IContact { 14 | 15 | @Column("name") 16 | public String name; 17 | 18 | @Column("email") 19 | public String email; 20 | 21 | @Column("addressBook") 22 | public AddressBook addressBook; 23 | 24 | @Override 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | @Override 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | @Override 35 | public String getEmail() { 36 | return email; 37 | } 38 | 39 | @Override 40 | public void setEmail(String email) { 41 | this.email = email; 42 | } 43 | 44 | @Override 45 | public AddressBook getAddressBookField() { 46 | return addressBook; 47 | } 48 | 49 | @Override 50 | public void setAddressBook(AddressBook addressBook) { 51 | this.addressBook = addressBook; 52 | } 53 | 54 | @Override 55 | public void saveAll() { 56 | super.save(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ollie/OllieTester.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ollie; 2 | 3 | import android.content.Context; 4 | 5 | import com.raizlabs.android.databasecomparison.Generator; 6 | import com.raizlabs.android.databasecomparison.Loader; 7 | import com.raizlabs.android.databasecomparison.MainActivity; 8 | import com.raizlabs.android.databasecomparison.Saver; 9 | import com.raizlabs.android.databasecomparison.events.LogTestDataEvent; 10 | import com.raizlabs.android.dbflow.runtime.TransactionManager; 11 | 12 | import java.util.Collection; 13 | 14 | import de.greenrobot.event.EventBus; 15 | import ollie.Ollie; 16 | import ollie.query.Delete; 17 | import ollie.query.Select; 18 | 19 | /** 20 | * Created by Tjones on 8/16/15. 21 | */ 22 | public class OllieTester 23 | { 24 | public static final String FRAMEWORK_NAME = "Ollie"; 25 | 26 | public static void testAddressBooks(Context context) { 27 | Delete.from(AddressItem.class).execute(); 28 | Delete.from(Contact.class).execute(); 29 | Delete.from(AddressBook.class).execute(); 30 | 31 | Collection addressBooks = Generator.createAddressBooks(AddressBook.class, Contact.class, AddressItem.class, MainActivity.ADDRESS_BOOK_COUNT); 32 | 33 | long startTime = System.currentTimeMillis(); 34 | final Collection finalAddressBooks = addressBooks; 35 | TransactionManager.transact(Ollie.getDatabase(), new Runnable() 36 | { 37 | @Override 38 | public void run() 39 | { 40 | Saver.saveAll(finalAddressBooks); 41 | } 42 | }); 43 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 44 | 45 | startTime = System.currentTimeMillis(); 46 | addressBooks = Select.from(AddressBook.class).fetch(); 47 | Loader.loadAllInnerData(addressBooks); 48 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 49 | 50 | Delete.from(AddressItem.class).execute(); 51 | Delete.from(Contact.class).execute(); 52 | Delete.from(AddressBook.class).execute(); 53 | } 54 | 55 | public static void testAddressItems(Context context) { 56 | Delete.from(SimpleAddressItem.class).execute(); 57 | 58 | final Collection ollieModels = 59 | Generator.getAddresses(SimpleAddressItem.class, MainActivity.LOOP_COUNT); 60 | 61 | long startTime = System.currentTimeMillis(); 62 | // Reuse method so we don't have to write 63 | TransactionManager.transact(Ollie.getDatabase(), new Runnable() { 64 | @Override 65 | public void run() { 66 | Saver.saveAll(ollieModels); 67 | } 68 | }); 69 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 70 | 71 | startTime = System.currentTimeMillis(); 72 | Collection activeAndroidModelLoad = 73 | Select.from(SimpleAddressItem.class).fetch(); 74 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 75 | 76 | Delete.from(SimpleAddressItem.class).execute(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ollie/SimpleAddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ollie; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 4 | 5 | import ollie.Model; 6 | import ollie.annotation.Column; 7 | import ollie.annotation.Table; 8 | 9 | /** 10 | * Description: 11 | */ 12 | @Table("SimpleAddressItem") 13 | public class SimpleAddressItem extends Model implements IAddressItem { 14 | 15 | @Column("name") 16 | public String name; 17 | 18 | @Column("address") 19 | public String address; 20 | 21 | @Column("city") 22 | public String city; 23 | 24 | @Column("state") 25 | public String state; 26 | 27 | @Column("phone") 28 | public Long phone; 29 | 30 | 31 | @Override 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | @Override 37 | public void setAddress(String address) { 38 | this.address = address; 39 | } 40 | 41 | @Override 42 | public void setCity(String city) { 43 | this.city = city; 44 | } 45 | 46 | @Override 47 | public void setState(String state) { 48 | this.state = state; 49 | } 50 | 51 | @Override 52 | public void setPhone(long phone) { 53 | this.phone = phone; 54 | } 55 | 56 | @Override 57 | public void setAddressBook(AddressBook addressBook) { 58 | 59 | } 60 | 61 | @Override 62 | public void saveAll() { 63 | super.save(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ormlite/AddressBook.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ormlite; 2 | 3 | import com.j256.ormlite.dao.Dao; 4 | import com.j256.ormlite.dao.ForeignCollection; 5 | import com.j256.ormlite.field.DatabaseField; 6 | import com.j256.ormlite.field.ForeignCollectionField; 7 | import com.j256.ormlite.table.DatabaseTable; 8 | import com.raizlabs.android.databasecomparison.interfaces.IAddressBook; 9 | 10 | import java.sql.SQLException; 11 | import java.util.Collection; 12 | 13 | 14 | /** 15 | * Description: Address Book DAO 16 | */ 17 | @DatabaseTable(tableName = "AddressBook") 18 | public class AddressBook implements IAddressBook { 19 | 20 | @DatabaseField(generatedId = true) 21 | private int id; 22 | 23 | @DatabaseField 24 | private String name; 25 | 26 | @DatabaseField 27 | private String author; 28 | 29 | @ForeignCollectionField 30 | private ForeignCollection addresses; 31 | private Collection nonDaoAddresses; 32 | 33 | @ForeignCollectionField 34 | private ForeignCollection contacts; 35 | private Collection nonDaoContacts; 36 | 37 | @Override 38 | public void setId(long id) { 39 | // not needed...we have autoincrementing primary keys 40 | } 41 | 42 | @Override 43 | public void setName(String name) { 44 | this.name = name; 45 | } 46 | 47 | @Override 48 | public void setAuthor(String author) { 49 | this.author = author; 50 | } 51 | 52 | @Override 53 | public void setAddresses(Collection addresses) { 54 | if (addresses instanceof ForeignCollection) { 55 | this.addresses = (ForeignCollection) addresses; 56 | } else { 57 | this.nonDaoAddresses = addresses; 58 | } 59 | } 60 | 61 | /** 62 | * We have to do this separate step because OrmLite doesn't automatically insert children 63 | */ 64 | public void insertNewAddresses(Dao addressBookDao, Dao addressItemDao) throws SQLException { 65 | if (this.nonDaoAddresses == null) { 66 | return; 67 | } 68 | addressBookDao.assignEmptyForeignCollection(this, "addresses"); 69 | for (AddressItem addressItem : nonDaoAddresses) { 70 | addressItem.setAddressBook(this); 71 | addressItemDao.create(addressItem); 72 | addresses.add(addressItem); 73 | } 74 | } 75 | 76 | @Override 77 | public Collection getAddresses() { 78 | return addresses; 79 | } 80 | 81 | @Override 82 | public Collection getContacts() { 83 | return contacts; 84 | } 85 | 86 | public void setContacts(Collection contacts) { 87 | if (contacts instanceof ForeignCollection) { 88 | this.contacts = (ForeignCollection) contacts; 89 | } else { 90 | this.nonDaoContacts = contacts; 91 | } 92 | } 93 | 94 | 95 | /** 96 | * We have to do this separate step because OrmLite doesn't automatically insert children 97 | */ 98 | public void insertNewContacts(Dao addressBookDao, Dao contactDao) throws SQLException { 99 | if (this.nonDaoContacts == null) { 100 | return; 101 | } 102 | addressBookDao.assignEmptyForeignCollection(this, "contacts"); 103 | for (Contact contact : nonDaoContacts) { 104 | contact.setAddressBook(this); 105 | contactDao.create(contact); 106 | contacts.add(contact); 107 | } 108 | } 109 | 110 | @Override 111 | public void saveAll() { 112 | 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ormlite/AddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ormlite; 2 | 3 | 4 | import com.j256.ormlite.field.DatabaseField; 5 | import com.j256.ormlite.table.DatabaseTable; 6 | 7 | /** 8 | * Description: Address Item DAO 9 | */ 10 | @DatabaseTable(tableName = "AddressItem") 11 | public class AddressItem extends SimpleAddressItem { 12 | 13 | @DatabaseField(foreign=true,foreignAutoRefresh=true) 14 | private AddressBook addressBook; 15 | 16 | @Override 17 | public void setAddressBook(AddressBook addressBook) { 18 | this.addressBook = addressBook; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ormlite/Contact.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ormlite; 2 | 3 | import com.j256.ormlite.field.DatabaseField; 4 | import com.j256.ormlite.table.DatabaseTable; 5 | import com.raizlabs.android.databasecomparison.interfaces.IContact; 6 | 7 | /** 8 | * Description: Contact DAO 9 | */ 10 | @DatabaseTable(tableName = "contact") 11 | public class Contact implements IContact { 12 | 13 | @DatabaseField(generatedId = true) 14 | private int id; 15 | 16 | @DatabaseField 17 | private String name; 18 | 19 | @DatabaseField 20 | private String email; 21 | 22 | @DatabaseField(foreign=true,foreignAutoRefresh=true) 23 | private AddressBook addressBook; 24 | 25 | @Override 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | @Override 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | @Override 36 | public String getEmail() { 37 | return email; 38 | } 39 | 40 | @Override 41 | public void setEmail(String email) { 42 | this.email = email; 43 | } 44 | 45 | @Override 46 | public AddressBook getAddressBookField() { 47 | return addressBook; 48 | } 49 | 50 | @Override 51 | public void setAddressBook(AddressBook addressBook) { 52 | this.addressBook = addressBook; 53 | } 54 | 55 | @Override 56 | public void saveAll() { 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ormlite/DatabaseHelper.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ormlite; 2 | 3 | import android.content.Context; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.util.Log; 6 | 7 | import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; 8 | import com.j256.ormlite.dao.Dao; 9 | import com.j256.ormlite.dao.RuntimeExceptionDao; 10 | import com.j256.ormlite.support.ConnectionSource; 11 | import com.j256.ormlite.table.TableUtils; 12 | import com.raizlabs.android.databasecomparison.R; 13 | 14 | import java.sql.SQLException; 15 | 16 | /** 17 | * Database helper class used to manage the creation and upgrading of your database. This class also usually provides 18 | * the DAOs used by the other classes. 19 | */ 20 | public class DatabaseHelper extends OrmLiteSqliteOpenHelper { 21 | 22 | // name of the database file for your application -- change to something appropriate for your app 23 | private static final String DATABASE_NAME = "ormlite.db"; 24 | // any time you make changes to your database objects, you may have to increase the database version 25 | private static final int DATABASE_VERSION = 1; 26 | 27 | // the DAO objects 28 | private Dao addressBookDao = null; 29 | private Dao addressItemDao = null; 30 | private Dao contactDao = null; 31 | private Dao simpleAddressDao = null; 32 | 33 | public DatabaseHelper(Context context) { 34 | super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config); 35 | } 36 | 37 | /** 38 | * This is called when the database is first created. Usually you should call createTable statements here to create 39 | * the tables that will store your data. 40 | */ 41 | @Override 42 | public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { 43 | try { 44 | Log.i(DatabaseHelper.class.getName(), "onCreate"); 45 | TableUtils.createTable(connectionSource, AddressBook.class); 46 | TableUtils.createTable(connectionSource, AddressItem.class); 47 | TableUtils.createTable(connectionSource, Contact.class); 48 | TableUtils.createTable(connectionSource, SimpleAddressItem.class); 49 | } catch (SQLException e) { 50 | Log.e(DatabaseHelper.class.getName(), "Can't create database", e); 51 | throw new RuntimeException(e); 52 | } 53 | } 54 | 55 | /** 56 | * This is called when your application is upgraded and it has a higher version number. This allows you to adjust 57 | * the various data to match the new version number. 58 | */ 59 | @Override 60 | public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) { 61 | try { 62 | Log.i(DatabaseHelper.class.getName(), "onUpgrade"); 63 | TableUtils.dropTable(connectionSource, AddressBook.class, true); 64 | TableUtils.dropTable(connectionSource, AddressItem.class, true); 65 | TableUtils.dropTable(connectionSource, Contact.class, true); 66 | TableUtils.dropTable(connectionSource, SimpleAddressItem.class, true); 67 | // after we drop the old databases, we create the new ones 68 | onCreate(db, connectionSource); 69 | } catch (SQLException e) { 70 | Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e); 71 | throw new RuntimeException(e); 72 | } 73 | } 74 | 75 | /** 76 | * Returns the Database Access Object (DAO) for our AddressBook class 77 | */ 78 | public Dao getAddressBookDao() throws SQLException { 79 | if (addressBookDao == null) { 80 | addressBookDao = getDao(AddressBook.class); 81 | } 82 | return addressBookDao; 83 | } 84 | 85 | /** 86 | * Returns the Database Access Object (DAO) for our AddressItem class 87 | */ 88 | public Dao getAddressItemDao() throws SQLException { 89 | if (addressItemDao == null) { 90 | addressItemDao = getDao(AddressItem.class); 91 | } 92 | return addressItemDao; 93 | } 94 | 95 | /** 96 | * Returns the Database Access Object (DAO) for our Contact class 97 | */ 98 | public Dao getContactDao() throws SQLException { 99 | if (contactDao == null) { 100 | contactDao = getDao(Contact.class); 101 | } 102 | return contactDao; 103 | } 104 | 105 | /** 106 | * Returns the Database Access Object (DAO) for our SimpleAddressItem class 107 | */ 108 | public Dao getSimpleAddressItemDao() throws SQLException { 109 | if (simpleAddressDao == null) { 110 | simpleAddressDao = getDao(SimpleAddressItem.class); 111 | } 112 | return simpleAddressDao; 113 | } 114 | 115 | /** 116 | * Close the database connections and clear any cached DAOs. 117 | */ 118 | @Override 119 | public void close() { 120 | super.close(); 121 | addressBookDao = null; 122 | addressItemDao = null; 123 | contactDao = null; 124 | simpleAddressDao = null; 125 | } 126 | } -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ormlite/OrmLiteTester.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ormlite; 2 | 3 | import android.content.Context; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.util.Log; 6 | 7 | import com.j256.ormlite.android.apptools.OpenHelperManager; 8 | import com.j256.ormlite.dao.Dao; 9 | import com.raizlabs.android.databasecomparison.Generator; 10 | import com.raizlabs.android.databasecomparison.Loader; 11 | import com.raizlabs.android.databasecomparison.MainActivity; 12 | import com.raizlabs.android.databasecomparison.events.LogTestDataEvent; 13 | 14 | import java.sql.SQLException; 15 | import java.util.Collection; 16 | 17 | import de.greenrobot.event.EventBus; 18 | 19 | /** 20 | * Runs benchmarks for OrmLite 21 | */ 22 | public class OrmLiteTester { 23 | public static final String FRAMEWORK_NAME = "OrmLite"; 24 | private static final String TAG = OrmLiteTester.class.getName(); 25 | 26 | public static void testAddressBooks(Context context) { 27 | DatabaseHelper dbHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class); 28 | 29 | try { 30 | dbHelper.getAddressBookDao().deleteBuilder().delete(); 31 | dbHelper.getAddressItemDao().deleteBuilder().delete(); 32 | dbHelper.getContactDao().deleteBuilder().delete(); 33 | } catch (SQLException e) { 34 | Log.e(TAG, "Error clearing DB", e); 35 | } 36 | 37 | Collection addressBooks = Generator.createAddressBooks( 38 | AddressBook.class, 39 | Contact.class, AddressItem.class, 40 | MainActivity.ADDRESS_BOOK_COUNT); 41 | long startTime = System.currentTimeMillis(); 42 | 43 | try { 44 | Dao addressBookDao = dbHelper.getAddressBookDao(); 45 | Dao addressItemDao = dbHelper.getAddressItemDao(); 46 | Dao contactDao = dbHelper.getContactDao(); 47 | 48 | final SQLiteDatabase db = dbHelper.getWritableDatabase(); 49 | db.beginTransaction(); 50 | try { 51 | // see http://stackoverflow.com/questions/17456321/how-to-insert-bulk-data-in-android-sqlite-database-using-ormlite-efficiently 52 | for (AddressBook addressBook : addressBooks) { 53 | // OrmLite isn't that smart, so we have to insert the root object and then we can add the children 54 | addressBookDao.create(addressBook); 55 | addressBook.insertNewAddresses(addressBookDao, addressItemDao); 56 | addressBook.insertNewContacts(addressBookDao, contactDao); 57 | } 58 | 59 | db.setTransactionSuccessful(); 60 | } finally { 61 | db.endTransaction(); 62 | } 63 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 64 | 65 | startTime = System.currentTimeMillis(); 66 | addressBooks = addressBookDao.queryForAll(); 67 | Loader.loadAllInnerData(addressBooks); 68 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 69 | 70 | // clean out DB for next run 71 | contactDao.deleteBuilder().delete(); 72 | addressItemDao.deleteBuilder().delete(); 73 | addressBookDao.deleteBuilder().delete(); 74 | } catch (SQLException e) { 75 | Log.e(TAG, "Error clearing DB", e); 76 | } 77 | 78 | OpenHelperManager.releaseHelper(); 79 | } 80 | 81 | public static void testAddressItems(Context context) { 82 | DatabaseHelper dbHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class); 83 | 84 | try { 85 | dbHelper.getSimpleAddressItemDao().deleteBuilder().delete(); 86 | } catch (SQLException e) { 87 | Log.e(TAG, "Error clearing DB", e); 88 | } 89 | 90 | Collection simpleAddressItems = Generator.getAddresses(SimpleAddressItem.class, MainActivity.LOOP_COUNT); 91 | long startTime = System.currentTimeMillis(); 92 | 93 | try { 94 | Dao simpleItemDao = dbHelper.getSimpleAddressItemDao(); 95 | final SQLiteDatabase db = dbHelper.getWritableDatabase(); 96 | db.beginTransaction(); 97 | try { 98 | // see http://stackoverflow.com/questions/17456321/how-to-insert-bulk-data-in-android-sqlite-database-using-ormlite-efficiently 99 | for (SimpleAddressItem simpleAddressItem : simpleAddressItems) { 100 | simpleItemDao.create(simpleAddressItem); 101 | } 102 | 103 | db.setTransactionSuccessful(); 104 | } finally { 105 | db.endTransaction(); 106 | } 107 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 108 | 109 | startTime = System.currentTimeMillis(); 110 | simpleAddressItems = simpleItemDao.queryForAll(); 111 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 112 | 113 | // clean out DB for next run 114 | simpleItemDao.deleteBuilder().delete(); 115 | } catch (SQLException e) { 116 | Log.e(TAG, "Error clearing DB", e); 117 | } 118 | 119 | OpenHelperManager.releaseHelper(); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/ormlite/SimpleAddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.ormlite; 2 | 3 | import com.j256.ormlite.field.DatabaseField; 4 | import com.j256.ormlite.table.DatabaseTable; 5 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 6 | 7 | /** 8 | * Description: 9 | */ 10 | @DatabaseTable(tableName = "SimpleAddressItem") 11 | public class SimpleAddressItem implements IAddressItem { 12 | 13 | @DatabaseField(generatedId = true) 14 | private int id; 15 | 16 | @DatabaseField 17 | private String name; 18 | 19 | @DatabaseField 20 | private String address; 21 | 22 | @DatabaseField 23 | private String city; 24 | 25 | @DatabaseField 26 | private String state; 27 | 28 | @DatabaseField 29 | private long phone; 30 | 31 | 32 | @Override 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | @Override 38 | public void setAddress(String address) { 39 | this.address = address; 40 | } 41 | 42 | @Override 43 | public void setCity(String city) { 44 | this.city = city; 45 | } 46 | 47 | @Override 48 | public void setState(String state) { 49 | this.state = state; 50 | } 51 | 52 | @Override 53 | public void setPhone(long phone) { 54 | this.phone = phone; 55 | } 56 | 57 | @Override 58 | public void setAddressBook(AddressBook addressBook) { 59 | 60 | } 61 | 62 | @Override 63 | public void saveAll() { 64 | 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/realm/AddressBook.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.realm; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressBook; 4 | 5 | import java.util.Collection; 6 | import java.util.List; 7 | 8 | import io.realm.RealmList; 9 | import io.realm.RealmObject; 10 | import io.realm.annotations.PrimaryKey; 11 | import io.realm.annotations.Required; 12 | 13 | /** 14 | * Realm address book object 15 | */ 16 | public class AddressBook extends RealmObject implements IAddressBook { 17 | @PrimaryKey 18 | public long id; 19 | 20 | public String name; 21 | 22 | public String author; 23 | 24 | public RealmList addresses = new RealmList<>(); 25 | 26 | public RealmList contacts = new RealmList<>(); 27 | 28 | @Override 29 | public void setId(long id) { 30 | this.id = id; 31 | } 32 | 33 | @Override 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | @Override 39 | public void setAuthor(String author) { 40 | this.author = author; 41 | } 42 | 43 | @Override 44 | public Collection getAddresses() { 45 | return addresses; 46 | } 47 | 48 | @Override 49 | public Collection getContacts() { 50 | return contacts; 51 | } 52 | 53 | @Override 54 | public void setContacts(Collection contacts) { 55 | List contactList = (List) contacts; 56 | this.contacts.addAll(contactList); 57 | } 58 | 59 | @Override 60 | public void setAddresses(Collection addresses) { 61 | List addressList = (List) addresses; 62 | this.addresses.addAll(addressList); 63 | } 64 | 65 | @Override 66 | public void saveAll() { 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/realm/AddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.realm; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 4 | 5 | import io.realm.RealmModel; 6 | import io.realm.RealmObject; 7 | import io.realm.annotations.RealmClass; 8 | 9 | /** 10 | * AddressItem for Realm 11 | */ 12 | public class AddressItem extends RealmObject implements IAddressItem { 13 | public Long id; 14 | 15 | public AddressBook addressBook; 16 | public String name; 17 | public String address; 18 | public String city; 19 | public String state; 20 | public long phone; 21 | 22 | @Override 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | @Override 28 | public void setAddress(String address) { 29 | this.address = address; 30 | } 31 | 32 | @Override 33 | public void setCity(String city) { 34 | this.city = city; 35 | } 36 | 37 | @Override 38 | public void setState(String state) { 39 | this.state = state; 40 | } 41 | 42 | @Override 43 | public void setPhone(long phone) { 44 | this.phone = phone; 45 | } 46 | 47 | @Override 48 | public void setAddressBook(AddressBook addressBook) { 49 | this.addressBook = addressBook; 50 | } 51 | 52 | @Override 53 | public void saveAll() { 54 | // TODO: ??? 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/realm/Contact.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.realm; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IContact; 4 | 5 | import io.realm.RealmObject; 6 | 7 | /** 8 | * contact for Realm 9 | */ 10 | public class Contact extends RealmObject implements IContact { 11 | 12 | private String name; 13 | 14 | private String email; 15 | 16 | private AddressBook addressBook; 17 | 18 | @Override 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | @Override 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | @Override 29 | public String getEmail() { 30 | return email; 31 | } 32 | 33 | @Override 34 | public void setEmail(String email) { 35 | this.email = email; 36 | } 37 | 38 | @Override 39 | public AddressBook getAddressBookField() { 40 | return addressBook; 41 | } 42 | 43 | @Override 44 | public void setAddressBook(AddressBook addressBook) { 45 | this.addressBook = addressBook; 46 | } 47 | 48 | @Override 49 | public void saveAll() { 50 | //TODO: ??? 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/realm/RealmTester.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.realm; 2 | 3 | import android.content.Context; 4 | 5 | import com.raizlabs.android.databasecomparison.Generator; 6 | import com.raizlabs.android.databasecomparison.MainActivity; 7 | import com.raizlabs.android.databasecomparison.events.LogTestDataEvent; 8 | 9 | import java.util.Collection; 10 | 11 | import de.greenrobot.event.EventBus; 12 | import io.realm.Realm; 13 | import io.realm.RealmList; 14 | 15 | /** 16 | * tester for Realm database 17 | */ 18 | public class RealmTester { 19 | public static final String FRAMEWORK_NAME = "Realm"; 20 | 21 | public static void testAddressBooks(Context context) { 22 | Realm realm = Realm.getDefaultInstance(); 23 | realm.beginTransaction(); 24 | realm.where(AddressItem.class).findAll().deleteAllFromRealm(); 25 | realm.where(AddressBook.class).findAll().deleteAllFromRealm(); 26 | realm.where(Contact.class).findAll().deleteAllFromRealm(); 27 | realm.commitTransaction(); 28 | 29 | Collection addressBooks = Generator.createAddressBooks(AddressBook.class, 30 | Contact.class, AddressItem.class, MainActivity.ADDRESS_BOOK_COUNT); 31 | long startTime = System.currentTimeMillis(); 32 | final Collection finalAddressBooks = addressBooks; 33 | realm.beginTransaction(); 34 | for (AddressBook book : finalAddressBooks) { 35 | realm.copyToRealmOrUpdate(book.contacts); 36 | realm.copyToRealmOrUpdate(book.addresses); 37 | } 38 | realm.copyToRealmOrUpdate(finalAddressBooks); 39 | realm.commitTransaction(); 40 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 41 | 42 | startTime = System.currentTimeMillis(); 43 | addressBooks = realm.where(AddressBook.class).findAll(); 44 | assert(50 == addressBooks.size()); 45 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 46 | 47 | realm.beginTransaction(); 48 | realm.where(AddressItem.class).findAll().deleteAllFromRealm(); 49 | realm.where(AddressBook.class).findAll().deleteAllFromRealm(); 50 | realm.where(Contact.class).findAll().deleteAllFromRealm(); 51 | realm.commitTransaction(); 52 | 53 | realm.close(); 54 | } 55 | 56 | public static void testAddressItems(Context context) { 57 | Realm realm = Realm.getDefaultInstance(); 58 | realm.beginTransaction(); 59 | realm.where(SimpleAddressItem.class).findAll().deleteAllFromRealm(); 60 | realm.commitTransaction(); 61 | 62 | Collection modelList = Generator.getAddresses(SimpleAddressItem.class, MainActivity.LOOP_COUNT); 63 | long startTime = System.currentTimeMillis(); 64 | realm.beginTransaction(); 65 | realm.copyToRealm(modelList); 66 | realm.commitTransaction(); 67 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 68 | 69 | startTime = System.currentTimeMillis(); 70 | modelList = realm.where(SimpleAddressItem.class).findAll(); 71 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 72 | 73 | realm.beginTransaction(); 74 | realm.where(SimpleAddressItem.class).findAll().deleteAllFromRealm(); 75 | realm.commitTransaction(); 76 | 77 | realm.close(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/realm/SimpleAddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.realm; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 4 | 5 | import io.realm.RealmObject; 6 | 7 | /** 8 | * Object for simple test 9 | */ 10 | public class SimpleAddressItem extends RealmObject implements IAddressItem { 11 | public String name; 12 | public String address; 13 | public String city; 14 | public String state; 15 | public long phone; 16 | 17 | @Override 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | 22 | @Override 23 | public void setAddress(String address) { 24 | this.address = address; 25 | } 26 | 27 | @Override 28 | public void setCity(String city) { 29 | this.city = city; 30 | } 31 | 32 | @Override 33 | public void setState(String state) { 34 | this.state = state; 35 | } 36 | 37 | @Override 38 | public void setPhone(long phone) { 39 | this.phone = phone; 40 | } 41 | 42 | @Override 43 | public void setAddressBook(AddressBook addressBook) { 44 | 45 | } 46 | 47 | @Override 48 | public void saveAll() { 49 | // TODO:?? 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/sprinkles/AddressBook.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.sprinkles; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressBook; 4 | 5 | import java.util.Collection; 6 | 7 | import se.emilsjolander.sprinkles.Model; 8 | import se.emilsjolander.sprinkles.Query; 9 | import se.emilsjolander.sprinkles.annotations.AutoIncrement; 10 | import se.emilsjolander.sprinkles.annotations.Column; 11 | import se.emilsjolander.sprinkles.annotations.Key; 12 | import se.emilsjolander.sprinkles.annotations.Table; 13 | 14 | /** 15 | * Description: 16 | */ 17 | @Table("AddressBook") 18 | public class AddressBook extends Model implements IAddressBook{ 19 | 20 | @Column("id") 21 | @AutoIncrement 22 | @Key 23 | private long id; 24 | 25 | @Column("name") 26 | private String name; 27 | 28 | @Column("author") 29 | private String author; 30 | 31 | Collection addresses; 32 | 33 | Collection contacts; 34 | 35 | @Override 36 | public void setId(long id) { 37 | // not needed...we have autoincrementing primary keys 38 | } 39 | 40 | @Override 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | @Override 46 | public void setAuthor(String author) { 47 | this.author = author; 48 | } 49 | 50 | @Override 51 | public void setAddresses(Collection addresses) { 52 | this.addresses = addresses; 53 | } 54 | 55 | @Override 56 | public Collection getAddresses() { 57 | if (addresses == null) { 58 | addresses = Query.many(SimpleAddressItem.class, "addressBook = ?", 59 | String.valueOf(id)).get().asList(); 60 | } 61 | return addresses; 62 | } 63 | 64 | @Override 65 | public Collection getContacts() { 66 | if (contacts == null) { 67 | contacts = Query.many(Contact.class, "addressBook = ?", 68 | String.valueOf(id)).get().asList(); 69 | } 70 | return contacts; 71 | } 72 | 73 | public long getId() { 74 | return id; 75 | } 76 | 77 | @Override 78 | public void setContacts(Collection contacts) { 79 | this.contacts = contacts; 80 | } 81 | 82 | @Override 83 | public void saveAll() { 84 | super.save(); 85 | for (SimpleAddressItem addressItem : addresses) { 86 | addressItem.saveAll(); 87 | } 88 | for (Contact contact : contacts) { 89 | contact.saveAll(); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/sprinkles/AddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.sprinkles; 2 | 3 | import se.emilsjolander.sprinkles.annotations.Column; 4 | import se.emilsjolander.sprinkles.annotations.Table; 5 | 6 | /** 7 | * Description: 8 | */ 9 | @Table("AddressItem") 10 | public class AddressItem extends SimpleAddressItem { 11 | 12 | @Column("addressBook") 13 | private long addressBook_id; 14 | 15 | @Override 16 | public void setAddressBook(AddressBook addressBook) { 17 | this.addressBook_id = addressBook.getId(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/sprinkles/Contact.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.sprinkles; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IContact; 4 | 5 | import se.emilsjolander.sprinkles.Model; 6 | import se.emilsjolander.sprinkles.Query; 7 | import se.emilsjolander.sprinkles.annotations.AutoIncrement; 8 | import se.emilsjolander.sprinkles.annotations.Column; 9 | import se.emilsjolander.sprinkles.annotations.Key; 10 | import se.emilsjolander.sprinkles.annotations.Table; 11 | 12 | /** 13 | * Description: 14 | */ 15 | @Table("contact") 16 | public class Contact extends Model implements IContact{ 17 | 18 | @Column("id") 19 | @AutoIncrement 20 | @Key 21 | private long id; 22 | 23 | @Column("name") 24 | private String name; 25 | 26 | @Column("email") 27 | private String email; 28 | 29 | @Column("addressBook") 30 | private long addressBook_id; 31 | 32 | @Override 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | @Override 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | @Override 43 | public String getEmail() { 44 | return email; 45 | } 46 | 47 | @Override 48 | public void setEmail(String email) { 49 | this.email = email; 50 | } 51 | 52 | @Override 53 | public AddressBook getAddressBookField() { 54 | return Query.one(AddressBook.class, "id = ?" , addressBook_id).get(); 55 | } 56 | 57 | @Override 58 | public void setAddressBook(AddressBook addressBook) { 59 | this.addressBook_id = addressBook.getId(); 60 | } 61 | 62 | @Override 63 | public void saveAll() { 64 | super.save(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/sprinkles/SimpleAddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.sprinkles; 2 | 3 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 4 | 5 | import se.emilsjolander.sprinkles.Model; 6 | import se.emilsjolander.sprinkles.annotations.AutoIncrement; 7 | import se.emilsjolander.sprinkles.annotations.Column; 8 | import se.emilsjolander.sprinkles.annotations.Key; 9 | import se.emilsjolander.sprinkles.annotations.Table; 10 | 11 | /** 12 | * Description: 13 | */ 14 | @Table("SimpleAddressItem") 15 | public class SimpleAddressItem extends Model implements IAddressItem { 16 | 17 | @Column("id") 18 | @AutoIncrement 19 | @Key 20 | private long id; 21 | 22 | @Column("name") 23 | private String name; 24 | 25 | @Column("address") 26 | private String address; 27 | 28 | @Column("city") 29 | private String city; 30 | 31 | @Column("state") 32 | private String state; 33 | 34 | @Column("phone") 35 | private long phone; 36 | 37 | @Override 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | @Override 43 | public void setAddress(String address) { 44 | this.address = address; 45 | } 46 | 47 | @Override 48 | public void setCity(String city) { 49 | this.city = city; 50 | } 51 | 52 | @Override 53 | public void setState(String state) { 54 | this.state = state; 55 | } 56 | 57 | @Override 58 | public void setPhone(long phone) { 59 | this.phone = phone; 60 | } 61 | 62 | @Override 63 | public void setAddressBook(AddressBook addressBook) { 64 | } 65 | 66 | @Override 67 | public void saveAll() { 68 | super.save(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/sprinkles/SprinklesTester.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.sprinkles; 2 | 3 | import android.content.Context; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.database.sqlite.SQLiteOpenHelper; 6 | 7 | import com.raizlabs.android.databasecomparison.Generator; 8 | import com.raizlabs.android.databasecomparison.MainActivity; 9 | import com.raizlabs.android.databasecomparison.Saver; 10 | import com.raizlabs.android.databasecomparison.events.LogTestDataEvent; 11 | 12 | import java.util.Collection; 13 | import java.util.List; 14 | 15 | import de.greenrobot.event.EventBus; 16 | import se.emilsjolander.sprinkles.ModelList; 17 | import se.emilsjolander.sprinkles.Query; 18 | import se.emilsjolander.sprinkles.Transaction; 19 | 20 | /** 21 | * Description: 22 | */ 23 | public class SprinklesTester { 24 | public static final String FRAMEWORK_NAME = "Sprinkles"; 25 | 26 | public static void testAddressItems(Context context) { 27 | SQLiteOpenHelper openHelper = new SQLiteOpenHelper(context, "sprinkles.db", null, 2) { 28 | @Override 29 | public void onCreate(SQLiteDatabase db) { 30 | } 31 | 32 | @Override 33 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 34 | 35 | } 36 | }; 37 | openHelper.getWritableDatabase().execSQL("CREATE TABLE IF NOT EXISTS AddressItem(" + 38 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 39 | "name TEXT, " + 40 | "address TEXT, " + 41 | "city TEXT, " + 42 | "state TEXT, " + 43 | "phone INTEGER, " + 44 | "addressBook INTEGER)"); 45 | deleteSprinklesTables(openHelper, "AddressItem"); 46 | 47 | Collection sprinkleModels = 48 | Generator.getAddresses(AddressItem.class, MainActivity.LOOP_COUNT); 49 | 50 | long startTime = System.currentTimeMillis(); 51 | // first copy everything into a model list so we can do a saveAll on it 52 | //NOTE: you'd think this would be faster than calling .save(trans) on each object, but it's not :-P 53 | ModelList addressItemModelList = new ModelList<>(); 54 | for (AddressItem addressItem : sprinkleModels) { 55 | addressItemModelList.add(addressItem); 56 | } 57 | // save everything in one transaction for best speed 58 | Transaction transaction = new Transaction(); 59 | try { 60 | addressItemModelList.saveAll(transaction); 61 | transaction.setSuccessful(true); 62 | } finally { 63 | transaction.finish(); 64 | } 65 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 66 | 67 | startTime = System.currentTimeMillis(); 68 | sprinkleModels = Query.all(AddressItem.class).get().asList(); 69 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 70 | 71 | deleteSprinklesTables(openHelper, "AddressItem"); 72 | } 73 | 74 | 75 | private static void deleteSprinklesTables(SQLiteOpenHelper openHelper, String...tables) { 76 | for(String table: tables) { 77 | openHelper.getWritableDatabase().delete(table, null, null); 78 | } 79 | } 80 | 81 | /** 82 | * Not currently implemented but we need values to graph 83 | * @param context main app context 84 | */ 85 | public static void testAddressBooks(Context context) { 86 | long startTime = System.currentTimeMillis(); 87 | EventBus.getDefault().post(new LogTestDataEvent(-1, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 88 | EventBus.getDefault().post(new LogTestDataEvent(-1, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/sugar/AddressBook.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.sugar; 2 | 3 | import com.orm.StringUtil; 4 | import com.orm.SugarRecord; 5 | import com.orm.dsl.Ignore; 6 | import com.raizlabs.android.databasecomparison.interfaces.IAddressBook; 7 | 8 | import java.util.Collection; 9 | 10 | /** 11 | * Description: 12 | */ 13 | public class AddressBook extends SugarRecord implements IAddressBook { 14 | private Long id; 15 | 16 | private String name; 17 | 18 | private String author; 19 | 20 | @Ignore 21 | Collection addresses; 22 | 23 | @Ignore 24 | Collection contacts; 25 | 26 | @Override 27 | public void setId(long id) { 28 | this.id = id; 29 | } 30 | 31 | @Override 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | @Override 37 | public void setAuthor(String author) { 38 | this.author = author; 39 | } 40 | 41 | @Override 42 | public void setAddresses(Collection addresses) { 43 | this.addresses = addresses; 44 | } 45 | 46 | @Override 47 | public Collection getAddresses() { 48 | if(addresses == null) { 49 | addresses = AddressItem.find(AddressItem.class, StringUtil.toSQLName("addressBook") + "= ?", 50 | String.valueOf(id)); 51 | } 52 | return addresses; 53 | } 54 | 55 | @Override 56 | public Collection getContacts() { 57 | if(contacts == null) { 58 | contacts = Contact.find(Contact.class, StringUtil.toSQLName("addressBook") + " = ?", 59 | String.valueOf(id)); 60 | } 61 | return contacts; 62 | } 63 | 64 | @Override 65 | public void setContacts(Collection contacts) { 66 | this.contacts = contacts; 67 | } 68 | 69 | @Override 70 | public void saveAll() { 71 | super.save(); 72 | for(AddressItem addressItem : addresses) { 73 | addressItem.saveAll(); 74 | } 75 | for(Contact contact: contacts) { 76 | contact.saveAll(); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/sugar/AddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.sugar; 2 | 3 | /** 4 | * Description: 5 | */ 6 | public class AddressItem extends SimpleAddressItem { 7 | 8 | private AddressBook addressBook; 9 | 10 | @Override 11 | public void setAddressBook(AddressBook addressBook) { 12 | this.addressBook = addressBook; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/sugar/Contact.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.sugar; 2 | 3 | import com.orm.SugarRecord; 4 | import com.raizlabs.android.databasecomparison.interfaces.IContact; 5 | 6 | /** 7 | * Description: 8 | */ 9 | public class Contact extends SugarRecord implements IContact { 10 | 11 | private String name; 12 | 13 | private String email; 14 | 15 | private AddressBook addressBook; 16 | 17 | @Override 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | @Override 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | @Override 28 | public String getEmail() { 29 | return email; 30 | } 31 | 32 | @Override 33 | public void setEmail(String email) { 34 | this.email = email; 35 | } 36 | 37 | @Override 38 | public AddressBook getAddressBookField() { 39 | return addressBook; 40 | } 41 | 42 | @Override 43 | public void setAddressBook(AddressBook addressBook) { 44 | this.addressBook = addressBook; 45 | } 46 | 47 | @Override 48 | public void saveAll() { 49 | super.save(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/sugar/SimpleAddressItem.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.sugar; 2 | 3 | import com.orm.SugarRecord; 4 | import com.raizlabs.android.databasecomparison.interfaces.IAddressItem; 5 | 6 | /** 7 | * Description: 8 | */ 9 | public class SimpleAddressItem extends SugarRecord implements IAddressItem { 10 | 11 | 12 | private String name; 13 | 14 | private String address; 15 | 16 | private String city; 17 | 18 | private String state; 19 | 20 | private long phone; 21 | 22 | @Override 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | @Override 28 | public void setAddress(String address) { 29 | this.address = address; 30 | } 31 | 32 | @Override 33 | public void setCity(String city) { 34 | this.city = city; 35 | } 36 | 37 | @Override 38 | public void setState(String state) { 39 | this.state = state; 40 | } 41 | 42 | @Override 43 | public void setPhone(long phone) { 44 | this.phone = phone; 45 | } 46 | 47 | @Override 48 | public void setAddressBook(AddressBook addressBook) { 49 | } 50 | 51 | @Override 52 | public void saveAll() { 53 | super.save(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/raizlabs/android/databasecomparison/sugar/SugarTester.java: -------------------------------------------------------------------------------- 1 | package com.raizlabs.android.databasecomparison.sugar; 2 | 3 | import android.content.Context; 4 | 5 | import com.raizlabs.android.databasecomparison.Generator; 6 | import com.raizlabs.android.databasecomparison.Loader; 7 | import com.raizlabs.android.databasecomparison.MainActivity; 8 | import com.raizlabs.android.databasecomparison.MainApplication; 9 | import com.raizlabs.android.databasecomparison.Saver; 10 | import com.raizlabs.android.databasecomparison.events.LogTestDataEvent; 11 | import com.raizlabs.android.dbflow.runtime.TransactionManager; 12 | 13 | import java.util.Collection; 14 | 15 | import de.greenrobot.event.EventBus; 16 | 17 | /** 18 | * Description: 19 | */ 20 | public class SugarTester { 21 | public static final String FRAMEWORK_NAME = "Sugar"; 22 | 23 | public static void testAddressBooks(Context context) { 24 | AddressItem.deleteAll(AddressItem.class); 25 | AddressBook.deleteAll(AddressBook.class); 26 | Contact.deleteAll(Contact.class); 27 | 28 | Collection addressBooks = Generator.createAddressBooks(AddressBook.class, 29 | Contact.class, AddressItem.class, MainActivity.ADDRESS_BOOK_COUNT); 30 | long startTime = System.currentTimeMillis(); 31 | final Collection finalAddressBooks = addressBooks; 32 | TransactionManager.transact(MainApplication.getSugarDatabase().getDB(), new Runnable() { 33 | @Override 34 | public void run() { 35 | Saver.saveAll(finalAddressBooks); 36 | } 37 | }); 38 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 39 | 40 | startTime = System.currentTimeMillis(); 41 | addressBooks = AddressBook.listAll(AddressBook.class); 42 | Loader.loadAllInnerData(addressBooks); 43 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 44 | 45 | AddressItem.deleteAll(AddressItem.class); 46 | AddressBook.deleteAll(AddressBook.class); 47 | Contact.deleteAll(Contact.class); 48 | } 49 | 50 | public static void testAddressItems(Context context) { 51 | SimpleAddressItem.deleteAll(SimpleAddressItem.class); 52 | 53 | Collection sugarModelList = Generator.getAddresses(SimpleAddressItem.class, MainActivity.LOOP_COUNT); 54 | long startTime = System.currentTimeMillis(); 55 | SimpleAddressItem.saveInTx(sugarModelList); 56 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.SAVE_TIME)); 57 | 58 | startTime = System.currentTimeMillis(); 59 | sugarModelList = SimpleAddressItem.listAll(SimpleAddressItem.class); 60 | EventBus.getDefault().post(new LogTestDataEvent(startTime, FRAMEWORK_NAME, MainActivity.LOAD_TIME)); 61 | 62 | SimpleAddressItem.deleteAll(SimpleAddressItem.class); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 16 | 17 |