Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jayin/sorm
Simple and stupid ORM for SQLite on Android
https://github.com/jayin/sorm
Last synced: 10 days ago
JSON representation
Simple and stupid ORM for SQLite on Android
- Host: GitHub
- URL: https://github.com/jayin/sorm
- Owner: Jayin
- License: mit
- Created: 2014-05-07T12:45:51.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-07-05T01:20:49.000Z (over 10 years ago)
- Last Synced: 2024-04-14T20:12:29.059Z (7 months ago)
- Language: Java
- Homepage:
- Size: 1.03 MB
- Stars: 4
- Watchers: 4
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
SORM [![GitHub version](https://badge.fury.io/gh/Jayin%2FSORM.svg)](http://badge.fury.io/gh/Jayin%2FSORM) ![build](https://travis-ci.org/Jayin/SORM.svg?branch=master) ![license](http://img.shields.io/badge/license-MIT-brightgreen.svg)
====Simple and stupid ORM for Android
setup
===
add this project as you Android project library## Usage
### Create a table
the model
```java@Table
public class User extends Model {@Column
@Index //index support
private long userid;@Column
private int age;@Column
private String name;public long getUserid() {
return userid;
}public void setUserid(long userid) {
this.userid = userid;
}public int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}}
```
### update & automantily insert if not exist
```javaAdmin a1 = new Admin();
a1.setAdminName("a111");
a1.setId(1);
a1.save(getContext());
//or you can save in async way
a1.saveAsync(getContext(), new ORMcallback() {@Override
public void onFinish() {
}@Override
public void onFaild() {
}
});
```### query
```javaList admins = new Query(new Selector().from(Admin.class))
.excute(getContext());
// or query in async way
Selector selector = new Selector().from(Admin.class);
new Query(selector).excuteAsync(getContext(), new QueryCallback() {@Override
public void onFinish(Object result) {
List res = (List) result;
}
});```
### delete
```java
List admins = new Query(new Selector().from(Admin.class))
.excute(getContext());
for(Admin a: admins){
a.delete(getContext());
//or you can save in async way
a.deleteAsync(getContext(), new ORMcallback() {@Override
public void onFinish() {
}@Override
public void onFaild() {
}
});
}```
### Five Operation
* build entity
```javanew Creater().from(User.class).build()
```* insert
```java
User u = new User();
u.setAge(11);
u.setName("aaa");
String sql = new Inserter().insert(u ).build();
```* delete
```javaString sql = new Deletor()
.from(User.class)
.where("id", "=", "1")
.and().where("age", ">", "18")
.build();```
* select
```javaString sql = new Selector("id","title","content") //the result columns. select all(*) when nothing here
.from(Database.class) //table
.distinct() // all() or distinct()
.where("showFlag","=", 1 + "") //`where` expression
.and().where("version", "!=", "0")
.groupBy("id","title") //more than one
.orderBy("id") //ablt to more than one
.limit(10)
.offset(10)
.build();```
* update
```javaUser u = new User();
u.setAge(11);
u.setName("bbb");
u.setSaveTime(12354546);
u.setId(1);
String sql = new Updater()
.update(u)
.where("id", "=", "1")
.build();
```### advance
* drop table & index
```java
//drop table
String sql = new Droper().Table().from(Animal.class).build();
//drop index
String sql = new Droper().Index().from(Animal.class).build();
//invoke sql
DBUtils.execSQL(context,sql);//why not do this:
new Droper().Index().from(cls).excute(getContext());
new Droper().Table().from(cls).excute(getContext());
```