https://github.com/byjg/temp
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/byjg/temp
- Owner: byjg
- Created: 2018-11-28T04:42:05.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-12-08T02:14:47.000Z (almost 5 years ago)
- Last Synced: 2025-01-01T17:11:09.230Z (9 months ago)
- Size: 21.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Database Relational AnyDataset
[](http://opensource.byjg.com)
[](https://travis-ci.org/byjg/anydataset-db)## Description
Anydataset Database Relational abstraction. Anydataset is an agnostic data source abstraction layer in PHP.
See more about Anydataset [here](https://github.com/byjg/anydataset).
## Features
- Connection based on URI
- Support and fix code tricks with several databases (MySQL, PostgresSql, MS SQL Server, etc)
- Natively supports Query Cache by implementing a PSR-6 interface
- Supports Connection Routes based on regular expression against the queries, that's mean a select in a table should be
executed in a database and in another table should be executed in another (even if in different DB)## Connection Based on URI
The connection string for databases is based on URL.
See below the current implemented drivers:
{:.table}
| Database | Connection String | Factory
| ------------- | ----------------------------------------------------- | ------------------------- |
| Sqlite | sqlite:///path/to/file | getDbRelationalInstance() |
| MySql/MariaDb | mysql://username:password@hostname:port/database | getDbRelationalInstance() |
| Postgres | psql://username:password@hostname:port/database | getDbRelationalInstance() |
| Sql Server | dblib://username:password@hostname:port/database | getDbRelationalInstance() |
| Oracle (OCI) | oci://username:password@hostname:port/database | getDbRelationalInstance() |
| Oracle (OCI8) | oci8://username:password@hostname:port/database | getDbRelationalInstance() |```php
getIterator('select * from table where field = :param', ['param' => 'value']);
foreach ($iterator as $row) {
// Do Something
// $row->getField('field');
}
```### Updating in Relational databases
```php
execute(
'update table set other = :value where field = :param',
[
'value' => 'othervalue',
'param' => 'value of param'
]
);
```### Inserting and Get Id
```php
executeAndGetId(
'insert into table (field1, field2) values (:param1, :param2)',
[
'param1' => 'value1',
'param2' => 'value2'
]
);
```### Database Transaction
```php
beginTransaction();// ... Do your queries
$dbDriver->commitTransaction(); // or rollbackTransaction()
```### Cache results
You can easily cache your results with the DbCached class; You need to add to your project an
implementation of PSR-6. We suggested you add "byjg/cache".```php
getIterator('select * from table where field = :param', ['param' => 'value']);
```### Load balance and connection pooling
The API have support for connection load balancing, connection pooling and persistent connection.
There is the Route class an DbDriverInterface implementation with route capabilities. Basically you have to define
the routes and the system will choose the proper DbDriver based on your route definition.Example:
```php
addDbDriverInterface('route1', 'sqlite:///tmp/a.db')
->addDbDriverInterface('route2', 'sqlite:///tmp/b.db')
->addDbDriverInterface('route3', 'sqlite:///tmp/c.db')
;// Define the route
$dbDriver
->addRouteForWrite('route1')
->addRouteForRead('route2', 'mytable')
->addRouteForRead('route3')
;// Query the database
$iterator = $dbDriver->getIterator('select * from mytable'); // Will select route2
$iterator = $dbDriver->getIterator('select * from othertable'); // Will select route3
$dbDriver->execute('insert into table (a) values (1)'); // Will select route1;
```The possible route types are:
- addRouteForWrite($routeName, $table = null): Filter any insert, update and delete. Optional specific table;
- addRouteForRead($routeName, $table = null): Filter any select. Optional specific table;
- addRouteForInsert($routeName, $table = null): Filter any insert. Optional specific table;
- addRouteForDelete($routeName, $table = null): Filter any delete. Optional specific table;
- addRouteForUpdate($routeName, $table = null): Filter any update. Optional specific table;
- addRouteForFilter($routeName, $field, $value): Filter any WHERE clause based on FIELD = VALUE
- addCustomRoute($routeName, $regEx): Filter by a custom regular expression.### Connecting To MySQL via SSL
(Read here https://gist.github.com/byjg/860065a828150caf29c20209ecbd5692 about create server mysql)```php
getIterator('select * from table where field = :value', ['value' => 10]);
foreach ($iterator as $row) {
// Do Something
// $row->getField('field');
}
```### Using IteratorFilter in order to get the SQL
You can use the IteratorFilter object to make easier create SQL
```php
addRelation('field', \ByJG\AnyDataset\Enum\Relation::EQUAL, 10);// Generate the SQL
$param = [];
$formatter = new \ByJG\AnyDataset\Db\IteratorFilterSqlFormatter();
$sql = $formatter->format(
$filter->getRawFilters(),
'mytable',
$param,
'field1, field2'
);// Execute the Query
$iterator = $db->getIterator($sql, $param);
```### Using IteratorFilter with Literal values
Sometimes you need an argument as a Literal value like a function or an explicit conversion.
In this case you have to create a class that expose the "__toString()" method
```php
addRelation('field', \ByJG\AnyDataset\Core\Enum\Relation::EQUAL, $literal);
```## Install
Just type:
```bash
composer require "byjg/anydataset=4.0.*"
```## Running Unit tests
```bash
vendor/bin/phpunit
```## Running database tests
Run integration tests require you to have the databases up and running.
The easiest way to run the tests is:
**Prepare the environment**
```php
npm i
node_modules/.bin/usdocker --refresh
node_modules/.bin/usdocker -v --no-link mssql up
node_modules/.bin/usdocker -v --no-link mysql up
node_modules/.bin/usdocker -v --no-link postgres up
```**Run the tests**
```
phpunit testsdb/PdoMySqlTest.php
phpunit testsdb/PdoSqliteTest.php
phpunit testsdb/PdoPostgresTest.php
phpunit testsdb/PdoDblibTest.php
```Optionally you can set the password for Mysql and PostgresSQL
```bash
export MYSQL_PASSWORD=newpassword # use '.' if want have a null password
export PSQL_PASSWORD=newpassword # use '.' if want have a null password
```----
[Open source ByJG](http://opensource.byjg.com)