android-sqlite-generator


Source link: https://github.com/Trikke/android-sqlite-generator

android-sqlite-generator

What

This compiler will generate you db class, content provider and several helper class for you from a describing file. You can set several options like package name, database name, database version, content authority and others.

Download

You can find releases here on GitHub.

How to run

java -jar <generator>.jar --in=<folder of json files> --config=<path to config json> --out=<output folder> 

Run the above command. There are only 2 parameters.

  • in : folder which contains json files that describe the database
  • out : path to put generated files. Please note that packagename will be used to generate subfolders
  • config : a json file which holds the configuration of the database. It can reside in the specified " in" folder.

Don't forget to add the content provider to the AndroidManifest.

<provider android:name="PACKAGE.CONTENT_PROVIDER_NAME" android:authorities="AUTHORITY">

Gradle configuration

On Android Studio, you can use Gradle to generate files. Just put downloaded sqlitegenerator.jar on your app/libs folder and create a new task on build.gradle:

android {

// Here are your default android configs
  // (...)

sourceSets {

main {

 java.srcDir("src-gen/main/java/db")

}

  
}
 
}
  task buildDb(type: Exec) {

  def config = "src/main/db-gen/db.json"
  def definitions = "src/main/db-gen/tables"
  def out = "src-gen/main/java/db"

commandLine 'java', '-jar', 'libs/sqlitegenerator.jar', '--in=' + definitions
\
  , '--config=' + config, '--out=' + out 
}

task cleanDb() {

  delete "src-gen/main/java/db/" 
}
  clean.dependsOn cleanDb preBuild.dependsOn buildDb

Generated Classes

By default, the following files will be generated. #####[name]DB.java

  • This is the main database. It extends SQLiteOpenHelper and contains all necessary code for maintaining the database and column names and positions.

#####[contentprovidername].java

  • This class is the content provider to use. It's your run of the mill android ContentProvider.

#####[contentprovidername]Client.java

  • The client provides easy access to CRUD operations on any described table. Below are the methods provided
- get[tablename]With[Unique] - getAll[tablename] - add[tablename] - remove[tablename]With[Unique] - removeAll[tablename] - update[tablename] - removee[tablename]With[Unique] 

#####[contentprovidername]BatchClient.java

  • The batch client works the same as above, but is used for batched ContentProviderOperation. It contains methods to easily start a batch of ContentProviderOperation and commit it with the correct authority. It contains basically the same methods as above.

Describing your database

It is up to you how you sort your json files inside of the path you pass to the generator. Tables and views are only recognized based on the fact that tables contain " fields" and views contains " selects".

A configuration can be specified in your config.json file (or however you wish to call it). It should however contain the following details

{

  "package": "your.package.name.where.generated.classes.live",
  "contentproviderName": "YourNameContentProvider",
  "authority": "your.package.name",
  "databaseName": "YourDatabaseName",
  "databaseVersion" : 1 // increment this if you make changes 
}
 

Tables

A table consists of fields and constraints (on a table-level). Each field can also have constraints (on a field-level). By default, the primary key is the " _id" field, which is used by Android's adapters. You can override this by setting a field as " autoincrement", or creating a constraint.

Consider the following example of a User table with just an id and a name.

{
  "fields": [  {

"name": "userid",
"type": "int",
"constraints": [
{

 "name": "userid",
 "definition": "default 10"

}
,
{

 "name": "idnotnull",
 "definition": "not null"

}
]  
}
,  {

"name": "username",
"type": "string"  
}
],  "constraints": [  {

"name": "userid",
"definition": "unique ( userid, username ) on conflict replace"  
}
] 
}
 

There are 2 fields specified under the " fields" array,and one constraint, which specifies that the combination of userid and username should be unique and on a conflict, is replaced. There are also constraints on the userid field, which state that the default value should be "10" and cannot be null. See this SQLite documentation on sqlite.org on more info on constraints.

Views

A view consists of selects across one or more tables, which can be grouped and ordered, and a join type can be specified.

Consider the following example of a View which combines our above user table with a table containing avatars of users, a table containing locations from users and a table which contains other photos of users.

{

 "selects":[

 {

 "select":"user.userid"

 
}
,

 {

 "select":"user.name"

 
}
,

 {

 "select":"avatar.url"

 
}
,

 {

 "select":"location.city"

 
}
,

 {

 "select":"location.country"

 
}
,

 {

 "select":"count(photo.photoid)"

 
}

 ],
 "from":[

 "user",

 "avatar",

 "location",

 "Photo"
 ],
 "on":[

 "userid",

 "userid",

 "userid"
 ],
 "group":[

 "user.userid"
 ],
 "order":[
{

"by":"basemessages.messageid",
"sort":"asc"

}

],
 "join":"inner join" 
}
 

We selected a few user details, the avatar's url, important location details and then a count on the user's photos to know how many he has. Next within " from", we defined the tables we select from. Within " on" we define the field we join on, in this case, all tables contain the userid field. You can also specify grouping and ordering by a field, and also what type of join you prefer.

###supported data types

These are just types you can use in your json schemes, internally these are converted to the ones SQlite supports. Everything else would be converted to a blob.

- Float/float - Double/double - Long/long - Integer/integer - Boolean/boolean - real - text - blob - autoincrement (special case, the field becomes an integer primary key) 

Acknowledgements

Thanks in part to Square, for their excellent Javawriter

Resources

This simple library is just a wrap on newly updated Location APIs.

This Library automatically search for permission in android manifest file and request for the same.

Also, if request is cancelled it shows a popup window with alert to grant permission. If the app can no longer request permission due to "Don't ask again" on permission it will toast a message to grant permission from settings.

All the action performed after grant or deny can be customized by overriding method shown below. Custom permission can be checked instead of all permission in android manifest file.

PrivacyStreams is an Android library for easy and privacy-friendly personal data access and processing. It offers a functional programming model for various types of personal data, including locations, photos, audios, sensors, contacts, messages, and more.

A simple library that provides utilities to use Firebase SDK in a reactive fashion.

Round Korner Layouts is an Android library create to build a layout with the round corners.

FunnyLoader is an android library which can be used for showing random messages while your app is loading something.

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