import 'dart:io';

import 'package:sqflite/sqflite.dart';

class DB {
  static final DB _singleton = DB._internal();
  String dbName = "workouttest.db";
  late Database _db;

  factory DB() {
    return _singleton;
  }

  DB._internal() {
    dbName = 'workouttest.db';
  }

  Future<void> initDb() async {
    _db = await openDatabase(dbName);

    File dbFile = File('database.sql');
    String contents = await dbFile.readAsString();

    List<String> commands = contents.split(";");
    commands.forEach((sqlCommand) {
      if (!sqlCommand.startsWith("--")) {
        DB().getDB().execute(sqlCommand);
      }
    });
  }

  Future<void> closeDb() async {
    this._db.close();
  }

  Database getDB() => this._db;
}
