package com.ttstd.dialer.db.contact;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import java.io.File;

@Database(entities = {ContactInfo.class}, version = 1, exportSchema = false)
public abstract class ContactDatabase extends RoomDatabase {
    public abstract ContactDao contactDao();

    private static volatile ContactDatabase INSTANCE;

    // 单例模式获取数据库实例
    public static ContactDatabase getDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (ContactDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            ContactDatabase.class, context.getExternalFilesDir("db") + File.separator + "contact" + File.separator + "contact_database")
//                            .allowMainThreadQueries() // 为了简化示例，允许主线程查询
                            .build();
                }
            }
        }
        return INSTANCE;
    }
}
