Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MysqlMetadata Enhancements #201

Open
wants to merge 1 commit into
base: 2.13.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Metadata/Object/ColumnObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ class ColumnObject
*/
protected $errata = [];

/**
*
* @var string
*/
protected $column_key = null;

/**
*
* @var string
*/
protected $extra = null;

/**
* Constructor
*
Expand Down Expand Up @@ -378,4 +390,40 @@ public function setErrata($errataName, $errataValue)
$this->errata[$errataName] = $errataValue;
return $this;
}

/**
* @return null|string the $columnKey
*/
public function getColumnKey()
{
return $this->column_key;
}

/**
* @param string $columnKey the $columnKey to set
* @return self Provides a fluent interface
*/
public function setColumnKey($columnKey)
{
$this->column_key = $columnKey;
return $this;
}

/**
* @return null|string the $extra
*/
public function getExtra()
{
return $this->extra;
}

/**
* @param string $extra the $extra to set
* @return self Provides a fluent interface
*/
public function setExtra($extra)
{
$this->extra = $extra;
return $this;
}
}
4 changes: 3 additions & 1 deletion src/Metadata/Source/AbstractSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function getColumn($columnName, $table, $schema = null)
'ordinal_position', 'column_default', 'is_nullable',
'data_type', 'character_maximum_length', 'character_octet_length',
'numeric_precision', 'numeric_scale', 'numeric_unsigned',
'erratas'
'erratas', 'column_key', 'extra'
];
foreach ($props as $prop) {
if (isset($info[$prop])) {
Expand All @@ -261,6 +261,8 @@ public function getColumn($columnName, $table, $schema = null)
$column->setNumericScale($info['numeric_scale']);
$column->setNumericUnsigned($info['numeric_unsigned']);
$column->setErratas($info['erratas']);
$column->setColumnKey($info['column_key']);
$column->setExtra($info['extra']);

return $column;
}
Expand Down
57 changes: 29 additions & 28 deletions src/Metadata/Source/MysqlMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ protected function loadColumnData($table, $schema)
['C', 'NUMERIC_SCALE'],
['C', 'COLUMN_NAME'],
['C', 'COLUMN_TYPE'],
['C', 'COLUMN_KEY'],
['C', 'EXTRA'],
];

array_walk($isColumns, function (&$c) use ($p) {
array_walk($isColumns, static function (&$c) use ($p) {
$c = $p->quoteIdentifierChain($c);
});

Expand Down Expand Up @@ -154,14 +156,16 @@ protected function loadColumnData($table, $schema)
$columns[$row['COLUMN_NAME']] = [
'ordinal_position' => $row['ORDINAL_POSITION'],
'column_default' => $row['COLUMN_DEFAULT'],
'is_nullable' => ('YES' == $row['IS_NULLABLE']),
'is_nullable' => ('YES' === $row['IS_NULLABLE']),
'data_type' => $row['DATA_TYPE'],
'character_maximum_length' => $row['CHARACTER_MAXIMUM_LENGTH'],
'character_octet_length' => $row['CHARACTER_OCTET_LENGTH'],
'numeric_precision' => $row['NUMERIC_PRECISION'],
'numeric_scale' => $row['NUMERIC_SCALE'],
'numeric_unsigned' => (false !== strpos($row['COLUMN_TYPE'], 'unsigned')),
'erratas' => $erratas,
'column_key' => $row['COLUMN_KEY'],
'extra' => $row['EXTRA'],
];
}

Expand Down Expand Up @@ -191,7 +195,7 @@ protected function loadConstraintData($table, $schema)

$p = $this->adapter->getPlatform();

array_walk($isColumns, function (&$c) use ($p) {
array_walk($isColumns, static function (&$c) use ($p) {
$c = $p->quoteIdentifierChain($c);
});

Expand Down Expand Up @@ -223,7 +227,7 @@ protected function loadConstraintData($table, $schema)
. ' AND ' . $p->quoteIdentifierChain(['T', 'TABLE_TYPE'])
. ' IN (\'BASE TABLE\', \'VIEW\')';

if ($schema != self::DEFAULT_SCHEMA) {
if ($schema !== self::DEFAULT_SCHEMA) {
$sql .= ' AND ' . $p->quoteIdentifierChain(['T', 'TABLE_SCHEMA'])
. ' = ' . $p->quoteTrustedValue($schema);
} else {
Expand All @@ -244,36 +248,33 @@ protected function loadConstraintData($table, $schema)

$realName = null;
$constraints = [];
foreach ($results->toArray() as $row) {
if ($row['CONSTRAINT_NAME'] !== $realName) {
$realName = $row['CONSTRAINT_NAME'];
$isFK = ('FOREIGN KEY' == $row['CONSTRAINT_TYPE']);
if ($isFK) {
if ($results !== null) {
foreach ($results->toArray() as $row) {
if ($row['CONSTRAINT_NAME'] !== $realName) {
$realName = $row['CONSTRAINT_NAME'];
$isFK = ('FOREIGN KEY' === $row['CONSTRAINT_TYPE']);
$name = $realName;
} else {
$name = '_laminas_' . $row['TABLE_NAME'] . '_' . $realName;
$constraints[$name] = [
'constraint_name' => $name,
'constraint_type' => $row['CONSTRAINT_TYPE'],
'table_name' => $row['TABLE_NAME'],
'columns' => [],
];
if ($isFK) {
$constraints[$name]['referenced_table_schema'] = $row['REFERENCED_TABLE_SCHEMA'];
$constraints[$name]['referenced_table_name'] = $row['REFERENCED_TABLE_NAME'];
$constraints[$name]['referenced_columns'] = [];
$constraints[$name]['match_option'] = $row['MATCH_OPTION'];
$constraints[$name]['update_rule'] = $row['UPDATE_RULE'];
$constraints[$name]['delete_rule'] = $row['DELETE_RULE'];
}
}
$constraints[$name] = [
'constraint_name' => $name,
'constraint_type' => $row['CONSTRAINT_TYPE'],
'table_name' => $row['TABLE_NAME'],
'columns' => [],
];
$constraints[$name]['columns'][] = $row['COLUMN_NAME'];
Copy link
Contributor

@tptrixtop tptrixtop Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this $name will be empty if above case wont be triggered?)

Copy link
Author

@willjones9 willjones9 Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tptrixtop possibly, however that potential case isn't new to this commit; it has existed for some time.
PhpStorm also warns me about line 272, saying $isFK is probably undefined:

$constraints[$name]['columns'][] = $row['COLUMN_NAME'];
if ($isFK) {
    $constraints[$name]['referenced_columns'][] = $row['REFERENCED_COLUMN_NAME'];
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like $name was initializing in else { case in previous version, in new version else case was removed and allows $name to be uninitialized or i am missing something

if ($isFK) {
$constraints[$name]['referenced_table_schema'] = $row['REFERENCED_TABLE_SCHEMA'];
$constraints[$name]['referenced_table_name'] = $row['REFERENCED_TABLE_NAME'];
$constraints[$name]['referenced_columns'] = [];
$constraints[$name]['match_option'] = $row['MATCH_OPTION'];
$constraints[$name]['update_rule'] = $row['UPDATE_RULE'];
$constraints[$name]['delete_rule'] = $row['DELETE_RULE'];
$constraints[$name]['referenced_columns'][] = $row['REFERENCED_COLUMN_NAME'];
}
}
$constraints[$name]['columns'][] = $row['COLUMN_NAME'];
if ($isFK) {
$constraints[$name]['referenced_columns'][] = $row['REFERENCED_COLUMN_NAME'];
}
}

$this->data['constraints'][$schema][$table] = $constraints;
}

Expand Down