From 711cc966cba1fc04aa384a053fcdace56967b608 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Sat, 5 Jun 2021 08:55:59 -0500 Subject: [PATCH 01/15] Refactor extensions into separate target --- Package.swift | 16 ++- Sources/CSQLite/csqlite_shims.c | 68 --------- Sources/CSQLite/include/csqlite_shims.h | 60 -------- .../{CSQLite => CSQLiteExtensions}/carray.c | 0 .../CSQLiteExtensions/csqlite_extensions.c | 84 +++++++++++ .../{CSQLite => CSQLiteExtensions}/decimal.c | 0 .../{CSQLite => CSQLiteExtensions}/ieee754.c | 0 .../include/csqlite_extensions.h | 76 ++++++++++ .../{CSQLite => CSQLiteExtensions}/series.c | 0 .../{CSQLite => CSQLiteExtensions}/shathree.c | 0 Sources/{CSQLite => CSQLiteExtensions}/uuid.c | 0 .../CSQLiteExtensionsTests.swift | 133 ++++++++++++++++++ Tests/CSQLiteTests/CSQLiteTests.swift | 122 ---------------- 13 files changed, 305 insertions(+), 254 deletions(-) rename Sources/{CSQLite => CSQLiteExtensions}/carray.c (100%) create mode 100644 Sources/CSQLiteExtensions/csqlite_extensions.c rename Sources/{CSQLite => CSQLiteExtensions}/decimal.c (100%) rename Sources/{CSQLite => CSQLiteExtensions}/ieee754.c (100%) create mode 100644 Sources/CSQLiteExtensions/include/csqlite_extensions.h rename Sources/{CSQLite => CSQLiteExtensions}/series.c (100%) rename Sources/{CSQLite => CSQLiteExtensions}/shathree.c (100%) rename Sources/{CSQLite => CSQLiteExtensions}/uuid.c (100%) create mode 100644 Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift diff --git a/Package.swift b/Package.swift index a82780f..03c54a1 100644 --- a/Package.swift +++ b/Package.swift @@ -16,7 +16,7 @@ let package = Package( // Products define the executables and libraries a package produces, and make them visible to other packages. .library( name: "CSQLite", - targets: ["CSQLite"]), + targets: ["CSQLite", "CSQLiteExtensions"]), ], dependencies: [ // Dependencies declare other packages that this package depends on. @@ -59,14 +59,22 @@ let package = Package( .define("SQLITE_ENABLE_RTREE", to: "1"), // .define("SQLITE_ENABLE_SESSION", to: "1"), .define("SQLITE_ENABLE_SNAPSHOT", to: "1"), - .define("SQLITE_ENABLE_STAT4", to: "1"), - .define("SQLITE_CORE", to: "1")], + .define("SQLITE_ENABLE_STAT4", to: "1")], linkerSettings: [ .linkedLibrary("m") ]), + .target( + name: "CSQLiteExtensions", + dependencies: [ .target(name: "CSQLite") ], + cSettings: [ + .define("SQLITE_CORE", to: "1"), + ]), .testTarget( name: "CSQLiteTests", - dependencies: ["CSQLite"]) + dependencies: ["CSQLite"]), + .testTarget( + name: "CSQLiteExtensionsTests", + dependencies: ["CSQLiteExtensions"]) ], cLanguageStandard: .gnu11 ) diff --git a/Sources/CSQLite/csqlite_shims.c b/Sources/CSQLite/csqlite_shims.c index 365fdc0..8717091 100644 --- a/Sources/CSQLite/csqlite_shims.c +++ b/Sources/CSQLite/csqlite_shims.c @@ -245,71 +245,3 @@ int csqlite_sqlite3_vtab_config_directonly(sqlite3 *db) { return sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); } - -// MARK: - Database extensions - -void sqlite3_carray_init(); -int csqlite_sqlite3_auto_extension_carray() -{ - return sqlite3_auto_extension(sqlite3_carray_init); -} - -int csqlite_sqlite3_cancel_auto_extension_carray() -{ - return sqlite3_cancel_auto_extension(sqlite3_carray_init); -} - -void sqlite3_decimal_init(); -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(); -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(); -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(); -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(); -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 9ee8bdc..483cb3b 100644 --- a/Sources/CSQLite/include/csqlite_shims.h +++ b/Sources/CSQLite/include/csqlite_shims.h @@ -117,63 +117,3 @@ int csqlite_sqlite3_vtab_config_constraint_support(sqlite3 *db, int x); int csqlite_sqlite3_vtab_config_innocuous(sqlite3 *db); /// Equivalent to `sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY)` int csqlite_sqlite3_vtab_config_directonly(sqlite3 *db); - -// MARK: - Database extensions -// See https://sqlite.org/loadext.html - -// The Carray() Table-Valued Function -// See https://sqlite.org/carray.html - -/* - ** Interface definitions for the CARRAY table-valued function - ** extension. - */ - -/* Use this interface to bind an array to the single-argument version - ** of CARRAY(). - */ -int sqlite3_carray_bind( - sqlite3_stmt *pStmt, /* Statement to be bound */ - int i, /* Parameter index */ - void *aData, /* Pointer to array data */ - int nData, /* Number of data elements */ - int mFlags, /* CARRAY flags */ - void (*xDel)(void*) /* Destructor for aData*/ -); - -/* Allowed values for the mFlags parameter to sqlite3_carray_bind(). - */ -#define CARRAY_INT32 0 /* Data is 32-bit signed integers */ -#define CARRAY_INT64 1 /* Data is 64-bit signed integers */ -#define CARRAY_DOUBLE 2 /* Data is doubles */ -#define CARRAY_TEXT 3 /* Data is char* */ - -/// Equivalent to `sqlite3_auto_extension(sqlite3_carray_init)` -int csqlite_sqlite3_auto_extension_carray(); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_carray_init)` -int csqlite_sqlite3_cancel_auto_extension_carray(); - -/// Equivalent to `sqlite3_auto_extension(sqlite3_decimal_init)` -int csqlite_sqlite3_auto_extension_decimal(); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_decimal_init)` -int csqlite_sqlite3_cancel_auto_extension_decimal(); - -/// Equivalent to `sqlite3_auto_extension(sqlite3_ieee_init)` -int csqlite_sqlite3_auto_extension_ieee754(); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_ieee_init)` -int csqlite_sqlite3_cancel_auto_extension_ieee754(); - -/// Equivalent to `sqlite3_auto_extension(sqlite3_series_init)` -int csqlite_sqlite3_auto_extension_series(); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_series_init)` -int csqlite_sqlite3_cancel_auto_extension_series(); - -/// Equivalent to `sqlite3_auto_extension(sqlite3_shathree_init)` -int csqlite_sqlite3_auto_extension_sha3(); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_shathree_init)` -int csqlite_sqlite3_cancel_auto_extension_sha3(); - -/// Equivalent to `sqlite3_auto_extension(sqlite3_uuid_init)` -int csqlite_sqlite3_auto_extension_uuid(); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_uuid_init)` -int csqlite_sqlite3_cancel_auto_extension_uuid(); diff --git a/Sources/CSQLite/carray.c b/Sources/CSQLiteExtensions/carray.c similarity index 100% rename from Sources/CSQLite/carray.c rename to Sources/CSQLiteExtensions/carray.c diff --git a/Sources/CSQLiteExtensions/csqlite_extensions.c b/Sources/CSQLiteExtensions/csqlite_extensions.c new file mode 100644 index 0000000..c5f5158 --- /dev/null +++ b/Sources/CSQLiteExtensions/csqlite_extensions.c @@ -0,0 +1,84 @@ +/* + ** 2021-06-04 + ** + ** 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. + ** + ****************************************************************************** + ** + ** Wrappers for C functions not easily accessible from Swift. + */ + +#include "csqlite_extensions.h" + +// MARK: - Database extensions + +void sqlite3_carray_init(); +int csqlite_sqlite3_auto_extension_carray() +{ + return sqlite3_auto_extension(sqlite3_carray_init); +} + +int csqlite_sqlite3_cancel_auto_extension_carray() +{ + return sqlite3_cancel_auto_extension(sqlite3_carray_init); +} + +void sqlite3_decimal_init(); +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(); +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(); +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(); +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(); +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/decimal.c b/Sources/CSQLiteExtensions/decimal.c similarity index 100% rename from Sources/CSQLite/decimal.c rename to Sources/CSQLiteExtensions/decimal.c diff --git a/Sources/CSQLite/ieee754.c b/Sources/CSQLiteExtensions/ieee754.c similarity index 100% rename from Sources/CSQLite/ieee754.c rename to Sources/CSQLiteExtensions/ieee754.c diff --git a/Sources/CSQLiteExtensions/include/csqlite_extensions.h b/Sources/CSQLiteExtensions/include/csqlite_extensions.h new file mode 100644 index 0000000..a0895be --- /dev/null +++ b/Sources/CSQLiteExtensions/include/csqlite_extensions.h @@ -0,0 +1,76 @@ +/* + ** 2021-06-04 + ** + ** 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. + ** + ****************************************************************************** + ** + ** Wrappers for C functions not easily accessible from Swift. + */ + +#include "sqlite3.h" + +// MARK: - Database extensions +// See https://sqlite.org/loadext.html + +// The Carray() Table-Valued Function +// See https://sqlite.org/carray.html + +/* + ** Interface definitions for the CARRAY table-valued function + ** extension. + */ + +/* Use this interface to bind an array to the single-argument version + ** of CARRAY(). + */ +int sqlite3_carray_bind( + sqlite3_stmt *pStmt, /* Statement to be bound */ + int i, /* Parameter index */ + void *aData, /* Pointer to array data */ + int nData, /* Number of data elements */ + int mFlags, /* CARRAY flags */ + void (*xDel)(void*) /* Destructor for aData*/ +); + +/* Allowed values for the mFlags parameter to sqlite3_carray_bind(). + */ +#define CARRAY_INT32 0 /* Data is 32-bit signed integers */ +#define CARRAY_INT64 1 /* Data is 64-bit signed integers */ +#define CARRAY_DOUBLE 2 /* Data is doubles */ +#define CARRAY_TEXT 3 /* Data is char* */ + +/// Equivalent to `sqlite3_auto_extension(sqlite3_carray_init)` +int csqlite_sqlite3_auto_extension_carray(); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_carray_init)` +int csqlite_sqlite3_cancel_auto_extension_carray(); + +/// Equivalent to `sqlite3_auto_extension(sqlite3_decimal_init)` +int csqlite_sqlite3_auto_extension_decimal(); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_decimal_init)` +int csqlite_sqlite3_cancel_auto_extension_decimal(); + +/// Equivalent to `sqlite3_auto_extension(sqlite3_ieee_init)` +int csqlite_sqlite3_auto_extension_ieee754(); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_ieee_init)` +int csqlite_sqlite3_cancel_auto_extension_ieee754(); + +/// Equivalent to `sqlite3_auto_extension(sqlite3_series_init)` +int csqlite_sqlite3_auto_extension_series(); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_series_init)` +int csqlite_sqlite3_cancel_auto_extension_series(); + +/// Equivalent to `sqlite3_auto_extension(sqlite3_shathree_init)` +int csqlite_sqlite3_auto_extension_sha3(); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_shathree_init)` +int csqlite_sqlite3_cancel_auto_extension_sha3(); + +/// Equivalent to `sqlite3_auto_extension(sqlite3_uuid_init)` +int csqlite_sqlite3_auto_extension_uuid(); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_uuid_init)` +int csqlite_sqlite3_cancel_auto_extension_uuid(); diff --git a/Sources/CSQLite/series.c b/Sources/CSQLiteExtensions/series.c similarity index 100% rename from Sources/CSQLite/series.c rename to Sources/CSQLiteExtensions/series.c diff --git a/Sources/CSQLite/shathree.c b/Sources/CSQLiteExtensions/shathree.c similarity index 100% rename from Sources/CSQLite/shathree.c rename to Sources/CSQLiteExtensions/shathree.c diff --git a/Sources/CSQLite/uuid.c b/Sources/CSQLiteExtensions/uuid.c similarity index 100% rename from Sources/CSQLite/uuid.c rename to Sources/CSQLiteExtensions/uuid.c diff --git a/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift b/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift new file mode 100644 index 0000000..b40c60e --- /dev/null +++ b/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift @@ -0,0 +1,133 @@ +import XCTest +@testable import CSQLite +@testable import CSQLiteExtensions + +final class CSQLiteTests: XCTestCase { + override class func setUp() { + super.setUp() + // It's necessary to call sqlite3_initialize() since SQLITE_OMIT_AUTOINIT is defined + XCTAssert(sqlite3_initialize() == SQLITE_OK) + } + + func testCarray() { + XCTAssert(csqlite_sqlite3_auto_extension_carray() == SQLITE_OK) + + var db: OpaquePointer? + XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) + + XCTAssert(sqlite3_exec(db, "create table t1(a);", nil, nil, nil) == SQLITE_OK) + XCTAssert(sqlite3_exec(db, "insert into t1(a) values (2),(3),(4),(5);", nil, nil, nil) == SQLITE_OK) + + var stmt: OpaquePointer? + XCTAssert(sqlite3_prepare_v2(db, "select a from t1 where a in carray(?);", -1, &stmt, nil) == SQLITE_OK) + let array: [Int32] = [3,5]; + let mem = UnsafeMutableBufferPointer.allocate(capacity: array.count) + _ = mem.initialize(from: array) + XCTAssert(sqlite3_carray_bind(stmt, 1, mem.baseAddress, Int32(array.count), CARRAY_INT32, { + $0?.deallocate() + }) == SQLITE_OK) + XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) + var i = sqlite3_column_int(stmt, 0) + XCTAssertEqual(i, 3) + XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) + i = sqlite3_column_int(stmt, 0) + XCTAssertEqual(i, 5) + + XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) + XCTAssert(sqlite3_close(db) == SQLITE_OK) + } + + func testDecimal() { + XCTAssert(csqlite_sqlite3_auto_extension_decimal() == SQLITE_OK) + + var db: OpaquePointer? + XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) + + var stmt: OpaquePointer? + XCTAssert(sqlite3_prepare_v2(db, "select decimal_add('1.67','2.33');", -1, &stmt, nil) == SQLITE_OK) + XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) + let r = String(cString: sqlite3_column_text(stmt, 0)) + XCTAssertEqual(r, "4.00") + + XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) + XCTAssert(sqlite3_close(db) == SQLITE_OK) + } + + func testIEEE() { + XCTAssert(csqlite_sqlite3_auto_extension_ieee754() == SQLITE_OK) + + var db: OpaquePointer? + XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) + + var stmt: OpaquePointer? + XCTAssert(sqlite3_prepare_v2(db, "select ieee754(45.25);", -1, &stmt, nil) == SQLITE_OK) + XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) + let f = String(cString: sqlite3_column_text(stmt, 0)) + XCTAssertEqual(f, "ieee754(181,-2)") + + XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) + XCTAssert(sqlite3_close(db) == SQLITE_OK) + } + + func testSeries() { + XCTAssert(csqlite_sqlite3_auto_extension_series() == SQLITE_OK) + + var db: OpaquePointer? + XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) + + var stmt: OpaquePointer? + XCTAssert(sqlite3_prepare_v2(db, "select * from generate_series(10,20,5);", -1, &stmt, nil) == SQLITE_OK) + XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) + var i = sqlite3_column_int(stmt, 0) + XCTAssertEqual(i, 10) + XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) + i = sqlite3_column_int(stmt, 0) + XCTAssertEqual(i, 15) + XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) + i = sqlite3_column_int(stmt, 0) + XCTAssertEqual(i, 20) + + XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) + XCTAssert(sqlite3_close(db) == SQLITE_OK) + } + + func testSHA3() { + XCTAssert(csqlite_sqlite3_auto_extension_sha3() == SQLITE_OK) + + var db: OpaquePointer? + XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) + + var stmt: OpaquePointer? + XCTAssert(sqlite3_prepare_v2(db, "select lower(hex(sha3('sqlite')));", -1, &stmt, nil) == SQLITE_OK) + XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) + let h = String(cString: sqlite3_column_text(stmt, 0)) + XCTAssertEqual(h, "963a88636d4c9cc3f011dfc9dc0058e96669d80a89893c68c0bb08a6a8208db3") + + XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) + XCTAssert(sqlite3_close(db) == SQLITE_OK) + } + + func testUUID() { + XCTAssert(csqlite_sqlite3_auto_extension_uuid() == SQLITE_OK) + + var db: OpaquePointer? + XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) + + var stmt: OpaquePointer? + XCTAssert(sqlite3_prepare_v2(db, "select uuid();", -1, &stmt, nil) == SQLITE_OK) + XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) + let u = String(cString: sqlite3_column_text(stmt, 0)) + let comps = u.components(separatedBy: "-") + XCTAssertEqual(comps.count, 5) + XCTAssertEqual(comps[0].count, 8) + XCTAssertEqual(comps[1].count, 4) + XCTAssertEqual(comps[2].count, 4) + XCTAssertEqual(comps[3].count, 4) + XCTAssertEqual(comps[4].count, 12) + let uu = UUID(uuidString: u) + XCTAssertNotNil(uu) + + XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) + XCTAssert(sqlite3_close(db) == SQLITE_OK) + } +} diff --git a/Tests/CSQLiteTests/CSQLiteTests.swift b/Tests/CSQLiteTests/CSQLiteTests.swift index bdfce98..8c62f6d 100644 --- a/Tests/CSQLiteTests/CSQLiteTests.swift +++ b/Tests/CSQLiteTests/CSQLiteTests.swift @@ -62,126 +62,4 @@ final class CSQLiteTests: XCTestCase { XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) XCTAssert(sqlite3_close(db) == SQLITE_OK) } - - func testCarray() { - XCTAssert(csqlite_sqlite3_auto_extension_carray() == SQLITE_OK) - - var db: OpaquePointer? - XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) - - XCTAssert(sqlite3_exec(db, "create table t1(a);", nil, nil, nil) == SQLITE_OK) - XCTAssert(sqlite3_exec(db, "insert into t1(a) values (2),(3),(4),(5);", nil, nil, nil) == SQLITE_OK) - - var stmt: OpaquePointer? - XCTAssert(sqlite3_prepare_v2(db, "select a from t1 where a in carray(?);", -1, &stmt, nil) == SQLITE_OK) - let array: [Int32] = [3,5]; - let mem = UnsafeMutableBufferPointer.allocate(capacity: array.count) - _ = mem.initialize(from: array) - XCTAssert(sqlite3_carray_bind(stmt, 1, mem.baseAddress, Int32(array.count), CARRAY_INT32, { - $0?.deallocate() - }) == SQLITE_OK) - XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) - var i = sqlite3_column_int(stmt, 0) - XCTAssertEqual(i, 3) - XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) - i = sqlite3_column_int(stmt, 0) - XCTAssertEqual(i, 5) - - XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) - XCTAssert(sqlite3_close(db) == SQLITE_OK) - } - - func testDecimal() { - XCTAssert(csqlite_sqlite3_auto_extension_decimal() == SQLITE_OK) - - var db: OpaquePointer? - XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) - - var stmt: OpaquePointer? - XCTAssert(sqlite3_prepare_v2(db, "select decimal_add('1.67','2.33');", -1, &stmt, nil) == SQLITE_OK) - XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) - let r = String(cString: sqlite3_column_text(stmt, 0)) - XCTAssertEqual(r, "4.00") - - XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) - XCTAssert(sqlite3_close(db) == SQLITE_OK) - } - - func testIEEE() { - XCTAssert(csqlite_sqlite3_auto_extension_ieee754() == SQLITE_OK) - - var db: OpaquePointer? - XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) - - var stmt: OpaquePointer? - XCTAssert(sqlite3_prepare_v2(db, "select ieee754(45.25);", -1, &stmt, nil) == SQLITE_OK) - XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) - let f = String(cString: sqlite3_column_text(stmt, 0)) - XCTAssertEqual(f, "ieee754(181,-2)") - - XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) - XCTAssert(sqlite3_close(db) == SQLITE_OK) - } - - func testSeries() { - XCTAssert(csqlite_sqlite3_auto_extension_series() == SQLITE_OK) - - var db: OpaquePointer? - XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) - - var stmt: OpaquePointer? - XCTAssert(sqlite3_prepare_v2(db, "select * from generate_series(10,20,5);", -1, &stmt, nil) == SQLITE_OK) - XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) - var i = sqlite3_column_int(stmt, 0) - XCTAssertEqual(i, 10) - XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) - i = sqlite3_column_int(stmt, 0) - XCTAssertEqual(i, 15) - XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) - i = sqlite3_column_int(stmt, 0) - XCTAssertEqual(i, 20) - - XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) - XCTAssert(sqlite3_close(db) == SQLITE_OK) - } - - func testSHA3() { - XCTAssert(csqlite_sqlite3_auto_extension_sha3() == SQLITE_OK) - - var db: OpaquePointer? - XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) - - var stmt: OpaquePointer? - XCTAssert(sqlite3_prepare_v2(db, "select lower(hex(sha3('sqlite')));", -1, &stmt, nil) == SQLITE_OK) - XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) - let h = String(cString: sqlite3_column_text(stmt, 0)) - XCTAssertEqual(h, "963a88636d4c9cc3f011dfc9dc0058e96669d80a89893c68c0bb08a6a8208db3") - - XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) - XCTAssert(sqlite3_close(db) == SQLITE_OK) - } - - func testUUID() { - XCTAssert(csqlite_sqlite3_auto_extension_uuid() == SQLITE_OK) - - var db: OpaquePointer? - XCTAssert(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) - - var stmt: OpaquePointer? - XCTAssert(sqlite3_prepare_v2(db, "select uuid();", -1, &stmt, nil) == SQLITE_OK) - XCTAssert(sqlite3_step(stmt) == SQLITE_ROW) - let u = String(cString: sqlite3_column_text(stmt, 0)) - let comps = u.components(separatedBy: "-") - XCTAssertEqual(comps.count, 5) - XCTAssertEqual(comps[0].count, 8) - XCTAssertEqual(comps[1].count, 4) - XCTAssertEqual(comps[2].count, 4) - XCTAssertEqual(comps[3].count, 4) - XCTAssertEqual(comps[4].count, 12) - let uu = UUID(uuidString: u) - XCTAssertNotNil(uu) - - XCTAssert(sqlite3_finalize(stmt) == SQLITE_OK) - XCTAssert(sqlite3_close(db) == SQLITE_OK) - } } From 3e49f1d06ee8204e33de23478c2be6073b557f19 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Fri, 24 May 2024 07:11:09 -0500 Subject: [PATCH 02/15] Rename class and add header --- .../CSQLiteExtensionsTests.swift | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift b/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift index b40c60e..2bc2a80 100644 --- a/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift +++ b/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift @@ -1,8 +1,22 @@ +/* + ** 2024-05-24 + ** + ** 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. + ** + ****************************************************************************** + ** + ** Basic tests of CSQLiteExtensions functionality. + */ + import XCTest -@testable import CSQLite @testable import CSQLiteExtensions -final class CSQLiteTests: XCTestCase { +final class CSQLiteExtensionsTests: XCTestCase { override class func setUp() { super.setUp() // It's necessary to call sqlite3_initialize() since SQLITE_OMIT_AUTOINIT is defined From bd61218c220fc063496e2e41afd3388fd2442cf4 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Fri, 24 May 2024 07:14:52 -0500 Subject: [PATCH 03/15] Update header comment --- Sources/CSQLiteExtensions/csqlite_extensions.c | 2 +- Sources/CSQLiteExtensions/include/csqlite_extensions.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/CSQLiteExtensions/csqlite_extensions.c b/Sources/CSQLiteExtensions/csqlite_extensions.c index 267429c..f86c66f 100644 --- a/Sources/CSQLiteExtensions/csqlite_extensions.c +++ b/Sources/CSQLiteExtensions/csqlite_extensions.c @@ -10,7 +10,7 @@ ** ****************************************************************************** ** - ** Wrappers for C functions not easily accessible from Swift. + ** Functions to manage automatic database extension registration from Swift. */ #include "csqlite_extensions.h" diff --git a/Sources/CSQLiteExtensions/include/csqlite_extensions.h b/Sources/CSQLiteExtensions/include/csqlite_extensions.h index 35c8248..3e3bce6 100644 --- a/Sources/CSQLiteExtensions/include/csqlite_extensions.h +++ b/Sources/CSQLiteExtensions/include/csqlite_extensions.h @@ -10,7 +10,7 @@ ** ****************************************************************************** ** - ** Wrappers for C functions not easily accessible from Swift. + ** Functions to manage automatic database extension registration from Swift. */ // MARK: - Database extensions From 63e8a0a18a9b06c999ec8ccf81143d907e306689 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Sun, 2 Jun 2024 12:00:44 -0500 Subject: [PATCH 04/15] Update module maps --- Sources/CSQLite/include/module.modulemap | 1 - Sources/CSQLiteExtensions/include/module.modulemap | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Sources/CSQLiteExtensions/include/module.modulemap diff --git a/Sources/CSQLite/include/module.modulemap b/Sources/CSQLite/include/module.modulemap index a569ebe..d64a094 100644 --- a/Sources/CSQLite/include/module.modulemap +++ b/Sources/CSQLite/include/module.modulemap @@ -1,7 +1,6 @@ module CSQLite { header "sqlite3.h" header "sqlite3ext.h" - header "carray.h" header "csqlite_shims.h" export * } diff --git a/Sources/CSQLiteExtensions/include/module.modulemap b/Sources/CSQLiteExtensions/include/module.modulemap new file mode 100644 index 0000000..4101667 --- /dev/null +++ b/Sources/CSQLiteExtensions/include/module.modulemap @@ -0,0 +1,5 @@ +module CSQLiteExtensions { + header "csqlite_extensions.h" + header "carray.h" + export * +} From 9afebf0160400b0dfd112bde6599a6e43b5a0942 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Thu, 24 Oct 2024 10:01:33 -0500 Subject: [PATCH 05/15] Move percentile.c --- Sources/{CSQLite => CSQLiteExtensions}/percentile.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Sources/{CSQLite => CSQLiteExtensions}/percentile.c (100%) diff --git a/Sources/CSQLite/percentile.c b/Sources/CSQLiteExtensions/percentile.c similarity index 100% rename from Sources/CSQLite/percentile.c rename to Sources/CSQLiteExtensions/percentile.c From 1d12543a4a0ab053b0ab1a29b14cadde85dc16a3 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Thu, 24 Oct 2024 10:06:53 -0500 Subject: [PATCH 06/15] Move SQLITE_CORE define --- Package.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Package.swift b/Package.swift index 8969d3a..628e9d2 100644 --- a/Package.swift +++ b/Package.swift @@ -74,9 +74,6 @@ let package = Package( .define("SQLITE_ENABLE_SNAPSHOT"), .define("SQLITE_ENABLE_STMTVTAB"), .define("SQLITE_ENABLE_STAT4"), - // For statically linking extensions - // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension - .define("SQLITE_CORE", to: "1"), ], linkerSettings: [ .linkedLibrary("m"), @@ -87,6 +84,8 @@ let package = Package( "CSQLite", ], cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension .define("SQLITE_CORE", to: "1"), ]), .testTarget( From e7927b8fa5318180c6cadbffab2dda840edba6f1 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Wed, 28 May 2025 13:17:28 -0500 Subject: [PATCH 07/15] Update Swift 5.3 package --- Package.swift | 2 +- Package@swift-5.3.swift | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Package.swift b/Package.swift index 2b11aca..65663f3 100644 --- a/Package.swift +++ b/Package.swift @@ -270,7 +270,7 @@ let package = Package( name: "CSQLiteExtensionsTests", dependencies: [ "CSQLiteExtensions", - ]) + ]), ], cLanguageStandard: .gnu11 ) diff --git a/Package@swift-5.3.swift b/Package@swift-5.3.swift index eacc02b..ce159f0 100644 --- a/Package@swift-5.3.swift +++ b/Package@swift-5.3.swift @@ -92,6 +92,7 @@ let package = Package( name: "CSQLite", targets: [ "CSQLite", + "CSQLiteExtensions", ]), ], targets: [ @@ -99,19 +100,30 @@ 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: [ + "CSQLite", + ], + cSettings: [ // For statically linking extensions // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension .define("SQLITE_CORE", to: "1"), - ], - linkerSettings: [ - .linkedLibrary("m"), ]), .testTarget( name: "CSQLiteTests", dependencies: [ "CSQLite", - ]) + ]), + .testTarget( + name: "CSQLiteExtensionsTests", + dependencies: [ + "CSQLiteExtensions", + ]), ], cLanguageStandard: .gnu11 ) From aa69fafb95f244bdd1723d393f5bf2add20f806c Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Thu, 29 May 2025 08:14:46 -0500 Subject: [PATCH 08/15] Reorder `header` --- Sources/CSQLiteExtensions/include/module.modulemap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CSQLiteExtensions/include/module.modulemap b/Sources/CSQLiteExtensions/include/module.modulemap index 4101667..d44bd73 100644 --- a/Sources/CSQLiteExtensions/include/module.modulemap +++ b/Sources/CSQLiteExtensions/include/module.modulemap @@ -1,5 +1,5 @@ module CSQLiteExtensions { - header "csqlite_extensions.h" header "carray.h" + header "csqlite_extensions.h" export * } From 3feeaadd9d1b16351915dd7bed490110c0a2bd05 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Fri, 30 May 2025 10:07:03 -0500 Subject: [PATCH 09/15] Reorganize extensions and add traits for each --- Package.swift | 115 ++++++++++++++++++ README.md | 14 +++ .../CSQLiteExtensions/CSQLiteExtensions.swift | 46 +++++++ .../CSQLiteExtensions/csqlite_extensions.c | 96 --------------- .../include/module.modulemap | 5 - .../carray}/carray.c | 0 .../carray/csqlite_carray_extension.c | 28 +++++ .../carray}/include/carray.h | 0 .../carray/include/csqlite_carray_extension.h | 22 ++++ .../carray/include/module.modulemap | 5 + .../decimal/csqlite_decimal_extension.c | 28 +++++ .../decimal}/decimal.c | 0 .../include/csqlite_decimal_extension.h} | 2 +- .../decimal/include/module.modulemap | 4 + .../ieee754/csqlite_ieee754_extension.c | 28 +++++ .../ieee754}/ieee754.c | 0 .../include/csqlite_ieee754_extension.h | 22 ++++ .../ieee754/include/module.modulemap | 4 + .../percentile/csqlite_percentile_extension.c | 28 +++++ .../include/csqlite_percentile_extension.h | 22 ++++ .../percentile/include/module.modulemap | 4 + .../percentile}/percentile.c | 0 .../series/csqlite_series_extension.c | 28 +++++ .../series/include/csqlite_series_extension.h | 22 ++++ .../series/include/module.modulemap | 4 + .../series}/series.c | 0 .../extensions/sha3/csqlite_sha3_extension.c | 28 +++++ .../sha3/include/csqlite_sha3_extension.h | 22 ++++ .../extensions/sha3/include/module.modulemap | 4 + .../sha3}/shathree.c | 0 .../extensions/uuid/csqlite_uuid_extension.c | 28 +++++ .../uuid/include/csqlite_uuid_extension.h | 22 ++++ .../extensions/uuid/include/module.modulemap | 4 + .../uuid}/uuid.c | 0 .../CSQLiteExtensionsTests.swift | 15 +++ 35 files changed, 548 insertions(+), 102 deletions(-) create mode 100644 Sources/CSQLiteExtensions/CSQLiteExtensions.swift delete mode 100644 Sources/CSQLiteExtensions/csqlite_extensions.c delete mode 100644 Sources/CSQLiteExtensions/include/module.modulemap rename Sources/{CSQLiteExtensions => extensions/carray}/carray.c (100%) create mode 100644 Sources/extensions/carray/csqlite_carray_extension.c rename Sources/{CSQLiteExtensions => extensions/carray}/include/carray.h (100%) create mode 100644 Sources/extensions/carray/include/csqlite_carray_extension.h create mode 100644 Sources/extensions/carray/include/module.modulemap create mode 100644 Sources/extensions/decimal/csqlite_decimal_extension.c rename Sources/{CSQLiteExtensions => extensions/decimal}/decimal.c (100%) rename Sources/{CSQLiteExtensions/include/csqlite_extensions.h => extensions/decimal/include/csqlite_decimal_extension.h} (99%) create mode 100644 Sources/extensions/decimal/include/module.modulemap create mode 100644 Sources/extensions/ieee754/csqlite_ieee754_extension.c rename Sources/{CSQLiteExtensions => extensions/ieee754}/ieee754.c (100%) create mode 100644 Sources/extensions/ieee754/include/csqlite_ieee754_extension.h create mode 100644 Sources/extensions/ieee754/include/module.modulemap create mode 100644 Sources/extensions/percentile/csqlite_percentile_extension.c create mode 100644 Sources/extensions/percentile/include/csqlite_percentile_extension.h create mode 100644 Sources/extensions/percentile/include/module.modulemap rename Sources/{CSQLiteExtensions => extensions/percentile}/percentile.c (100%) create mode 100644 Sources/extensions/series/csqlite_series_extension.c create mode 100644 Sources/extensions/series/include/csqlite_series_extension.h create mode 100644 Sources/extensions/series/include/module.modulemap rename Sources/{CSQLiteExtensions => extensions/series}/series.c (100%) create mode 100644 Sources/extensions/sha3/csqlite_sha3_extension.c create mode 100644 Sources/extensions/sha3/include/csqlite_sha3_extension.h create mode 100644 Sources/extensions/sha3/include/module.modulemap rename Sources/{CSQLiteExtensions => extensions/sha3}/shathree.c (100%) create mode 100644 Sources/extensions/uuid/csqlite_uuid_extension.c create mode 100644 Sources/extensions/uuid/include/csqlite_uuid_extension.h create mode 100644 Sources/extensions/uuid/include/module.modulemap rename Sources/{CSQLiteExtensions => extensions/uuid}/uuid.c (100%) diff --git a/Package.swift b/Package.swift index 65663f3..3422c3a 100644 --- a/Package.swift +++ b/Package.swift @@ -219,6 +219,35 @@ let package = Package( name: "ENABLE_STAT4", description: "Enables the sqlite_stat4 table" ), + // Statically linked extensions + .trait( + name: "CSQLITE_ENABLE_CARRAY_EXTENSION", + description: "Enables the statically linked carray extension" + ), + .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_PERCENTILE_EXTENSION", + description: "Enables the statically linked percentile 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", @@ -240,6 +269,13 @@ let package = Package( "ENABLE_SNAPSHOT", "ENABLE_STMTVTAB", "ENABLE_STAT4", + "CSQLITE_ENABLE_CARRAY_EXTENSION", + "CSQLITE_ENABLE_DECIMAL_EXTENSION", + "CSQLITE_ENABLE_IEEE754_EXTENSION", + "CSQLITE_ENABLE_PERCENTILE_EXTENSION", + "CSQLITE_ENABLE_SERIES_EXTENSION", + "CSQLITE_ENABLE_SHA3_EXTENSION", + "CSQLITE_ENABLE_UUID_EXTENSION", ]), ], targets: [ @@ -254,8 +290,87 @@ let package = Package( .target( name: "CSQLiteExtensions", dependencies: [ +// "CSQLite", + .targetItem(name: "CSQLiteCArrayExtension", condition: .when(traits: ["CSQLITE_ENABLE_CARRAY_EXTENSION"])), + .targetItem(name: "CSQLiteDecimalExtension", condition: .when(traits: ["CSQLITE_ENABLE_DECIMAL_EXTENSION"])), + .targetItem(name: "CSQLiteIEEE754Extension", condition: .when(traits: ["CSQLITE_ENABLE_IEEE754_EXTENSION"])), + .targetItem(name: "CSQLitePercentileExtension", condition: .when(traits: ["CSQLITE_ENABLE_PERCENTILE_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: "CSQLiteCArrayExtension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/carray", + cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension + .define("SQLITE_CORE", to: "1"), + ]), + .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", + ], + 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: "CSQLitePercentileExtension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/percentile", + 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 diff --git a/README.md b/README.md index 55102b3..955edaa 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,20 @@ 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_CARRAY_EXTENSION | Y | [carray](https://sqlite.org/carray.html) | +| 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_PERCENTILE_EXTENSION | Y | [percentile](https://www.sqlite.org/src/file/ext/misc/percentile.c) | +| 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/CSQLiteExtensions/CSQLiteExtensions.swift b/Sources/CSQLiteExtensions/CSQLiteExtensions.swift new file mode 100644 index 0000000..22a5e5d --- /dev/null +++ b/Sources/CSQLiteExtensions/CSQLiteExtensions.swift @@ -0,0 +1,46 @@ +/* + ** 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_CARRAY_EXTENSION +@_exported import CSQLiteCArrayExtension +#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_PERCENTILE_EXTENSION +@_exported import CSQLitePercentileExtension +#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/CSQLiteExtensions/csqlite_extensions.c b/Sources/CSQLiteExtensions/csqlite_extensions.c deleted file mode 100644 index 443f5fc..0000000 --- a/Sources/CSQLiteExtensions/csqlite_extensions.c +++ /dev/null @@ -1,96 +0,0 @@ -/* - ** 2021-06-04 - ** - ** 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_extensions.h" -#include "sqlite3.h" - -// MARK: - Database extensions - -void sqlite3_carray_init(void); -int csqlite_sqlite3_auto_extension_carray() -{ - return sqlite3_auto_extension(sqlite3_carray_init); -} - -int csqlite_sqlite3_cancel_auto_extension_carray() -{ - return sqlite3_cancel_auto_extension(sqlite3_carray_init); -} - -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_percentile_init(void); -int csqlite_sqlite3_auto_extension_percentile() -{ - return sqlite3_auto_extension(sqlite3_percentile_init); -} - -int csqlite_sqlite3_cancel_auto_extension_percentile() -{ - return sqlite3_cancel_auto_extension(sqlite3_percentile_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/CSQLiteExtensions/include/module.modulemap b/Sources/CSQLiteExtensions/include/module.modulemap deleted file mode 100644 index d44bd73..0000000 --- a/Sources/CSQLiteExtensions/include/module.modulemap +++ /dev/null @@ -1,5 +0,0 @@ -module CSQLiteExtensions { - header "carray.h" - header "csqlite_extensions.h" - export * -} diff --git a/Sources/CSQLiteExtensions/carray.c b/Sources/extensions/carray/carray.c similarity index 100% rename from Sources/CSQLiteExtensions/carray.c rename to Sources/extensions/carray/carray.c diff --git a/Sources/extensions/carray/csqlite_carray_extension.c b/Sources/extensions/carray/csqlite_carray_extension.c new file mode 100644 index 0000000..6bc3b95 --- /dev/null +++ b/Sources/extensions/carray/csqlite_carray_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_carray_extension.h" +#include "sqlite3.h" + +void sqlite3_carray_init(void); +int csqlite_sqlite3_auto_extension_carray() +{ + return sqlite3_auto_extension(sqlite3_carray_init); +} + +int csqlite_sqlite3_cancel_auto_extension_carray() +{ + return sqlite3_cancel_auto_extension(sqlite3_carray_init); +} diff --git a/Sources/CSQLiteExtensions/include/carray.h b/Sources/extensions/carray/include/carray.h similarity index 100% rename from Sources/CSQLiteExtensions/include/carray.h rename to Sources/extensions/carray/include/carray.h diff --git a/Sources/extensions/carray/include/csqlite_carray_extension.h b/Sources/extensions/carray/include/csqlite_carray_extension.h new file mode 100644 index 0000000..0a6e54d --- /dev/null +++ b/Sources/extensions/carray/include/csqlite_carray_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_carray_init)` +int csqlite_sqlite3_auto_extension_carray(void); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_carray_init)` +int csqlite_sqlite3_cancel_auto_extension_carray(void); diff --git a/Sources/extensions/carray/include/module.modulemap b/Sources/extensions/carray/include/module.modulemap new file mode 100644 index 0000000..d3611dc --- /dev/null +++ b/Sources/extensions/carray/include/module.modulemap @@ -0,0 +1,5 @@ +module CSQLiteCArrayExtension { + header "carray.h" + header "csqlite_carray_extension.h" + export * +} 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/CSQLiteExtensions/decimal.c b/Sources/extensions/decimal/decimal.c similarity index 100% rename from Sources/CSQLiteExtensions/decimal.c rename to Sources/extensions/decimal/decimal.c diff --git a/Sources/CSQLiteExtensions/include/csqlite_extensions.h b/Sources/extensions/decimal/include/csqlite_decimal_extension.h similarity index 99% rename from Sources/CSQLiteExtensions/include/csqlite_extensions.h rename to Sources/extensions/decimal/include/csqlite_decimal_extension.h index 913fe86..bf93aca 100644 --- a/Sources/CSQLiteExtensions/include/csqlite_extensions.h +++ b/Sources/extensions/decimal/include/csqlite_decimal_extension.h @@ -1,5 +1,5 @@ /* - ** 2021-06-04 + ** 2025-05-30 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: 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/CSQLiteExtensions/ieee754.c b/Sources/extensions/ieee754/ieee754.c similarity index 100% rename from Sources/CSQLiteExtensions/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/percentile/csqlite_percentile_extension.c b/Sources/extensions/percentile/csqlite_percentile_extension.c new file mode 100644 index 0000000..1af1bf8 --- /dev/null +++ b/Sources/extensions/percentile/csqlite_percentile_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_percentile_extension.h" +#include "sqlite3.h" + +void sqlite3_percentile_init(void); +int csqlite_sqlite3_auto_extension_percentile() +{ + return sqlite3_auto_extension(sqlite3_percentile_init); +} + +int csqlite_sqlite3_cancel_auto_extension_percentile() +{ + return sqlite3_cancel_auto_extension(sqlite3_percentile_init); +} diff --git a/Sources/extensions/percentile/include/csqlite_percentile_extension.h b/Sources/extensions/percentile/include/csqlite_percentile_extension.h new file mode 100644 index 0000000..d379b61 --- /dev/null +++ b/Sources/extensions/percentile/include/csqlite_percentile_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_percentile_init)` +int csqlite_sqlite3_auto_extension_percentile(void); +/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_percentile_init)` +int csqlite_sqlite3_cancel_auto_extension_percentile(void); diff --git a/Sources/extensions/percentile/include/module.modulemap b/Sources/extensions/percentile/include/module.modulemap new file mode 100644 index 0000000..682c716 --- /dev/null +++ b/Sources/extensions/percentile/include/module.modulemap @@ -0,0 +1,4 @@ +module CSQLitePercentileExtension { + header "csqlite_percentile_extension.h" + export * +} diff --git a/Sources/CSQLiteExtensions/percentile.c b/Sources/extensions/percentile/percentile.c similarity index 100% rename from Sources/CSQLiteExtensions/percentile.c rename to Sources/extensions/percentile/percentile.c 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/CSQLiteExtensions/series.c b/Sources/extensions/series/series.c similarity index 100% rename from Sources/CSQLiteExtensions/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/CSQLiteExtensions/shathree.c b/Sources/extensions/sha3/shathree.c similarity index 100% rename from Sources/CSQLiteExtensions/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/CSQLiteExtensions/uuid.c b/Sources/extensions/uuid/uuid.c similarity index 100% rename from Sources/CSQLiteExtensions/uuid.c rename to Sources/extensions/uuid/uuid.c diff --git a/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift b/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift index 6bf6438..ad7e1f5 100644 --- a/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift +++ b/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift @@ -14,6 +14,7 @@ */ import XCTest +import CSQLite @testable import CSQLiteExtensions final class CSQLiteExtensionTests: XCTestCase { @@ -25,6 +26,7 @@ final class CSQLiteExtensionTests: XCTestCase { } #endif +#if swift(<6.1) || CSQLITE_ENABLE_CARRAY_EXTENSION func testCarray() { XCTAssertEqual(csqlite_sqlite3_auto_extension_carray(), SQLITE_OK) @@ -50,7 +52,9 @@ final class CSQLiteExtensionTests: XCTestCase { XCTAssertEqual(sqlite3_finalize(stmt), SQLITE_OK) XCTAssertEqual(sqlite3_close(db), SQLITE_OK) } +#endif +#if swift(<6.1) || CSQLITE_ENABLE_DECIMAL_EXTENSION func testDecimal() { XCTAssertEqual(csqlite_sqlite3_auto_extension_decimal(), SQLITE_OK) @@ -66,7 +70,9 @@ final class CSQLiteExtensionTests: XCTestCase { XCTAssertEqual(sqlite3_finalize(stmt), SQLITE_OK) XCTAssertEqual(sqlite3_close(db), SQLITE_OK) } +#endif +#if swift(<6.1) || CSQLITE_ENABLE_IEEE754_EXTENSION func testIEEE() { XCTAssertEqual(csqlite_sqlite3_auto_extension_ieee754(), SQLITE_OK) @@ -82,7 +88,9 @@ final class CSQLiteExtensionTests: XCTestCase { XCTAssertEqual(sqlite3_finalize(stmt), SQLITE_OK) XCTAssertEqual(sqlite3_close(db), SQLITE_OK) } +#endif +#if swift(<6.1) || CSQLITE_ENABLE_PERCENTILE_EXTENSION func testPercentile() { XCTAssertEqual(csqlite_sqlite3_auto_extension_percentile(), SQLITE_OK) @@ -97,7 +105,9 @@ final class CSQLiteExtensionTests: XCTestCase { XCTAssertEqual(sqlite3_finalize(stmt), SQLITE_OK) XCTAssertEqual(sqlite3_close(db), SQLITE_OK) } +#endif +#if swift(<6.1) || CSQLITE_ENABLE_SERIES_EXTENSION func testSeries() { XCTAssertEqual(csqlite_sqlite3_auto_extension_series(), SQLITE_OK) @@ -116,7 +126,9 @@ final class CSQLiteExtensionTests: XCTestCase { XCTAssertEqual(sqlite3_finalize(stmt), SQLITE_OK) XCTAssertEqual(sqlite3_close(db), SQLITE_OK) } +#endif +#if swift(<6.1) || CSQLITE_ENABLE_SHA3_EXTENSION func testSHA3() { XCTAssertEqual(csqlite_sqlite3_auto_extension_sha3(), SQLITE_OK) @@ -132,7 +144,9 @@ final class CSQLiteExtensionTests: XCTestCase { XCTAssertEqual(sqlite3_finalize(stmt), SQLITE_OK) XCTAssertEqual(sqlite3_close(db), SQLITE_OK) } +#endif +#if swift(<6.1) || CSQLITE_ENABLE_UUID_EXTENSION func testUUID() { XCTAssertEqual(csqlite_sqlite3_auto_extension_uuid(), SQLITE_OK) @@ -156,4 +170,5 @@ final class CSQLiteExtensionTests: XCTestCase { XCTAssertEqual(sqlite3_finalize(stmt), SQLITE_OK) XCTAssertEqual(sqlite3_close(db), SQLITE_OK) } +#endif } From 1ce16697359f991773a832cf12709be0edaf3592 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Fri, 30 May 2025 10:35:12 -0500 Subject: [PATCH 10/15] Remove comment --- Package.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Package.swift b/Package.swift index 3422c3a..9c3d9d7 100644 --- a/Package.swift +++ b/Package.swift @@ -290,7 +290,6 @@ let package = Package( .target( name: "CSQLiteExtensions", dependencies: [ -// "CSQLite", .targetItem(name: "CSQLiteCArrayExtension", condition: .when(traits: ["CSQLITE_ENABLE_CARRAY_EXTENSION"])), .targetItem(name: "CSQLiteDecimalExtension", condition: .when(traits: ["CSQLITE_ENABLE_DECIMAL_EXTENSION"])), .targetItem(name: "CSQLiteIEEE754Extension", condition: .when(traits: ["CSQLITE_ENABLE_IEEE754_EXTENSION"])), From 47fc68b613bb2b63ddafd157acc3108b8b4f72d0 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Fri, 30 May 2025 10:35:51 -0500 Subject: [PATCH 11/15] Add extension targets --- Package@swift-5.3.swift | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/Package@swift-5.3.swift b/Package@swift-5.3.swift index ce159f0..6992968 100644 --- a/Package@swift-5.3.swift +++ b/Package@swift-5.3.swift @@ -106,9 +106,87 @@ let package = Package( ]), .target( name: "CSQLiteExtensions", + dependencies: [ + "CSQLiteCArrayExtension", + "CSQLiteDecimalExtension", + "CSQLiteIEEE754Extension", + "CSQLitePercentileExtension", + "CSQLiteSeriesExtension", + "CSQLiteSHA3Extension", + "CSQLiteUUIDExtension", + ]), + .target( + name: "CSQLiteCArrayExtension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/carray", + cSettings: [ + // For statically linking extensions + // https://sqlite.org/loadext.html#statically_linking_a_run_time_loadable_extension + .define("SQLITE_CORE", to: "1"), + ]), + .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", + ], + 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: "CSQLitePercentileExtension", + dependencies: [ + "CSQLite", + ], + path: "Sources/extensions/percentile", + 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 From 96205313666dc7e9e1967da6904c1618a685d4ca Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Fri, 30 May 2025 10:53:06 -0500 Subject: [PATCH 12/15] Fix copy/paste error --- .../include/csqlite_decimal_extension.h | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/Sources/extensions/decimal/include/csqlite_decimal_extension.h b/Sources/extensions/decimal/include/csqlite_decimal_extension.h index bf93aca..05a1f1f 100644 --- a/Sources/extensions/decimal/include/csqlite_decimal_extension.h +++ b/Sources/extensions/decimal/include/csqlite_decimal_extension.h @@ -16,37 +16,7 @@ // MARK: - Database extensions // See https://sqlite.org/loadext.html -/// Equivalent to `sqlite3_auto_extension(sqlite3_carray_init)` -int csqlite_sqlite3_auto_extension_carray(void); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_carray_init)` -int csqlite_sqlite3_cancel_auto_extension_carray(void); - /// 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_percentile_init)` -int csqlite_sqlite3_auto_extension_percentile(void); -/// Equivalent to `sqlite3_cancel_auto_extension(sqlite3_percentile_init)` -int csqlite_sqlite3_cancel_auto_extension_percentile(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); From 02fd6be33bbb373fdb918f37a6579761775e5065 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Fri, 30 May 2025 11:31:08 -0500 Subject: [PATCH 13/15] Remove - --- Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift b/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift index ad7e1f5..481c6f3 100644 --- a/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift +++ b/Tests/CSQLiteExtensionsTests/CSQLiteExtensionsTests.swift @@ -10,7 +10,7 @@ ** ****************************************************************************** ** - ** Basic tests of CSQLite statically-linked extension functionality. + ** Basic tests of CSQLite statically linked extension functionality. */ import XCTest From 57674323a48f01abebabd1422b8c41948655b400 Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Fri, 30 May 2025 11:32:10 -0500 Subject: [PATCH 14/15] Use plain C for CSQLiteExtensions module --- .../CSQLiteExtensions/CSQLiteExtensions.swift | 46 ------------------- Sources/CSQLiteExtensions/dummy.c | 1 + .../include/csqlite_extensions.h | 45 ++++++++++++++++++ .../include/module.modulemap | 4 ++ 4 files changed, 50 insertions(+), 46 deletions(-) delete mode 100644 Sources/CSQLiteExtensions/CSQLiteExtensions.swift create mode 100644 Sources/CSQLiteExtensions/dummy.c create mode 100644 Sources/CSQLiteExtensions/include/csqlite_extensions.h create mode 100644 Sources/CSQLiteExtensions/include/module.modulemap diff --git a/Sources/CSQLiteExtensions/CSQLiteExtensions.swift b/Sources/CSQLiteExtensions/CSQLiteExtensions.swift deleted file mode 100644 index 22a5e5d..0000000 --- a/Sources/CSQLiteExtensions/CSQLiteExtensions.swift +++ /dev/null @@ -1,46 +0,0 @@ -/* - ** 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_CARRAY_EXTENSION -@_exported import CSQLiteCArrayExtension -#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_PERCENTILE_EXTENSION -@_exported import CSQLitePercentileExtension -#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/CSQLiteExtensions/dummy.c b/Sources/CSQLiteExtensions/dummy.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Sources/CSQLiteExtensions/dummy.c @@ -0,0 +1 @@ + diff --git a/Sources/CSQLiteExtensions/include/csqlite_extensions.h b/Sources/CSQLiteExtensions/include/csqlite_extensions.h new file mode 100644 index 0000000..5497d26 --- /dev/null +++ b/Sources/CSQLiteExtensions/include/csqlite_extensions.h @@ -0,0 +1,45 @@ +/* + ** 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 + +#if __has_include("csqlite_carray_extension.h") +#include "csqlite_carray_extension.h" +#endif + +#if __has_include("csqlite_decimal_extension.h") +#include "csqlite_decimal_extension.h" +#endif + +#if __has_include("csqlite_ieee754_extension.h") +#include "csqlite_ieee754_extension.h" +#endif + +#if __has_include("csqlite_percentile_extension.h") +#include "csqlite_percentile_extension.h" +#endif + +#if __has_include("csqlite_series_extension.h") +#include "csqlite_series_extension.h" +#endif + +#if __has_include("csqlite_sha3_extension.h") +#include "csqlite_sha3_extension.h" +#endif + +#if __has_include("csqlite_uuid_extension.h") +#include "csqlite_uuid_extension.h" +#endif diff --git a/Sources/CSQLiteExtensions/include/module.modulemap b/Sources/CSQLiteExtensions/include/module.modulemap new file mode 100644 index 0000000..a7e2995 --- /dev/null +++ b/Sources/CSQLiteExtensions/include/module.modulemap @@ -0,0 +1,4 @@ +module CSQLiteExtensions { + header "csqlite_extensions.h" + export * +} From 9c8f913b6ca63e8b6de375c872ccd8c879d7397c Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Sat, 31 May 2025 06:49:10 -0500 Subject: [PATCH 15/15] Revert "Use plain C for CSQLiteExtensions module" This reverts commit 57674323a48f01abebabd1422b8c41948655b400. --- .../CSQLiteExtensions/CSQLiteExtensions.swift | 46 +++++++++++++++++++ Sources/CSQLiteExtensions/dummy.c | 1 - .../include/csqlite_extensions.h | 45 ------------------ .../include/module.modulemap | 4 -- 4 files changed, 46 insertions(+), 50 deletions(-) create mode 100644 Sources/CSQLiteExtensions/CSQLiteExtensions.swift delete mode 100644 Sources/CSQLiteExtensions/dummy.c delete mode 100644 Sources/CSQLiteExtensions/include/csqlite_extensions.h delete mode 100644 Sources/CSQLiteExtensions/include/module.modulemap diff --git a/Sources/CSQLiteExtensions/CSQLiteExtensions.swift b/Sources/CSQLiteExtensions/CSQLiteExtensions.swift new file mode 100644 index 0000000..22a5e5d --- /dev/null +++ b/Sources/CSQLiteExtensions/CSQLiteExtensions.swift @@ -0,0 +1,46 @@ +/* + ** 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_CARRAY_EXTENSION +@_exported import CSQLiteCArrayExtension +#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_PERCENTILE_EXTENSION +@_exported import CSQLitePercentileExtension +#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/CSQLiteExtensions/dummy.c b/Sources/CSQLiteExtensions/dummy.c deleted file mode 100644 index 8b13789..0000000 --- a/Sources/CSQLiteExtensions/dummy.c +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Sources/CSQLiteExtensions/include/csqlite_extensions.h b/Sources/CSQLiteExtensions/include/csqlite_extensions.h deleted file mode 100644 index 5497d26..0000000 --- a/Sources/CSQLiteExtensions/include/csqlite_extensions.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - ** 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 - -#if __has_include("csqlite_carray_extension.h") -#include "csqlite_carray_extension.h" -#endif - -#if __has_include("csqlite_decimal_extension.h") -#include "csqlite_decimal_extension.h" -#endif - -#if __has_include("csqlite_ieee754_extension.h") -#include "csqlite_ieee754_extension.h" -#endif - -#if __has_include("csqlite_percentile_extension.h") -#include "csqlite_percentile_extension.h" -#endif - -#if __has_include("csqlite_series_extension.h") -#include "csqlite_series_extension.h" -#endif - -#if __has_include("csqlite_sha3_extension.h") -#include "csqlite_sha3_extension.h" -#endif - -#if __has_include("csqlite_uuid_extension.h") -#include "csqlite_uuid_extension.h" -#endif diff --git a/Sources/CSQLiteExtensions/include/module.modulemap b/Sources/CSQLiteExtensions/include/module.modulemap deleted file mode 100644 index a7e2995..0000000 --- a/Sources/CSQLiteExtensions/include/module.modulemap +++ /dev/null @@ -1,4 +0,0 @@ -module CSQLiteExtensions { - header "csqlite_extensions.h" - export * -}