PHPackages                             chevere/sql2p - 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. chevere/sql2p

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

chevere/sql2p
=============

Parameter(s) for MySQL schemas

1.0.0(2y ago)3162[2 issues](https://github.com/chevere/sql2p/issues)1Apache-2.0PHP

Since Jan 16Pushed 1y agoCompare

[ Source](https://github.com/chevere/sql2p)[ Packagist](https://packagist.org/packages/chevere/sql2p)[ Docs](https://chevere.org)[ RSS](/packages/chevere-sql2p/feed)WikiDiscussions 1.0 Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (1)

SQL2P
=====

[](#sql2p)

[![Chevere](chevere.svg)](chevere.svg)

[![Build](https://camo.githubusercontent.com/b7be3a3d7642778122d9b11a9e3a6716d28943236a38048a129dd58867bd79d1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636865766572652f73716c32702f746573742e796d6c3f6272616e63683d312e30267374796c653d666c61742d737175617265)](https://github.com/chevere/sql2p/actions)[![Code size](https://camo.githubusercontent.com/b6ff6cd55f3c3e4bf27886aa6d39ead9d5f5f9041bfcfda2439e6ee4089ddb18/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f636865766572652f73716c32703f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/b6ff6cd55f3c3e4bf27886aa6d39ead9d5f5f9041bfcfda2439e6ee4089ddb18/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f636865766572652f73716c32703f7374796c653d666c61742d737175617265)[![Apache-2.0](https://camo.githubusercontent.com/ffb86fefc603c39e627add27de1dcea88435da57e33314fcd95aa45663d47104/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f636865766572652f73716c32703f7374796c653d666c61742d737175617265)](LICENSE)[![PHPStan](https://camo.githubusercontent.com/6016298b28550819030c76e9327f62501596a31fd76406695bae2f3d2a1f26a4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d626c756576696f6c65743f7374796c653d666c61742d737175617265)](https://phpstan.org/)[![Mutation testing badge](https://camo.githubusercontent.com/0989f173bc118b33fb3f1294af706fd0414311a919036ecc0277f7d2b5d4e404/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742d7371756172652675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d2532466368657665726525324673716c3270253246312e30)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/sql2p/1.0)

[![Quality Gate Status](https://camo.githubusercontent.com/d6100e9bb558f3d7751736214ced089eef6d801a96ed7f841391bed9b1aed7e5/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c3270266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=chevere_sql2p)[![Maintainability Rating](https://camo.githubusercontent.com/167a4098eb76f3b01f5d54975c9f40176c6cfe3ee11f08673a92af1ee99193b7/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c3270266d65747269633d7371616c655f726174696e67)](https://sonarcloud.io/dashboard?id=chevere_sql2p)[![Reliability Rating](https://camo.githubusercontent.com/36d048e6b04cf0ebe30476127ce5f5a2ace64c827c2825bb7c5abf4116fde650/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c3270266d65747269633d72656c696162696c6974795f726174696e67)](https://sonarcloud.io/dashboard?id=chevere_sql2p)[![Security Rating](https://camo.githubusercontent.com/85b9d304d487faaa09daf7d22d46cc7db4e440c52c69cdd2676137a541511b47/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c3270266d65747269633d73656375726974795f726174696e67)](https://sonarcloud.io/dashboard?id=chevere_sql2p)[![Coverage](https://camo.githubusercontent.com/caf080f8fe17d91326b4a9615b47c4e61b53708c5981cfdac1f8de44d3b6f859/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c3270266d65747269633d636f766572616765)](https://sonarcloud.io/dashboard?id=chevere_sql2p)[![Technical Debt](https://camo.githubusercontent.com/79f8edde6c7fe02fca40944e6d9045510d9565f1a13684907c585c79bfc30031/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c3270266d65747269633d7371616c655f696e646578)](https://sonarcloud.io/dashboard?id=chevere_sql2p)[![CodeFactor](https://camo.githubusercontent.com/8cea4b02f2c7e41ba10a22dc845c284ba3b3c5e3c40e385eb55763b1506643ec/68747470733a2f2f7777772e636f6465666163746f722e696f2f7265706f7369746f72792f6769746875622f636865766572652f73716c32702f6261646765)](https://www.codefactor.io/repository/github/chevere/sql2p)

Summary
-------

[](#summary)

SQL2P generates [Parameter(s)](https://chevere.org/packages/parameter) for MySQL schemas. It represents database tables and its columns using the Parameter package.

Installing
----------

[](#installing)

SQL2P is available through [Packagist](https://packagist.org/packages/chevere/sql2p) and the repository source is at [chevere/sql2p](https://github.com/chevere/sql2p).

```
composer require chevere/sql2p
```

What it does?
-------------

[](#what-it-does)

From a `CREATE TABLE` statement like this one below.

```
CREATE TABLE `invoice` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `client_id` INT UNSIGNED NOT NULL,
  `datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `details` LONGTEXT NULL,
  `quantity` INT UNSIGNED NOT NULL,
  `rate` DECIMAL(10,2) NOT NULL,
  `total` DECIMAL(19,4) GENERATED ALWAYS AS (quantity*rate),
  PRIMARY KEY (`id`)
) ENGINE = InnoDB;
```

SQL2P generates the following PHP code.

```
use Chevere\Parameter\Interfaces\ArrayParameterInterface;
use function Chevere\Parameter\arrayp;
use function Chevere\Parameter\datetime;
use function Chevere\Parameter\float;
use function Chevere\Parameter\int;
use function Chevere\Parameter\null;
use function Chevere\Parameter\string;
use function Chevere\Parameter\union;

function invoiceTable(): ArrayParameterInterface
{
    return arrayp(
        id: int(min: 0),
        client_id: int(min: 0),
        datetime: datetime(),
        details: union(
            null(),
            string()
        ),
        quantity: int(min: 0),
        rate: float(),
        total: float()
    );
}
```

The `invoiceTable()` function returns table `invoice` schema in PHP code using [Array](https://chevere.org/packages/parameter#array) Parameter where each column is represented by another Parameter.

From this you can add your own validation rules on top of generated code.

For example, you limit `quantity` to a range of `100, 200` by adding `max` and `min` arguments. Add a regex to `details` to validate string shape.

```
details: union(
    null(),
-    string()
+    string('/^(?!\s*$)./')
),
-quantity: int(min: 0),
+quantity: int(max: 200, min: 100),
```

Array Parameter object returned by this function can be also used to dynamic interact with one or more of these columns. See [Array Composing](#array-composing) to learn more.

Creating SQL2P
--------------

[](#creating-sql2p)

Create a `SQL2P` instance by passing the SQL and a [Writer](https://chevere.org/packages/writer) instance. On instance creation the SQL is parsed and the writer is used to write the generated code.

```
use Chevere\SQL2P\SQL2P;
use Chevere\Writer\StreamWriter;
use function Chevere\Filesystem\fileForPath;
use function Chevere\Writer\streamFor;

$schema = __DIR__ . '/schema.sql';
$output = __DIR__ . '/sql2p.php';
$header =  1,
    'client_id' => 1234,
    'datetime' => '2023-10-22 19:58:44',
    'details' => null,
    'quantity' => 100,
    'rate' => 16.5,
    'total' => 1650,
];
```

Function `invoiceTable()` can be used to validate `$fetch` by invoking it.

```
$table = invoiceTable();
$table($fetch); // validation
```

Use `arrayFrom` function to create an array taking only the columns you need.

```
SELECT id, total FROM invoice WHERE id = 1
```

```
use function Chevere\Parameter\arrayFrom;

$fetch = [
    'id' => 1,
    'total' => 1650,
];
$table = arrayFrom(invoiceTable(), 'id', 'total');
$table($fetch);
```

Use `arguments` function to get typed access to fetched array members.

```
use function Chevere\Parameter\arguments;

$invoice = arguments($table, $fetch);
$total = $invoice->required('total')->int(); // 1650
```

When fetching multiple rows wrap Array table with [iterable](https://chevere.org/packages/parameter.html#iterable) function.

```
SELECT id, total FROM invoice WHERE id > 0
```

```
$fetchAll = [
    0 => [
        'id' => 1,
        'total' => 1650,
    ],
    1 => [
        'id' => 2,
        'total' => 1820,
    ],
];
$iterable = iterable($table);
$iterable($fetchAll);
```

Note that `arguments` function supports iterable.

```
$invoices = arguments($iterable, $fetchAll);
$secondRow = $invoices->required('1')->array();
```

Array composing
---------------

[](#array-composing)

Parameter provides a set of tools to work with arrays, enabling to dynamically add, remove or modify values. It also enables to compose arrays from other arrays.

For example to add a `total_usd` virtual column to `invoiceTable()`.

```
SELECT
    id,
    total,
    total/100 total_usd
FROM invoice WHERE id = 1
```

```
$fetch = [
    'id' => 1,
    'total' => 1650,
    'total_usd' => 16.5,
];
$table = arrayFrom(invoiceTable(), 'id', 'total');
$table = $table
    ->withRequired(
        total_usd: float(),
    );
$table($fetch);
```

When `JOIN` tables you may want to take columns based on joined tables. Use `takeFrom` function to create a iterable with `column => parameter` pairs.

```
SELECT
    invoice.id,
    invoice.total,
    client.name,
    client.email
FROM invoice
JOIN client ON client.id = invoice.client_id
WHERE invoice.id = 1
```

```
$fetch = [
    'id' => 1,
    'total' => 1650,
    'name' => 'Rodolfo',
    'email' => 'rodolfo@chevere.org'
];
$invoice = arrayFrom(invoiceTable(), 'id', 'total');
$client = takeFrom(clientTable(), 'name', 'email');
$table = $invoice->withRequired(...$client);
$table($fetch);
```

For this code `$client` is assigned to an iterable containing `name` and `email` column pairs from `clientTable()`. Then by calling `withRequired` on `$invoice` it gets these columns on spread.

Documentation
-------------

[](#documentation)

Documentation is available at [chevere.org](https://chevere.org/packages/sql2p).

License
-------

[](#license)

Copyright [Rodolfo Berrios A.](https://rodolfoberrios.com/)

Chevere is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the full license text.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~310 days

Total

2

Last Release

543d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a87fbbcb1518fe64878bc53dedd9f61b81243cbb21b73c26f5e299d501375984?d=identicon)[rodolfo](/maintainers/rodolfo)

---

Top Contributors

[![rodber](https://avatars.githubusercontent.com/u/20590102?v=4)](https://github.com/rodber "rodber (16 commits)")

---

Tags

cheveremysqlphpphp-library

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/chevere-sql2p/health.svg)

```
[![Health](https://phpackages.com/badges/chevere-sql2p/health.svg)](https://phpackages.com/packages/chevere-sql2p)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
