From 636891043442853a9c953688f569a85b50441890 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 23 Jul 2021 09:52:46 -0400 Subject: [PATCH] Modifications to $database->getPrimaryKey() method (I thought these were in the previous commit that added this method but looks like they weren't) --- wire/core/Sanitizer.php | 2 ++ wire/core/WireDatabasePDO.php | 28 +++++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/wire/core/Sanitizer.php b/wire/core/Sanitizer.php index 4b337ba9..717f4880 100644 --- a/wire/core/Sanitizer.php +++ b/wire/core/Sanitizer.php @@ -261,6 +261,8 @@ class Sanitizer extends Wire { 'intSigned' => 'i', 'intUnsigned' => 'i', 'kebabCase' => 's', + 'line' => 's', + 'lines' => 's', 'markupToLine' => 's', 'markupToText' => 's', 'max' => 'fi', diff --git a/wire/core/WireDatabasePDO.php b/wire/core/WireDatabasePDO.php index 3576ef18..01795ce2 100644 --- a/wire/core/WireDatabasePDO.php +++ b/wire/core/WireDatabasePDO.php @@ -994,7 +994,7 @@ class WireDatabasePDO extends Wire implements WireDatabase { * also makes the return value indexed by column name (associative array). * * @param string $table Table name or or `table.column` to get for specific column (when combined with verbose=true) - * @param bool|string $verbose Include array of verbose information for each? (default=false) + * @param bool|int|string $verbose Include array of verbose information for each? (default=false) * - Omit or false (bool) to just get column names. * - True (bool) or 1 (int) to get a verbose array of information for each column, indexed by column name. * - 2 (int) to get raw MySQL column information, indexed by column name (added 3.0.182). @@ -1040,7 +1040,7 @@ class WireDatabasePDO extends Wire implements WireDatabase { * index `name`, `type` and `columns` (array) for each index. * * @param string $table Name of table to get indexes for or `table.index` (usually combined with verbose option). - * @param bool $verbose Include array of verbose information for each? (default=false) + * @param bool|int|string $verbose Include array of verbose information for each? (default=false) * - Omit or false (bool) to just get index names. * - True (bool) or 1 (int) to get a verbose array of information for each index, indexed by index name. * - 2 (int) to get regular PHP array of raw MySQL index information. @@ -1079,23 +1079,33 @@ class WireDatabasePDO extends Wire implements WireDatabase { $query->closeCursor(); if($getIndex) return isset($indexes[$getIndex]) ? $indexes[$getIndex] : array(); return $indexes; - } /** - * Get array of info for given table’s primary key/index + * Get column(s) or info for given table’s primary key/index + * + * By default it returns a string with the column name compromising the primary key, i.e. `col1`. + * If the primary key is multiple columns then it returns a CSV string, like `col1,col2,col3`. + * + * If you specify boolean `true` for the verbose option then it returns an simplified array of + * information about the primary key. If you specify integer `2` then it returns an array of + * raw MySQL SHOW INDEX information. * * @param string $table - * @param bool $getRaw Get raw MySQL array of info rather than simplied array? (default=false) - * @return array + * @param bool|int $verbose Get array of info rather than column(s) string? (default=false) + * @return string|array * @since 3.0.182 * */ - public function getPrimaryKey($table, $getRaw = false) { - if($getRaw) { + public function getPrimaryKey($table, $verbose = false) { + if($verbose === 2) { return $this->getIndexes("$table.PRIMARY", 2); - } else { + } else if($verbose) { return $this->getIndexes($table, 'PRIMARY'); + } else { + $a = $this->getIndexes($table, 'PRIMARY'); + if(empty($a) || empty($a['columns'])) return ''; + return implode(',', $a['columns']); } }