1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 17:24:46 +02:00

Add new $database->getColumns('table'); method in WireDatabasePDO

This commit is contained in:
Ryan Cramer
2021-06-18 11:28:30 -04:00
parent ca69df49de
commit d38931cb26

View File

@@ -984,6 +984,43 @@ class WireDatabasePDO extends Wire implements WireDatabase {
return $tables;
}
/**
* Get all columns from given table
*
* By default returns array of column names. If verbose option is true then it returns
* an array of arrays, each having 'name', 'type', 'null', 'default', and 'extra' keys,
* indicating the column name, column type, whether it can be null, what its default value
* is, and any extra information, such as whether it is auto_increment. The verbose option
* also makes the return value indexed by column name (associative array).
*
* @param string $table
* @param bool $verbose Include array of verbose information for each? (default=false)
* @return array
* @since 3.0.180
*
*/
public function getColumns($table, $verbose = false) {
$columns[] = array();
$table = $this->escapeTable($table);
$query = $this->query("SHOW COLUMNS FROM $table");
while($col = $query->fetch(\PDO::FETCH_ASSOC)) {
$name = $col['Field'];
if($verbose) {
$columns[$name] = array(
'name' => $name,
'type' => $col['Type'],
'null' => (strtoupper($col['Null']) === 'YES' ? true : false),
'default' => $col['Default'],
'extra' => $col['Extra'],
);
} else {
$columns[] = $name;
}
}
$query->closeCursor();
return $columns;
}
/**
* Does the given table exist in this database?
*