Skip to content

Commit

Permalink
Merge pull request #2 from byjg/3.0.0
Browse files Browse the repository at this point in the history
3.0.0
  • Loading branch information
byjg committed May 27, 2017
2 parents ae01032 + 60582ad commit 9b98c5c
Show file tree
Hide file tree
Showing 114 changed files with 4,890 additions and 4,159 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,9 +1,9 @@
language: php
php:
- "7.1"
- "7.0"
- "5.6"
- "5.5"
- "5.4"

install:
- composer install
Expand Down
138 changes: 94 additions & 44 deletions README.md
Expand Up @@ -10,66 +10,116 @@ A data abstraction layer in PHP to manipulate any set of data with a standardize
## Features

### Read an write* with a number of data sources accessed by a standardized interface ([see more here](docs/Connecting-to-Data-Sources.md)):

* Array
* Relational Databases (based on PDO)
* DBLib (SQL Server php native - windows only)
* 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

## Examples

### 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.

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

// and then:
foreach ($iterator as $row) {
echo $row->getField('field1'); // or $row->toArray();
echo $row->get('field1'); // or $row->toArray();
}

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

### Querying Relational Databases

```php
<?php
$dbDriver = \ByJG\AnyDataset\Factory::getDbRelationalInstance('mysql://username:password@host/database');
$iterator = $dbDriver->getIterator('select * from table where field = :param', ['param' => 'value']);
```

### Cache results

You can easily cache your results with the CachedDBDataset class;
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
$repository = new DBDataset('connection');
$repository->setCacheEngine(new \ByJG\Cache\MemcachedEngine());
$iterator = $repository->getIterator('select field1, field2 from sometable', [], 120); // cache for 2 minutes
<?php
$dbDriver = \ByJG\AnyDataset\Factory::getDbRelationalInstance('mysql://username:password@host/database');
$dbCached = new \ByJG\AnyDataset\Store\DbCached($dbDriver, $psrCacheEngine, 30);
$iterator = $dbDriver->getIterator('select * from table where field = :param', ['param' => 'value']);
```

### Relational database connections string based on URL
### Connection based on URI

The connection string for databases is based on URL.

The connection string for relational databases is based on URL. Connect to mysql in the server localhost with user 'root'
and password 'somepass' is easy as `mysql://root:somepass@localhost/schema`
See below the current implemented drivers:

You can store your connections string in the file `config/anydatasetconfig.php` like
| 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() |
| Sql Relay | sqlrelay://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
return [
'connections' => [
'development' => [
'url' => 'mysql://root:somepass@localhost/schema',
'type' => 'dsn'
],
'staging' => [
'url' => 'mysql://root:otherpass@192.168.1.205:3307/schema',
'type' => 'dsn'
]
]
];
<?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);
```

### Querying Key/Value Databases

```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');
```



### Load balance and connection pooling

The API have support for connection load balancing, connection pooling and persistent connection with
Expand All @@ -81,39 +131,39 @@ You only need change your connection string to:
sqlrelay://root:somepass@server/schema
```

### Create DAL class easily

```php
class MyDAL extends BaseDBAccess
{
public function getById($id)
{
return $this->getIterator('select * from sometable where id = :id', [ 'id' => $id ]);
}

// Some query need to be cached for 180 seconds
public function getExpensiveQuery()
{
return $this->getIterator('select * from expensive_query', null, 180);
}
}
```

### And more

And more...


## Install

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

## Running Tests
#### 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

- Server: XXXXX_container (where XXXX is the driver e.g. mysql)
- Database: test
- Username: root
- password: password

```
phpunit testsdb/PdoMySqlTest.php
phpunit testsdb/PdoSqliteTest.php
phpunit testsdb/PdoPostgresTest.php
phpunit testsdb/PdoDblibTest.php
phpunit testsdb/MongoDbDriverTest.php
```

----
[Open source ByJG](http://opensource.byjg.com)
19 changes: 11 additions & 8 deletions composer.json
@@ -1,6 +1,6 @@
{
"name": "byjg/anydataset",
"description": "A data abstraction layer in PHP to manipulate any set of data with a standardized interface from any data source.",
"description": "A data abstraction layer in PHP for access read and write Relational Databases, No Sql, Text, Xml, Json, SparQl and others",
"authors": [
{
"name": "João Gilberto Magalhães",
Expand All @@ -16,20 +16,23 @@
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"php": ">=5.4.0",
"php": ">=5.5.0",
"ext-json": "*",
"byjg/xmlutil": "1.0.*",
"byjg/sparqllib": "1.0.*",
"byjg/singleton-pattern": "1.0.*",
"neitanod/forceutf8": "v2.0",
"monolog/monolog": "1.*",
"sheikhheera/iconfig": "1.0.*",
"byjg/cache-engine": "1.0.*",
"byjg/serializer": "1.0.*"
"psr/log": "1.0.*",
"psr/cache": "1.0.*",
"byjg/serializer": "1.0.*",
"byjg/uri": "1.0.*",
"aws/aws-sdk-php": "3.*"
},
"suggest": {
"ext-curl": "*",
"ext-pdo_mysql": "*"
"ext-pdo_mysql": "*",
"ext-pdo_pgsql": "*",
"ext-mongodb": "*",
"byjg/cache-engine": "1.0.*"
},
"license": "MIT"
}
31 changes: 0 additions & 31 deletions config/anydatasetconfig-dist.php

This file was deleted.

23 changes: 12 additions & 11 deletions docs/Connecting-to-Data-Sources.md
Expand Up @@ -3,18 +3,19 @@ The big advantage is you can use a standard interface to navigate the data by us
understand the IteratorInterface.


| Object | Data Source | Read | Write | Reference |
| ------------ | ------------- |:----:|:-----:| ----------------------- |
| DBDataSet | Relational DB | yes | yes | [Connecting to a relational Databases](Connecting-to-a-relational-databases.md) |
| AnyDataSet | Anydataset | yes | yes | |
| ArrayDataSet | Array | yes | no | |
| TextFileDataSet | Delimited CSV / RegEx from file, http or ftp | yes | no | |
| Object | Data Source | Read | Write | Reference |
| ---------------------- | ------------- |:----:|:-----:| ----------------------- |
| DbDriverInterface | Relational DB | yes | yes | [Connecting to a relational Databases](Connecting-to-a-relational-databases.md) |
| AnyDataSet | Anydataset | yes | yes | |
| ArrayDataSet | Array | yes | no | |
| TextFileDataSet | Delimited CSV / RegEx from file, http or ftp | yes | no | |
| FixedTextFileDataSet | Fixed layout from file, http or ftp | yes | no | |
| XmlDataSet | Xml | yes | no | |
| JSONDataSet | Json | yes | no | |
| SparQLDataSet| SparQl Repositories | yes | no | |
| SocketDataset| Text(Deprecated) | yes | no | |
| NoSQLDataSet | MongoDB | yes | yes | [Connecting to MongoDB]([MongoDB](Connecting-to-MongoDB.md)) |
| XmlDataSet | Xml | yes | no | |
| JSONDataSet | Json | yes | no | |
| SparQLDataSet | SparQl Repositories | yes | no | |
| SocketDataset | Text(Deprecated) | yes | no | |
| NoSqlDocumentInterface | NoSql Document Based | yes | yes | [Connecting to MongoDB]([MongoDB](Connecting-to-MongoDB.md)) |
| KeyValueInterface | NoSql Key/Value Based | yes | yes | |



0 comments on commit 9b98c5c

Please sign in to comment.