https://sqlite.org/src4/doc/trunk/www/design.wiki logo SQLite4 The Design Of SQLite4 Not logged in HomeTimelineBranchesTagsWikiLogin 1.0 Executive Summary 1. SQLite4 is a compact, self-contained, zero-adminstration, ACID database engine in a library, just like SQLite3, but with an improved interface and file format. 2. The run-time environment is encapsulated in an object. 3. A greatly simplified Key/Value storage engine is used: a. A single large key space - not separate key spaces for each table and index as in SQLite3. b. Keys sort in lexicographical order. c. Multiple storage engines, interchangeable at run-time. d. Default on-disk storage engine uses a log-structured merge database. 4. The PRIMARY KEY of a table really is used as the key to the storage engine. 5. Decimal arithmetic is used. 6. Foreign key constraints and recursive triggers are on by default. 7. Covering indices can be declared explicitly. 2.0 Overview SQLite4 is an alternative, not a replacement, for SQLite3. SQLite3 is not going away. SQLite3 and SQLite4 will be supported in parallel. The SQLite3 legacy will not be abandoned. SQLite3 will continue to be maintained and improved. But designers of new systems will now have the option to select SQLite4 instead of SQLite3 if desired. SQLite4 strives to keep the best features of SQLite3 while addressing issues with SQLite3 that can not be fixed without breaking compatibility. Among the features that will remain in common between SQLite3 that SQLite4 are: 1. SQLite4 is a complete, relational, transactional, ACID, SQL database database engine contained in a compact library that links into a larger application. There is no server. I/O is direct to disk. 2. The source code for SQLite4 is available to anyone for any purpose. There are no restrictions on copying, distribution, or publication of sources or compiled binaries. There are no viral licenses to worry over. 3. Dynamic typing is used, rather than the rigid static typing of most other SQL database engines. 4. The (default) on-disk database image is a single disk file with a well-documented and stable file format, making the SQLite4 library suitable for use as an application file format. 5. SQLite4 will be fast and reliable and require no administrator attention. It just works. 6. The implementation of SQLite4 has minimal external dependencies so that it is easy to incorporate into embedded systems or other unusual runtime environments. The implementation is still in C, which we regard as the universal assembly language. However, SQLite4 will use more C99 features than did SQLite3, while still striving to maintain compatibility with popular compilers. SQLite4 makes use of standard data types such as size_t, int64_t, uint64_t, and others. The programming interface for SQLite4 is similar to that of SQLite3, though with all name prefixes changed from "sqlite3_" to "sqlite4_". Legacy and deprecated interfaces in SQLite3 have been removed from SQLite4. There are additional parameters to some routines, and on occasion the parameters are altered slightly or reordered. Some of the interface names have been modified to be more rational. But on the whole, the programming interface for SQLite4 is remarkably similar to SQLite3, and porting an application from SQLite3 to SQLite4 will usually involve only an hour or two search-and-replace. SQLite3 and SQLite4 share no symbols in common, so it is possible to link both SQLite3 and SQLite4 into the same process and use them both at the same time. 3.0 Key Changes In SQLite4 3.1 The Run-Time Environment Object Some of the interfaces in SQLite4 take a new (added) first parameter which is a pointer to an sqlite4_env object that defines the run-time environment. Examples of routines that require the sqlite4_env pointer include: * sqlite4_open() * sqlite4_malloc(), sqlite4_realloc(), and sqlite4_free() * sqlite4_mprintf() * sqlite4_random() * sqlite4_config() An instance of an sqlite4_env object defines how SQLite4 interacts with the rest of the system. An sqlite4_env object includes methods to: * allocate, enter, leave, and deallocate mutexes, * allocate, resize, and free heap memory, * access and control the underlying key/value storage engines, * initialize the built-in PRNG with a high-quality random seed, * acquire the current time and date and the local timezone, * record error log message. Builds of SQLite4 for standard platforms (windows and unix) contain a global sqlite4_env object that is normally appropriate for use on that platform. Any interface routine that requires a pointer to an sqlite4_env object will use the global default sqlite4_env object if a NULL pointer is passed on its sqlite4_env parameter. Most applications never need to pass anything other than this NULL pointer. However, some applications may want to have two or more instances of SQLite4 running in the same address space where each instance uses different mutex primitives, a different heap, different date/time functions, etc. SQLite4 accommodates this need by allowing each database instance to be created using a different sqlite4_env object. The sqlite4_env object also eliminates all use of global and static variables in SQLite4, making SQLite4 easier to port to some embedded systems with limited support for static or global data. 3.2 Simplified Key/Value Storage Engine SQLite4 uses a key/value storage engine which has a greatly simplified interface relative to SQLite3. The storage engine is pluggable; it can be changed out at runtime by making appropriate alterations to the sqlite4_env object prior to opening a new database connection. SQLite4 needs a storage engine that implements ordered key/value pairs where the key and value are arbitrary-length binary data. Keys must be unique and must be ordered lexicographically. In other words, the keys should be ordered according to a comparison function like the following: int key_compare(const void *key1, int n1, const void *key2, int n2){ int c = memcmp(key1, key2, n1