Giter Club home page Giter Club logo

sorm's Introduction

SORM GitHub version build license

Simple and stupid ORM for Android

setup

add this project as you Android project library

Usage

Create a table

the model

@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

	Admin 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

	List<Admin> 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<Admin> res = (List<Admin>) result;
			}
		});

delete

	List<Admin> 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
	new Creater().from(User.class).build()
  • insert
  
    User u = new User();    
    u.setAge(11);     
    u.setName("aaa");     
    String sql = new Inserter().insert(u ).build();    
  • delete
	String sql = new Deletor()
				.from(User.class)
				.where("id", "=", "1")
				.and().where("age", ">", "18")
				.build();
  • select
	String 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
	User 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
//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());  

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.