DbSync


Source link: https://github.com/claudiodegio/dbsync

DbSync

Small library for sync android SqlLite database to cloud storage (for now only GDrive)

Motivation

There is not solution to sync SqLite database between device without the of custom server application or use of paid online cloud service. My solution it's base a json file on GDrive cloud account of user with no server involved, so all data are private to user e non saved on server.

How work

The library write a file on GDrive called cloud file like:

{

"name" : "db2.db",
"formatVersion" : 1,
"schemaVersion" : 1,
"tableCount" : 2,
"tables" : [ {

  "name" : "name",
  "recordsCount" : 1,
  "records" : [ {

 "NAME" : "myname",

 "SEND_TIME" : 1487887189513,

 "CLOUD_ID" : "c2bd71ee-3f1b-4dca-b478-ebecaaa1f835"
  
}
 ]

}
, {

  "name" : "category",
  "recordsCount" : 2,
  "records" : [ {

 "NAME" : "cat1",

 "SEND_TIME" : 1488038770858,

 "CLOUD_ID" : "6f2a8bcc-f1ee-4f14-8987-9f4275205793"
  
}
, {

 "NAME" : "musica",

 "SEND_TIME" : 1487887376113,

 "CLOUD_ID" : "52a292b5-8f38-4f30-b13f-7f86bbff49a6"
  
}
 ]

}
 ] 
}

The library download the file from cloud storage and update the local database using the cloud id used to connect a record in 2 different device and and the send time use to understand is the record it's to update. The solution try to update only the record who are been update from last sync time, for performance reason don't update all database, after the sync process it generates the cloud id (uuid) and rewrite the json cloud file and upload to file storage.

Note: with GDrive you can share between more user

Your table need to have two columns for syn SEND_TIME long and CLOUD_ID TEXT unique

Features

The library support:

  • Sync of 1 o more table
  • Sync join table (updating the correct id)
  • Support more match rule (default use CLOUD_ID counted with uiid faction)
  • Support for ignore selected column
  • Support for filter on what to sync (usefull for multi account application but different sync file)

Not supported yet

  • Other cloud storage
  • Support for images and file
  • Delete elements (to solve use a state column or column whit deleted flag)

Usage

Simple example usage of library

1 Table

CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext())

.setSyncFileByDriveId(mDriveId)

.setGoogleApiClient(mGoogleApiClient)

.build();
  DBSync  dbSync = new DBSync.Builder(this.getBaseContext())

.setCloudProvider(gDriveProvider)

.setSQLiteDatabase(mDB)

.setDataBaseName("db1")

.addTable(new TableToSync.Builder("name").build())

.build();

2 Table

CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext())

.setSyncFileByDriveId(mDriveId)

.setGoogleApiClient(mGoogleApiClient)

.build();
  DBSync dbSync = new DBSync.Builder(this.getBaseContext())

.setCloudProvider(gDriveProvider)

.setSQLiteDatabase(mDb)

.setDataBaseName("db2")

.addTable(new TableToSync.Builder("name").build())

.addTable(new TableToSync.Builder("category").build())

.build();

1 Table whit filter

CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext())

.setSyncFileByDriveId(mDriveId)

.setGoogleApiClient(mGoogleApiClient)

.build();
  TableToSync tableName = new TableToSync.Builder("name")

.setFilter("FILTER = 0") // send data with apply FILTER = 0 before send

.build();
  DBSync dbSync = new DBSync.Builder(this.getBaseContext())

.setCloudProvider(gDriveProvider)

.setSQLiteDatabase(mDB)

.setDataBaseName("db3")

.addTable(tableName)

.build();

2 Table whit join

CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext())

.setSyncFileByDriveId(mDriveId)

.setGoogleApiClient(mGoogleApiClient)

.build();
  // Keep the order TableToSync tableCategory = new TableToSync.Builder("CATEGORY")

.build();
  TableToSync tableArticle = new TableToSync.Builder("ARTICLE")

.addJoinTable(tableCategory, "CATEGORY_ID")

.build();
  DBSync dbSync = new DBSync.Builder(this.getBaseContext())

.setCloudProvider(gDriveProvider)

.setSQLiteDatabase(mDB)

.setDataBaseName("db4")

.addTable(tableCategory)

.addTable(tableArticle)

.build();

Configure of gdrive service

<service android:name="com.claudiodegio.dbsync.provider.GDriveEventService" android:exported="true">
  <intent-filter>

<action android:name="com.google.android.gms.drive.events.HANDLE_EVENT"/>
  </intent-filter> </service>

How sync

To start sync process

dbSync.sync();

How Insert/Update Data

Every time you insert or update a record simple set SEND_TIME to null

Installation

Add the dependencies to your gradle file:

allprojects {

  repositories {

...

maven {
 url 'https://jitpack.io' 
}

  
}
 
}
 dependencies {

compile 'com.github.claudiodegio:dbsync:X.Y.Z' 
}

License

Copyright 2017 Claudio Degioanni

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Resources

A simple cool dismissal animation for activities.

Star DNS Changer is a DNS Changer application.

A plugin for Kotlin generate Kotlin data class code from a json string.

A simple cool text carousel for Android.

A sample app that shows how we can apply unidirectional data flow architecture on Android using Kotlin.

Kotlin-AgendaCalendarView is a calendar widget with list of events which can be dynamically filled.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes