Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:jira_viewer/utils/serializable.dart';
- import 'package:logger/logger.dart';
- import 'package:path/path.dart';
- import 'package:sqflite/sqflite.dart';
- typedef T Converter<T>(Map<String, dynamic> data);
- class DBProvider {
- DBProvider._internal();
- static const _DATABASE_NAME = 'database.db';
- static late final DBProvider _dbProvider = DBProvider._internal();
- factory DBProvider() => _dbProvider;
- static final Logger _logger = Logger();
- late final Future<Database> _database = _init();
- static Future<Database> _init() async {
- await Sqflite.setDebugModeOn(true);
- return await openDatabase(join(await getDatabasesPath(), _DATABASE_NAME),
- version: 1, onCreate: (db, version) async {
- _logger.d('Creating DB started');
- try {
- await db.transaction((txn) async {
- await txn.execute("CREATE TABLE Board ("
- "id INTEGER PRIMARY KEY,"
- "name TEXT,"
- "type TEXT"
- ")");
- await txn.execute("CREATE TABLE Sprint ("
- "id INTEGER PRIMARY KEY,"
- "name TEXT,"
- "state TEXT,"
- "startDate TEXT,"
- "endDate TEXT,"
- "goal TEXT,"
- "boardId INTEGER"
- ")");
- await txn.execute("CREATE TABLE Issue ("
- "id TEXT PRIMARY KEY,"
- "key TEXT,"
- "type TEXT,"
- "description TEXT,"
- "status TEXT,"
- "priority TEXT,"
- "storyPoints DOUBLE,"
- "timeEstimate INTEGER,"
- "timeSpent INTEGER,"
- "sprintId INTEGER"
- ")");
- });
- _logger.d('DB creating completed successfully');
- } catch (e) {
- _logger.e("DB creation failed, reason: $e");
- throw e;
- }
- });
- }
- Future<int> insert<T extends Serializable>(String table, T value,
- {String? nullColumnHack, ConflictAlgorithm? conflictAlgorithm}) async {
- final values = value.serialize();
- final database = await _database;
- final result = await database.insert(table, values);
- return result;
- }
- Future<void> insertBatch<T extends Serializable>(String table, List<T> values,
- {String? nullColumnHack, ConflictAlgorithm? conflictAlgorithm}) async {
- final database = await _database;
- _logger.w('Batch started');
- await database.transaction((txn) async {
- final batch = txn.batch();
- values.map((e) => e.serialize()).forEach((element) {
- _logger.w(element);
- batch.insert(table, element,
- nullColumnHack: nullColumnHack,
- conflictAlgorithm: conflictAlgorithm);
- });
- await batch.commit(noResult: true, continueOnError: true);
- });
- _logger.w('Batch completed');
- }
- Future<T?> firstOrNull<T>(String table, Converter<T> converter,
- {bool? distinct,
- List<String>? columns,
- String? where,
- List<Object?>? whereArgs}) async {
- final result = await _database.then((value) => value.query(table,
- distinct: distinct,
- columns: columns,
- where: where,
- whereArgs: whereArgs,
- limit: 1));
- return result.isEmpty ? null : converter(result.first);
- }
- Future<List<T>> getAll<T>(String table, Converter<T> converter,
- {bool? distinct,
- List<String>? columns,
- String? where,
- List<Object?>? whereArgs,
- String? groupBy,
- String? having,
- String? orderBy,
- int? limit,
- int? offset}) async {
- final result = await _database.then((value) => value.query(table,
- distinct: distinct,
- columns: columns,
- where: where,
- whereArgs: whereArgs,
- groupBy: groupBy,
- having: having,
- orderBy: orderBy,
- limit: limit,
- offset: offset));
- return result.isEmpty ? List.empty() : result.map<T>(converter).toList();
- }
- Future<int> delete(String table, {String? where, List<Object?>? whereArgs}) =>
- _database
- .then((value) => delete(table, where: where, whereArgs: whereArgs));
- Future<int> deleteAll(String table) =>
- _database.then((value) => value.delete(table));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement