pimbay/sequence-number-sql

Centralized SQL snippets for atomic number sequence operations (UPSERT, pessimistic locking) and table schema definitions across MySQL, MariaDB, PostgreSQL, MSSQL, SQLite, Oracle and IBM DB2.

Maintainers

Package info

codeberg.org/pimbay/sequence-number-sql

Homepage

Issues

Documentation

pkg:composer/pimbay/sequence-number-sql

Statistics

Installs: 2

Dependents: 2

Suggesters: 0

v1.0.0 2026-05-15 07:28 UTC

This package is auto-updated.

Last update: 2026-05-15 21:07:12 UTC


README

License

Centralized SQL snippets for atomic number sequence operations across MySQL, MariaDB, PostgreSQL, MSSQL, SQLite, Oracle and IBM DB2. Part of the PimBay Sequence Stack.

Purpose

Decouples SQL from PHP adapter code. PDO adapters load snippets via file_get_contents(), replace the {table} placeholder, bind ? positional parameters and execute. No PHP code — SQL and documentation only.

Installation

composer require pimbay/sequence-number-sql

SQL Snippets

UPSERT (sql/upsert/)

Atomic insert-or-increment in a single statement. Available for databases that support returning the new value without a follow-up query.

FileDatabaseStrategy
mysql.sqlMySQLON DUPLICATE KEY UPDATE + LAST_INSERT_ID()
mariadb.sqlMariaDB 10.3+ON DUPLICATE KEY UPDATE + LAST_INSERT_ID()
mariadb-returning.sqlMariaDB 10.5+ON DUPLICATE KEY UPDATE RETURNING
postgres.sqlPostgreSQLON CONFLICT DO UPDATE RETURNING
mssql.sqlMSSQLMERGE WITH (HOLDLOCK) OUTPUT

Oracle and IBM DB2 do not support atomic UPSERT with single-statement value retrieval — use pessimistic locking instead.

Pessimistic locking (sql/pessimistic-locking/)

Transaction-based locking. Required for SQLite, Oracle and IBM DB2. Available as an alternative for all other databases.

FileDatabasesNotes
select_for_update.sqlMySQL, MariaDB, PostgreSQL, OracleStandard FOR UPDATE
select_for_update_db2.sqlIBM DB2FOR UPDATE WITH RS — explicit isolation level
select_with_lock.sqlMSSQLWITH (UPDLOCK, ROWLOCK)
select.sqlSQLitePlain SELECT — file-level lock provides isolation
insert.sqlAllINSERT when sequence does not exist
update.sqlAllUPDATE existing sequence — sets updated_at explicitly

Schema (schema/)

CREATE TABLE definitions for each database.

FileDatabase
mysql.sqlMySQL
mariadb.sqlMariaDB
postgres.sqlPostgreSQL
sqlite.sqlSQLite
mssql.sqlMSSQL
oracle.sqlOracle
db2.sqlIBM DB2

Usage

$sql = file_get_contents(__DIR__ . '/vendor/pimbay/sequence-number-sql/sql/upsert/mysql.sql');
$sql = str_replace('{table}', $tableName, $sql);
$stmt = $pdo->prepare($sql);
$stmt->execute([$group, $name, $initialValue, $metadata]);

The {table} placeholder must always be replaced before prepare(). Never pass table names from user input.

Documentation

License

Public domain — Unlicense

Created by Jan Sarmir · No conditions · No copyright