Git Product home page Git Product logo

microorm's Introduction

MicroOrm

A library for creating POJOs from Android Cursors and converting them back into ContentValues.

The following pattern is quite common:

private static import MyContract.SomeObjectColumns.*;

private static class SomeObject {

  private final String mSomeField;
  private final long mSomeOtherField;
  private final Long mSomeOptionalField;
  private final boolean mBooleanField;

  private SomeObject (String someValue, long someOtherValue, Long someOptionalValue, boolean booleanValue) {
    mSomeField = someValue;
    mSomeOtherField = someOtherValue;
    mSomeOptionalField = someOptionalValue;
    mBooleanField = booleanValue;
  }

  public ContentValues getContentValues() {
    ContentValues values =  new ContentValues();

    values.put(SOME_FIELD, mSomeField);
    values.put(SOME_OTHER_FIELD, mSomeOtherField);
    values.put(SOME_OPTIONAL_FIELD, mSomeOptionalField);
    values.put(SOME_BOOLEAN_FIELD, mBooleanField);

    return values;
  }

  public static SomeObject fromCursor(Cursor c) {
    String someValue = c.getString(c.getColumnIndexOrThrow(SOME_FIELD));
    long someOtherValue = c.getLong(c.getColumnIndexOrThrow(SOME_OTHER_FIELD));

    int optionalValueColumnIndex = c.getColumnIndexOrThrow(SOME_OPTIONAL_FIELD);
    someOptionalValue = c.isNull(optionalValueColumnIndex)
      ? null
      : c.getLong(optionalValueColumnIndex);

    booleanValue = c.getInt(c.getColumnIndexOrThrow(SOME_BOOLEAN_FIELD)) == 1;

    return new SomeObject(someValue, someOtherValue, someOptionalValue, booleanValue);
  }

  // getters and setters
}

Kind of meh, especially if you repeat it several times. With MicroOrm you can reduce the boilerplate to this:

private static import MyContract.SomeObjectColumns.*;

private static class SomeObject {
  @Column(SOME_FIELD)
  private String mSomeField;

  @Column(SOME_OTHER_FIELD)
  private long mSomeOtherField;

  @Column(SOME_OPTIONAL_FIELD)
  private Long mSomeOptionalField;

  @Column(SOME_BOOLEAN_FIELD)
  private boolean mBooleanField;

  // getters and setters
}

And then use MicroOrm:

MicroOrm uOrm = new MicroOrm();
SomeObject o = uOrm.fromCursor(c, SomeObject.class);
ContentValues values = uOrm.toContentValues(o);

// in case you'll iterate over the whole cursor
SomeObject o = new SomeObject();
do {
  uORM.fromCursor(c, o);
} while (c.moveToNext());

// if you need to dump the whole cursor to list
List<SomeObject> someObjects = uOrm.listFromCursor(c, SomeObject.class);

Caveats

  • Generic entities or fields are not supported.
  • Unlike gson, MicroOrm works only on explicitly annotated fields.
  • Current implementation is roughly 2-2.5 times slower than handrolled methods.

Usage

Just add the dependency to your build.gradle:

dependencies {
    compile 'org.chalup.microorm:microorm:0.8.0'
}

minSdkVersion = 10

MicroOrm is compatibile with Android 2.3 and newer.

Download

Download jar or add the dependency to your pom.xml:

<dependency>
  <groupId>org.chalup.microorm</groupId>
  <artifactId>microorm</artifactId>
  <version>0.2</version>
</dependency>

License

Copyright (C) 2013 Jerzy Chalupski

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.

microorm's People

Contributors

bartoszfilipowicz avatar chalup avatar henieek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

microorm's Issues

MicroOrm and Guava method count

Hi, i wanna say first that your library is very interesting and we really like it at my job. It is far more simple and usefule than bigger lib.
I have one suggestion though :
Microorm has a tiny number of method, which is nice (because more and more we outnumber the max number of method 65536) but has it include Guava, the number of method use by your lib rise to almost 15 000 which is a lot !

  • com.google: 13594
  • or.chalup.microorm: 97

displayed by https://github.com/mihaip/dex-method-counts

I was thinking about how hard would it be to remove Guava, in order to have a tinier library. I understand that you might not want to do that, so if I have time i'll try to fork you project to see if I can do it by myself.
Anyway, thanks for your great work !

Could not resolve dependencies for com.android.support:support-annotations:jar:22.2.1

[WARNING] The POM for com.android.support:support-annotations:jar:22.2.1 is missing, no dependency information available
I get this warning when I attempt to include this library as a dependency in a Maven project. I see that the error pertains to a Android support library (not part of the SDK either). I see that the pom.xml has that library as a dependency however, it doesn't seem like the library exists in maven central. Is this correct?

I think you should include how to add this library to Android projects and perhaps for non-Android projects (haven't looked for a guide for this). I was going to add this library to my Maven project because this will save me a lot of time in my project but wil now rebuild the project as an Android module instead.

FYI, Kotlin Anko already does this

I just came across this library from a StackOverflow link. Didn't get a chance to use it as I just found out there's an official library that does exactly the same thing, but I do wanna say my thanks anyway to this library author! :)

For those who don't already know that Kotlin's Anko library does what this library does:
https://github.com/Kotlin/anko/wiki/Anko-SQLite

It's an official library from Kotlin team, so it's always preferred :)

Have a great day!

best approach to use microorm with joins

I am wondering how microorm can be implemented to deal with a cursor from a join result having same column names.

After joining two columns my cursor looks like:

0 = "id"
1 = "name"
2 = "category_id"
3 = "id"
4 = "name"
5 = "price"

Of course microorm cannot handle something like this automatically.
My approach was to give the column names an alias.

0 = "Foo_id"
1 = "Foo_name"
2 = "Foo_category_id"
3 = "Bar_id"
4 = "Bar_name"
5 = "Bar_price"

But now i need to rename the @Column annotation, so it doesn't fit to my database structure anymore.
Any ideas?

Date type not have Type Adapter

the solutions is create DateAdapter in TypeAdapters file. It is a source

public static class DateAdapter implements TypeAdapter {
@OverRide
public Date fromCursor(Cursor c, String columnName) {
return new Date(c.getLong(c.getColumnIndexOrThrow(columnName))*1000);
}
@OverRide
public void toContentValues(ContentValues values, String columnName, Date object) {
values.put(columnName, object);
}
}

Type Timestamp not supported

Works very good, but type timestamp (Java.sql) is not supported.
For example:
@Column("created")
private Timestamp created;

And it would be nice if the column name is the same as the name of the variable that it works per default.

Differing preconditions of fromCursor() vs listFromCursor()

Before calling fromCursor() and handing it a Cursor instance, you have to call cursorInstance.moveToFirst() in order to avoid a CursorIndexOutOfBoundsException. This seems a little strange since your listFromCursor() method takes care of this itself. Not sure if this is intended? If so, could you update your documentation / examples to reflect this?

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.