1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00
Files
archived-php-src/ext/pdo_sqlite/php_pdo_sqlite_int.h
Samuel Štancl a09d919ce8 Implement GH-8967: Add PDO_SQLITE_ATTR_TRANSACTION_MODE (#19317)
This commit implements GH-8967.

SQLite supports multiple transaction modes. These include:
- DEFERRED (default) only acquires a lock when you start a read/write
- IMMEDIATE acquires a reserved lock
- EXCLUSIVE acquires an exclusive lock (stricter than immediate)

In WAL mode IMMEDIATE and EXCLUSIVE are identical.

One reason for wanting to specify a transaction mode is that SQLite
doesn't respect busy_timeout when a DEFERRED transaction tries to
upgrade a read lock to a write lock. Normally if you try to acquire a
lock and have busy_timeout configured, SQLite will wait for that period
until giving up and erroring out (SQLITE_BUSY). With DEFERRED, if you
have a transaction that first reads and there's a concurrent writer
while it's trying to upgrade to a write lock, you will immediately get
SQLITE_BUSY regardless of your busy_timeout.

Prior to this commit, the only available workarounds were:
- Using $pdo->exec("BEGIN IMMEDIATE TRANSACTION") instead of
  $pdo->beginTransaction()
- Doing a dummy write at the start of each transaction so you don't get
  stuck with a read lock

Both of those aren't very usable, especially in a framework context
where the user doesn't have complete control over how transactions are
started.

To address that, this commit adds four class constants to Pdo\Sqlite:
- ATTR_TRANSACTION_MODE -- a new attribute
- TRANSACTION_MODE_DEFERRED = 0
- TRANSACTION_MODE_IMMEDIATE = 1
- TRANSACTION_MODE_EXCLUSIVE = 2

These can be used as:
  $pdo->setAttribute(
    $pdo::ATTR_TRANSACTION_MODE,
    $pdo::TRANSACTION_MODE_IMMEDIATE
  );
2025-09-07 15:42:30 +02:00

90 lines
2.9 KiB
C

/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Wez Furlong <wez@php.net> |
+----------------------------------------------------------------------+
*/
#ifndef PHP_PDO_SQLITE_INT_H
#define PHP_PDO_SQLITE_INT_H
#include <sqlite3.h>
typedef struct {
const char *file;
int line;
unsigned int errcode;
char *errmsg;
} pdo_sqlite_error_info;
struct pdo_sqlite_func {
struct pdo_sqlite_func *next;
int argc;
zend_string *funcname;
/* accelerated callback references */
zend_fcall_info_cache func;
zend_fcall_info_cache step;
zend_fcall_info_cache fini;
};
struct pdo_sqlite_collation {
struct pdo_sqlite_collation *next;
zend_string *name;
zend_fcall_info_cache callback;
};
typedef struct {
sqlite3 *db;
pdo_sqlite_error_info einfo;
struct pdo_sqlite_func *funcs;
struct pdo_sqlite_collation *collations;
zend_fcall_info_cache authorizer_fcc;
enum pdo_sqlite_transaction_mode transaction_mode;
} pdo_sqlite_db_handle;
typedef struct {
pdo_sqlite_db_handle *H;
sqlite3_stmt *stmt;
unsigned pre_fetched:1;
unsigned done:1;
} pdo_sqlite_stmt;
extern const pdo_driver_t pdo_sqlite_driver;
extern int pdo_sqlite_scanner(pdo_scanner_t *s);
extern int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line);
#define pdo_sqlite_error(s) _pdo_sqlite_error(s, NULL, __FILE__, __LINE__)
#define pdo_sqlite_error_stmt(s) _pdo_sqlite_error(stmt->dbh, stmt, __FILE__, __LINE__)
extern const struct pdo_stmt_methods sqlite_stmt_methods;
enum {
PDO_SQLITE_ATTR_OPEN_FLAGS = PDO_ATTR_DRIVER_SPECIFIC,
PDO_SQLITE_ATTR_READONLY_STATEMENT,
PDO_SQLITE_ATTR_EXTENDED_RESULT_CODES,
PDO_SQLITE_ATTR_BUSY_STATEMENT,
PDO_SQLITE_ATTR_EXPLAIN_STATEMENT,
PDO_SQLITE_ATTR_TRANSACTION_MODE
};
typedef int pdo_sqlite_create_collation_callback(void*, int, const void*, int, const void*);
void pdo_sqlite_create_function_internal(INTERNAL_FUNCTION_PARAMETERS);
void pdo_sqlite_create_aggregate_internal(INTERNAL_FUNCTION_PARAMETERS);
void pdo_sqlite_create_collation_internal(INTERNAL_FUNCTION_PARAMETERS, pdo_sqlite_create_collation_callback callback);
#endif