|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import '../lib/database.dart'; |
| 4 | +import 'package:path/path.dart' as path; |
| 5 | +import 'package:drift/drift.dart'; |
| 6 | + |
| 7 | +// ignore: public_member_api_docs |
| 8 | +void copyBinaryIfNecessary() { |
| 9 | + const libraryName = 'libsqlite3.so'; |
| 10 | + final systemBinary = File('/lib/$libraryName')..createSync(recursive: true); |
| 11 | + |
| 12 | + final customBinary = () { |
| 13 | + file({required bool debug}) => File(path.join(Directory.current.path, |
| 14 | + 'target', debug ? 'debug' : 'release', libraryName)); |
| 15 | + |
| 16 | + if (file(debug: false).existsSync()) { |
| 17 | + return file(debug: false); |
| 18 | + } else if (file(debug: true).existsSync()) { |
| 19 | + return file(debug: true); |
| 20 | + } else { |
| 21 | + throw Exception('No custom SQLite binary found in target directory.'); |
| 22 | + } |
| 23 | + }(); |
| 24 | + |
| 25 | + customBinary.copySync(systemBinary.path); |
| 26 | +} |
| 27 | + |
| 28 | +void main() async { |
| 29 | + copyBinaryIfNecessary(); |
| 30 | + |
| 31 | + final database = AppDatabase(); |
| 32 | + |
| 33 | + await database.transaction(() async { |
| 34 | + // Create the table if it doesn't exist |
| 35 | + final result = await database.into(database.todoItems).insert( |
| 36 | + TodoItemsCompanion.insert( |
| 37 | + title: 'todo: setup drift', |
| 38 | + content: 'We need to set up drift for our SQLite database.', |
| 39 | + ), |
| 40 | + ); |
| 41 | + |
| 42 | + await (database |
| 43 | + .update(database.todoItems) |
| 44 | + ..where((tbl) => tbl.id.equals(result))) |
| 45 | + .write(TodoItemsCompanion(content: const Value('Updated content'))); |
| 46 | + |
| 47 | + |
| 48 | + print('Inserted item with ID: $result'); |
| 49 | + }); |
| 50 | + |
| 51 | + // final allItems = await database.select(database.todoItems).get(); |
| 52 | + // for (final item in allItems) { |
| 53 | + // print('Item: ${item.title}, Content: ${item.content}'); |
| 54 | + // } |
| 55 | + |
| 56 | + // final server = await HttpServer.bind(InternetAddress.anyIPv4, 8081); |
| 57 | + |
| 58 | + // var index = 0; |
| 59 | + |
| 60 | + // server.listen((request) async { |
| 61 | + // final isEven = index % 2 == 0; |
| 62 | + |
| 63 | + // final stopwatch = Stopwatch()..start(); |
| 64 | + |
| 65 | + // final result = await Future.wait([ |
| 66 | + // db.getAll('SELECT * FROM notes WHERE is_deleted = 0'), |
| 67 | + // db.getAll( |
| 68 | + // 'SELECT * FROM notes WHERE id = ?', |
| 69 | + // ['8703e588-c847-4cb6-b250-726db8afb49a'], |
| 70 | + // ), |
| 71 | + // ]); |
| 72 | + // stopwatch.stop(); |
| 73 | + |
| 74 | + // print('Query took ${stopwatch.elapsedMilliseconds} ms'); |
| 75 | + |
| 76 | + // request.response |
| 77 | + // ..statusCode = HttpStatus.ok |
| 78 | + // ..headers.contentType = ContentType.json |
| 79 | + // ..write(isEven ? result[0] : result[1]) |
| 80 | + // ..close(); |
| 81 | + |
| 82 | + // index++; |
| 83 | + // }); |
| 84 | + |
| 85 | + // print('Server running on http://${server.address.address}:${server.port}'); |
| 86 | + // ProcessSignal.sigterm.watch().listen((_) async { |
| 87 | + // await server.close(); |
| 88 | + // await db.close(); |
| 89 | + // print('Server stopped.'); |
| 90 | + // }); |
| 91 | +} |
0 commit comments