Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,29 @@ It also contains a simple [Key-Value store](https://op-engineering.github.io/op-
# License

MIT License.

## Exact decimal SQL (optional)

Enable `sqliteDecimal` in the app's `package.json` and rebuild the native app,
just like the existing `sqliteVec` option:

```json
{
"op-sqlite": {
"sqliteDecimal": true
}
}
```

This statically includes SQLite's public-domain
`ext/misc/decimal.c` (SQLite 3.50.4) and registers it on every SQLite/SQLCipher connection.
For example, `SELECT decimal_sum(amount) FROM payments` returns exact decimal TEXT;
`SELECT decimal_sub('1000.00', '0.01')` returns `'999.99'`.

The option is off by default and can be combined with `sqlcipher: true`.
It does not change column types or ordinary SQL `+`, `-`,
`SUM`, or comparisons. Pass decimal strings rather than JS numbers to avoid losing
precision before SQLite receives the value. See [SQLite's decimal documentation](https://sqlite.org/floatingpoint.html#decext)
for supported functions and input semantics. LibSQL/Turso use separate engines and
are not covered by this option. SQLCipher's extra-init hook remains intact; decimal is
initialized per connection after `sqlite3_open_v2`.
1 change: 1 addition & 0 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ add_library(
${PACKAGE_NAME}
SHARED
../cpp/OPSqlite.cpp
../cpp/decimal.c
../cpp/OPUtils.cpp
../cpp/OPThreadPool.cpp
../cpp/OPSmartHostObject.cpp
Expand Down
3 changes: 3 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ if(opsqliteConfig) {
useSqliteVec = !!opsqliteConfig["sqliteVec"]
performanceMode = opsqliteConfig["performanceMode"]
sqliteFlags = opsqliteConfig["sqliteFlags"] ? opsqliteConfig["sqliteFlags"] : ""
if (opsqliteConfig["sqliteDecimal"] == true) {
sqliteFlags = "${sqliteFlags} -DOP_SQLITE_ENABLE_DECIMAL=1".trim()
}
enableFTS5 = !!opsqliteConfig["fts5"]
useLibsql = !!opsqliteConfig["libsql"]
useTurso = !!opsqliteConfig["turso"]
Expand Down
14 changes: 14 additions & 0 deletions cpp/OPBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#define TOKENIZER_LIST
#endif

#ifdef OP_SQLITE_ENABLE_DECIMAL
extern "C" int sqlite3_decimal_init(sqlite3 *, char **, const sqlite3_api_routines *);
#endif

namespace opsqlite {

/// The codes are on the error object as `code`/`extendedCode` once it reaches
Expand Down Expand Up @@ -161,6 +165,16 @@ sqlite3 *opsqlite_open(std::string const &name, std::string const &path,
throw error;
}

#ifdef OP_SQLITE_ENABLE_DECIMAL
// Per-connection registration preserves SQLCipher's SQLITE_EXTRA_INIT hook.
status = sqlite3_decimal_init(db, nullptr, nullptr);
if (status != SQLITE_OK) {
auto error = capture_error(db, "could not initialize decimal extension", status);
sqlite3_close_v2(db);
throw error;
}
#endif

#ifdef OP_SQLITE_USE_SQLCIPHER
if (!encryption_key.empty()) {
// Use the SQLCipher C API directly instead of `PRAGMA key = '...'`.
Expand Down
Loading