2025-12-16 11:23:09 +08:00
|
|
|
part of 'database.dart';
|
|
|
|
|
|
|
|
|
|
@DataClassName('RawScript')
|
|
|
|
|
class Scripts extends Table {
|
|
|
|
|
@override
|
|
|
|
|
String get tableName => 'scripts';
|
|
|
|
|
|
|
|
|
|
IntColumn get id => integer()();
|
|
|
|
|
|
|
|
|
|
TextColumn get label => text()();
|
|
|
|
|
|
|
|
|
|
DateTimeColumn get lastUpdateTime => dateTime()();
|
|
|
|
|
|
2026-09-25 03:56:58 +08:00
|
|
|
TextColumn get url => text().nullable()();
|
|
|
|
|
|
|
|
|
|
IntColumn get order => integer().nullable()();
|
|
|
|
|
|
2025-12-16 11:23:09 +08:00
|
|
|
@override
|
|
|
|
|
Set<Column> get primaryKey => {id};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DriftAccessor(tables: [Scripts])
|
|
|
|
|
class ScriptsDao extends DatabaseAccessor<Database> with _$ScriptsDaoMixin {
|
|
|
|
|
ScriptsDao(super.attachedDatabase);
|
|
|
|
|
|
2026-02-02 10:14:13 +08:00
|
|
|
Selectable<Script> query() {
|
2026-10-08 17:58:43 +08:00
|
|
|
return scripts.readable(
|
|
|
|
|
(row) => row.toScript(),
|
|
|
|
|
orderBy: [
|
|
|
|
|
(t) => OrderingTerm(expression: t.order, nulls: NullsOrder.last),
|
|
|
|
|
(t) => OrderingTerm.asc(t.id),
|
|
|
|
|
],
|
|
|
|
|
);
|
2026-09-25 03:56:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> putAll(Iterable<ScriptsCompanion> items) async {
|
|
|
|
|
await batch((b) async {
|
|
|
|
|
b.insertAllOnConflictUpdate(scripts, items);
|
|
|
|
|
});
|
2025-12-16 11:23:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Selectable<Script> get(int scriptId) {
|
2026-10-08 17:58:43 +08:00
|
|
|
return scripts.readable(
|
|
|
|
|
(row) => row.toScript(),
|
|
|
|
|
where: (t) => t.id.equals(scriptId),
|
|
|
|
|
);
|
2025-12-16 11:23:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-08 17:58:43 +08:00
|
|
|
Selectable<int> ids() {
|
2026-02-02 10:14:13 +08:00
|
|
|
final query = scripts.selectOnly()..addColumns([scripts.id]);
|
2026-10-08 17:58:43 +08:00
|
|
|
return query.map((row) => row.read(scripts.id)!);
|
2026-02-02 10:14:13 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-08 17:58:43 +08:00
|
|
|
Selectable<String> fileNames() => ids().map((id) => '$id.js');
|
|
|
|
|
|
2025-12-16 11:23:09 +08:00
|
|
|
Future<void> setAll(Iterable<Script> scripts) async {
|
|
|
|
|
await batch((b) async {
|
|
|
|
|
await setAllWithBatch(b, scripts);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 20:09:39 +08:00
|
|
|
void putAllWithBatch(Batch batch, Iterable<Script> scripts) {
|
|
|
|
|
batch.insertAllOnConflictUpdate(
|
|
|
|
|
this.scripts,
|
|
|
|
|
scripts.map((item) => item.toCompanion()),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-16 11:23:09 +08:00
|
|
|
Future<void> setAllWithBatch(Batch batch, Iterable<Script> scripts) async {
|
|
|
|
|
final List<ScriptsCompanion> items = [];
|
|
|
|
|
final List<int> ids = [];
|
|
|
|
|
for (final script in scripts) {
|
|
|
|
|
ids.add(script.id);
|
|
|
|
|
items.add(script.toCompanion());
|
|
|
|
|
}
|
|
|
|
|
this.scripts.setAll(batch, items, deleteFilter: (t) => t.id.isNotIn(ids));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension RawScriptExt on RawScript {
|
|
|
|
|
Script toScript() {
|
2026-09-25 03:56:58 +08:00
|
|
|
return Script(
|
|
|
|
|
id: id,
|
|
|
|
|
label: label,
|
|
|
|
|
lastUpdateTime: lastUpdateTime,
|
|
|
|
|
url: url,
|
|
|
|
|
order: order,
|
|
|
|
|
);
|
2025-12-16 11:23:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension ScriptsCompanionExt on Script {
|
2026-09-25 03:56:58 +08:00
|
|
|
ScriptsCompanion toCompanion([int? order]) {
|
2025-12-16 11:23:09 +08:00
|
|
|
return ScriptsCompanion.insert(
|
|
|
|
|
id: Value(id),
|
|
|
|
|
label: label,
|
|
|
|
|
lastUpdateTime: lastUpdateTime,
|
2026-09-25 03:56:58 +08:00
|
|
|
url: Value(url),
|
|
|
|
|
order: Value(order ?? this.order),
|
2025-12-16 11:23:09 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|