App(3)

 

DaoSession.java

package com.copasso.cocobill.model.gen;

import java.util.Map;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.AbstractDaoSession;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.identityscope.IdentityScopeType;
import org.greenrobot.greendao.internal.DaoConfig;

import com.bill.model.bean.local.BBill;
import com.bill.model.bean.local.BPay;
import com.bill.model.bean.local.BSort;

import com.bill.model.gen.BBillDao;
import com.bill.model.gen.BPayDao;
import com.bill.model.gen.BSortDao;

public class DaoSession extends AbstractDaoSession {

    private final DaoConfig bBillDaoConfig;
    private final DaoConfig bPayDaoConfig;
    private final DaoConfig bSortDaoConfig;

    private final BBillDao bBillDao;
    private final BPayDao bPayDao;
    private final BSortDao bSortDao;

    public DaoSession(Database db, IdentityScopeType type, Mapextends AbstractDao>, DaoConfig>
            daoConfigMap) {
        super(db);

        bBillDaoConfig = daoConfigMap.get(BBillDao.class).clone();
        bBillDaoConfig.initIdentityScope(type);

        bPayDaoConfig = daoConfigMap.get(BPayDao.class).clone();
        bPayDaoConfig.initIdentityScope(type);

        bSortDaoConfig = daoConfigMap.get(BSortDao.class).clone();
        bSortDaoConfig.initIdentityScope(type);

        bBillDao = new BBillDao(bBillDaoConfig, this);
        bPayDao = new BPayDao(bPayDaoConfig, this);
        bSortDao = new BSortDao(bSortDaoConfig, this);

        registerDao(BBill.class, bBillDao);
        registerDao(BPay.class, bPayDao);
        registerDao(BSort.class, bSortDao);
    }
    
    public void clear() {
        bBillDaoConfig.clearIdentityScope();
        bPayDaoConfig.clearIdentityScope();
        bSortDaoConfig.clearIdentityScope();
    }

    public BBillDao getBBillDao() {
        return bBillDao;
    }

    public BPayDao getBPayDao() {
        return bPayDao;
    }

    public BSortDao getBSortDao() {
        return bSortDao;
    }

}

 

DaoMaster.java

package com.bill.model.gen;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import org.greenrobot.greendao.AbstractDaoMaster;
import org.greenrobot.greendao.database.StandardDatabase;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseOpenHelper;
import org.greenrobot.greendao.identityscope.IdentityScopeType;


// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
 * Master of DAO (schema version 1): knows all DAOs.
 */
public class DaoMaster extends AbstractDaoMaster {
    public static final int SCHEMA_VERSION = 1;

    /** Creates underlying database table using DAOs. */
    public static void createAllTables(Database db, boolean ifNotExists) {
        BBillDao.createTable(db, ifNotExists);
        BPayDao.createTable(db, ifNotExists);
        BSortDao.createTable(db, ifNotExists);
    }

    /** Drops underlying database table using DAOs. */
    public static void dropAllTables(Database db, boolean ifExists) {
        BBillDao.dropTable(db, ifExists);
        BPayDao.dropTable(db, ifExists);
        BSortDao.dropTable(db, ifExists);
    }

    /**
     * WARNING: Drops all table on Upgrade! Use only during development.
     * Convenience method using a {@link DevOpenHelper}.
     */
    public static DaoSession newDevSession(Context context, String name) {
        Database db = new DevOpenHelper(context, name).getWritableDb();
        DaoMaster daoMaster = new DaoMaster(db);
        return daoMaster.newSession();
    }

    public DaoMaster(SQLiteDatabase db) {
        this(new StandardDatabase(db));
    }

    public DaoMaster(Database db) {
        super(db, SCHEMA_VERSION);
        registerDaoClass(BBillDao.class);
        registerDaoClass(BPayDao.class);
        registerDaoClass(BSortDao.class);
    }

    public DaoSession newSession() {
        return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
    }

    public DaoSession newSession(IdentityScopeType type) {
        return new DaoSession(db, type, daoConfigMap);
    }

    /**
     * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} -
     */
    public static abstract class OpenHelper extends DatabaseOpenHelper {
        public OpenHelper(Context context, String name) {
            super(context, name, SCHEMA_VERSION);
        }

        public OpenHelper(Context context, String name, CursorFactory factory) {
            super(context, name, factory, SCHEMA_VERSION);
        }

        @Override
        public void onCreate(Database db) {
            Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
            createAllTables(db, false);
        }
    }

    /** WARNING: Drops all table on Upgrade! Use only during development. */
    public static class DevOpenHelper extends OpenHelper {
        public DevOpenHelper(Context context, String name) {
            super(context, name);
        }

        public DevOpenHelper(Context context, String name, CursorFactory factory) {
            super(context, name, factory);
        }

        @Override
        public void onUpgrade(Database db, int oldVersion, int newVersion) {
            Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
            dropAllTables(db, true);
            onCreate(db);
        }
    }

}

 

数据库工具类

package com.bill.model.repository;

import android.database.sqlite.SQLiteDatabase;

import com.bill.MyApplication;
import com.bill.model.gen.DaoMaster;
import com.bill.model.gen.DaoSession;

public class DaoDbHelper {
    private static final String DB_NAME = "db";

    private static volatile DaoDbHelper sInstance;
    private SQLiteDatabase mDb;
    private DaoMaster mDaoMaster;
    private DaoSession mSession;

    private DaoDbHelper(){
        //封装数据库的创建、更新、删除
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(MyApplication.getContext(),DB_NAME,null);
        //获取数据库
        mDb = openHelper.getWritableDatabase();
        //封装数据库中表的创建、更新、删除
        mDaoMaster = new DaoMaster(mDb);  //合起来就是对数据库的操作
        //对表操作的对象。
        mSession = mDaoMaster.newSession(); //可以认为是对数据的操作
    }


    public static DaoDbHelper getInstance(){
        if (sInstance == null){
            synchronized (DaoDbHelper.class){
                if (sInstance == null){
                    sInstance = new DaoDbHelper();
                }
            }
        }
        return sInstance;
    }

    public DaoSession getSession(){
        return mSession;
    }

    public SQLiteDatabase getDatabase(){
        return mDb;
    }

    public DaoSession getNewSession(){
        return mDaoMaster.newSession();
    }
}

 

你可能感兴趣的:(App(3))