Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Fabrégat committed Nov 22, 2018
0 parents commit 84efab6
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea
/composer.lock
/vendor
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Joan Fabrégat / Code Inc. SAS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Microsoft Access database reader

This library is a Microsoft Access database reader written in PHP 7.1. It uses [mdbtools](https://sourceforge.net/projects/mdbtools/) to access the database schema and content.

## Installation

This library is available through [Packagist](https://packagist.org/packages/codeinc/ms-access-reader) and can be installed using [Composer](https://getcomposer.org/):

```bash
composer require codeinc/ms-access-reader
```


## License

The library is published under the MIT license (see [`LICENSE`](LICENSE) file).
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "codeinc/ms-access-reader",
"version": "1.0.0",
"description": "Microsoft Access database reader using mdbtools",
"homepage": "https://github.com/CodeIncHQ/MsAccessReader",
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.1"
},
"require-dev": {
"codeinc/error-renderer": "^1.0"
},
"autoload": {
"psr-4": {
"CodeInc\\MsAccessReader\\": "src"
}
},
"authors": [
{
"name": "Joan Fabrégat",
"email": "[email protected]",
"homepage": "https://www.codeinc.fr",
"role": "developer"
}
],
"config": {
"preferred-install": {
"*": "dist"
}
}
}
147 changes: 147 additions & 0 deletions src/AccessReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2018 - Code Inc. SAS - All Rights Reserved. |
// | Visit https://www.codeinc.fr for more information about licensing. |
// +---------------------------------------------------------------------+
// | NOTICE: All information contained herein is, and remains the |
// | property of Code Inc. SAS. The intellectual and technical concepts |
// | contained herein are proprietary to Code Inc. SAS are protected by |
// | trade secret or copyright law. Dissemination of this information or |
// | reproduction of this material is strictly forbidden unless prior |
// | written permission is obtained from Code Inc. SAS. |
// +---------------------------------------------------------------------+
//
// Author: Joan Fabrégat <[email protected]>
// Date: 15/11/2018
// Project: MsAccessReader
//
declare(strict_types=1);
namespace CodeInc\MsAccessReader;

/**
* Class AccessReader
*
* @package CodeInc\MsAccessReader
* @author Joan Fabrégat <[email protected]>
*/
class AccessReader
{
/**
* @var string
*/
private $dbPath;

/**
* AccessReader constructor.
*
* @param string $dbPath
*/
public function __construct(string $dbPath)
{
$this->setDbPath($dbPath);
$this->checkShellCommands();
}

/**
* @param string $dbPath
* @throws \RuntimeException
*/
private function setDbPath(string $dbPath):void
{
if (!file_exists($dbPath)) {
throw new \RuntimeException(
sprintf("The Access DB file '%s' does not exist", $dbPath)
);
}
$this->dbPath = $dbPath;
}

/**
* @throws \RuntimeException
*/
private function checkShellCommands():void
{
foreach (['mdb-tables', 'mdb-schema', 'mdb-export'] as $command) {
if (empty(shell_exec('which '.escapeshellarg($command)))) {
throw new \RuntimeException(
sprintf("The command '%s' is missing. "
."Please check if the mdbtools (https://sourceforge.net/projects/mdbtools/) packages is installed",
$command)
);
}
}
}

/**
* @return string
*/
private function getBoundary():string
{
return '--'.uniqid('boundary').'--';
}

/**
* @return \Generator|string[]
*/
public function listTables():\Generator
{
$boundary = $this->getBoundary();
$tablesList = shell_exec('mdb-tables -d'.escapeshellarg($boundary).' '.escapeshellarg($this->dbPath));
foreach (explode($boundary, trim($tablesList)) as $table) {
if (!empty($table)) {
yield $table;
}
}
}

/**
* @param string $table
* @param bool $dropTable
* @param string $backend
* @return string
*/
public function exportSchemaToSql(string $table, bool $dropTable = false, string $backend = 'mysql'):string
{
$schema = shell_exec('mdb-schema --default-values'.($dropTable ? ' --drop-table' : '')
.' -T '.escapeshellarg($table).' '.escapeshellarg($this->dbPath).' '.escapeshellarg($backend));
if ($backend == 'mysql') {
$schema = preg_replace_callback("/varchar ?\\(([0-9]+)\\)/ui", function (array $matches):string {
return $matches[1] > 255 ? 'text' : $matches[0];
}, $schema);
}
return $schema;
}

/**
* @param string $table
* @return \Generator|string[]
*/
public function exportDataToSql(string $table):\Generator
{
$boundary = $this->getBoundary();
$data = shell_exec('mdb-export -H -R'.escapeshellarg($boundary).' -I mysql '
.escapeshellarg($this->dbPath).' '.escapeshellarg($table));
foreach (explode($boundary, $data) as $query) {
if (!empty($query)) {
yield $query;
}
}
}

/**
* @param string $table
* @return \Generator|array[]
*/
public function exportDataToArray(string $table):\Generator
{
$data = shell_exec('mdb-export -H '.escapeshellarg($this->dbPath).' '.escapeshellarg($table));
foreach (explode("\n", $data) as $line) {
if (!empty($line)) {
yield str_getcsv($line, ',', '"');
}
}
}
}

0 comments on commit 84efab6

Please sign in to comment.