sqlite3ext.h

Overview

The sqlite3ext.h header file provides the interface specifically designed for building shared library extensions that can be dynamically loaded into an SQLite instance. Rather than including the standard sqlite3.h file, extensions should include sqlite3ext.h to access the SQLite API through a versioned, stable function pointer table. This indirection supports binary compatibility across different SQLite versions by avoiding direct linking with SQLite’s internal symbols.

The core of this interface is the sqlite3_api_routines structure, which contains pointers to all public SQLite API functions. Extensions receive a pointer to this structure on initialization, allowing them to invoke SQLite functionality safely and portably.

This file also defines macros that redirect calls to SQLite API functions through the global sqlite3_api pointer, enabling extensions to call SQLite functions as if they were normal API calls.

Key Components

struct sqlite3_api_routines

This structure encapsulates the entire SQLite API as function pointers. Each member corresponds to one SQLite API function, such as sqlite3_bind_blob, sqlite3_step, or sqlite3_exec. To maintain backward compatibility, new API functions must be appended at the end of this structure.

Important Details:

Usage Example:

When an extension is loaded, SQLite passes a pointer to an instance of sqlite3_api_routines:

int sqlite3_extension_init(
  sqlite3 *db,
  char **pzErrMsg,
  const sqlite3_api_routines *pApi
){
  SQLITE_EXTENSION_INIT2(pApi);  // Initialize global pointer
  // Now the extension can call SQLite functions via sqlite3_api pointer
  // e.g., sqlite3_api->exec(db, "CREATE TABLE...", NULL, NULL, NULL);
  return SQLITE_OK;
}

typedef int (*sqlite3_loadext_entry)(sqlite3*, char**, const sqlite3_api_routines*);

Defines the standard function signature for SQLite extension entry points. Extensions must implement an initialization function matching this signature to be loaded correctly.

API Redirection Macros

These macros redefine all SQLite API calls so that calls in extensions are routed through the global sqlite3_api pointer, which points to the sqlite3_api_routines structure provided at runtime.

For example:

#define sqlite3_bind_blob sqlite3_api->bind_blob
#define sqlite3_step sqlite3_api->step
#define sqlite3_finalize sqlite3_api->finalize

This means extension code can use normal SQLite API function calls transparently, while under the hood calls are dispatched through the function pointers.

Conditional Compilation

Extension Initialization Macros

Defines macros to assist extension authors in managing the global sqlite3_api pointer:

Example usage in an extension source file:

SQLITE_EXTENSION_INIT1

int sqlite3_extension_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi){
  SQLITE_EXTENSION_INIT2(pApi);
  // Extension initialization code
  return SQLITE_OK;
}

Implementation Details and Algorithms

Interaction with Other Parts of the System

Mermaid Diagram

flowchart TD
A[Extension Entry Point] --> B[Receives sqlite3_api_routines*]
B --> C[sqlite3_api pointer initialized]
C --> D[SQLite API calls via macros]
D --> E{sqlite3_api_routines struct}
E --> F[Function pointer: bind_blob]
E --> G[Function pointer: step]
E --> H[Function pointer: finalize]
E --> I[... other API functions ...]

Summary of the Main API Functions in sqlite3_api_routines

Usage Patterns

References


This file is a critical component in the SQLite extension mechanism, enabling binary compatibility and safe interaction between dynamically loaded extensions and the SQLite core engine.