Flocon/docs/database.md
Florent CHAMPIGNY 195893ab67
Add image for database query logging section
Added an image to illustrate database query logging.
2025-12-30 15:08:14 +01:00

102 lines
3.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### 🧩 Database Explorer & SQL Editor
<img width="1726" height="1080" alt="Screenshot 2025-10-14 at 23 40 58" src="https://github.com/user-attachments/assets/47360e06-43af-4713-b0ed-a6728a6b49ad" />
<img width="1728" height="1077" alt="Screenshot 2025-10-14 at 23 44 16" src="https://github.com/user-attachments/assets/f351970f-0511-4b54-af5e-55dcd209f2e2" />
Flocon gives you direct access to your apps **local databases** (SQLite, Room, SQLDelight, etc.), with a clean interface for exploring schemas and querying data.
Key capabilities include:
- **Automatic detection** of all SQLite databases on Android.
- Listing all tables and their schemas.
- Running **custom SQL queries** with syntax highlighting.
- Auto-updating queries and saving favorites.
- Generating `INSERT` and `DELETE` queries automatically.
#### Android (Automatic Detection)
On Android, Flocon automatically scans your app's internal storage and lists all SQLite databases. You don't need additional configuration to see them in the desktop app.
#### Manual Registration (Android)
If you want to use a custom display name or register an **in-memory database**, you can use `floconRegisterDatabase`:
```kotlin
// Register an In-Memory Room Database
val dogDatabase = Room.inMemoryDatabaseBuilder(context, DogDatabase::class.java).build()
floconRegisterDatabase(
displayName = "In-Memory Dogs",
openHelper = dogDatabase.openHelper
)
```
#### Multiplatform (Desktop & iOS)
For Kotlin Multiplatform projects (Desktop and iOS), you must provide the absolute path to the database file:
```kotlin
// On Desktop
val dbFile = File(System.getProperty("java.io.tmpdir"), "app_database.db")
floconRegisterDatabase(
displayName = "App DB",
absolutePath = dbFile.absolutePath,
)
```
```kotlin
// On iOS
val dbPath = "${documentDirectory()}/app_database.db"
floconRegisterDatabase(
displayName = "App DB",
absolutePath = dbPath
)
```
#### Database Query Logging
Flocon can also track and display all SQL queries executed by your app in real-time. This is particularly useful for debugging transactions, performance issues, or verifying the exact SQL generated by ORMs like Room.
<img width="1440" height="968" alt="Screenshot 2025-12-30 at 15 01 42" src="https://github.com/user-attachments/assets/f7c06191-17c1-41f6-9184-86b50d6f9945" />
##### Room Integration
For Room databases, you can easily log all queries by using the `setQueryCallback` method when building your database:
```kotlin
val dbName = "dogs_database"
val database = Room.databaseBuilder(
context.applicationContext,
DogDatabase::class.java,
dbName
).setQueryCallback({ sqlQuery, bindArgs ->
// Log the query to Flocon
floconLogDatabaseQuery(
dbName = dbName,
sqlQuery = sqlQuery,
bindArgs = bindArgs
)
}, Executors.newSingleThreadExecutor())
.build()
```
##### Manual Logging
If you are not using Room or want to log queries manually, you can use the `floconLogDatabaseQuery` function directly:
```kotlin
floconLogDatabaseQuery(
dbName = "my_custom_db",
sqlQuery = "SELECT * FROM users WHERE id = ?",
bindArgs = listOf(42)
)
```
#### SQL Workspace
The Flocon Desktop app provides a full SQL workspace where you can:
1. **Explore**: See all tables and their columns.
2. **Query**: Write any SQL query and see the results in a formatted table.
3. **Favorites**: Save your most used queries for quick access later.
4. **Toolbox**: Quickly generate common SQL statements from the UI.