56 lines
1.8 KiB
Dart
56 lines
1.8 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'dart:io';
|
|
|
|
import 'package:drift/native.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:sqlite3/sqlite3.dart';
|
|
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
|
|
|
|
part 'database.g.dart';
|
|
|
|
class Wolle extends Table {
|
|
IntColumn get id => integer().autoIncrement()();
|
|
TextColumn get manufacture => text()();
|
|
TextColumn get title => text()();
|
|
TextColumn get color => text().withLength(min: 7, max: 7)();
|
|
}
|
|
|
|
class TempRange extends Table {
|
|
IntColumn get id => integer().autoIncrement()();
|
|
RealColumn get min => real()();
|
|
RealColumn get max => real()();
|
|
IntColumn get wolle => integer().references(Wolle, #id)();
|
|
}
|
|
|
|
@DriftDatabase(tables: [Wolle, TempRange])
|
|
class AppDatabase extends _$AppDatabase {
|
|
AppDatabase() : super(_openConnection());
|
|
|
|
@override
|
|
int get schemaVersion => 1;
|
|
}
|
|
|
|
LazyDatabase _openConnection() {
|
|
// the LazyDatabase util lets us find the right location for the file async.
|
|
return LazyDatabase(() async {
|
|
// put the database file, called db.sqlite here, into the documents folder
|
|
// for your app.
|
|
final dbFolder = await getApplicationDocumentsDirectory();
|
|
final file = File(p.join(dbFolder.path, 'db.sqlite'));
|
|
|
|
// Also work around limitations on old Android versions
|
|
if (Platform.isAndroid) {
|
|
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
|
|
}
|
|
|
|
// Make sqlite3 pick a more suitable location for temporary files - the
|
|
// one from the system may be inaccessible due to sandboxing.
|
|
final cachebase = (await getTemporaryDirectory()).path;
|
|
// We can't access /tmp on Android, which sqlite3 would try by default.
|
|
// Explicitly tell it about the correct temporary directory.
|
|
sqlite3.tempDirectory = cachebase;
|
|
|
|
return NativeDatabase.createInBackground(file);
|
|
});
|
|
}
|