MemoryORM


Source link: https://github.com/hyperoslo/memory-orm

memory-orm

This library is an ORM for Android developed in Java with Kotlin usage in mind. With Memory you will be able to use your classic POJO Java classes but also the data class from Kotlin.

Gradle

compile 'no.hyper.memoryorm:memoryorm:0.6.7' 

Usage

The following examples are written in Kotlin.

Model

The library relies on a json description of your database to build it. You need to follow this example to create yours :

{

"tables": [{

  "name": "Profile",
  "columns": [{

 "label": "id",

 "type": "text",

 "primary": true
  
}
, {

 "label": "name",

 "type": "text"
  
}
, {

 "label": "age",

 "type": "integer"
  
}
, {

 "label": "human",

 "type": "integer"
  
}
, {

 "label": "gear",

 "type": "Gear",

 "custom": true,

 "list": true
  
}
]

}
, {

  "name": "Gear",
  "columns": [{

 "label": "id",

 "type": "text",

 "primary": true
  
}
, {

 "label": "type",

 "type": "text",

 "enum": true
  
}
, {

 "label": "name",

 "type": "text"
  
}
, {

 "label": "id_Profile",

 "type": "integer",

 "foreign_key": true
  
}
]

}
, {

  "name": "Animal",
  "columns": [{

 "label": "id",

 "type": "text",

 "primary": true
  
}
, {

 "label": "name",

 "type": "text"
  
}
, {

 "label": "magic",

 "type": "integer"
  
}
, {

 "label": "id_Profile",

 "type": "integer",

 "foreign_key": true
  
}
]

}
] 
}

The file HAS TO be placed like this asset/schema/database.json. Of course, you also need to have the corresponding POJO or data class object that go with each table described in your json. The "type" parameter accept two options : "text" or "integer" based on the type supported by SQLite. So if you have a boolean parameter, the library will use 0 or 1 in the database and convert to false or true accordingly.

data class Animal(val id: String, val name: String, val magic: Boolean)  data class Gear(val id: String, val type: Type, val name: String) {

companion object {

val SOME_COMPANION_GEAR_VAL = 1
  
}

enum class Type {

SWORD{

 override fun isGolden() = false

}
,

 ARMOR{

 override fun isGolden() = true

}
;

 abstract fun isGolden() : Boolean
  
}
  
}
  data class Profile (

var id : String?,

var name : String,

var age : Int,

var human : Boolean,

var gear : MutableList<Gear>,

var animal : Animal ) {

companion object {

val SOME_COMPANION_PROFILE_VAL = 1
  
}
  
}

Note 1 : If your tables have relations, you need to specify the foreigns key in the correct table and follow this convention for its name id_NameOfTheTable.

Note 2 : An id field is not mandatory, Memory uses the ROWID field of each row to maintain the relations through the database.

Note 3 : The Serialisable interface is supported, you class/data class can implement it.

Creating and deleting tables

The first thing you need to do is instantiating the only object you need to use the library by passing the context and our database description as a String:

val memory = Memory(context) memory.deleteDatabase() // not necessary memory.createDatabase()

Save in database

Memory makes it really easy, it also supports nested oject and list :

val sword = Gear(UUID.randomUUID().toString(), Gear.Type.SWORD, "grass sword") val armor = Gear(UUID.randomUUID().toString(), Gear.Type.ARMOR, "armor of zeldron") val gears = mutableListOf(sword, armor) val animal = Animal(UUID.randomUUID().toString(), "jake", true) val profile = Profile(UUID.randomUUID().toString(), "finn", 13, true, gears, animal) memory.save(profile)

The function save returns you the rowId value of the row created by the insertion. You can also pass a list parameter, the function will return you the list of corresponding row id.

Fetch rows from a table

It's as simple as saving:

val profiles = memory.fetchAll(Profile::class.java) profiles.forEach() {

  //do some stuff... 
}

Three other methods are available : fetchFirst, fetchById and fetchByRowId

Update a row

val profile = memory.fetchFirst(Profile::class.java) profile.name = "new name"  memory.update(name)

Finally, in the case you don't know if an item does not exists in your DB, you can use the saveOrUpdate method that check if an item with the same id exists. (Your table need to declare explicitely an id field for this to work)

Proguard

The library needs to be able to read your model class names in order to save and fetch values in your database. I advice you to add this to your proguard file :

# Application classes that will be serialized/deserialized over Gson -keep class no.hyper.memoryormdemo.model.** {
 *; 
}
 -keep class <path.to.your.model.package>.** {
 *; 
}
 

Resources

Android library that allows you to bind a LinearLayout with a ListAdapter.

Generate selectors for background drawable.

OPFIab is a next step from OpenIAB. It's an Android library intended to make in-app billing integration easy while supporting multiple billing providers (Appstores).

A simple fake VuMeter (or equalizer) for Android.

A small utility to record Android device screen to an optimized GIF so you can paste it to GitHub or a similar service.

A simple formatter library which formats dates in a fuzzy form for display in apps.

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