2019-11-27 22:19:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Installer\Utils;
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Real basic to interface with an importer
|
|
|
|
*/
|
|
|
|
class ImporterDB
|
|
|
|
{
|
2019-12-02 22:57:35 +08:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $batchSize;
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
/**
|
|
|
|
* @var PDO
|
|
|
|
*/
|
|
|
|
private $conn;
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-11-27 22:19:20 +08:00
|
|
|
private $dsn;
|
2019-12-02 22:57:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-27 22:19:20 +08:00
|
|
|
private $creds;
|
|
|
|
|
|
|
|
public function __construct($creds)
|
|
|
|
{
|
|
|
|
$this->creds = $creds;
|
|
|
|
$this->dsn = 'mysql:'.implode(';', [
|
|
|
|
'host='.$this->creds['host'],
|
|
|
|
'port='.$this->creds['port'],
|
|
|
|
'dbname='.$this->creds['name'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
Log::info('Using DSN: '.$this->dsn);
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
$this->batchSize = config('installer.importer.batch_size', 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$this->close();
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function connect()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->conn = new PDO($this->dsn, $this->creds['user'], $this->creds['pass']);
|
|
|
|
$this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
Log::error($e);
|
2019-12-02 22:57:35 +08:00
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function close()
|
|
|
|
{
|
|
|
|
if ($this->conn) {
|
|
|
|
$this->conn = null;
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the table name with the prefix
|
|
|
|
*
|
|
|
|
* @param $table
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function tableName($table)
|
|
|
|
{
|
|
|
|
if ($this->creds['table_prefix'] !== false) {
|
|
|
|
return $this->creds['table_prefix'].$table;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $table;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $table
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getTotalRows($table)
|
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
|
|
|
|
$sql = 'SELECT COUNT(*) FROM '.$this->tableName($table);
|
|
|
|
$rows = $this->conn->query($sql)->fetchColumn();
|
|
|
|
|
|
|
|
Log::info('Found '.$rows.' rows in '.$table);
|
|
|
|
|
|
|
|
return (int) $rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read all the rows in a table, but read them in a batched manner
|
|
|
|
*
|
|
|
|
* @param string $table The name of the table
|
|
|
|
* @param int [$start_offset]
|
2019-12-02 22:57:35 +08:00
|
|
|
* @param string [$fields]
|
2019-11-27 22:19:20 +08:00
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*/
|
2019-12-02 22:57:35 +08:00
|
|
|
public function readRows($table, $start_offset = 0, $fields = '*')
|
2019-11-27 22:19:20 +08:00
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
|
|
|
|
$offset = $start_offset;
|
|
|
|
$total_rows = $this->getTotalRows($table);
|
|
|
|
|
|
|
|
while ($offset < $total_rows) {
|
|
|
|
$rows_to_read = $offset + $this->batchSize;
|
|
|
|
if ($rows_to_read > $total_rows) {
|
|
|
|
$rows_to_read = $total_rows;
|
|
|
|
}
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
// Log::info('Reading '.$offset.' to '.$rows_to_read.' of '.$total_rows);
|
|
|
|
yield from $this->readRowsOffset($table, $this->batchSize, $offset, $fields);
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
$offset += $this->batchSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $table
|
|
|
|
* @param int $limit Number of rows to read
|
|
|
|
* @param int $offset Where to start from
|
2019-12-02 22:57:35 +08:00
|
|
|
* @param string [$fields]
|
2019-11-27 22:19:20 +08:00
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*/
|
2019-12-02 22:57:35 +08:00
|
|
|
public function readRowsOffset($table, $limit, $offset, $fields = '*')
|
2019-11-27 22:19:20 +08:00
|
|
|
{
|
2019-12-02 22:57:35 +08:00
|
|
|
if (is_array($fields)) {
|
|
|
|
$fields = implode(',', $fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = 'SELECT '.$fields.' FROM '.$this->tableName($table).' LIMIT '.$limit.' OFFSET '.$offset;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
$result = $this->conn->query($sql);
|
2019-12-02 22:57:35 +08:00
|
|
|
if (!$result || $result->rowCount() === 0) {
|
|
|
|
return;
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($result as $row) {
|
|
|
|
yield $row;
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
// Without incrementing the offset, it should re-run the same query
|
|
|
|
Log::error('Error readRowsOffset: '.$e->getMessage());
|
|
|
|
|
|
|
|
if (strpos($e->getMessage(), 'server has gone away') !== false) {
|
|
|
|
$this->connect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|