Skip to content

Commit

Permalink
Merge pull request #10 from activecollab/feature/import-from-file
Browse files Browse the repository at this point in the history
Add executeFromFile() method.
  • Loading branch information
ilijastuden authored Feb 8, 2017
2 parents dc42d21 + ad7ef4a commit f38accc
Show file tree
Hide file tree
Showing 5 changed files with 841 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ $num = $this->connection->count('writers', null, '*');
$num = $this->connection->count('writers', null, 'name')
```

## Running Queries from File

In order to run all queries from a file, use `executeFromFile()` method:

```php
$connection->executeFromFile('/path/to/file');
```

**Note:** This method is not implemented to handle large data dumps. Use `mysqldump` instead, or another specialized backup utility instead.

## Connection Factory

Quickest way to connect is to use `ConnectionFactory` class:
Expand Down
22 changes: 22 additions & 0 deletions src/Connection/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,28 @@ public function inTransaction()
return $this->transaction_level > 0;
}

/**
* @param string $file_path
*/
public function executeFromFile($file_path)
{
if (!is_file($file_path)) {
throw new RuntimeException('File not found');
}

if ($this->link->multi_query(file_get_contents($file_path))) {
do {
if ($result = $this->link->store_result()) {
$result->free();
}

if (!$this->link->more_results()) {
break;
}
} while ($this->link->next_result());
}
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions src/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ public function rollback();
*/
public function inTransaction();

/**
* @param string $file_path
*/
public function executeFromFile($file_path);

/**
* @param string $database_name
* @return bool
Expand Down
Loading

0 comments on commit f38accc

Please sign in to comment.