PHPackages                             redsql/redsql - 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. redsql/redsql

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

redsql/redsql
=============

Programmatic and database agnostic SQL helper for RedBean delivered as a plugin.

1.0.3(11y ago)135261[3 issues](https://github.com/marcioAlmada/redsql/issues)MITPHPPHP &gt;=5.4.0

Since Mar 11Pushed 11y ago3 watchersCompare

[ Source](https://github.com/marcioAlmada/redsql)[ Packagist](https://packagist.org/packages/redsql/redsql)[ RSS](/packages/redsql-redsql/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

RedSQL
======

[](#redsql)

[![Build Status](https://camo.githubusercontent.com/fd44602895eb879146a928118788da9eb2a3e5fcb3bea96269973d5733c2598e/68747470733a2f2f7472617669732d63692e6f72672f6d617263696f416c6d6164612f72656473716c2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/marcioAlmada/redsql)[![Coverage Status](https://camo.githubusercontent.com/ae21927eb50cccd61f6f48bbdf4aad9eb70c6ba1f226fd6fc541acf8e308056d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d617263696f416c6d6164612f72656473716c2f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/marcioAlmada/redsql?branch=master)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/f31bab0c782e7e195919ed57a33337de799ecb0296054bd7a72e045ca651dfd0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d617263696f416c6d6164612f72656473716c2f6261646765732f7175616c6974792d73636f72652e706e673f733d65353133306331366665363639353833343463373664363332623936333138353235323334616639)](https://scrutinizer-ci.com/g/marcioAlmada/redsql/)[![Latest Stable Version](https://camo.githubusercontent.com/439fd7c484111cbb0c112412783acec9bce8941bc7e036cbc70d164ded42b4ad/68747470733a2f2f706f7365722e707567782e6f72672f72656473716c2f72656473716c2f762f737461626c652e706e67)](https://packagist.org/packages/redsql/redsql)[![Total Downloads](https://camo.githubusercontent.com/803a068a3fe6a232c91a6805c78a0e8c12cbcf9e156ef741d00059158f0d8d42/68747470733a2f2f706f7365722e707567782e6f72672f72656473716c2f72656473716c2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/redsql/redsql)[![License](https://camo.githubusercontent.com/141cc700593acb703bcd27720d3924f8abdd13c2766959df99c4920a34b23146/68747470733a2f2f706f7365722e707567782e6f72672f72656473716c2f72656473716c2f6c6963656e73652e706e67)](https://packagist.org/packages/redsql/redsql)

RedSQL is a database agnostic helper for RedBean delivered as a plugin. It's a tiny, fun and predictable DSL to create SQL queries in a programmatic way.

Features
========

[](#features)

- Express syntax
- Dynamic API: available methods are just a mirror of your table (bean) structure.
- Programmatic: avoid nasty string manipulations to achieve dynamic SQL construction.
- Database agnostic: SQL inconsistencies across databases (aka Oracle) like LIMIT and OFFSET are gracefully normalized.
- Lazy field loading: restrict wich fields you want to select.
- Still looks like SQL

Installation
------------

[](#installation)

### With Composer

[](#with-composer)

```
{
  "require": {
    "redsql/redsql": "~1.0"
  }
}
```

Or just use your terminal: `composer require redsql/redsql:~1.0` 🎱

### Phar (single file)

[](#phar-single-file)

For the folks that are not using composer yet (why u no?) you can download [redsql.phar](https://github.com/marcioAlmada/redsql/raw/master/dist/redsql.phar)single distribution file and include it on your project:

```
include 'path/to/redsql.phar';
```

Usage
-----

[](#usage)

RedSQL public API is awsome fluid and completely achieved with PHP wizardry magic. So, given the following table structure:

[![table](https://camo.githubusercontent.com/8301b82222ef20b98f5a20bd95f5fc3c028f875479974ccf319bbc113ec02933/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f34393534393533302f72656473716c2d70726f6a6563742d7461626c652e706e67)](https://camo.githubusercontent.com/8301b82222ef20b98f5a20bd95f5fc3c028f875479974ccf319bbc113ec02933/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f34393534393533302f72656473716c2d70726f6a6563742d7461626c652e706e67)

Express syntax:

```
$projects =
    R::redsql('project')
        ->name('like', '%project x')
        ->priority('>', 5)
        ->created_at('between', [$time1, $time2])
        ->find($limit, $offset);

// parses to:

SELECT * FROM `project`
WHERE `name` LIKE ?
    AND `priority` > ?
    AND `created_at` BETWEEN ? AND ?
LIMIT ? OFFSET ?
```

\+ Syntatic sugar:

```
$projects =
    R::redsql('project')
        ->name('like', '%secret%')->AND->priority('>', 9)
        ->OR
        ->OPEN
            ->code('in', [007, 51])
            ->AND->NOT->created_at('between', [$time1, $time2])
        ->CLOSE
        ->find($limit, $offset)

// parses to:

SELECT * FROM `project`
WHERE `name` LIKE ?
    AND `priority` > ?
    OR (
        `code` IN (?,?)
        AND  NOT  `created_at` BETWEEN ? AND ?
    )
LIMIT ? OFFSET ?
```

Select specific fields:

```
$projects = R::redsql('project', ['name', 'description'])->find($limit, $offset);

// parses to:

SELECT `name`, `description` FROM `project` LIMIT ? OFFSET ?
```

\- Houston, we got Oracle! - No problem.

```
$projects = R::redsql('project', ['name', 'description'])->find($limit, $offset);

// parses to:

SELECT * FROM  (
    SELECT VIRTUAL.*, ROWNUM ROWOFFSET FROM  (
       SELECT `name`, `description` FROM `project`
    ) VIRTUAL
) WHERE ROWNUM = ?
```

Public API
----------

[](#public-api)

### R::redsql($table, array $fields = \[\])

[](#rredsqltable-array-fields--)

Returns a configured `RedBeanPHP\Plugins\RedSql\Finder` instance restricted to a given entity:

```
$project_query = R::redsql('project');
```

New conditions can be queued lazily:

```
$project_query->created_at('>', '2014-04-03 00:00:00');
$projects = $project_query->find(); // finally reach database
```

Finder can restrict what fields should be queried:

```
$project_query = R::redsql('project', ['name', 'priority']);
//
SELECT `id`, `name`, `priority` FROM `project`
```

### find(*$limit = null, $offset = null*)

[](#findlimit--null-offset--null)

Fetches an array of `RedBean_OODBBean` instances from database:

```
$projects = R::redsql('project')->NOT->priority(3)->find();
//
SELECT * FROM `project` WHERE  NOT  `priority` = ?
```

### findFirst()

[](#findfirst)

Applies `LIMIT` and `ORDER BY ASC` statements and fetches a single `RedBean_OODBBean` instance from database:

```
$project = R::redsql('project')->NOT->priority(3)->findFirst();
//
SELECT * FROM `project` WHERE NOT `priority` = ? ORDER BY `id` ASC LIMIT 1 OFFSET ?
```

### findLast()

[](#findlast)

Applies `LIMIT` and `ORDER BY DESC` statements and fetches a single `RedBean_OODBBean` instance from database:

```
$project = R::redsql('project')->NOT->priority(3)->findLast();
//
SELECT * FROM `project` WHERE NOT `priority` = ? ORDER BY `id` DESC LIMIT 1 OFFSET ?
```

### findAlike(*array $conditions, $limit = null, $offset = null*)

[](#findalikearray-conditions-limit--null-offset--null)

Applies a batch of conditions and fetches an array of `RedBean_OODBBean` instances from database:

```
$projects = R::redsql('project')->findAlike([
    'name' => 'X Project',
    'description' => '%secret%',
    'priority' => [1, 2, 3]
    ], $limit, $offset
);

//

SELECT *
FROM `project`
WHERE  `name` = ?
    AND  `description` LIKE ?
    AND  `priority` IN (?,?,?)
LIMIT ?  OFFSET ?
```

### toString()

[](#tostring)

Finders `RedBeanPHP\Plugins\RedSql\Finder` can be converted to plain SQL using `(string)` casting or `toString` method:

```
$project_finder = R::redsql('project');
echo (string) $project_finder;
$project_finder->NOT->priority(3);
echo $project_finder->toString();

// >

SELECT * FROM `project`
SELECT * FROM `project` WHERE  NOT  `priority` = ?
```

Database Support [![Build Status](https://camo.githubusercontent.com/fd44602895eb879146a928118788da9eb2a3e5fcb3bea96269973d5733c2598e/68747470733a2f2f7472617669732d63692e6f72672f6d617263696f416c6d6164612f72656473716c2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/marcioAlmada/redsql)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[](#database-support-)

If build badge is green it means RedSql latest version is working on:

- Oracle
- Postgre
- MySQL
- SQLite
- CUBRID
- MSSQL Server (as soon as RedBean supports it)

Roadmap
-------

[](#roadmap)

- RedBean ~4.0 support
- Beans collection
- Relationships
- Single file release (phar)

Contributing
------------

[](#contributing)

1. Fork redsql
2. Clone redsql `git clone git@github.com:marcioAlmada/redsql.git`
3. Install composer dependencies `$ composer install --prefer-dist`
4. Run desired unit tests `$ phpunit` or at least `$ phpunit --group sqlite`
5. Correct bug, implement feature
6. Back to step 4

When everything is ready, create a pull request to master branch :)

Building
--------

[](#building)

1. Clone redsql `git clone git@github.com:marcioAlmada/redsql.git`
2. Install [kherge/php-box](https://github.com/kherge/php-box)
3. Run `$ box build`
4. A new phar will be available at `dist` folder

PS: This plugin follows specification discussed in [\#311](https://github.com/gabordemooij/redbean/issues/311).

Copyright
---------

[](#copyright)

Copyright (c) 2014 Márcio Almada. Distributed under the terms of an MIT-style license. See LICENSE for details.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity61

Established project with proven stability

 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 ~29 days

Total

4

Last Release

4358d ago

### Community

Maintainers

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

---

Top Contributors

[![marcioAlmada](https://avatars.githubusercontent.com/u/227395?v=4)](https://github.com/marcioAlmada "marcioAlmada (89 commits)")

---

Tags

phpredbean pluginredsql

### Embed Badge

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

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

###  Alternatives

[eftec/pdoone

Minimaist procedural PDO wrapper library

1105.9k9](/packages/eftec-pdoone)[simple-swoole/db

A db component for Simps.

216.3k3](/packages/simple-swoole-db)

PHPackages © 2026

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