An open API service indexing awesome lists of open source software.

https://github.com/quickstartlibs/db

Stash Queries - A thin and simple yet elegant database abstraction layer around PDO.
https://github.com/quickstartlibs/db

database injection mysql pdo pdo-replacement pdo-wrapper persistent-connections php queries query-builder querying sql stash-queries stash-query

Last synced: about 1 month ago
JSON representation

Stash Queries - A thin and simple yet elegant database abstraction layer around PDO.

Awesome Lists containing this project

README

          

# Stash Queries

> Stash Queries is an eloquent and expressive PHP library that simplifies database interactions.

It acts as a lightweight wrapper around PHP's native PDO (PHP Data Objects) extension, providing a fluent API to easily execute `SELECT`, `INSERT`, `UPDATE`, and `DELETE` operations using your actual SQL files.
Originally developed as the *DB* service for the Skyfire PHP framework, this library is specifically designed as a native PDO replacement rather than a full Object-Relational Mapper (ORM).

_Note: Currently, Stash Queries supports MySQL and PostgreSQL databases. Support for additional database are up for request._

## Requirements

- PHP 5.6 or higher
- `PDO_MYSQL` PHP extension

*(Note: PHP 5.6 is the minimum required version for security and modern language features.)*

## Code Examples

```php
// setting the DB display encoding type (if needed)
FixCollation::charset('utf-8', FixCollation::TEXT_HTML);

// setting Database credentials
DB::define('stash_dir', getcwd()); // or dirname(__FILE__)
DB::define('host', 'localhost');
DB::define('dbname', 'test_db1');
DB::define('dbuser', 'root');
DB::define('dbpassword', '');

// SQL select query (with prepare variables)
$prepare = array
(
'label' => 'test'
);
$data = DB::select('get.HomeTextByLabel')->prepare($prepare);
var_dump($data);

// SQL simple select query
$data = DB::select('get.AllHomeTextData')->execute();
var_dump($data);

// raw SQL query (with prepare variables)
$data = DB::query('SELECT * FROM test WHERE data IS NOT NULL AND id > :count AND data != :text', array
(
':id' => 10,
':text' => 'test'
))->execute();
var_dump($data);

// displays the prepare update statement in plain text (ideal for debugging queries)
$query = DB::update('PostfromTestById')->text($prepare);
echo $query;
```

## Injections

You can securely inject dynamic variables directly into your queries (e.g., table names or column fields) when standard PDO bindings cannot be used:

```php
$data = DB::select('get.fieldData.byId')->inject(array
(
'field' => $data->field,
'table' => $table_name
))->prepare(array('id' => (int) $record->id));
```

## Persistent Connections

To enable persistent database connections for all queries, define the `persistent` configuration setting as follows:

```php
DB::define('persistent', TRUE);
// DB::define('persistent', 'yes');
```

You can use either the boolean `TRUE` or the string `'yes'` to enable this feature.

## Creating Query Folders

If you are unsure whether your SQL query folders exist, you can call the following function to create them automatically. It returns `FALSE` if no directories were created, or an integer representing the number of directories created (e.g., `4`).

```php
DB::createQueryDirectories();
```

**Note:** Ensure you place this function call after all `DB::define()` configurations.

## Quickly Create a Database

You can conveniently create a new database if it doesn't already exist:

```php
DB::createNotExist('database_name');
```

**Note:** Ensure you place this function call after all `DB::define()` configurations.

## Installation

**Manual Installation:**
To include the library manually, simply require the `StashQueries.php` file in your project:

```php
require_once 'DB/StashQueries.php';
```

**Via Composer:**
Alternatively, you can install the library via Composer by adding `skyfirephp/db` to the `require` section of your `composer.json` file:

```json
{
"require": {
"skyfirephp/db": "dev-master"
}
}
```

## License

Stash Queries is open-sourced software licensed under the [MIT License](http://opensource.org/licenses/MIT).

Copyright 2015-2017 [Travis van der Font](http://travisfont.com)