Code Monkey home page Code Monkey logo

dbframework's Introduction

DBFramework

Lite sqlite Database framework on Android which can handle table hierarchies elegantly and efficiently. It support upgrade database seamlessly and subscribe events of table change too.

Features

1.Handle table hierarchies elegantly.
2.Support upgrade database seamlessly.
3.Support subscribe events of table change.
4.Structured, Efficient and Easy to use, save you out of a lot of redundant code.

How to use

1.Download or clone project and import sub module called database into your project. Then implement your daos which should extends from BaseDao:

public class UserDao extends BaseDao<User, Long> implements User.Columns {

     @Override
    public int addProperties(List<Property> list, int offset) {
        //Add columns here.
        list.add(new Property(ID, Property.TYPE_INTEGER).setPrimaryKey(true));
        list.add(new Property(NAME, Property.TYPE_TEXT));
        list.add(new Property(AGE, Property.TYPE_INTEGER));
        return 0;
    }

    @Override
    public User createEntity() {
        //Create entity instance.
        return new User();
    }

    @Override
    public int fillEntity(USER user, Cursor cursor, int offset) {
        //Fill entity instance with data from cursor in the order of add columns to increase efficiency.
        user.setId(cursor.getLong(offset++));
        user.setName(cursor.getString(offset++));
        user.setAge(cursor.getInt(offset++));
        return offset;
    }

    @Override
    public String getTableName() {
        //Return table name.
        return "User";
    }

    @Override
    public ContentValues convertValues(User user) {
        //Convert entity to content values for inserting or updating.
        ContentValues values = new ContentValues();
        putAutoIncrementID(values, user.getId());
        values.put(NAME, user.getName());
        values.put(AGE, user.getAge());
        return values;
    }
}

2.Implement your own DatabaseHelper extends from BaseDBHelper:

public class MyDBHelper extends BaseDBHelper {
    @Override
    public List<AbstractDao> getTables() {
	    //Return your daos here then we can create/update table for you.
        ArrayList<AbstractDao> list = new ArrayList<>();
        list.add(UserDao.getInstance());
        list.add(EmployeeDao.getInstance());
        return list;
    }
	
   ...
   
}

3.Use daos to access data.

        new MyDBHelper(this, DB_NAME, null, DB_VERSION);
        Random random = new Random(System.currentTimeMillis());
        User user = new User();
        user.setAge(random.nextInt(80));
        user.setName("Irwin[" + random.nextInt(100) + "]");
        UserDao.getInstance().insert(user);

        Employee employee = new Employee();
        employee.setAge(random.nextInt(60));
        employee.setName("Jack[" + random.nextInt(100) + "]");
        employee.setSalary(random.nextInt(50000));
        employee.setPost(random.nextInt(8));
        employee.setCode(String.valueOf(random.nextInt()));
        employee.setScore(random.nextInt(100));
        EmployeeDao.getInstance().insert(employee);

        //We provided many convenient methods for using in common database development. See {@link com.irwin.database.AbstractDao} for more information.
        User savedUser = (User) UserDao.getInstance().queryByID(1L);
        Log.i(TAG, "Saved user:\n" + savedUser);

        List<Employee> list = EmployeeDao.getInstance().queryAll();

4.We use DefaultUpgrader to upgrade the database as default. You can choose StrictUpgrader which will treat columns changes strictly, or implement your own upgrader and tell your Database helper:

 new MyDBHelper(this, DB_NAME, null, DB_VERSION).setUpgrader(YOUR CHOOSEN/IMPLEMENTATION);

5.Implement your dao as ObservableDao or Decorate a dao with ObservableDaoDecor so you can subscribe events of table change by registerObserver(IDBObserver observer), Enjoy it.


Future

Maybe we can add support for annotation-processing so we can generate daos according to entities like GreenDao.


Any advice will be appreciated:D
More information about DBFramework:http://www.cnblogs.com/oxgen/p/7161986.html
Email:[email protected]

dbframework's People

Contributors

irwin-yang avatar

Watchers

James Cloos avatar 冷&眸 avatar

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.