touchlab / SQLiter

Minimal multiplatform sqlite library
https://touchlab.co
172 stars 35 forks source link

Support in memory sqlite db #7

Open AlecKazakova opened 5 years ago

AlecKazakova commented 5 years ago

specifically the name property of DatabaseConfiguration should be nullable, and when null should mean the created SQLite db is in memory

kpgalligan commented 5 years ago

Hmm. You can use an in memory db with a param, and the name is used so you can have more than one distinct in memory db but also have multiple connections to it. Will think about config more, though.

https://github.com/touchlab/SQLiter/blob/master/SQLiter/src/commonMain/kotlin/co/touchlab/sqliter/DatabaseConfiguration.kt#L30

https://github.com/touchlab/SQLiter/blob/master/SQLiter/src/nativeCommonMain/kotlin/co/touchlab/sqliter/NativeFileContext.kt#L33

LouisCAD commented 3 years ago

An auto-generated name could be used under the hood when passing null as the name, so every new call creates a database with a different name that won't clash with other instances.

Example generated name: in_memory_database_1 where the number is taken from a Long that auto-increments.

martinbonnin commented 2 years ago

Just got hit by this. Looks like passing DatabaseConfiguration.inMemory = true and DatabaseConfiguration.name = null is invalid? All my statements fail compilation with this (while it's ok if I pass en actual name). Maybe just throw in that case?

kpgalligan commented 2 years ago

It's not invalid to sqliter, but may be in the context of the sqldelight driver. The basic difference is sqlite lets you create an in memory db with multiple connections using a shared memory cache with a name( "file:${configuration.name}?mode=memory&cache=shared"). However, that needs to be managed and deleted like any other db or it'll just accumulate. Probably not an issue in most cases, but not ideal either.

A null name creates a connection like you'd usually expect for in memory, but it can only have one connection. The sqldelight driver and sqliter's feature sets probably drifted independently. Will need to actually solve for this. For now, add a name and delete when done.