``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" => "varchar", ], "surname" => [ "position" => 3, "null" => 0, "key" => null, "default" => null, "extra" => "", "signed" => 0, "maxlength" => "30", "type" => "varchar", ], "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 ($full = $this->tableFullName($table)) {
$t = explode('.', $full);
[$db, $table] = $t;
$sql = <<getRows($sql, $table, $db)) {
$p = 1;
foreach ($rows as $row) {
$f = $row['COLUMN_NAME'];
$has_length = (stripos($row['DATA_TYPE'], 'text') === false)
&& (stripos($row['DATA_TYPE'], 'blob') === false)
&& ($row['EXTRA'] !== 'VIRTUAL GENERATED');
$r[$f] = [
'position' => $p++,
'type' => $row['DATA_TYPE'],
'null' => $row['IS_NULLABLE'] === 'NO' ? 0 : 1,
'key' => \in_array($row['COLUMN_KEY'], ['PRI', 'UNI', 'MUL']) ? $row['COLUMN_KEY'] : null,
'extra' => $row['EXTRA'],
'signed' => strpos($row['COLUMN_TYPE'], ' unsigned') === false ? 1 : 0,
'virtual' => $row['EXTRA'] === 'VIRTUAL GENERATED',
'generation' => $row['GENERATION_EXPRESSION'],
];
if (($row['COLUMN_DEFAULT'] !== null) || ($row['IS_NULLABLE'] === 'YES')) {
$r[$f]['default'] = \is_null($row['COLUMN_DEFAULT']) ? 'NULL' : $row['COLUMN_DEFAULT'];
$r[$f]['defaultExpression'] = $row['EXTRA'] === 'DEFAULT_GENERATED';
}
if (($r[$f]['type'] === 'enum') || ($r[$f]['type'] === 'set')) {
if (preg_match_all('/\((.*?)\)/', $row['COLUMN_TYPE'], $matches)
&& !empty($matches[1])
&& \is_string($matches[1][0])
&& ($matches[1][0][0] === "'")
) {
$r[$f]['values'] = explode("','", substr($matches[1][0], 1, -1));
$r[$f]['extra'] = $matches[1][0];
} else {
$r[$f]['values'] = [];
}
} elseif (preg_match_all('/\((\d+)?(?:,)|(\d+)\)/', $row['COLUMN_TYPE'], $matches)) {
if (empty($matches[1][0])) {
if (!empty($matches[2][0])) {
$r[$f]['maxlength'] = (int)$matches[2][0];
}
} else {
$r[$f]['maxlength'] = (int)$matches[1][0];
$r[$f]['decimals'] = (int)$matches[2][1];
}
}
}
/*
else{
preg_match_all('/(.*?)\(/', $row['Type'], $real_type);
if ( strpos($row['Type'],'text') !== false ){
$r[$f]['type'] = 'text';
}
else if ( strpos($row['Type'],'blob') !== false ){
$r[$f]['type'] = 'blob';
}
else if ( strpos($row['Type'],'int(') !== false ){
$r[$f]['type'] = 'int';
}
else if ( strpos($row['Type'],'char(') !== false ){
$r[$f]['type'] = 'varchar';
}
if ( preg_match_all('/\((.*?)\)/', $row['Type'], $matches) ){
$r[$f]['maxlength'] = (int)$matches[1][0];
}
if ( !isset($r[$f]['type']) ){
$r[$f]['type'] = strpos($row['Type'], '(') ? substr($row['Type'],0,strpos($row['Type'], '(')) : $row['Type'];
}
}
*/
}
}
return $r;
}