simolus3 / sqlite3.dart

sqlite3 bindings for Dart
MIT License
227 stars 76 forks source link

Support for libsql? (Turso DB) #224

Closed leccles1 closed 7 months ago

leccles1 commented 7 months ago

Hi,

Recently I've been looking into SQLite for my Flutter application & love using Drift. I was wondering if there is currently support for libsql based connections?

I've been scouring the net to find if there is a Flutter implementation for libsql as I'd love to use Turso in my latest Flutter application, but I'm not having much luck.

I started looking into library overrides with this package, but I've not gotten very far. I tried as is in a new Dart project with Sqlite3 but most likely expectedly so run into the following error;

SqliteException(14): while opening the database, bad parameter or other API misuse, bad parameter or other API misuse (code 21)
#0      Sqlite3Implementation.open (package:sqlite3/src/implementation/sqlite3.dart:57:7)
#1      FfiSqlite3.open (package:sqlite3/src/ffi/implementation.dart:33:18)
#2      main (file:///Users/lewiseccles/dev/libsql-dart/bin/libsql_dart.dart:5:11)
#3      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295:33)
#4      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)

Any info on this would be great!

Thanks

simolus3 commented 7 months ago

Not sure if Turso is using yet another C library, but I've checked out the libsql repository, ran cargo xtask build and then used the resulting libsql-sqlite3/.libs/liblibsql.so. Everything seemed to work fine with that though, I know I was using libsql because things like RANDOM ROWID worked.

The API misuse error can happen if you try to open a database in a directory that doesn't exist, could that be the problem? You need to recursively create the directory first.

leccles1 commented 7 months ago

That's really good to know.

After digging around a lot today I suspect the error is due to the way Turso makes initial connections as it is over http, for which sqlite obviously doesn't support and at the time I was just attempting a straight up file handle -> url swap 😅

I did dig around for a download of the libsql .so file, but couldn't find it - So thanks for sharing the build method. In honesty I am way out of my depth in diving this deep into the inner workings of things so doubt I'm going to be able to take this quest much farther.

Thanks for the speedy response!