import 'package:path/path.dart'; import 'package:sqflite/sqflite.dart'; import '../data/word.dart'; class DatabaseManager { //prywatny konstruktor - singleton pattern aby była tylko jedna instancja bazy danych DatabaseManager._internal(); static final _instance = DatabaseManager._internal(); factory DatabaseManager() => _instance; //kod bazy danych final String _database = 'flashcards2.db'; final String _table = 'words'; final String _column1 = 'topic'; final String _column2 = 'polish '; final String _column3 = 'german'; Future initDatabase() async { final devicesPath = await getDatabasesPath(); final path = join(devicesPath, _database); return await openDatabase(path, onCreate: (db, version) { db.execute( 'CREATE TABLE $_table($_column1 TEXT, $_column2 TEXT PRIMARY KEY, $_column3 TEXT)'); }, version: 1); } Future insertWord({required Word word}) async { final db = await initDatabase(); await db.insert(_table, word.toMap(), conflictAlgorithm: ConflictAlgorithm.replace); } Future> selectWords({int? limit}) async { final db = await initDatabase(); List> maps = await db.query(_table, limit: limit, orderBy: 'RANDOM()'); return List.generate( maps.length, (index) => Word.fromMap(map: maps[index])); } Future removeWord({required Word word}) async { final db = await initDatabase(); await db.delete(_table, where: 'polish = ?', whereArgs: [word.polish]); } Future removeAllWords() async { final db = await initDatabase(); await db.delete(_table); } Future removeDatabase() async { final devicesPath = await getDatabasesPath(); final path = join(devicesPath, _database); await deleteDatabase(path); } }