Advertisement
Guest User

Untitled

a guest
Apr 20th, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.24 KB | None | 0 0
  1. import 'package:jira_viewer/utils/serializable.dart';
  2. import 'package:logger/logger.dart';
  3. import 'package:path/path.dart';
  4. import 'package:sqflite/sqflite.dart';
  5.  
  6. typedef T Converter<T>(Map<String, dynamic> data);
  7.  
  8. class DBProvider {
  9.   DBProvider._internal();
  10.  
  11.   static const _DATABASE_NAME = 'database.db';
  12.  
  13.   static late final DBProvider _dbProvider = DBProvider._internal();
  14.  
  15.   factory DBProvider() => _dbProvider;
  16.  
  17.   static final Logger _logger = Logger();
  18.  
  19.   late final Future<Database> _database = _init();
  20.  
  21.   static Future<Database> _init() async {
  22.     await Sqflite.setDebugModeOn(true);
  23.  
  24.     return await openDatabase(join(await getDatabasesPath(), _DATABASE_NAME),
  25.         version: 1, onCreate: (db, version) async {
  26.       _logger.d('Creating DB started');
  27.  
  28.       try {
  29.         await db.transaction((txn) async {
  30.           await txn.execute("CREATE TABLE Board ("
  31.               "id INTEGER PRIMARY KEY,"
  32.               "name TEXT,"
  33.               "type TEXT"
  34.               ")");
  35.  
  36.           await txn.execute("CREATE TABLE Sprint ("
  37.               "id INTEGER PRIMARY KEY,"
  38.               "name TEXT,"
  39.               "state TEXT,"
  40.               "startDate TEXT,"
  41.               "endDate TEXT,"
  42.               "goal TEXT,"
  43.               "boardId INTEGER"
  44.               ")");
  45.  
  46.           await txn.execute("CREATE TABLE Issue ("
  47.               "id TEXT PRIMARY KEY,"
  48.               "key TEXT,"
  49.               "type TEXT,"
  50.               "description TEXT,"
  51.               "status TEXT,"
  52.               "priority TEXT,"
  53.               "storyPoints DOUBLE,"
  54.               "timeEstimate INTEGER,"
  55.               "timeSpent INTEGER,"
  56.               "sprintId INTEGER"
  57.               ")");
  58.         });
  59.  
  60.         _logger.d('DB creating completed successfully');
  61.       } catch (e) {
  62.         _logger.e("DB creation failed, reason: $e");
  63.         throw e;
  64.       }
  65.     });
  66.   }
  67.  
  68.   Future<int> insert<T extends Serializable>(String table, T value,
  69.       {String? nullColumnHack, ConflictAlgorithm? conflictAlgorithm}) async {
  70.     final values = value.serialize();
  71.  
  72.     final database = await _database;
  73.  
  74.     final result = await database.insert(table, values);
  75.  
  76.     return result;
  77.   }
  78.  
  79.   Future<void> insertBatch<T extends Serializable>(String table, List<T> values,
  80.       {String? nullColumnHack, ConflictAlgorithm? conflictAlgorithm}) async {
  81.     final database = await _database;
  82.  
  83.     _logger.w('Batch started');
  84.  
  85.     await database.transaction((txn) async {
  86.       final batch = txn.batch();
  87.  
  88.       values.map((e) => e.serialize()).forEach((element) {
  89.         _logger.w(element);
  90.         batch.insert(table, element,
  91.             nullColumnHack: nullColumnHack,
  92.             conflictAlgorithm: conflictAlgorithm);
  93.       });
  94.  
  95.       await batch.commit(noResult: true, continueOnError: true);
  96.     });
  97.  
  98.     _logger.w('Batch completed');
  99.   }
  100.  
  101.   Future<T?> firstOrNull<T>(String table, Converter<T> converter,
  102.       {bool? distinct,
  103.       List<String>? columns,
  104.       String? where,
  105.       List<Object?>? whereArgs}) async {
  106.     final result = await _database.then((value) => value.query(table,
  107.         distinct: distinct,
  108.         columns: columns,
  109.         where: where,
  110.         whereArgs: whereArgs,
  111.         limit: 1));
  112.  
  113.     return result.isEmpty ? null : converter(result.first);
  114.   }
  115.  
  116.   Future<List<T>> getAll<T>(String table, Converter<T> converter,
  117.       {bool? distinct,
  118.       List<String>? columns,
  119.       String? where,
  120.       List<Object?>? whereArgs,
  121.       String? groupBy,
  122.       String? having,
  123.       String? orderBy,
  124.       int? limit,
  125.       int? offset}) async {
  126.     final result = await _database.then((value) => value.query(table,
  127.         distinct: distinct,
  128.         columns: columns,
  129.         where: where,
  130.         whereArgs: whereArgs,
  131.         groupBy: groupBy,
  132.         having: having,
  133.         orderBy: orderBy,
  134.         limit: limit,
  135.         offset: offset));
  136.  
  137.     return result.isEmpty ? List.empty() : result.map<T>(converter).toList();
  138.   }
  139.  
  140.   Future<int> delete(String table, {String? where, List<Object?>? whereArgs}) =>
  141.       _database
  142.           .then((value) => delete(table, where: where, whereArgs: whereArgs));
  143.  
  144.   Future<int> deleteAll(String table) =>
  145.       _database.then((value) => value.delete(table));
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement