PHPackages                             swouters/postgresql-tools - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. swouters/postgresql-tools

ActiveLibrary[Database &amp; ORM](/categories/database)

swouters/postgresql-tools
=========================

PHP package containing a set of tools to build SQL queries

2.2.0(3mo ago)1643↓50%1MITPHPPHP &gt;=8.4

Since Apr 11Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/Doelia/postgresql-tools)[ Packagist](https://packagist.org/packages/swouters/postgresql-tools)[ RSS](/packages/swouters-postgresql-tools/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (3)Versions (10)Used By (0)

Postgresql Tools
================

[](#postgresql-tools)

A PHP library to help you to build complexes SQL queries for PostgreSQL.

Usage
-----

[](#usage)

### Requirements

[](#requirements)

- PHP 8.4 or higher
- PostgreSQL database, any abstract layer (like Doctrine DBAL) or raw SQL queries.

### Installation

[](#installation)

```
composer require swouters/postgresql-tools
```

### Condition Builder

[](#condition-builder)

A tool to build easily complex conditions for SQL queries.

#### Signature

[](#signature)

```
// Array example
$array = [
    'age' => 30,
    'name' => ['John', 'Doe'],
];

// Signature
public function build(
    array  $array, // The array of conditions
    string $firstLevelOperator = 'AND', // The operator used between 1st level values (age=30 and ...)
    string $secondLevelOperator = 'OR', // The operator used if a value is an array (name=John OR name=Doe)
    array  $comparisonOperators = [], // Default is '='. You can set a specific operator for a key
    array  $replaces = [] // Replace the key used for the SQL column
): array
```

#### Build condition exemple

[](#build-condition-exemple)

```
use SWouters\PostgresqlTools\ConditionBuilder;

// name = "John Doe"
PostgreSQLConditionBuilder::buildCondition([
    'name' => 'John Doe',
]);

// Firstname=John AND Lastname=Doe
PostgreSQLConditionBuilder::buildCondition([
    'firstname' => 'John',
    'lastname' => 'Doe',
]);

// Firstname=John OR Lastname=Doe
PostgreSQLConditionBuilder::buildCondition([
        'firstname' => 'John',
        'lastname' => 'Doe',
], 'OR');

// Name=John OR Name=Doe
PostgreSQLConditionBuilder::buildCondition([
    'name' => ['John', 'Doe'],
]);

// (Name=John OR Name=Doe) AND Age=30
PostgreSQLConditionBuilder::buildCondition([
    'name' => ['John', 'Doe'],
    'age' => 30,
]);

// status != 'deleted'
PostgreSQLConditionBuilder::buildCondition([
    'status' => 'deleted',
], 'AND', 'OR', [
    'status' => '!='
]);

// age=30 AND (name!=John AND name!=Doe)
PostgreSQLConditionBuilder::buildCondition([
    'age' => 30,
    'name' => ['John', 'Doe'],
], 'AND', 'AND', [
    'name' => '!='
]);

// user.name=John
PostgreSQLConditionBuilder::buildCondition([
    'name' => 'John',
], 'AND', 'OR', [], [
    'name' => 'user.name'
]);

// created_at > '2024-03-01' AND created_at < '2024-03-02'
PostgreSQLConditionBuilder::buildCondition([
    'date_min' => '2024-03-01',
    'date_max' => '2024-03-02',
], 'AND', 'OR', [
    'date_min' => '>',
    'date_max' => '
