Skip to content

Commit 8a391c3

Browse files
authored
feat: Add sqlite_changes function (#3)
* expose sqlite_changes function * _
1 parent 533037e commit 8a391c3

File tree

10 files changed

+337
-122
lines changed

10 files changed

+337
-122
lines changed

bin/libsqlite3_turso.dart

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

bin/proxy_server.dart

Lines changed: 0 additions & 44 deletions
This file was deleted.

bin/sqlite_test.dart

Lines changed: 0 additions & 50 deletions
This file was deleted.

lib/database.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:drift/drift.dart';
2+
import 'package:drift/native.dart';
3+
import 'package:sqlite3/sqlite3.dart';
4+
5+
part 'database.g.dart';
6+
7+
class TodoItems extends Table {
8+
IntColumn get id => integer().autoIncrement()();
9+
TextColumn get title => text().withLength(min: 6, max: 32)();
10+
TextColumn get content => text().named('body')();
11+
DateTimeColumn get createdAt => dateTime().nullable()();
12+
}
13+
14+
@DriftDatabase(tables: [TodoItems])
15+
class AppDatabase extends _$AppDatabase {
16+
AppDatabase() : super(_openConnection());
17+
18+
@override
19+
int get schemaVersion => 1;
20+
21+
static QueryExecutor _openConnection() {
22+
return NativeDatabase.opened(sqlite3.open('simple-nubian.db'));
23+
}
24+
}

0 commit comments

Comments
 (0)