Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Driver Options

php-sqlx drivers can be configured with various options when created.

Using Options

use Sqlx\DriverFactory;
use Sqlx\DriverOptions;

$driver = DriverFactory::make([
    DriverOptions::OPT_URL => "postgres://localhost/mydb",
    DriverOptions::OPT_MAX_CONNECTIONS => 10,
    DriverOptions::OPT_ASSOC_ARRAYS => true,
]);

Core Options

OPT_URL

Required. The database connection URL.

DriverOptions::OPT_URL => "postgres://user:pass@localhost:5432/mydb"

See Connection Strings for URL formats.

OPT_ASSOC_ARRAYS

Return query results as associative arrays instead of objects.

DriverOptions::OPT_ASSOC_ARRAYS => true  // Default: false
// With OPT_ASSOC_ARRAYS => false (default)
$user = $driver->queryRow("SELECT * FROM users WHERE id = 1");
echo $user->name;  // Object property access

// With OPT_ASSOC_ARRAYS => true
$user = $driver->queryRow("SELECT * FROM users WHERE id = 1");
echo $user['name'];  // Array access

OPT_READONLY

Mark the driver as read-only. Write operations will throw NotPermittedException.

DriverOptions::OPT_READONLY => true  // Default: false

Useful for:

  • Replica-only connections
  • Preventing accidental writes
  • Analytics/reporting connections

OPT_PERSISTENT_NAME

Enable persistent connections with a named pool.

DriverOptions::OPT_PERSISTENT_NAME => "myapp_primary"

Connections persist across PHP requests (in PHP-FPM). Different names create separate pools.

Query Behavior

OPT_COLLAPSIBLE_IN

When true, empty arrays in IN clauses become FALSE (or TRUE for NOT IN).

DriverOptions::OPT_COLLAPSIBLE_IN => true  // Default: true
// With OPT_COLLAPSIBLE_IN => true
$driver->queryAll("SELECT * FROM users WHERE id IN (?)", [[]]);
// Becomes: SELECT * FROM users WHERE FALSE

// With OPT_COLLAPSIBLE_IN => false
$driver->queryAll("SELECT * FROM users WHERE id IN (?)", [[]]);
// Throws ParameterException

AST Cache Options

php-sqlx caches parsed query ASTs for performance.

OPT_AST_CACHE_SHARD_COUNT

Number of cache shards (for concurrent access).

DriverOptions::OPT_AST_CACHE_SHARD_COUNT => 8  // Default: 8

OPT_AST_CACHE_SHARD_SIZE

Maximum entries per shard.

DriverOptions::OPT_AST_CACHE_SHARD_SIZE => 256  // Default: 256

Total cache capacity = shard_count × shard_size (default: 2048 entries)

All Options Reference

OptionTypeDefaultDescription
OPT_URLstring(required)Database connection URL
OPT_ASSOC_ARRAYSboolfalseReturn arrays instead of objects
OPT_READONLYboolfalseDisable write operations
OPT_PERSISTENT_NAMEstringnullPersistent pool name
OPT_COLLAPSIBLE_INbooltrueCollapse empty IN to FALSE
OPT_AST_CACHE_SHARD_COUNTint8AST cache shards
OPT_AST_CACHE_SHARD_SIZEint256Entries per cache shard
OPT_MAX_CONNECTIONSint2Max pool connections
OPT_MIN_CONNECTIONSint0Min idle connections
OPT_MAX_LIFETIMEstring/intnullConnection max age
OPT_IDLE_TIMEOUTstring/intnullIdle connection timeout
OPT_ACQUIRE_TIMEOUTstring/intnullPool acquire timeout
OPT_TEST_BEFORE_ACQUIREboolfalsePing before acquiring
OPT_READ_REPLICASarray[]Read replica URLs
OPT_RETRY_MAX_ATTEMPTSint0Max retry attempts
OPT_RETRY_INITIAL_BACKOFFstring/int"100ms"Initial retry delay
OPT_RETRY_MAX_BACKOFFstring/int"10s"Max retry delay
OPT_RETRY_MULTIPLIERfloat2.0Backoff multiplier

Duration Formats

Options that accept durations support:

  • String format: "100ms", "30s", "5m", "1h"
  • Integer: Seconds
DriverOptions::OPT_IDLE_TIMEOUT => "5m"      // 5 minutes
DriverOptions::OPT_IDLE_TIMEOUT => "30s"     // 30 seconds
DriverOptions::OPT_IDLE_TIMEOUT => "100ms"   // 100 milliseconds
DriverOptions::OPT_IDLE_TIMEOUT => 300       // 300 seconds

Environment-Based Configuration

$driver = DriverFactory::make([
    DriverOptions::OPT_URL => getenv('DATABASE_URL'),
    DriverOptions::OPT_MAX_CONNECTIONS => (int) getenv('DB_POOL_SIZE') ?: 5,
    DriverOptions::OPT_ASSOC_ARRAYS => getenv('DB_ASSOC_ARRAYS') === 'true',
]);

Profile-Based Configuration

$profiles = [
    'development' => [
        DriverOptions::OPT_URL => "postgres://localhost/myapp_dev",
        DriverOptions::OPT_MAX_CONNECTIONS => 2,
    ],
    'production' => [
        DriverOptions::OPT_URL => getenv('DATABASE_URL'),
        DriverOptions::OPT_MAX_CONNECTIONS => 10,
        DriverOptions::OPT_READ_REPLICAS => [
            getenv('DATABASE_REPLICA_1'),
            getenv('DATABASE_REPLICA_2'),
        ],
        DriverOptions::OPT_TEST_BEFORE_ACQUIRE => true,
    ],
];

$env = getenv('APP_ENV') ?: 'development';
$driver = DriverFactory::make($profiles[$env]);