diff options
| author | Damien Regad <dregad@mantisbt.org> | 2022-03-20 11:12:00 +0100 |
|---|---|---|
| committer | Damien Regad <dregad@mantisbt.org> | 2022-03-20 11:27:30 +0100 |
| commit | 184ec5178b1dc02601f4c496d6de98179d423c34 (patch) | |
| tree | 9e51a70430a9014796ae43a089300bed98b27391 | |
| parent | 21ad50724b8bf909da0a4612e81e9589fcc26e55 (diff) | |
| download | adodb-184ec5178b1dc02601f4c496d6de98179d423c34.tar.gz adodb-184ec5178b1dc02601f4c496d6de98179d423c34.tar.bz2 adodb-184ec5178b1dc02601f4c496d6de98179d423c34.zip | |
Refactor ADODB_mysqli::execute()
- New private method getBindParamWithType() to remove code duplication
when generating the bind param type string
- Remove unnecessary variables
| -rw-r--r-- | drivers/adodb-mysqli.inc.php | 102 |
1 files changed, 47 insertions, 55 deletions
diff --git a/drivers/adodb-mysqli.inc.php b/drivers/adodb-mysqli.inc.php index 466d5a66..83840b36 100644 --- a/drivers/adodb-mysqli.inc.php +++ b/drivers/adodb-mysqli.inc.php @@ -1114,74 +1114,36 @@ class ADODB_mysqli extends ADOConnection { } if (!is_array($sql)) { - $typeString = ''; - $bulkTypeArray = array(); - $typeArray = array(''); // placeholder for type list - // Check if we are bulkbinding. If so, $inputarr is a 2d array, // and we make a gross assumption that all rows have the same number - // of columns, we will use the elements of the first row. - if (is_array($inputarr[0]) && $this->bulkBind) { - $bulkBindArray = $inputarr; - foreach ($bulkBindArray as $inputarr) { + // of columns of the same type, and use the elements of the first row + // to determine the MySQL bind param types. + if (is_array($inputarr[0])) { + if (!$this->bulkBind) { + $this->outp_throw( + "2D Array of values sent to execute and 'ADOdb_mysqli::bulkBind' not set", + 'Execute' + ); + return false; + } + + $bulkTypeArray = []; + foreach ($inputarr as $v) { if (is_string($this->bulkBind)) { - $typeArray = array_merge((array)$this->bulkBind, $inputarr); + $typeArray = array_merge((array)$this->bulkBind, $v); } else { - $typeString = ''; - $typeArray = array(''); - - foreach ($inputarr as $v) { - $typeArray[] = $v; - if (is_integer($v) || is_bool($v)) { - $typeString .= 'i'; - } elseif (is_float($v)) { - $typeString .= 'd'; - } elseif (is_object($v)) { - // Assume a blob - $typeString .= 'b'; - } else { - $typeString .= 's'; - } - } - - // Place the field type list at the front of the parameter array. - // This is the mysql specific format - $typeArray[0] = $typeString; + $typeArray = $this->getBindParamWithType($v); } - $bulkTypeArray[] = $typeArray; - } $this->bulkBind = false; $ret = $this->_execute($sql, $bulkTypeArray); - } elseif (is_array($inputarr[0]) && !$this->bulkBind) { - $this->outp_throw( - "2D Array of values sent to execute and 'ADOdb_mysqli::bulkBind' not set", - 'Execute' - ); - return false; } else { - foreach ($inputarr as $v) { - $typeArray[] = $v; - if (is_integer($v) || is_bool($v)) { - $typeString .= 'i'; - } elseif (is_float($v)) { - $typeString .= 'd'; - } elseif (is_object($v)) { - // Assume a blob - $typeString .= 'b'; - } else { - $typeString .= 's'; - } - } - - // Place the field type list at the front of the parameter array. - // This is the mysql specific format - $typeArray[0] = $typeString; - + $typeArray = $this->getBindParamWithType($inputarr); $ret = $this->_execute($sql, $typeArray); } + // @TODO unnecessary or should return false ? see https://github.com/ADOdb/ADOdb/pull/655/files#r830582165 if (!$ret) { return $ret; } @@ -1192,6 +1154,36 @@ class ADODB_mysqli extends ADOConnection { } /** + * Inserts the bind param type string at the front of the parameter array. + * + * @see https://www.php.net/manual/en/mysqli-stmt.bind-param.php + * + * @param array $inputArr + * @return array + */ + private function getBindParamWithType($inputArr): array + { + $typeString = ''; + foreach ($inputArr as $v) { + if (is_integer($v) || is_bool($v)) { + $typeString .= 'i'; + } elseif (is_float($v)) { + $typeString .= 'd'; + } elseif (is_object($v)) { + // Assume a blob + $typeString .= 'b'; + } else { + $typeString .= 's'; + } + } + + // Place the field type list at the front of the parameter array. + // This is the mysql specific format + array_unshift($inputArr, $typeString); + return $inputArr; + } + + /** * Return the query id. * * @param string|array $sql |
