Line data Source code
1 : import 'dart:io';
2 :
3 : import 'package:drift/drift.dart';
4 : import 'package:drift/native.dart';
5 : import 'package:network_tools/network_tools.dart';
6 : import 'package:network_tools/src/models/drift/arp_data.dart';
7 : import 'package:path/path.dart' as path;
8 : part 'drift_database.g.dart';
9 :
10 : @DriftDatabase(tables: [ARPDrift])
11 : class AppDatabase extends _$AppDatabase {
12 : // After generating code, this class needs to define a `schemaVersion` getter
13 : // and a constructor telling drift where the database should be stored.
14 : // These are described in the getting started guide: https://drift.simonbinder.eu/setup/
15 20 : AppDatabase([QueryExecutor? executor]) : super(executor ?? _openConnection());
16 :
17 7 : @override
18 : int get schemaVersion => 2;
19 :
20 : /// Creates lookup indexes for the repository queries.
21 : ///
22 : /// Without them every ARP lookup scans the whole ARP table.
23 6 : Future<void> ensureIndexes() async {
24 6 : await customStatement(
25 : 'CREATE INDEX IF NOT EXISTS idx_arp_ip_address '
26 : 'ON a_r_p_drift (i_p_address)',
27 : );
28 : }
29 :
30 4 : static QueryExecutor _openConnection() {
31 4 : return NativeDatabase.createInBackground(
32 12 : File(path.join(dbDirectory, 'network_tools_v1')),
33 : );
34 : }
35 : }
|