Giter Site home page Giter Site logo

kailiii / dbflow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rejasupotaro/dbflow

0.0 1.0 0.0 1.43 MB

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

License: MIT License

Java 100.00%

dbflow's Introduction

Android Arsenal

DBFlow

Image

A robust, powerful, and very simple ORM android database library with annotation processing.

The library eliminates the need for writing most SQL statements, writing ContentValues for every table, converting cursors into models, and so much more.

Let DBFlow make SQL code flow like a steady stream so you can focus on your complex problem and not be hindered by repetitive code writing.

This library is based on Active Android, Schematic, Ollie, and Sprinkles, but takes the best of each while offering much more functionality and extensibility.

What sets this library apart: every feature has been unit tested to ensure functionality, baked in support for multiple databases seamlessly, powerful and fluid builder logic in expressing SQL statements, annotation processing to enable blistering speed, ModelContainer classes that enable direct to database parsing for data such as JSON, and rich interface classes that enable powerful flexibility.

Including in your project

Gradle

Using the apt plugin for gradle

dependencies {
  apt 'com.github.agrosner:DBFlow-Compiler:1.+'
  compile 'com.github.agrosner:DBFlow-Core:1.+' 
  compile 'com.github.agrosner:DBFlow:1.+'
}

Eclipse

No official support as of now, if anyone gets it working in a pull request, send it my way!

Configuration

First class you need to define is the @Database. It is recommended you store the name and version as static final fields. The database name is not required for singular databases, however it is good practice to include it here.

@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION, foreignKeysSupported = true)
public class AppDatabase {

    public static final String NAME = "App";

    public static final int VERSION = 1;
}

Second, you need to define at least one @Table class. The databaseName is only required when dealing with multiple databases. You can either implement the Model interface or extend BaseModel.

@Table(databaseName = TestDatabase.NAME)
public class TestModel1 extends BaseModel {
    @Column(columnType = Column.PRIMARY_KEY)
    public
    String name;
}

Prepackaged Databases

So you have an existing DB you wish to include in your project. Just name the database the same as the database to copy, and put it in the app/src/main/assets/ directory.

Migrations

in DBFlow migrations are separate, public classes that contain both the @Migration and Migrationinterface. If you are using multiple databases, you're required to specify it for the migration.

@Migration(version = 2, databaseName = TestDatabase.NAME)
public class Migration1 extends BaseMigration {

    @Override
    public void onPreMigrate() {
      // called before migration, instantiate any migration query here
    }

    @Override
    public void migrate(SQLiteDatabase database) {
      // call your migration query
    }

    @Override
    public void onPostMigrate() {
      // release migration resources here
    }
}

Basic Query Wrapping

The SQL language is wrapped in a nice builder notation. DBFlow generates a $Table containing static final column strings to use in your queries!

new Select().from(DeviceObject.class)
                             .where(Condition.column(DeviceObject$Table.NAME).is("Samsung-Galaxy S5"))
                             .and(Condition.column(DeviceObject$Table.CARRIER).is("T-MOBILE"))
                             .and(Condition.column(DeviceObject$Table.LOCATION).is(location);

To see more go to the full tutorial

Model Containers

Model containers are classes that imitate and use the blueprint of Model classes in order to save data such as JSON, Hashmap, or your own kind of data to the database. To create your own, extend the BaseModelContainer class or implement the ModelContainer interface. More info here

For example here is the JSONModel implementation:

public class JSONModel<ModelClass extends Model> extends BaseModelContainer<ModelClass, JSONObject> implements Model {

    public JSONModel(JSONObject jsonObject, Class<ModelClass> table) {
        super(table, jsonObject);
    }

    public JSONModel(Class<ModelClass> table) {
        super(table, new JSONObject());
    }

    @Override
    @SuppressWarnings("unchecked")
    public BaseModelContainer getInstance(Object inValue, Class<? extends Model> columnClass) {
        return new JSONModel((JSONObject) inValue, columnClass);
    }

    @Override
    public JSONObject newDataInstance() {
        return new JSONObject();
    }

    @Override
    public Object getValue(String columnName) {
        return getData().opt(columnName);
    }

    @Override
    public void put(String columnName, Object value) {
        try {
            getData().put(columnName, value);
        } catch (JSONException e) {
            FlowLog.logError(e);
        }
    }
}

And then in every Model class you wish to use this class, you need to add the annotation @ContainerAdapter. This generates the definition required to save objects correctly to the DB.

Type Converters

TypeConverter allows non-Model objects to save to the database by converting it from its Model value to its Database value. These are statically allocated accross all databases. More info here

@com.grosner.dbflow.annotation.TypeConverter
public class CalendarConverter extends TypeConverter<Long, Calendar> {

    @Override
    public Long getDBValue(Calendar model) {
        return model.getTimeInMillis();
    }

    @Override
    public Calendar getModelValue(Long data) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(data);
        return calendar;
    }
}

Model Views

ModelView are a special kind of Model that creates a database VIEW based on a special SQL statement. They must reference another Model class currently.

@ModelView(query = "SELECT * FROM TestModel2 WHERE model_order > 5", databaseName = TestDatabase.NAME)
public class TestModelView extends BaseModelView<TestModel2> {
    @Column
    long model_order;
}

dbflow's People

Contributors

agrosner avatar

Watchers

Darko avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.