Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
42.86% covered (warning)
42.86%
3 / 7
CRAP
87.88% covered (success)
87.88%
29 / 33
MySQLDatabase
0.00% covered (danger)
0.00%
0 / 1
42.86% covered (warning)
42.86%
3 / 7
14.35
87.88% covered (success)
87.88%
29 / 33
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.02
83.33% covered (success)
83.33%
5 / 6
 refresh
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 getTableList
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getTableByName
0.00% covered (danger)
0.00%
0 / 1
3.14
75.00% covered (success)
75.00%
3 / 4
 readMySQLTable
0.00% covered (danger)
0.00%
0 / 1
3.04
83.33% covered (success)
83.33%
5 / 6
 getTableDTO
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
 getStoredProcedureList
0.00% covered (danger)
0.00%
0 / 1
2.02
83.33% covered (success)
83.33%
5 / 6
<?php
declare(strict_types = 1);
namespace Siesta\Driver\MySQL\MetaData;
use Siesta\Database\Connection;
use Siesta\Database\CreateStatementFactory;
use Siesta\Database\MetaData\DatabaseMetaData;
use Siesta\Database\MetaData\TableMetaData;
/**
 * @author Gregor Müller
 */
class MySQLDatabase implements DatabaseMetaData
{
    const SQL_GET_SP_LIST = "SELECT ROUTINE_NAME FROM information_schema.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_SCHEMA = '%s';";
    const SQL_GET_TABLE_LIST = "SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA='%s';";
    const ROUTINE_NAME = "ROUTINE_NAME";
    /**
     * @var Connection
     */
    protected $connection;
    /**
     * @var MySQLTable[]
     */
    protected $tableList;
    /**
     * MySQLDatabase constructor.
     *
     * @param Connection $connection
     * @param string|null $databaseName
     */
    public function __construct(Connection $connection, string $databaseName = null)
    {
        $this->connection = $connection;
        $this->tableList = [];
        if ($databaseName !== null) {
            $this->connection->useDatabase($databaseName);
        }
        $this->readMySQLTable();
    }
    /**
     *
     */
    public function refresh()
    {
        $this->tableList = [];
        $this->readMySQLTable();
    }
    /**
     * @return array
     */
    public function getTableList() : array
    {
        return $this->tableList;
    }
    /**
     * @param string $tableName
     *
     * @return TableMetaData|null
     */
    public function getTableByName(string $tableName)
    {
        foreach ($this->tableList as $table) {
            if ($table->getName() === $tableName) {
                return $table;
            }
        }
        return null;
    }
    /**
     *
     */
    protected function readMySQLTable()
    {
        $tableDTOList = $this->getTableDTO();
        foreach ($tableDTOList as $tableDTO) {
            // do not care about sequencer table
            if ($tableDTO->name === CreateStatementFactory::SEQUENCER_TABLE_NAME) {
                continue;
            }
            $this->tableList[] = new MySQLTable($this->connection, $tableDTO);
        }
    }
    /**
     * @return TableDTO[]
     */
    protected function getTableDTO() : array
    {
        $tableDTOList = [];
        $sql = sprintf(self::SQL_GET_TABLE_LIST, $this->connection->getDatabase());
        $resultSet = $this->connection->query($sql);
        while ($resultSet->hasNext()) {
            $tableDTOList[] = new TableDTO($resultSet);
        }
        $resultSet->close();
        return $tableDTOList;
    }
    /**
     * @return string[]
     */
    public function getStoredProcedureList() : array
    {
        $spNameList = [];
        $sql = sprintf(self::SQL_GET_SP_LIST, $this->connection->getDatabase());
        $resultSet = $this->connection->query($sql);
        while ($resultSet->hasNext()) {
            $spNameList[] = $resultSet->getStringValue(self::ROUTINE_NAME);
        }
        return $spNameList;
    }
}