Skip to content

Commit

Permalink
Merge pull request #7 from xprojects-de/development
Browse files Browse the repository at this point in the history
Better ConnectionHandling for Database
  • Loading branch information
xprojects-de authored Nov 4, 2020
2 parents ad3dab8 + 43b7e5d commit 701de96
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 155 deletions.
139 changes: 0 additions & 139 deletions src/Database/AlpdeskcoreConnectionFactory.php

This file was deleted.

163 changes: 150 additions & 13 deletions src/Model/Database/AlpdeskcoreDatabasemanagerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,167 @@

use Contao\Model;
use Alpdesk\AlpdeskCore\Library\Cryption\Cryption;
use Alpdesk\AlpdeskCore\Database\AlpdeskcoreConnectionFactory;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Column;

class AlpdeskcoreDatabasemanagerModel extends Model {

protected static $strTable = 'tl_alpdeskcore_databasemanager';
private static $connectionsTable = [];

public function findById(int $id) {
$result = self::findBy(array('id=?'), array($id));
if ($result !== null) {
$decryption = new Cryption(true);
$result->password = $decryption->safeDecrypt($result->password);
private static function create(int $id, string $host, int $port, string $username, string $password, string $database): ?Connection {

if (\array_key_exists($id, self::$connectionsTable)) {
if (self::$connectionsTable[$id] instanceof Connection) {
if (self::$connectionsTable[$id] !== null) {
return self::$connectionsTable[$id];
}
}
}

$params = [
'driver' => 'pdo_mysql',
'host' => $host,
'port' => $port,
'user' => $username,
'password' => $password,
'dbname' => $database
];

try {
self::$connectionsTable[$id] = DriverManager::getConnection($params);
if (!self::$connectionsTable[$id]->isConnected()) {
self::$connectionsTable[$id]->connect();
}
return self::$connectionsTable[$id];
} catch (DBALException $e) {

}
return null;
}

public static function destroy(int $id) {
if (\array_key_exists($id, self::$connectionsTable)) {
if (self::$connectionsTable[$id] instanceof Connection) {
if (self::$connectionsTable[$id] !== null) {
self::$connectionsTable[$id]->close();
self::$connectionsTable[$id] = null;
}
}
}
}

public static function listTables(int $id, ?string $name): array {

if (null === $name || !\array_key_exists($id, self::$connectionsTable)) {
throw new \Exception($GLOBALS['TL_LANG']['tl_alpdeskcore_databasemanager']['invalid_parameters']);
}

if (self::$connectionsTable[$id] == null) {
throw new \Exception($GLOBALS['TL_LANG']['tl_alpdeskcore_databasemanager']['invalid_parameters']);
}

try {
$tables = self::$connectionsTable[$id]->getSchemaManager()->createSchema()->getTables();
$structure = array();
foreach ($tables as $table) {
if ($table instanceof Table) {

$options = "";
$tableOptions = $table->getOptions();
if ($tableOptions !== null && \is_array($tableOptions) && count($tableOptions) > 0) {
if (\array_key_exists('engine', $tableOptions)) {
$options .= 'engine: ' . $tableOptions['engine'];
}
if (\array_key_exists('collation', $tableOptions)) {
$options .= ' | collation: ' . $tableOptions['collation'];
}
if (\array_key_exists('autoincrement', $tableOptions)) {
$options .= ' | autoincrement: ' . $tableOptions['autoincrement'];
}
}

$primaryKey = array();
foreach ($table->getPrimaryKey()->getColumns() as $column) {
array_push($primaryKey, $column);
}
$indexes = array();
foreach ($table->getIndexes() as $indexEntry) {
if ('PRIMARY' !== $indexEntry->getName()) {
$indexInfo = array(
'indextype' => ($indexEntry->isUnique() ? 1 : 0),
'indexfields' => '',
'indexname' => $indexEntry->getName()
);
$tmpIndexFields = array();
foreach ($indexEntry->getColumns() as $column) {
array_push($tmpIndexFields, $column);
}
$indexInfo['indexfields'] = implode(',', $tmpIndexFields);
array_push($indexes, $indexInfo);
}
}

$structure[$table->getName()] = array(
'options' => $options,
'primary' => implode(', ', $primaryKey),
'indexes' => implode(', ', $indexes)
);

$columns = $table->getColumns();
if ($columns !== null && \is_array($columns) && count($columns) > 0) {
foreach ($columns as $column) {
if ($column instanceof Column) {
$type = $column->getType();
$autoincrement = $column->getAutoincrement();
$output = $type->getName();
if ($column->getAutoincrement()) {
$output .= ' | autoincrement';
}
if ($column->getUnsigned()) {
$output .= ' | unsigned';
}
if ($column->getNotnull()) {
$output .= ' | NOT NULL';
}
$default = $column->getDefault();
if ($default !== null) {
$output .= ' | DEFAULT "' . $default . '"';
}
$length = $column->getLength();
if ($length !== null && $length != "") {
$output .= ' | LENGTH ' . $length;
}
$platformOptions = $column->getPlatformOptions();
if ($platformOptions !== null && is_array($platformOptions) && count($platformOptions) > 0) {
foreach ($platformOptions as $pKey => $pValue) {
$output .= ' | ' . $pKey . ' ' . $pValue;
}
}
$structure[$table->getName()][$column->getName()] = $output;
}
}
}
}
}

return $structure;
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
return $result;
}

public function connectionById(int $id): ?Connection {
$value = null;
$dbresult = self::findById($id);
if ($dbresult !== null) {
$connection = AlpdeskcoreConnectionFactory::create($dbresult->host, intval($dbresult->port), $dbresult->username, $dbresult->password, $dbresult->database);
return $connection;
$result = self::findByPk($id);
if ($result !== null) {
$decryption = new Cryption(true);
$result->password = $decryption->safeDecrypt($result->password);
return self::create($id, $result->host, intval($result->port), $result->username, $result->password, $result->database);
}
return $value;
return null;
}

}
8 changes: 5 additions & 3 deletions src/Widget/AlpdeskcoreDatabasemanagerWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Alpdesk\AlpdeskCore\Widget;

use Contao\Widget;
use Alpdesk\AlpdeskCore\Database\AlpdeskcoreConnectionFactory;
use Alpdesk\AlpdeskCore\Model\Database\AlpdeskcoreDatabasemanagerModel;

class AlpdeskcoreDatabasemanagerWidget extends Widget {

Expand All @@ -16,17 +16,19 @@ class AlpdeskcoreDatabasemanagerWidget extends Widget {
public function generate(): string {
$outputValue = '';
if ($this->activeRecord !== null) {
$id = intval($this->activeRecord->id);
$host = $this->activeRecord->host;
$port = intval($this->activeRecord->port);
$username = $this->activeRecord->username;
$password = $this->activeRecord->password;
$database = $this->activeRecord->database;
if ($host != '' && $port != '' && $username != '' && $password != '' && $database != '') {
$outputValue = $GLOBALS['TL_LANG']['tl_alpdeskcore_databasemanager']['valid_parameters'] . '<br>';
$connection = AlpdeskcoreConnectionFactory::create($host, $port, $username, $password, $database);
$connection = AlpdeskcoreDatabasemanagerModel::connectionById($id);
try {
if ($connection !== null) {
$structure = AlpdeskcoreConnectionFactory::listTables($connection, $database);
$structure = AlpdeskcoreDatabasemanagerModel::listTables($id, $database);
AlpdeskcoreDatabasemanagerModel::destroy($id);
$outputValue .= $GLOBALS['TL_LANG']['tl_alpdeskcore_databasemanager']['valid_connection'] . '<br>';
$outputValue .= '<hr>';
foreach ($structure as $key => $value) {
Expand Down

0 comments on commit 701de96

Please # to comment.