``php X::dump($db->getColumns('table_users')); /* (array)[ "id" => [ "position" => 1, "null" => 0, "key" => "PRI", "default" => null, "extra" => "auto_increment", "signed" => 0, "maxlength" => "8", "type" => "int", ], "name" => [ "position" => 2, "null" => 0, "key" => null, "default" => null, "extra" => "", "signed" => 0, "maxlength" => "30", "type" => "character varying", ], "surname" => [ "position" => 3, "null" => 0, "key" => null, "default" => null, "extra" => "", "signed" => 0, "maxlength" => "30", "type" => "character varying", ], "address" => [ "position" => 4, "null" => 0, "key" => "UNI", "default" => null, "extra" => "", "signed" => 0, "maxlength" => "30", "type" => "varchar", ], ]
function(string $table)
{
if (!$this->check()) {
return null;
}
$r = [];
if ($table = $this->tableFullName($table)) {
$keys = $this->getKeys($table);
$primary_keys = $keys['keys']['PRIMARY']['columns'] ?? [];
$unique_keys_arr = array_filter($keys['keys'] ?? [], function ($item, $key) {
return (bool)$item['unique'] === true && $key !== 'PRIMARY';
}, ARRAY_FILTER_USE_BOTH);
$unique_keys = array_map(function ($item) {
return $item['columns'][0] ?? null;
}, array_values($unique_keys_arr));
$sql = <<getRows($sql, $table)) {
foreach ($rows as $row) {
$f = $row['column_name'];
$r[$f] = [
'position' => $row['ordinal_position'],
'type' => $row['data_type'] === 'bytea' ? 'binary' : $row['data_type'],
'udt_name' => $row['udt_name'],
'null' => $row['is_nullable'] === 'NO' ? 0 : 1,
'key' => in_array($row['column_name'], $primary_keys)
? 'PRI'
: (in_array($row['column_name'], $unique_keys)
? 'UNI'
: null),
'extra' => strpos($row['column_default'], 'nextval') !== false &&
strpos($row['data_type'], 'int') !== false
? 'auto_increment'
: '',
'signed' => $row['numeric_precision'] !== null,
'virtual' => false,
'generation' => $row['generation_expression'],
];
if ($row['column_default'] !== null || $row['is_nullable'] === 'YES') {
$r[$f]['default'] = \is_null($row['column_default']) || strpos($row['column_default'], 'NULL') !== false
? 'NULL'
: $row['column_default'];
$r[$f]['defaultExpression'] = false;
if (in_array(strtoupper($row['column_default']), ['CURRENT_TIME', 'CURRENT_DATE', 'CURRENT_TIMESTAMP', 'NOW()'])) {
$r[$f]['defaultExpression'] = true;
}
}
if ($row['character_maximum_length'] !== null) {
$r[$f]['maxlength'] = $row['character_maximum_length'];
}
elseif ($row['numeric_precision'] !== null && $row['numeric_scale'] !== null) {
$r[$f]['maxlength'] = $row['numeric_precision'];
$r[$f]['decimals'] = $row['numeric_scale'];
}
elseif ($row['data_type'] === 'bytea') {
$r[$f]['maxlength'] = 16;
}
}
}
}
return $r;
}