From d38931cb26ae6fc979e025cf49ed3c4ae52db788 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 18 Jun 2021 11:28:30 -0400 Subject: [PATCH] Add new $database->getColumns('table'); method in WireDatabasePDO --- wire/core/WireDatabasePDO.php | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/wire/core/WireDatabasePDO.php b/wire/core/WireDatabasePDO.php index 62dd2f16..5ef15217 100644 --- a/wire/core/WireDatabasePDO.php +++ b/wire/core/WireDatabasePDO.php @@ -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 it’s 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? *