SQLiteDBFactory.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.yc.database.sql;
  2. import android.content.Context;
  3. import java.util.HashMap;
  4. import com.yc.database.utils.ValueUtil;
  5. /**
  6. * <pre>
  7. * @author yangchong
  8. * email : yangchong211@163.com
  9. * time : 2017/8/6
  10. * desc : 数据库管理工厂
  11. * revise:
  12. * </pre>
  13. */
  14. public class SQLiteDBFactory {
  15. /**
  16. * 多个数据库集合对象<dbName, {@link}SQLiteDB>
  17. */
  18. private static HashMap<String, SQLiteDB> dbMap = new HashMap<String, SQLiteDB>();
  19. /**
  20. * 生成一个名为dnName的数据库,目录为默认目录(参考SQLiteDBConfig里面的目录设置)}
  21. * @param context
  22. * @param dbName 要生成的数据库名称
  23. * @return
  24. */
  25. public static SQLiteDB createSQLiteDB(Context context, String dbName) {
  26. SQLiteDBConfig confing = new SQLiteDBConfig(context);
  27. confing.setDbName(dbName);
  28. return createSQLiteDB(confing);
  29. }
  30. /**
  31. * 在默认目录下生成默认名称的数据库
  32. * @param context
  33. * @return
  34. */
  35. public static SQLiteDB createSQLiteDB(Context context) {
  36. return createSQLiteDB(new SQLiteDBConfig(context));
  37. }
  38. /**
  39. * 根据自定义配置生成数据库
  40. * @param config
  41. * @return
  42. */
  43. public static SQLiteDB createSQLiteDB(SQLiteDBConfig config) {
  44. if(config.getVersion() < 0) {
  45. config.setVersion(SQLiteDBConfig.DEFAULT_VERSION);
  46. }
  47. if(ValueUtil.isEmpty(config.getDbName())) {
  48. config.setDbName(SQLiteDBConfig.DEFAULT_DB_NAME);
  49. }
  50. if(ValueUtil.isEmpty(config.getDbDirectoryPath())) {
  51. config.setDbDirectoryPath(SQLiteDBConfig.DEFAULT_DB_DIRECTORY_PATH);
  52. }
  53. if(!dbMap.containsKey(config.getDbName())) {
  54. synchronized (SQLiteDBFactory.class) {
  55. if(!dbMap.containsKey(config.getDbName())) {
  56. dbMap.put(config.getDbName(), new SQLiteDB(config));
  57. }
  58. }
  59. }
  60. SQLiteDB db = dbMap.get(config.getDbName());
  61. if(!db.isOpen()) {
  62. db.reOpen();
  63. }
  64. return dbMap.get(config.getDbName());
  65. }
  66. }