Skip to content

Commit

Permalink
Merge 063623f into 4116174
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Nov 24, 2018
2 parents 4116174 + 063623f commit 1c25c38
Show file tree
Hide file tree
Showing 102 changed files with 230 additions and 8,479 deletions.
28 changes: 0 additions & 28 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,9 @@ php:
- "7.0"
- "5.6"


addons:
hosts:
- mysql-container
- postgres-container
- mongodb-container
- mssql-container

services:
- docker

before_install:
- sudo service mysql stop || echo "mysql not stopped"
- sudo service postgresql stop || echo "postgresql not stopped"
- npm install
- node_modules/.bin/usdocker --refresh
# - node_modules/.bin/usdocker mssql up
- node_modules/.bin/usdocker --no-link postgres up
- node_modules/.bin/usdocker --no-link mysql up
- node_modules/.bin/usdocker --no-link mongodb up
- echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini


install:
- php -i
- composer install

script:
- vendor/bin/phpunit
- vendor/bin/phpunit testsdb/PdoSqliteTest.php
- vendor/bin/phpunit testsdb/MongoDbDriverTest.php
- vendor/bin/phpunit testsdb/PdoMySqlTest.php
- vendor/bin/phpunit testsdb/PdoPostgresTest.php
# - vendor/bin/phpunit testsdb/PdoDblibTest.php
203 changes: 42 additions & 161 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,210 +1,91 @@
# AnyDataset

[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg.com-brightgreen.svg)](http://opensource.byjg.com)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/159bc0fe-42dd-4022-a3a2-67e871491d6c/mini.png)](https://insight.sensiolabs.com/projects/159bc0fe-42dd-4022-a3a2-67e871491d6c)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/byjg/anydataset/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/byjg/anydataset/?branch=master)
[![Build Status](https://travis-ci.org/byjg/anydataset.svg?branch=master)](https://travis-ci.org/byjg/anydataset)
[![Code Coverage](https://scrutinizer-ci.com/g/byjg/anydataset/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/byjg/anydataset/?branch=master)

## Description

A data abstraction layer in PHP to manipulate any set of data with a standardized interface from any data source.
Anydataset Core Module. Anydataset is an agnostic data source abstraction layer in PHP.

## Features

### Read an write* with a number of data sources accessed by a standardized interface ([see more here](docs/Connecting-to-Data-Sources.md)):
- Access different data sources using the same interface.
- Iterable results
- Convert results to array

* Array
* Relational Databases (based on PDO - Sqlite, MySql, Postgres, Sql Server, Oracle, and others)
* DBLib (SQL Server php native)
* OCI8 (Oracle php native interface)
* Text files (fixed and delimeted like CSV)
* Json documents
* Xml documents
* Sockets
* [MongoDB](docs/Connecting-to-MongoDB.md)
* Amazon Aws S3
* SparQL
## Current Implementations

## Examples
| Object | Data Source | Read | Write | Reference |
| ---------------------- | --------------------- |:----:|:-----:| ----------------------- |
| DbDriverInterface | Relational DB | yes | yes | [Github](https://github.com/byjg/anydataset-db) |
| AnyDataSet | Anydataset | yes | yes | [Github](https://github.com/byjg/anydataset) |
| ArrayDataSet | Array | yes | no | [Github](https://github.com/byjg/anydataset-array) |
| TextFileDataSet | Delimited Fields | yes | no | [Github](https://github.com/byjg/anydataset-text) |
| FixedTextFileDataSet | Fixed Size fields | yes | no | [Github](https://github.com/byjg/anydataset-text) |
| XmlDataSet | Xml | yes | no | [Github](https://github.com/byjg/anydataset-xml) |
| JSONDataSet | Json | yes | no | [Github](https://github.com/byjg/anydataset-json) |
| SparQLDataSet | SparQl Repositories | yes | no | [Github](https://github.com/byjg/anydataset-sparql) |
| NoSqlDocumentInterface | NoSql Document Based | yes | yes | [Github](https://github.com/byjg/anydataset-nosql) |
| KeyValueInterface | NoSql Key/Value Based | yes | yes | [Github](https://github.com/byjg/anydataset-nosql) |

### Querying Datasets (Text, Xml, Json, Sockets, Array, etc)

The easiest way to work is to get an repository and get an iterator for navigate throught the data.
## Examples

### Iterating with foreach

```php
<?php
$repository = new \ByJG\AnyDataset\Dataset\TextFileDataset(
'myfile',
['field1', 'field2', 'field3'],
\ByJG\AnyDataset\Dataset\TextFileDataset::CSVFILE
);
$iterator = $repository->getIterator();

// and then:
$dataset = new \ByJG\AnyDataset\Core\AnyDataset("example");

$iterator = $dataset->getIterator();
foreach ($iterator as $row) {
echo $row->get('field1'); // or $row->toArray();
print $row->toArray();
}

// Or
print_r($iterator->toArray());
```

### Querying Relational Databases
### Filtering results

```php
<?php
$dbDriver = \ByJG\AnyDataset\Factory::getDbRelationalInstance('mysql://username:password@host/database');
$iterator = $dbDriver->getIterator('select * from table where field = :param', ['param' => 'value']);
$filter = new \ByJG\AnyDataset\Core\IteratorFilter();
$filter->addRelation("field1", \ByJG\AnyDataset\Core\Enum\Relation::EQUAL, 10);
$iterator2 = $dataset->getIterator($filter);
```

### 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".
### Conveting to Array

```php
<?php
$dbDriver = \ByJG\AnyDataset\Factory::getDbRelationalInstance('mysql://username:password@host/database');
$dbCached = new \ByJG\AnyDataset\Store\DbCached($dbDriver, $psrCacheEngine, 30);

// Use the DbCached instance instead the DbDriver
$iterator = $dbCached->getIterator('select * from table where field = :param', ['param' => 'value']);
```

### Connection based on URI

The connection string for databases is based on URL.

See below the current implemented drivers:

| 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() |
| MongoDB | mongodb://username:passwortd@host:port/database | getNoSqlInstance() |
| Amazon S3 | s3://key:secret@region/bucket | getKeyValueInstance() |


### Querying Non-Relational Databases

```php
<?php
// Get a document
$dbDriver = \ByJG\AnyDataset\Factory::getNoSqlInstance('mongodb://host');
$document = $dbDriver->getDocumentById('iddcouemnt');

// Update some fields in there
$data = $document->getDocument();
$data['some_field'] = 'some_value';
$document->setDocument($data);

// Save the document
$dbDriver->save($document);
$iterator = $dataset->getIterator();
print_r($iterator->toArray());
```

### Querying Key/Value Databases
### Iterating with While

```php
<?php
// Get a document
$dbDriver = \ByJG\AnyDataset\Factory::getKeyValueInstance('s3://awsid:secret@region');
$file = $dbDriver->get('key');

// Save the document
$dbDriver->put('key', file_get_contents('/path/to/file'));

// Delete the document
$dbDriver->remove('key');
$iterator = $dataset->getIterator();
while ($iterator->hasNext()) {
$row = $iterator->moveNext();

print_r($row->get("field1"));
}
```



### 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
<?php
$dbDriver = new \ByJG\AnyDataset\Store\Route();

// Define the available connections (even different databases)
$dbDriver
->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;
```

### And more

And more...


## Install

Just type: `composer require "byjg/anydataset=3.0.*"`
Just type: `composer require "byjg/anydataset=4.0.*"`

#### Running Unit tests

Running the Unit tests

```php
phpunit
```

#### Running database tests

Run integration tests require you to have the databases up e run with the follow configuration

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
node_modules/.bin/usdocker -v --no-link mongodb up
```

**Run the tests**


```
phpunit testsdb/PdoMySqlTest.php
phpunit testsdb/PdoSqliteTest.php
phpunit testsdb/PdoPostgresTest.php
phpunit testsdb/PdoDblibTest.php
phpunit testsdb/MongoDbDriverTest.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
vendor/bin/phpunit
```

----
Expand Down
28 changes: 6 additions & 22 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "byjg/anydataset",
"description": "A data abstraction layer in PHP for access read and write Relational Databases, No Sql, Text, Xml, Json, SparQl and others",
"description": "Anydataset is an agnostic data source abstraction layer in PHP. Core Module.",
"authors": [
{
"name": "João Gilberto Magalhães",
Expand All @@ -9,37 +9,21 @@
],
"autoload": {
"psr-4": {
"ByJG\\AnyDataset\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\AnyDataset\\": "tests/",
"TestsDb\\AnyDataset\\": "testsdb/"
"ByJG\\AnyDataset\\Core\\": "src/"
}
},
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"php": ">=5.6.0",
"ext-json": "*",
"byjg/xmlutil": "1.0.*",
"byjg/sparqllib": "1.0.*",
"psr/log": "1.0.*",
"psr/cache": "1.0.*",
"byjg/serializer": "1.0.*",
"byjg/uri": "1.0.*",
"aws/aws-sdk-php": "3.*"
"byjg/serializer": "1.0.*"
},
"suggest": {
"ext-dom": "*"
},
"require-dev": {
"phpunit/phpunit": ">5.7"
},
"suggest": {
"ext-curl": "*",
"ext-pdo_mysql": "*",
"ext-pdo_pgsql": "*",
"ext-mongodb": "*",
"byjg/cache-engine": "1.0.*"
},
"license": "MIT"
}
21 changes: 0 additions & 21 deletions docs/Connecting-to-Data-Sources.md

This file was deleted.

0 comments on commit 1c25c38

Please sign in to comment.