diff --git a/Package.swift b/Package.swift index fe8d28f..3d2f250 100644 --- a/Package.swift +++ b/Package.swift @@ -113,6 +113,7 @@ let package = Package( name: "CSQLite", targets: [ "CSQLite", + "CSQLiteExtensions", ]), ], traits: [ @@ -261,6 +262,27 @@ let package = Package( name: "ENABLE_STAT4", description: "Enables the sqlite_stat4 table" ), + // Statically linked extensions + .trait( + name: "CSQLITE_ENABLE_DECIMAL_EXTENSION", + description: "Enables the statically linked decimal extension" + ), + .trait( + name: "CSQLITE_ENABLE_IEEE754_EXTENSION", + description: "Enables the statically linked ieee754 extension" + ), + .trait( + name: "CSQLITE_ENABLE_SERIES_EXTENSION", + description: "Enables the statically linked series extension" + ), + .trait( + name: "CSQLITE_ENABLE_SHA3_EXTENSION", + description: "Enables the statically linked sha3 extension" + ), + .trait( + name: "CSQLITE_ENABLE_UUID_EXTENSION", + description: "Enables the statically linked uuid extension" + ), // Default traits .default(enabledTraits: [ "DQS_0", @@ -284,6 +306,11 @@ let package = Package( "ENABLE_SNAPSHOT", "ENABLE_STMTVTAB", "ENABLE_STAT4", + "CSQLITE_ENABLE_DECIMAL_EXTENSION", + "CSQLITE_ENABLE_IEEE754_EXTENSION", + "CSQLITE_ENABLE_SERIES_EXTENSION", + "CSQLITE_ENABLE_SHA3_EXTENSION", + "CSQLITE_ENABLE_UUID_EXTENSION", ]), ], targets: [ @@ -291,19 +318,84 @@ let package = Package( // Targets can depend on other targets in this package and products from dependencies. .target( name: "CSQLite", - cSettings: compileTimeOptions + platformConfiguration + features + [ + cSettings: compileTimeOptions + platformConfiguration + features, + linkerSettings: [ + .linkedLibrary("m"), + ]), + .target( + name: "CSQLiteExtensions", + dependencies: [ + .targetItem(name: "CSQLiteDecimalExtension", condition: .when(traits: ["CSQLITE_ENABLE_DECIMAL_EXTENSION"])), + .targetItem(name: "CSQLiteIEEE754Extension", condition: .when(traits: ["CSQLITE_ENABLE_IEEE754_EXTENSION"])), + .targetItem(name: "CSQLiteSeriesExtension", condition: .when(traits: ["CSQLITE_ENABLE_SERIES_EXTENSION"])), + .targetItem(name: "CSQLiteSHA3Extension", condition: .when(traits: ["CSQLITE_ENABLE_SHA3_EXTENSION"])), + .targetItem(name: "CSQLiteUUIDExtension", condition: .when(traits: ["CSQLITE_ENABLE_UUID_EXTENSION"])), + ]), + .target( + name: "CSQLiteDecimalExtension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/decimal", + cSettings: [ // For statically linking extensions // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension .define("SQLITE_CORE", to: "1"), + ]), + .target( + name: "CSQLiteIEEE754Extension", + dependencies: [ + "CSQLite", ], - linkerSettings: [ - .linkedLibrary("m"), + path: "Sources/extensions/ieee754", + cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension + .define("SQLITE_CORE", to: "1"), + ]), + .target( + name: "CSQLiteSeriesExtension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/series", + cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension + .define("SQLITE_CORE", to: "1"), + ]), + .target( + name: "CSQLiteSHA3Extension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/sha3", + cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension + .define("SQLITE_CORE", to: "1"), + ]), + .target( + name: "CSQLiteUUIDExtension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/uuid", + cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension + .define("SQLITE_CORE", to: "1"), ]), .testTarget( name: "CSQLiteTests", dependencies: [ "CSQLite", - ]) + ]), + .testTarget( + name: "CSQLiteExtensionsTests", + dependencies: [ + "CSQLiteExtensions", + ]), ], cLanguageStandard: .gnu11 ) diff --git a/Package@swift-5.3.swift b/Package@swift-5.3.swift index 3229c7e..6344815 100644 --- a/Package@swift-5.3.swift +++ b/Package@swift-5.3.swift @@ -96,6 +96,7 @@ let package = Package( name: "CSQLite", targets: [ "CSQLite", + "CSQLiteExtensions", ]), ], targets: [ @@ -103,19 +104,84 @@ let package = Package( // Targets can depend on other targets in this package and products from dependencies. .target( name: "CSQLite", - cSettings: compileTimeOptions + platformConfiguration + features + [ + cSettings: compileTimeOptions + platformConfiguration + features, + linkerSettings: [ + .linkedLibrary("m"), + ]), + .target( + name: "CSQLiteExtensions", + dependencies: [ + "CSQLiteDecimalExtension", + "CSQLiteIEEE754Extension", + "CSQLiteSeriesExtension", + "CSQLiteSHA3Extension", + "CSQLiteUUIDExtension", + ]), + .target( + name: "CSQLiteDecimalExtension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/decimal", + cSettings: [ // For statically linking extensions // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension .define("SQLITE_CORE", to: "1"), + ]), + .target( + name: "CSQLiteIEEE754Extension", + dependencies: [ + "CSQLite", ], - linkerSettings: [ - .linkedLibrary("m"), + path: "Sources/extensions/ieee754", + cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension + .define("SQLITE_CORE", to: "1"), + ]), + .target( + name: "CSQLiteSeriesExtension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/series", + cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension + .define("SQLITE_CORE", to: "1"), + ]), + .target( + name: "CSQLiteSHA3Extension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/sha3", + cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension + .define("SQLITE_CORE", to: "1"), + ]), + .target( + name: "CSQLiteUUIDExtension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/uuid", + cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension + .define("SQLITE_CORE", to: "1"), ]), .testTarget( name: "CSQLiteTests", dependencies: [ "CSQLite", - ]) + ]), + .testTarget( + name: "CSQLiteExtensionsTests", + dependencies: [ + "CSQLiteExtensions", + ]), ], cLanguageStandard: .gnu11 ) diff --git a/README.md b/README.md index 76eea66..644f3f4 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,18 @@ The following traits enable commonly-used SQLite features: > [!NOTE] > The `ENABLE_SESSION` trait also sets the `ENABLE_PREUPDATE_HOOK` trait. +### Statically Linked Extensions + +The following traits enable statically linked SQLite extensions and helper functions for automatic extension registration: + +| Package Trait | Default | SQLite Extension | +| --- | :---: | --- | +| CSQLITE_ENABLE_DECIMAL_EXTENSION | Y | [decimal](https://sqlite.org/floatingpoint.html#the_decimal_c_extension) | +| CSQLITE_ENABLE_IEEE754_EXTENSION | Y | [ieee754](https://sqlite.org/floatingpoint.html#the_ieee754_c_extension) | +| CSQLITE_ENABLE_SERIES_EXTENSION | Y | [series](https://www.sqlite.org/src/file/ext/misc/series.c) | +| CSQLITE_ENABLE_SHA3_EXTENSION | Y | [sha3](https://www.sqlite.org/src/file/ext/misc/shathree.c) | +| CSQLITE_ENABLE_UUID_EXTENSION | Y | [uuid](https://www.sqlite.org/src/file/ext/misc/uuid.c) | + ## License SQLite is in the [public domain](https://sqlite.org/copyright.html). diff --git a/Sources/CSQLite/csqlite_shims.c b/Sources/CSQLite/csqlite_shims.c index 2927386..bea84e8 100644 --- a/Sources/CSQLite/csqlite_shims.c +++ b/Sources/CSQLite/csqlite_shims.c @@ -295,60 +295,3 @@ int csqlite_sqlite3_vtab_uses_all_schemas(sqlite3 *db) { return sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS); } - -// MARK: - Database extensions - -void sqlite3_decimal_init(void); -int csqlite_sqlite3_auto_extension_decimal() -{ - return sqlite3_auto_extension(sqlite3_decimal_init); -} - -int csqlite_sqlite3_cancel_auto_extension_decimal() -{ - return sqlite3_cancel_auto_extension(sqlite3_decimal_init); -} - -void sqlite3_ieee_init(void); -int csqlite_sqlite3_auto_extension_ieee754() -{ - return sqlite3_auto_extension(sqlite3_ieee_init); -} - -int csqlite_sqlite3_cancel_auto_extension_ieee754() -{ - return sqlite3_cancel_auto_extension(sqlite3_ieee_init); -} - -void sqlite3_series_init(void); -int csqlite_sqlite3_auto_extension_series() -{ - return sqlite3_auto_extension(sqlite3_series_init); -} - -int csqlite_sqlite3_cancel_auto_extension_series() -{ - return sqlite3_cancel_auto_extension(sqlite3_series_init); -} - -void sqlite3_shathree_init(void); -int csqlite_sqlite3_auto_extension_sha3() -{ - return sqlite3_auto_extension(sqlite3_shathree_init); -} - -int csqlite_sqlite3_cancel_auto_extension_sha3() -{ - return sqlite3_cancel_auto_extension(sqlite3_shathree_init); -} - -void sqlite3_uuid_init(void); -int csqlite_sqlite3_auto_extension_uuid() -{ - return sqlite3_auto_extension(sqlite3_uuid_init); -} - -int csqlite_sqlite3_cancel_auto_extension_uuid() -{ - return sqlite3_cancel_auto_extension(sqlite3_uuid_init); -} diff --git a/Sources/CSQLite/include/csqlite_shims.h b/Sources/CSQLite/include/csqlite_shims.h index c9200fa..acef530 100644 --- a/Sources/CSQLite/include/csqlite_shims.h +++ b/Sources/CSQLite/include/csqlite_shims.h @@ -141,31 +141,3 @@ int csqlite_sqlite3_vtab_config_innocuous(sqlite3 *db); int csqlite_sqlite3_vtab_config_directonly(sqlite3 *db); /// Equivalent to `sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS)` int csqlite_sqlite3_vtab_uses_all_schemas(sqlite3 *db); - -// MARK: - Database extensions -// See https://sqlite.org/loadext.html - -/// Equivalent to `sqlite3_auto_extension(sqlite3_decimal_init)` -int csqlite_sqlite3_auto_extension_decimal(void); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_decimal_init)` -int csqlite_sqlite3_cancel_auto_extension_decimal(void); - -/// Equivalent to `sqlite3_auto_extension(sqlite3_ieee_init)` -int csqlite_sqlite3_auto_extension_ieee754(void); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_ieee_init)` -int csqlite_sqlite3_cancel_auto_extension_ieee754(void); - -/// Equivalent to `sqlite3_auto_extension(sqlite3_series_init)` -int csqlite_sqlite3_auto_extension_series(void); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_series_init)` -int csqlite_sqlite3_cancel_auto_extension_series(void); - -/// Equivalent to `sqlite3_auto_extension(sqlite3_shathree_init)` -int csqlite_sqlite3_auto_extension_sha3(void); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_shathree_init)` -int csqlite_sqlite3_cancel_auto_extension_sha3(void); - -/// Equivalent to `sqlite3_auto_extension(sqlite3_uuid_init)` -int csqlite_sqlite3_auto_extension_uuid(void); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_uuid_init)` -int csqlite_sqlite3_cancel_auto_extension_uuid(void); diff --git a/Sources/CSQLiteExtensions/CSQLiteExtensions.swift b/Sources/CSQLiteExtensions/CSQLiteExtensions.swift new file mode 100644 index 0000000..0529549 --- /dev/null +++ b/Sources/CSQLiteExtensions/CSQLiteExtensions.swift @@ -0,0 +1,38 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +#if false +@_exported import CSQLite +#endif + +#if swift(<6.1) || CSQLITE_ENABLE_DECIMAL_EXTENSION +@_exported import CSQLiteDecimalExtension +#endif + +#if swift(<6.1) || CSQLITE_ENABLE_IEEE754_EXTENSION +@_exported import CSQLiteIEEE754Extension +#endif + +#if swift(<6.1) || CSQLITE_ENABLE_SERIES_EXTENSION +@_exported import CSQLiteSeriesExtension +#endif + +#if swift(<6.1) || CSQLITE_ENABLE_SHA3_EXTENSION +@_exported import CSQLiteSHA3Extension +#endif + +#if swift(<6.1) || CSQLITE_ENABLE_UUID_EXTENSION +@_exported import CSQLiteUUIDExtension +#endif diff --git a/Sources/extensions/decimal/csqlite_decimal_extension.c b/Sources/extensions/decimal/csqlite_decimal_extension.c new file mode 100644 index 0000000..018e8be --- /dev/null +++ b/Sources/extensions/decimal/csqlite_decimal_extension.c @@ -0,0 +1,28 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +#include "csqlite_decimal_extension.h" +#include "sqlite3.h" + +void sqlite3_decimal_init(void); +int csqlite_sqlite3_auto_extension_decimal() +{ + return sqlite3_auto_extension(sqlite3_decimal_init); +} + +int csqlite_sqlite3_cancel_auto_extension_decimal() +{ + return sqlite3_cancel_auto_extension(sqlite3_decimal_init); +} diff --git a/Sources/CSQLite/decimal.c b/Sources/extensions/decimal/decimal.c similarity index 100% rename from Sources/CSQLite/decimal.c rename to Sources/extensions/decimal/decimal.c diff --git a/Sources/extensions/decimal/include/csqlite_decimal_extension.h b/Sources/extensions/decimal/include/csqlite_decimal_extension.h new file mode 100644 index 0000000..05a1f1f --- /dev/null +++ b/Sources/extensions/decimal/include/csqlite_decimal_extension.h @@ -0,0 +1,22 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +// MARK: - Database extensions +// See https://sqlite.org/loadext.html + +/// Equivalent to `sqlite3_auto_extension(sqlite3_decimal_init)` +int csqlite_sqlite3_auto_extension_decimal(void); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_decimal_init)` +int csqlite_sqlite3_cancel_auto_extension_decimal(void); diff --git a/Sources/extensions/decimal/include/module.modulemap b/Sources/extensions/decimal/include/module.modulemap new file mode 100644 index 0000000..663c837 --- /dev/null +++ b/Sources/extensions/decimal/include/module.modulemap @@ -0,0 +1,4 @@ +module CSQLiteDecimalExtension { + header "csqlite_decimal_extension.h" + export * +} diff --git a/Sources/extensions/ieee754/csqlite_ieee754_extension.c b/Sources/extensions/ieee754/csqlite_ieee754_extension.c new file mode 100644 index 0000000..1ac1076 --- /dev/null +++ b/Sources/extensions/ieee754/csqlite_ieee754_extension.c @@ -0,0 +1,28 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +#include "csqlite_ieee754_extension.h" +#include "sqlite3.h" + +void sqlite3_ieee_init(void); +int csqlite_sqlite3_auto_extension_ieee754() +{ + return sqlite3_auto_extension(sqlite3_ieee_init); +} + +int csqlite_sqlite3_cancel_auto_extension_ieee754() +{ + return sqlite3_cancel_auto_extension(sqlite3_ieee_init); +} diff --git a/Sources/CSQLite/ieee754.c b/Sources/extensions/ieee754/ieee754.c similarity index 100% rename from Sources/CSQLite/ieee754.c rename to Sources/extensions/ieee754/ieee754.c diff --git a/Sources/extensions/ieee754/include/csqlite_ieee754_extension.h b/Sources/extensions/ieee754/include/csqlite_ieee754_extension.h new file mode 100644 index 0000000..50417d5 --- /dev/null +++ b/Sources/extensions/ieee754/include/csqlite_ieee754_extension.h @@ -0,0 +1,22 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +// MARK: - Database extensions +// See https://sqlite.org/loadext.html + +/// Equivalent to `sqlite3_auto_extension(sqlite3_ieee_init)` +int csqlite_sqlite3_auto_extension_ieee754(void); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_ieee_init)` +int csqlite_sqlite3_cancel_auto_extension_ieee754(void); diff --git a/Sources/extensions/ieee754/include/module.modulemap b/Sources/extensions/ieee754/include/module.modulemap new file mode 100644 index 0000000..a2390e9 --- /dev/null +++ b/Sources/extensions/ieee754/include/module.modulemap @@ -0,0 +1,4 @@ +module CSQLiteIEEE754Extension { + header "csqlite_ieee754_extension.h" + export * +} diff --git a/Sources/extensions/series/csqlite_series_extension.c b/Sources/extensions/series/csqlite_series_extension.c new file mode 100644 index 0000000..e59a71a --- /dev/null +++ b/Sources/extensions/series/csqlite_series_extension.c @@ -0,0 +1,28 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +#include "csqlite_series_extension.h" +#include "sqlite3.h" + +void sqlite3_series_init(void); +int csqlite_sqlite3_auto_extension_series() +{ + return sqlite3_auto_extension(sqlite3_series_init); +} + +int csqlite_sqlite3_cancel_auto_extension_series() +{ + return sqlite3_cancel_auto_extension(sqlite3_series_init); +} diff --git a/Sources/extensions/series/include/csqlite_series_extension.h b/Sources/extensions/series/include/csqlite_series_extension.h new file mode 100644 index 0000000..0619aa0 --- /dev/null +++ b/Sources/extensions/series/include/csqlite_series_extension.h @@ -0,0 +1,22 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +// MARK: - Database extensions +// See https://sqlite.org/loadext.html + +/// Equivalent to `sqlite3_auto_extension(sqlite3_series_init)` +int csqlite_sqlite3_auto_extension_series(void); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_series_init)` +int csqlite_sqlite3_cancel_auto_extension_series(void); diff --git a/Sources/extensions/series/include/module.modulemap b/Sources/extensions/series/include/module.modulemap new file mode 100644 index 0000000..5789092 --- /dev/null +++ b/Sources/extensions/series/include/module.modulemap @@ -0,0 +1,4 @@ +module CSQLiteSeriesExtension { + header "csqlite_series_extension.h" + export * +} diff --git a/Sources/CSQLite/series.c b/Sources/extensions/series/series.c similarity index 100% rename from Sources/CSQLite/series.c rename to Sources/extensions/series/series.c diff --git a/Sources/extensions/sha3/csqlite_sha3_extension.c b/Sources/extensions/sha3/csqlite_sha3_extension.c new file mode 100644 index 0000000..e3ed958 --- /dev/null +++ b/Sources/extensions/sha3/csqlite_sha3_extension.c @@ -0,0 +1,28 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +#include "csqlite_sha3_extension.h" +#include "sqlite3.h" + +void sqlite3_shathree_init(void); +int csqlite_sqlite3_auto_extension_sha3() +{ + return sqlite3_auto_extension(sqlite3_shathree_init); +} + +int csqlite_sqlite3_cancel_auto_extension_sha3() +{ + return sqlite3_cancel_auto_extension(sqlite3_shathree_init); +} diff --git a/Sources/extensions/sha3/include/csqlite_sha3_extension.h b/Sources/extensions/sha3/include/csqlite_sha3_extension.h new file mode 100644 index 0000000..19b5ec6 --- /dev/null +++ b/Sources/extensions/sha3/include/csqlite_sha3_extension.h @@ -0,0 +1,22 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +// MARK: - Database extensions +// See https://sqlite.org/loadext.html + +/// Equivalent to `sqlite3_auto_extension(sqlite3_shathree_init)` +int csqlite_sqlite3_auto_extension_sha3(void); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_shathree_init)` +int csqlite_sqlite3_cancel_auto_extension_sha3(void); diff --git a/Sources/extensions/sha3/include/module.modulemap b/Sources/extensions/sha3/include/module.modulemap new file mode 100644 index 0000000..497ec49 --- /dev/null +++ b/Sources/extensions/sha3/include/module.modulemap @@ -0,0 +1,4 @@ +module CSQLiteSHA3Extension { + header "csqlite_sha3_extension.h" + export * +} diff --git a/Sources/CSQLite/shathree.c b/Sources/extensions/sha3/shathree.c similarity index 100% rename from Sources/CSQLite/shathree.c rename to Sources/extensions/sha3/shathree.c diff --git a/Sources/extensions/uuid/csqlite_uuid_extension.c b/Sources/extensions/uuid/csqlite_uuid_extension.c new file mode 100644 index 0000000..0b1c39b --- /dev/null +++ b/Sources/extensions/uuid/csqlite_uuid_extension.c @@ -0,0 +1,28 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +#include "csqlite_uuid_extension.h" +#include "sqlite3.h" + +void sqlite3_uuid_init(void); +int csqlite_sqlite3_auto_extension_uuid() +{ + return sqlite3_auto_extension(sqlite3_uuid_init); +} + +int csqlite_sqlite3_cancel_auto_extension_uuid() +{ + return sqlite3_cancel_auto_extension(sqlite3_uuid_init); +} diff --git a/Sources/extensions/uuid/include/csqlite_uuid_extension.h b/Sources/extensions/uuid/include/csqlite_uuid_extension.h new file mode 100644 index 0000000..e218831 --- /dev/null +++ b/Sources/extensions/uuid/include/csqlite_uuid_extension.h @@ -0,0 +1,22 @@ +/* + ** 2025-05-30 + ** + ** The author disclaims copyright to this source code. In place of + ** a legal notice, here is a blessing: + ** + ** May you do good and not evil. + ** May you find forgiveness for yourself and forgive others. + ** May you share freely, never taking more than you give. + ** + ****************************************************************************** + ** + ** Functions to manage automatic database extension registration from Swift. + */ + +// MARK: - Database extensions +// See https://sqlite.org/loadext.html + +/// Equivalent to `sqlite3_auto_extension(sqlite3_uuid_init)` +int csqlite_sqlite3_auto_extension_uuid(void); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_uuid_init)` +int csqlite_sqlite3_cancel_auto_extension_uuid(void); diff --git a/Sources/extensions/uuid/include/module.modulemap b/Sources/extensions/uuid/include/module.modulemap new file mode 100644 index 0000000..f73c296 --- /dev/null +++ b/Sources/extensions/uuid/include/module.modulemap @@ -0,0 +1,4 @@ +module CSQLiteUUIDExtension { + header "csqlite_uuid_extension.h" + export * +} diff --git a/Sources/CSQLite/uuid.c b/Sources/extensions/uuid/uuid.c similarity index 100% rename from Sources/CSQLite/uuid.c rename to Sources/extensions/uuid/uuid.c diff --git a/Tests/CSQLiteTests/CSQLiteExtensionTests.swift b/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift similarity index 90% rename from Tests/CSQLiteTests/CSQLiteExtensionTests.swift rename to Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift index 65dc193..9913c37 100644 --- a/Tests/CSQLiteTests/CSQLiteExtensionTests.swift +++ b/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift @@ -1,5 +1,5 @@ /* - ** 2021-06-08 + ** 2024-05-24 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -10,12 +10,13 @@ ** ****************************************************************************** ** - ** Basic tests of CSQLite statically-linked extension functionality. + ** Basic tests of CSQLite statically linked extension functionality. */ import Testing import Foundation -@testable import CSQLite +import CSQLite +@testable import CSQLiteExtensions #if swift(<6.1) || THREADSAFE_0 @MainActor @@ -28,6 +29,7 @@ import Foundation } #endif +#if swift(<6.1) || CSQLITE_ENABLE_DECIMAL_EXTENSION @Test func decimal() throws { try #require(csqlite_sqlite3_auto_extension_decimal() == SQLITE_OK) @@ -37,13 +39,16 @@ import Foundation var stmt: OpaquePointer? #expect(sqlite3_prepare_v2(db, "select decimal_add('1.67','2.33');", -1, &stmt, nil) == SQLITE_OK) #expect(sqlite3_step(stmt) == SQLITE_ROW) + let r = String(cString: sqlite3_column_text(stmt, 0)) #expect(r == "4.00") #expect(sqlite3_finalize(stmt) == SQLITE_OK) #expect(sqlite3_close(db) == SQLITE_OK) } +#endif +#if swift(<6.1) || CSQLITE_ENABLE_IEEE754_EXTENSION @Test func ieee() throws { try #require(csqlite_sqlite3_auto_extension_ieee754() == SQLITE_OK) @@ -59,7 +64,9 @@ import Foundation #expect(sqlite3_finalize(stmt) == SQLITE_OK) #expect(sqlite3_close(db) == SQLITE_OK) } +#endif +#if swift(<6.1) || CSQLITE_ENABLE_SERIES_EXTENSION @Test func series() throws { try #require(csqlite_sqlite3_auto_extension_series() == SQLITE_OK) @@ -67,6 +74,7 @@ import Foundation #expect(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) var stmt: OpaquePointer? + #expect(sqlite3_prepare_v2(db, "select * from generate_series(10,20,5);", -1, &stmt, nil) == SQLITE_OK) #expect(sqlite3_step(stmt) == SQLITE_ROW) #expect(sqlite3_column_int(stmt, 0) == 10) @@ -78,7 +86,9 @@ import Foundation #expect(sqlite3_finalize(stmt) == SQLITE_OK) #expect(sqlite3_close(db) == SQLITE_OK) } +#endif +#if swift(<6.1) || CSQLITE_ENABLE_SHA3_EXTENSION @Test func sha3() throws { try #require(csqlite_sqlite3_auto_extension_sha3() == SQLITE_OK) @@ -88,13 +98,16 @@ import Foundation var stmt: OpaquePointer? #expect(sqlite3_prepare_v2(db, "select lower(hex(sha3('sqlite')));", -1, &stmt, nil) == SQLITE_OK) #expect(sqlite3_step(stmt) == SQLITE_ROW) + let h = String(cString: sqlite3_column_text(stmt, 0)) #expect(h == "963a88636d4c9cc3f011dfc9dc0058e96669d80a89893c68c0bb08a6a8208db3") #expect(sqlite3_finalize(stmt) == SQLITE_OK) #expect(sqlite3_close(db) == SQLITE_OK) } +#endif +#if swift(<6.1) || CSQLITE_ENABLE_UUID_EXTENSION @Test func uuid() throws { try #require(csqlite_sqlite3_auto_extension_uuid() == SQLITE_OK) @@ -118,4 +131,5 @@ import Foundation #expect(sqlite3_finalize(stmt) == SQLITE_OK) #expect(sqlite3_close(db) == SQLITE_OK) } +#endif }