All Projects → wangjiegulu → RapidORM

wangjiegulu / RapidORM

Licence: Apache-2.0 License
Quick solutions for Android ORM

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to RapidORM

simple-annotation-processor
Simple annotation processor example. Inspired by the idea of "How ButterKnife works?"
Stars: ✭ 54 (+125%)
Mutual labels:  annotation-processor, annotation-processing, javapoet
Kotlin-Annotation-Processor
Annotation Processor Sample in Kotlin
Stars: ✭ 19 (-20.83%)
Mutual labels:  annotation-processor, annotation-processing, javapoet
AnnotationProcessorStarter
Project to set up basics of a Java annotation processor
Stars: ✭ 19 (-20.83%)
Mutual labels:  apt, annotation-processor, annotation-processing
dagger2-ktx
Kotlin extension bridge library for Dagger2 (proof-of-concept)
Stars: ✭ 41 (+70.83%)
Mutual labels:  annotation-processor, annotation-processing
generate-kotlin-multiple-rounds
Android sample project demonstrating how to generate Kotlin code through annotation processing, and then feeding it into a second round of annotation processing.
Stars: ✭ 25 (+4.17%)
Mutual labels:  annotation-processor, annotation-processing
Placeholderview
This library provides advance views for lists and stacks. Some of the views are build on top of RecyclerView and others are written in their own. Annotations are compiled by annotation processor to generate bind classes. DOCS -->
Stars: ✭ 2,104 (+8666.67%)
Mutual labels:  annotation-processor, annotation-processing
Jackdaw
Java Annotation Processor which allows to simplify development
Stars: ✭ 306 (+1175%)
Mutual labels:  apt, annotation-processor
Jooq
jOOQ is the best way to write SQL in Java
Stars: ✭ 4,695 (+19462.5%)
Mutual labels:  sql-query, sql-builder
MethodScope
Reduce repetitive inheritance works in OOP world using @MethodScope.
Stars: ✭ 33 (+37.5%)
Mutual labels:  annotation-processor, annotation-processing
avaje-inject
Dependency injection via APT (source code generation) ala "Server side Dagger DI"
Stars: ✭ 114 (+375%)
Mutual labels:  apt, annotation-processing
AnnotationProcessing
✔️ㅤ[ARTICLE] Writing your own Annotation Processors in Android
Stars: ✭ 47 (+95.83%)
Mutual labels:  annotation-processor, annotation-processing
fit
easy storage Object on SharedPreferences
Stars: ✭ 53 (+120.83%)
Mutual labels:  apt, javapoet
piri
Piri is a lightweight annotation processing library that generates static factory methods for your Activities and Fragments.
Stars: ✭ 53 (+120.83%)
Mutual labels:  annotation-processor, annotation-processing
jeta
brooth.github.io/jeta
Stars: ✭ 21 (-12.5%)
Mutual labels:  apt, annotation-processing
easybundler
A code generator for Android Bundles
Stars: ✭ 53 (+120.83%)
Mutual labels:  javapoet
CrowdTruth-core
CrowdTruth framework for crowdsourcing ground truth for training & evaluation of AI systems
Stars: ✭ 45 (+87.5%)
Mutual labels:  annotation-processing
jtsgen
Convert Java Types to TypeScript
Stars: ✭ 34 (+41.67%)
Mutual labels:  annotation-processor
lib12
lib12 is a library of universal helpers and extensions useful in any .NET project
Stars: ✭ 30 (+25%)
Mutual labels:  sql-query
fragmenter
[Android Library] Remove Boilerplate code for initializing fragments
Stars: ✭ 62 (+158.33%)
Mutual labels:  annotation-processing
package-build
A toolset for building system packages using Docker and fpm-cookery
Stars: ✭ 36 (+50%)
Mutual labels:  apt

RapidORM

Quick solution for Android ORM

API

Android lightweight, high performance ORM framework.

About RapidORM

  • Primary key is supported with any type.
  • Non-reflective to execute SQLs.
  • Compatible with both android.database.sqlite.SQLiteDatabase and net.sqlcipher.database.SQLiteDatabase.
  • Join query NOT supported.

v2.0 blog: http://www.cnblogs.com/tiantianbyconan/p/5626716.html

v1.0 blog: http://www.cnblogs.com/tiantianbyconan/p/4748077.html

How to use

1. Compile it in build.gradle

Gadle(Check newest version)

  • rapidorm: Maven Central

  • rapidorm-api: Maven Central

  • rapidorm-compiler: Maven Central

implementation "com.github.wangjiegulu:rapidorm:x.x.x@aar"

implementation "com.github.wangjiegulu:rapidorm-api:x.x.x"

annotationProcessor "com.github.wangjiegulu:rapidorm-compiler:x.x.x"

Maven

<dependency>
        <groupId>com.github.wangjiegulu</groupId>
        <artifactId>rapidorm</artifactId>
        <version>x.x.x</version>
</dependency>

2. Create persistent class mapping to table

/**
 * Author: wangjie
 * Email: [email protected]
 * Date: 6/25/15.
 */
@Table(indices = {
        @Index(value = "birth, student", unique = true),
        @Index(value = "is_succeed", name = "INDEX_CUSTOM_NAME_IS_SUCCEED", unique = false)
})
public class Person implements Serializable {

    @Column(primaryKey = true)
    Integer id;

    @Column(primaryKey = true, name = "type_id")
    Integer typeId;

    @Column
    String name;

    @Column
    int age;

    @Column
    String address;

    @Column
    Long birth;

    @Column
    Boolean student;

    @Column(name = "is_succeed")
    boolean isSucceed;

3. Generate persistent helper class in compile time

public class Person_RORM extends TableConfig<Person> {
  /**
   * Column name: "id", field name: {@link Person#id}
   */
  public static final String ID = "id";

  /**
   * Column name: "type_id", field name: {@link Person#typeId}
   */
  public static final String TYPE_ID = "type_id";

  /**
   * Column name: "name", field name: {@link Person#name}
   */
  public static final String NAME = "name";

  /**
   * Column name: "age", field name: {@link Person#age}
   */
  public static final String AGE = "age";

  /**
   * Column name: "address", field name: {@link Person#address}
   */
  public static final String ADDRESS = "address";

  /**
   * Column name: "birth", field name: {@link Person#birth}
   */
  public static final String BIRTH = "birth";

  /**
   * Column name: "student", field name: {@link Person#student}
   */
  public static final String STUDENT = "student";

  /**
   * Column name: "is_succeed", field name: {@link Person#isSucceed}
   */
  public static final String IS_SUCCEED = "is_succeed";

  public Person_RORM() {
    super(Person.class);
  }

  @Override
  protected void parseAllConfigs() {
    tableName = "Person";
    ColumnConfig idColumnConfig = buildColumnConfig("id"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, true/*primaryKey*/, "INTEGER"/*dbType*/);
    allColumnConfigs.add(idColumnConfig);
    allFieldColumnConfigMapper.put("id"/*field name*/, idColumnConfig);
    pkColumnConfigs.add(idColumnConfig);
    ColumnConfig typeIdColumnConfig = buildColumnConfig("type_id"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, true/*primaryKey*/, "INTEGER"/*dbType*/);
    allColumnConfigs.add(typeIdColumnConfig);
    allFieldColumnConfigMapper.put("typeId"/*field name*/, typeIdColumnConfig);
    pkColumnConfigs.add(typeIdColumnConfig);
    ColumnConfig nameColumnConfig = buildColumnConfig("name"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "TEXT"/*dbType*/);
    allColumnConfigs.add(nameColumnConfig);
    allFieldColumnConfigMapper.put("name"/*field name*/, nameColumnConfig);
    noPkColumnConfigs.add(nameColumnConfig);
    ColumnConfig ageColumnConfig = buildColumnConfig("age"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "INTEGER"/*dbType*/);
    allColumnConfigs.add(ageColumnConfig);
    allFieldColumnConfigMapper.put("age"/*field name*/, ageColumnConfig);
    noPkColumnConfigs.add(ageColumnConfig);
    ColumnConfig addressColumnConfig = buildColumnConfig("address"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "TEXT"/*dbType*/);
    allColumnConfigs.add(addressColumnConfig);
    allFieldColumnConfigMapper.put("address"/*field name*/, addressColumnConfig);
    noPkColumnConfigs.add(addressColumnConfig);
    ColumnConfig birthColumnConfig = buildColumnConfig("birth"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "LONG"/*dbType*/);
    allColumnConfigs.add(birthColumnConfig);
    allFieldColumnConfigMapper.put("birth"/*field name*/, birthColumnConfig);
    noPkColumnConfigs.add(birthColumnConfig);
    ColumnConfig studentColumnConfig = buildColumnConfig("student"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "INTEGER"/*dbType*/);
    allColumnConfigs.add(studentColumnConfig);
    allFieldColumnConfigMapper.put("student"/*field name*/, studentColumnConfig);
    noPkColumnConfigs.add(studentColumnConfig);
    ColumnConfig isSucceedColumnConfig = buildColumnConfig("is_succeed"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "INTEGER"/*dbType*/);
    allColumnConfigs.add(isSucceedColumnConfig);
    allFieldColumnConfigMapper.put("isSucceed"/*field name*/, isSucceedColumnConfig);
    noPkColumnConfigs.add(isSucceedColumnConfig);
  }

  @Override
  public int bindInsertArgs(Person model, RapidORMSQLiteStatementDelegate statement, int indexOffset) {
    indexOffset++;
    Integer id = model.id;
    if (null == id) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindLong(indexOffset, id);
    }
    indexOffset++;
    Integer typeId = model.typeId;
    if (null == typeId) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindLong(indexOffset, typeId);
    }
    indexOffset++;
    String name = model.name;
    if (null == name) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindString(indexOffset, name);
    }
    indexOffset++;
    int age = model.age;
    statement.bindLong(indexOffset, age);
    indexOffset++;
    String address = model.address;
    if (null == address) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindString(indexOffset, address);
    }
    indexOffset++;
    Long birth = model.birth;
    if (null == birth) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindLong(indexOffset, birth);
    }
    indexOffset++;
    Boolean student = model.student;
    if (null == student) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindLong(indexOffset, student ? 1 : 0);
    }
    indexOffset++;
    boolean isSucceed = model.isSucceed;
    statement.bindLong(indexOffset, isSucceed ? 1 : 0);
    return indexOffset;
  }

  @Override
  public int bindUpdateArgs(Person model, RapidORMSQLiteStatementDelegate statement, int indexOffset) {
    indexOffset++;
    String name = model.name;
    if (null == name) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindString(indexOffset, name);
    }
    indexOffset++;
    int age = model.age;
    statement.bindLong(indexOffset, age);
    indexOffset++;
    String address = model.address;
    if (null == address) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindString(indexOffset, address);
    }
    indexOffset++;
    Long birth = model.birth;
    if (null == birth) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindLong(indexOffset, birth);
    }
    indexOffset++;
    Boolean student = model.student;
    if (null == student) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindLong(indexOffset, student ? 1 : 0);
    }
    indexOffset++;
    boolean isSucceed = model.isSucceed;
    statement.bindLong(indexOffset, isSucceed ? 1 : 0);
    return indexOffset;
  }

  @Override
  public int bindPkArgs(Person model, RapidORMSQLiteStatementDelegate statement, int indexOffset) {
    indexOffset++;
    Integer id = model.id;
    if (null == id) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindLong(indexOffset, id);
    }
    indexOffset++;
    Integer typeId = model.typeId;
    if (null == typeId) {
      statement.bindNull(indexOffset);
    } else {
      statement.bindLong(indexOffset, typeId);
    }
    return indexOffset;
  }

  @Override
  public Person parseFromCursor(Cursor cursor) {
    Person model = new Person();
    int index;
    index = cursor.getColumnIndex("id");
    if(-1 != index) {
      model.id = cursor.isNull(index) ? null : (cursor.getInt(index));
    }
    index = cursor.getColumnIndex("type_id");
    if(-1 != index) {
      model.typeId = cursor.isNull(index) ? null : (cursor.getInt(index));
    }
    index = cursor.getColumnIndex("name");
    if(-1 != index) {
      model.name = cursor.isNull(index) ? null : (cursor.getString(index));
    }
    index = cursor.getColumnIndex("age");
    if(-1 != index) {
      model.age = cursor.isNull(index) ? null : (cursor.getInt(index));
    }
    index = cursor.getColumnIndex("address");
    if(-1 != index) {
      model.address = cursor.isNull(index) ? null : (cursor.getString(index));
    }
    index = cursor.getColumnIndex("birth");
    if(-1 != index) {
      model.birth = cursor.isNull(index) ? null : (cursor.getLong(index));
    }
    index = cursor.getColumnIndex("student");
    if(-1 != index) {
      model.student = cursor.isNull(index) ? null : (cursor.getInt(index) == 1);
    }
    index = cursor.getColumnIndex("is_succeed");
    if(-1 != index) {
      model.isSucceed = cursor.isNull(index) ? null : (cursor.getInt(index) == 1);
    }
    return model;
  }

  @Override
  public void createTable(RapidORMSQLiteDatabaseDelegate db, boolean ifNotExists) throws Exception {
    String ifNotExistsConstraint = ifNotExists? "IF NOT EXISTS " : "";
    db.execSQL("CREATE TABLE " + ifNotExistsConstraint + "`Person` ( \n"
            + "`id` INTEGER,\n"
            + "`type_id` INTEGER,\n"
            + "`name` TEXT,\n"
            + "`age` INTEGER,\n"
            + "`address` TEXT,\n"
            + "`birth` LONG,\n"
            + "`student` INTEGER,\n"
            + "`is_succeed` INTEGER,\n"
            + " PRIMARY KEY (id, type_id));");
    db.execSQL("CREATE UNIQUE INDEX " + ifNotExistsConstraint + "INDEX_BIRTH_STUDENT ON `Person`(\"birth, student\");");
    db.execSQL("CREATE INDEX " + ifNotExistsConstraint + "INDEX_CUSTOM_NAME_IS_SUCCEED ON `Person`(\"is_succeed\");");
  }
}

4. Register persistent class

Extends RapidORMConnection and override registerTableConfigMapper(HashMap<Class, TableConfig> tableConfigMapper) method:

@Override
protected void registerTableConfigMapper(HashMap<Class, TableConfig> tableConfigMapper) {
    tableConfigMapper.put(Person.class, new Person_RORM());
    // register all table config here...
}

5. Execute SQL use Builder

QueryBuilder, UpdateBuilder, DeleteBuilder

5.1 QueryBuilder

public List<Person> findPersonsByWhere() throws Exception {
    return queryBuilder()
            .addSelectColumn(Person_RORM.ID, Person_RORM.TYPE_ID, Person_RORM.NAME,
                    Person_RORM.AGE, Person_RORM.BIRTH, Person_RORM.ADDRESS)
            .setWhere(
            	Where.and(
	                Where.like(Person_RORM.NAME, "%wangjie%"),
	                Where.lt(Person_RORM.ID, 200),
	                Where.or(
	                        Where.between(Person_RORM.AGE, 19, 39),
	                        Where.isNull(Person_RORM.ADDRESS)
	                ),
	                Where.eq(Person_RORM.TYPE_ID, 1)
        		)
            )
            .addOrder(Person_RORM.ID, false)
            .addOrder(Person_RORM.NAME, true)
            .setLimit(10)
            .query(this);
}

5.2 UpdateBuilder

public void updatePerson() throws Exception {
    updateBuilder()
        .setWhere(
            Where.and(
                Where.like(Person_RORM.NAME, "%wangjie%"),
                Where.lt(Person_RORM.ID, 200),
                Where.or(
                        Where.between(Person_RORM.AGE, 19, 39),
                        Where.isNull(Person_RORM.ADDRESS)
                ),
                Where.eq(Person_RORM.TYPE_ID, 1)
            )
        )
        .addUpdateColumn(Person_RORM.BIRTH, System.currentTimeMillis())
        .addUpdateColumn(Person_RORM.ADDRESS, "New address")
        .update(this);
}

5.3 DeleteBuilder

public void deletePerson() throws Exception {
    deleteBuilder()
        .setWhere(
            Where.and(
                Where.like(Person_RORM.NAME, "%wangjie%"),
                Where.lt(Person_RORM.ID, 200),
                Where.or(
                        Where.between(Person_RORM.AGE, 19, 39),
                        Where.isNull(Person_RORM.ADDRESS)
                ),
                Where.eq(Person_RORM.TYPE_ID, 1)
            )
        )
        .delete(this);
}

License

Copyright 2015 Wang Jie

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 blacklist and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].