Skip to content

calambrenet/DroidDatabase

Repository files navigation

DroidDatabase

DroidDatabase it is a ORM designed to run only on Android. It provides a simple way of working with our SQLite database.
DroidDatabase is in very early stage of development, so it must be used very carefully. They are missing a lot of work to run at 100% and to have all the characteristics of a complete ORM. (See TODO section)


How to use

To use this library you just have to download it, or clone, and import it into your Android project.
In Manifest project, you can add the following lines, are optional:

<meta-data android:name="DATABASE_NAME" android:value="community" />
<meta-data android:name="DATABASE_VERSION" android:value="1" />
<meta-data android:name="QUERY_LOG" android:value="true" />

Now we create our table to the database, it is actually a class (magic):

user.class

@table(name = "user")
public class User implements DatabaseModel{
    @field(type = "integer")
    @primary_key(type = "autoincrement")
    Integer id = null;

    @field(type = "varchar")
    @size(value = 200)
    @notnull(value = true)
    private String _id;

    @field(type = "varchar")
    @size(value = 255)
    @notnull(value = true)
    @unique(value = true)
    private String email;

    @field(type = "varchar")
    @size(value = 255)
    private String name;

    @Override
    public Integer getId() {
        return id;
    }
    @Override
    public void setId(int id) {
        this.id = id;
    }    
    public String get_id() {
        return _id;
    }
    public void set_id(String _id) {
        this._id = _id;
    }    
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }    
    public String getName() {
        return name;
    }    
    public void setName(String name) {
        this.name = name;
    }
}

At the beginning of the app, before making any calls to the database, you must register the tables and initialize DroidDatabase:

Class[] modelList = new Class[]{
    User.class,
};

try {
    DroidDatabase.Register(getContext(), modelList);
} catch (Exception | NotValidDatabaseTableException e) {
    e.printStackTrace();
}

We can now use our database!

Create a new record:

User user = new User();            
user.set_id("user21");
user.setEmail("[email protected]");
user.setName("tom tucker");

DroidDatabase db = new DroidDatabase<>(getContext(), user);
User user_saved = null;
try {
    user_saved = (User) db.save()
} catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "An error occurred while saving: " + e.getMessage());
} catch (NotNullException e) {
        e.printStackTrace();
        Log.d(TAG, "Null value in a field" + e.getMessage());
}

Update existing one:

user_saved.setName("Family Guy");

db = new DroidDatabase<>(getContext(), user_saved);
try {
    db.save();
} catch (NotNullException e) {
        e.printStackTrace();
}

Queries:

List<User> table_list = new ArrayList<>();
User table = null;

//search for an item using a filter    
table = new Database<>(getContext(), User.class).filterby("_id", "user21", Database.EQUAL).findOne();

//search for an item using several filters
Map<String, String> filter = new HashMap<>();
filter.put("_id", "user21");
filter.put("email", "[email protected]");
table = new Database<>(getContext(), User.class).filterby(filter, Database.EQUAL).findOne();

//search multiple elements 
table_list = new Database<>(getContext(), User.class).filterby("email", "[email protected]", Database.EQUAL).find();

//remove one or more elements
new Database<>(getContext(), User.class).filterby("_id", "user21", Database.EQUAL).delete();

TODO


Things missing, any volunteers?

  • Implement system versions when making modifications to the tables.
  • Relations between tables.
  • That no fault XD.
  • Better use of exceptions.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages