-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdatabase.dart
More file actions
164 lines (146 loc) · 4.41 KB
/
database.dart
File metadata and controls
164 lines (146 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import 'dart:developer';
import 'package:drift/drift.dart';
import 'package:drift_dev/api/migrations_native.dart';
import 'package:drift_flutter/drift_flutter.dart';
import 'package:riverpod/riverpod.dart';
import 'package:tattoo/database/schema.dart';
import 'package:tattoo/database/views.dart';
import 'package:tattoo/models/course.dart';
import 'package:tattoo/models/ranking.dart';
import 'package:tattoo/models/score.dart';
import 'package:tattoo/models/user.dart';
export 'package:tattoo/database/actions.dart';
part 'database.g.dart';
/// Provides the singleton [AppDatabase] instance.
final databaseProvider = Provider<AppDatabase>((ref) {
final db = AppDatabase();
ref.onDispose(db.close);
return db;
});
@DriftDatabase(
tables: [
// Base tables
Users,
Students,
Semesters,
Courses,
Departments,
Teachers,
Classes,
Classrooms,
// Tables with foreign keys to base tables
CourseOfferings,
// Junction tables and dependent tables
CourseOfferingTeachers,
CourseOfferingClasses,
CourseOfferingStudents,
Schedules,
Materials,
TeacherSemesters,
TeacherOfficeHours,
Scores,
UserSemesterSummaries,
UserSemesterSummaryTutors,
UserSemesterSummaryCadreRoles,
UserSemesterRankings,
CalendarEvents,
],
views: [
CourseTableSlots,
ScoreDetails,
UserAcademicSummaries,
UserRegistrations,
],
)
class AppDatabase extends _$AppDatabase {
// After generating code, this class needs to define a `schemaVersion` getter
// and a constructor telling drift where the database should be stored.
// These are described in the getting started guide: https://drift.simonbinder.eu/setup/
AppDatabase([QueryExecutor? executor]) : super(executor ?? _openConnection());
@override
int get schemaVersion => 1;
@override
MigrationStrategy get migration => MigrationStrategy(
onUpgrade: destructiveFallback.onUpgrade,
beforeOpen: (details) async {
await customStatement('PRAGMA foreign_keys = ON');
if (details.wasCreated) {
log('Database created with schema v$schemaVersion', name: 'DB');
return;
}
try {
await validateDatabaseSchema();
log('Database schema is up to date', name: 'DB');
} on SchemaMismatch catch (_) {
log('Schema mismatch detected, recreating database', name: 'DB');
await destructiveFallback.onUpgrade(createMigrator(), 0, schemaVersion);
}
},
);
static QueryExecutor _openConnection() {
return driftDatabase(name: 'tattoo').interceptWith(_LogInterceptor());
}
}
class _LogInterceptor extends QueryInterceptor {
static final _fromPattern = RegExp(r'FROM "(\w+)"', caseSensitive: false);
static final _firstQuoted = RegExp(r'"(\w+)"');
static String _describeStatement(String statement) {
final verb = statement.split(' ').first;
final table =
_fromPattern.firstMatch(statement)?.group(1) ??
_firstQuoted.firstMatch(statement)?.group(1) ??
'?';
return '$verb $table';
}
Future<T> _run<T>(
Future<T> Function() op,
String statement,
List<Object?> args, [
String Function(T result)? describeResult,
]) async {
final result = await op();
final desc = _describeStatement(statement);
final parts = [
desc,
if (args.isNotEmpty) '${args.length} arg${args.length != 1 ? 's' : ''}',
if (describeResult != null) describeResult(result),
];
log(parts.join(' '), name: 'DB');
return result;
}
@override
Future<void> runCustom(
QueryExecutor e,
String statement,
List<Object?> args,
) => _run(() => e.runCustom(statement, args), statement, args);
@override
Future<int> runInsert(
QueryExecutor e,
String statement,
List<Object?> args,
) => _run(() => e.runInsert(statement, args), statement, args);
@override
Future<int> runUpdate(
QueryExecutor e,
String statement,
List<Object?> args,
) => _run(() => e.runUpdate(statement, args), statement, args);
@override
Future<int> runDelete(
QueryExecutor e,
String statement,
List<Object?> args,
) => _run(() => e.runDelete(statement, args), statement, args);
@override
Future<List<Map<String, Object?>>> runSelect(
QueryExecutor e,
String statement,
List<Object?> args,
) => _run(
() => e.runSelect(statement, args),
statement,
args,
(rows) => '=> ${rows.length} row${rows.length != 1 ? 's' : ''}',
);
}