RushOrm for Android replaces the need for SQL by mapping java classes to SQL tables.
The aim is to abstract away all SQLite interaction under a very simple interface to make object storage incredibly quick to implement.
public class ExampleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Rush is initialized asynchronously to recieve a callback after it initialized
// set an InitializeListener on the config object
// All classes that extent RushObject or implement Rush must be added to the config
// this is new to v1.3.0 to fix issues introduced in android 6,
// this is far more efficient that searching for classes like in pervious versions
List<Class<? extends Rush>> classes = new ArrayList<>();
classes.add(MyRushObject1.class);
classes.add(MyRushObject2.class);
...
AndroidInitializeConfig config
= new AndroidInitializeConfig(getApplicationContext(), classes);
RushCore.initialize(config);
}
}
<application
...
<!-- Updating this will cause a database upgrade -->
<meta-data android:name="Rush_db_version" android:value="1" />
<!-- Database name -->
<meta-data android:name="Rush_db_name" android:value="rush.db" />
<!-- Setting this to true will cause a migration to happen every launch,
this is very handy during development although could cause data loss -->
<meta-data android:name="Rush_debug" android:value="false" />
<!-- Turning on logging can be done by settings this value to true -->
<meta-data android:name="Rush_log" android:value="false" />
</application>
public class ExampleObject extends RushObject {
private String stringField;
private double doubleField;
private int intField;
private long longField;
private short shortField;
private boolean booleanField;
private Date dateField;
/* Use @RushIgnore annotation on fields you do not want to be saved */
@RushIgnore
public String ignoredField;
/* Classes must include an empty constructer */
public ExampleObject(){}
}
/* Create and save */
ExampleObject object = new ExampleObject();
/* Calling save will create or update table */
/* Save synchronously */
object.save();
/* Save asynchronously */
object.save(new RushCallback() {
@Override
public void complete() {
/* Callback is on the thread objects where saved on,
not thread that called save */
}
});
public class ExampleObject implements Rush {
@Override
public void save() { RushCore.getInstance().save(this); }
@Override
public void save(RushCallback callback) { RushCore.getInstance().save(this, callback); }
@Override
public void delete() { RushCore.getInstance().delete(this); }
@Override
public void delete(RushCallback callback) { RushCore.getInstance().delete(this, callback); }
@Override
public String getId() { return RushCore.getInstance().getId(this); }
}
public class ExampleObject extends RushObject {
private ExampleObject otherObject;
/* By default all children are deleted when the parent is deleted,
there are many reasons why this might not be wanted,
to disable this add the annotation @RushDisableAutodelete */
@RushDisableAutodelete
private ExampleObject multiplyReferencedObject;
/* Lists must have @RushList annotation with classType,
listType can also be added the default is ArrayList */
@RushList(classType = ExampleObject.class)
private List<ExampleObject> otherObjects;
/* Classes must include an empty constructer */
public ExampleObject(){}
}
ExampleObject object = new ExampleObject();
/* Saving will also save all children */
object.save();
/* Get all objects */
List<ExampleObject> objects = new RushSearch().find(ExampleObject.class);
/* getId() will return object id or null if it has not been saved */
ExampleObject table = new RushSearch().whereId("1").findSingle(ExampleObject.class);
/* Get all objects with id 1 or intField greater than 5 */
List<ExampleObject> objects = new RushSearch()
.whereId("1")
.or()
.whereGreaterThan("intField", 5)
.find(ExampleObject.class);
/* Get all objects with id 1 or
stringField equal "Hello world" and intField greater than 5 */
List<ExampleObject> objects = new RushSearch()
.whereId("1")
.or()
.startGroup()
.whereEqual("stringField", "Hello world")
.and()
.whereGreaterThan("intField", 5)
.endGroup()
.find(ExampleObject.class);
/* Get all objects with id 1 or
stringField equal "Hello world" and intField greater than 5
order ascending by intField */
List<ExampleObject> objects = new RushSearch()
.whereId("1")
.or()
.startGroup()
.whereEqual("stringField", "Hello world")
.and()
.whereGreaterThan("intField", 5)
.endGroup()
.orderAsc("intField")
.find(ExampleObject.class);
RushTextFile file = new RushTextFile(getContext().getFilesDir().getAbsolutePath());
file.setText("Hello world"); /* Set text saves table and write text to file */
String text = file.getText();
RushJSONFile file = new RushJSONFile(getContext().getFilesDir().getAbsolutePath());
file.setJSON(object); /* Set JSON saves table and write text to file */
JSONObject jsonObject = file.getJSON():
RushBitmapFile file = new RushBitmapFile(getContext().getFilesDir().getAbsolutePath());
file.setImage(bitmap); /* Set image saves table and write image to file */
Bitmap bitmap = file.getBitmap();
# Add this to the 'proguard-rules.pro' file
-keep public class * implements co.uk.rushorm.core.Rush { *; }
Copyright (C) 2015 Stuart Campbell
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.