Skip to content

Commit

Permalink
Update readme and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Oct 20, 2024
1 parent ea7f7df commit 1e1e978
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 36 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [7.0.0] - 2024-10-xx
### Added
- Official support for PostgreSQL.
- It is now possible to bind and set binary column values in a prepared statement which is executed multiple times.

### Changed
- Rewrote library using PDO instead of driver-specific connection objects. Rather than constructing `PeachySQL\Mysql` or `PeachySQL\SqlServer`, simply construct `PeachySQL\PeachySql` with the PDO object for your connection.

> [!IMPORTANT]
> If using SQL Server, make sure your PDO connection has the `PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE`
> option set to `true`, so column values are returned with the same native types as before.
- When using MySQL, the `Statement` object no longer has a `getInsertId()` method. This does not affect shorthand insert methods, however, which provide insert IDs just like before.
- The `getAffected()` method on a select query result now returns the number of selected rows instead of `-1` for MySQL.
- A non-zero length can no longer be passed to `makeBinaryParam()` for input parameters.
- PHP 8.1+ is now required.

### Removed
- All previously deprecated methods have been removed.
- `SqlException` no longer has properties for the failed query and params.


## [6.3.1] - 2024-10-13
### Changed
- Improved `makeBinaryParam()` implementation for SQL Server.
Expand Down Expand Up @@ -338,6 +360,7 @@ insert a single row.
- Initial release


[7.0.0]: https://github.com/theodorejb/peachy-sql/compare/v6.3.1...v7.0.0
[6.3.1]: https://github.com/theodorejb/peachy-sql/compare/v6.3.0...v6.3.1
[6.3.0]: https://github.com/theodorejb/peachy-sql/compare/v6.2.0...v6.3.0
[6.2.0]: https://github.com/theodorejb/peachy-sql/compare/v6.1.0...v6.2.0
Expand Down
69 changes: 33 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,35 @@
[![Total Downloads](https://img.shields.io/packagist/dt/theodorejb/peachy-sql.svg)](https://packagist.org/packages/theodorejb/peachy-sql)
[![License](https://img.shields.io/packagist/l/theodorejb/peachy-sql.svg)](https://packagist.org/packages/theodorejb/peachy-sql)

PeachySQL is a speedy database abstraction layer which makes it easy to execute
prepared statements and work with large amounts of data. It supports both MySQL
and SQL Server, and runs on PHP 7.4+.
PeachySQL is a high-performance query builder and runner which simplifies using prepared statements
and working with large amounts of data. It is officially tested with MySQL, PostgreSQL, and SQL Server,
but it should also work with any standards-compliant database which has a driver for PDO.

## Install via Composer

`composer require theodorejb/peachy-sql`

## Usage

Start by instantiating the `Mysql` or `SqlServer` class with a database connection,
which should be an existing [mysqli object](https://www.php.net/manual/en/mysqli.construct.php)
or [SQLSRV connection resource](https://www.php.net/manual/en/function.sqlsrv-connect.php):
Start by instantiating the `PeachySql` class with a database connection,
which should be an existing [PDO object](https://www.php.net/manual/en/class.pdo.php):

```php
$peachySql = new PeachySQL\Mysql($mysqlConn);
```
or
```php
$peachySql = new PeachySQL\SqlServer($sqlSrvConn);
$connection = new PDO('sqlsrv:server=(local)', $username, $password, [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
'Database' => 'someDbName',
]);

$db = new PeachySQL\PeachySql($connection);
```

After instantiation, arbitrary statements can be prepared by passing a
SQL string and array of bound parameters to the `prepare()` method:

```php
$sql = "UPDATE Users SET fname = ? WHERE user_id = ?";
$stmt = $peachySql->prepare($sql, [&$fname, &$id]);
$stmt = $db->prepare($sql, [&$fname, &$id]);

$nameUpdates = [
3 => 'Theodore',
Expand All @@ -51,7 +52,7 @@ prepares, executes, and closes a statement after results are retrieved:

```php
$sql = 'SELECT * FROM Users WHERE fname LIKE ? AND lname LIKE ?';
$result = $peachySql->query($sql, ['theo%', 'b%']);
$result = $db->query($sql, ['theo%', 'b%']);
echo json_encode($result->getAll());
```

Expand All @@ -66,8 +67,6 @@ Both `prepare()` and `query()` return a `Statement` object with the following me
| `getAffected()` | Returns the number of rows affected by the query. |
| `close()` | Closes the prepared statement and frees its resources (automatically called when using `query()`). |

If using MySQL, the `Mysql\Statement` object additionally includes a `getInsertId()` method.

Internally, `getAll()` and `getFirst()` are implemented using `getIterator()`.
As such they can only be called once for a given statement.

Expand All @@ -93,12 +92,12 @@ and a `query()` method which executes the query and returns a `Statement` object

```php
// select all columns and rows in a table, ordered by last name and then first name
$rows = $peachySql->selectFrom("SELECT * FROM Users")
$rows = $db->selectFrom("SELECT * FROM Users")
->orderBy(['lname', 'fname'])
->query()->getAll();

// select from multiple tables with conditions and pagination
$rows = $peachySql->selectFrom("SELECT * FROM Users u INNER JOIN Customers c ON c.CustomerID = u.CustomerID")
$rows = $db->selectFrom("SELECT * FROM Users u INNER JOIN Customers c ON c.CustomerID = u.CustomerID")
->where(['c.CustomerName' => 'Amazing Customer'])
->orderBy(['u.fname' => 'desc', 'u.lname' => 'asc'])
->offset(0, 50) // page 1 with 50 rows per page
Expand All @@ -125,7 +124,7 @@ $sql = "

$date = (new DateTime('2 months ago'))->format('Y-m-d');

$rows = $peachySql->select(new SqlParams($sql, [$date]))
$rows = $db->select(new SqlParams($sql, [$date]))
->where(['u.status' => 'verified'])
->query()->getIterator();
```
Expand Down Expand Up @@ -167,7 +166,7 @@ $userData = [
'lname' => 'Chamberlin'
];

$id = $peachySql->insertRow('Users', $userData)->id;
$id = $db->insertRow('Users', $userData)->id;
```

#### insertRows
Expand All @@ -191,7 +190,7 @@ $userData = [
]
];

$result = $peachySql->insertRows('Users', $userData);
$result = $db->insertRows('Users', $userData);
$ids = $result->ids; // e.g. [64, 65, 66]
$affected = $result->affected; // 3
$queries = $result->queryCount; // 1
Expand All @@ -201,16 +200,16 @@ An optional third parameter can be passed to `insertRows()` to override the defa
identity increment value:

```php
$result = $peachySql->insertRows('Users', $userData, 2);
$result = $db->insertRows('Users', $userData, 2);
$ids = $result->ids; // e.g. [64, 66, 68]
```

Note: SQL Server allows a maximum of 1,000 rows to be inserted at a time, and limits
individual queries to 2,099 or fewer bound parameters. MySQL supports a maximum of
65,535 bound parameters per query. These limits can be easily reached when attempting
to bulk-insert hundreds or thousands of rows at a time. To avoid these limits, the
`insertRows()` method automatically splits large queries into batches to efficiently
handle any number of rows (`queryCount` contains the number of required batches).
Note: SQL Server allows a maximum of 1,000 rows to be inserted at a time, and limits individual
queries to 2,099 or fewer bound parameters. MySQL and PostgreSQL support a maximum of 65,535 bound
parameters per query. These limits can be easily reached when attempting to bulk-insert hundreds or
thousands of rows at a time. To avoid these limits, the `insertRows()` method automatically splits
row sets that exceed the limits into chunks to efficiently insert any number of rows
(`queryCount` contains the number of required queries).

#### updateRows and deleteFrom

Expand All @@ -224,7 +223,7 @@ Both methods return the number of affected rows.
```php
// update the user with user_id 4
$newData = ['fname' => 'Raymond', 'lname' => 'Boyce'];
$peachySql->updateRows('Users', $newData, ['user_id' => 4]);
$db->updateRows('Users', $newData, ['user_id' => 4]);

// delete users with IDs 1, 2, and 3
$userTable->deleteFrom('Users', ['user_id' => [1, 2, 3]]);
Expand All @@ -238,18 +237,16 @@ or rolling back the transaction with `commit()` or `rollback()`.

### Binary columns

In order to insert/update raw binary data in SQL Server (e.g. to a binary or varbinary column),
the bound parameter must be a special array which sets the encoding type to binary. PeachySQL
provides a `makeBinaryParam()` method to simplify this:
In order to insert/update raw binary data (e.g. to a binary, varbinary, or bytea column),
the bound parameter must have its encoding type set to binary. PeachySQL provides a
`makeBinaryParam()` method to simplify this:

```php
$colVals = [
$db->insertRow('Users', [
'fname' => 'Tony',
'lname' => 'Hoare',
'uuid' => $peachySql->makeBinaryParam(Uuid::uuid4()->getBytes()),
];

$peachySql->insertRow('Users', $colVals);
'uuid' => $db->makeBinaryParam(Uuid::uuid4()->getBytes()),
]);
```

## Author
Expand Down

0 comments on commit 1e1e978

Please sign in to comment.