jenkins-bot has submitted this change and it was merged. Change subject: Refactor common database code ......................................................................
Refactor common database code Identical calls to create parameter lists and bind values Change-Id: I22e2d3101383be42286e66b69dfd80c2a463e3c5 --- M Core/DataStores/DamagedDatabase.php M Core/DataStores/PaymentsInitialDatabase.php M Core/DataStores/PendingDatabase.php M Core/DataStores/SmashPigDatabase.php 4 files changed, 63 insertions(+), 55 deletions(-) Approvals: Awight: Looks good to me, approved jenkins-bot: Verified diff --git a/Core/DataStores/DamagedDatabase.php b/Core/DataStores/DamagedDatabase.php index fb85a16..c5498ba 100644 --- a/Core/DataStores/DamagedDatabase.php +++ b/Core/DataStores/DamagedDatabase.php @@ -58,26 +58,16 @@ $dbRecord[$fieldName] = $message[$fieldName]; } } - $fieldList = implode( ',', array_keys( $dbRecord ) ); - // Build a list of parameter names for safe db insert. Same as - // the field list, but each parameter is prefixed with a colon. - $paramList = ':' . implode( ', :', array_keys( $dbRecord ) ); + list( $fieldList, $paramList ) = self::formatInsertParameters( + $dbRecord + ); $insert = "INSERT INTO damaged ( $fieldList ) VALUES ( $paramList );"; - $prepared = self::$db->prepare( $insert ); - - foreach ( $dbRecord as $field => $value ) { - $prepared->bindValue( - ':' . $field, - $value, - PDO::PARAM_STR - ); - } - if ( $prepared->execute() ) { - return self::$db->lastInsertId(); + if ( $this->prepareAndExecute( $insert, $dbRecord ) ) { + return $this->getDatabase()->lastInsertId(); } throw new SmashPigException( 'Unable to insert into damaged db' ); } diff --git a/Core/DataStores/PaymentsInitialDatabase.php b/Core/DataStores/PaymentsInitialDatabase.php index d18701f..490b6f5 100644 --- a/Core/DataStores/PaymentsInitialDatabase.php +++ b/Core/DataStores/PaymentsInitialDatabase.php @@ -53,24 +53,13 @@ return $row; } - /** - * TODO: reuse vs PendingDatabase::storeMessage - */ public function storeMessage( $message ) { - $fieldList = implode( ',', array_keys( $message ) ); - $paramList = ':' . implode( ', :', array_keys( $message ) ); + list( $fieldList, $paramList ) = self::formatInsertParameters( + $message + ); $sql = "INSERT INTO payments_initial ( $fieldList ) VALUES ( $paramList )"; - $prepared = self::$db->prepare( $sql ); - - foreach ( $message as $field => $value ) { - $prepared->bindValue( - ':' . $field, - $value, - PDO::PARAM_STR - ); - } - $prepared->execute(); + $this->prepareAndExecute( $sql, $message ); } protected function getConfigKey() { diff --git a/Core/DataStores/PendingDatabase.php b/Core/DataStores/PendingDatabase.php index c006595..80bb327 100644 --- a/Core/DataStores/PendingDatabase.php +++ b/Core/DataStores/PendingDatabase.php @@ -3,10 +3,8 @@ use PDO; use RuntimeException; -use SmashPig\Core\Logging\Logger; use SmashPig\Core\SmashPigException; use SmashPig\Core\UtcDate; -use SmashPig\CrmLink\Messages\DonationInterfaceMessage; /** * Data store containing messages waiting to be finalized. @@ -53,23 +51,13 @@ // Dump the whole message into a text column $dbRecord['message'] = json_encode( $message ); - $fields = array_keys( $dbRecord ); if ( isset( $message['pending_id'] ) ) { - $sql = $this->getUpdateStatement( $fields ); + $sql = $this->getUpdateStatement( $dbRecord ); $dbRecord['id'] = $message['pending_id']; } else { - $sql = $this->getInsertStatement( $fields ); + $sql = $this->getInsertStatement( $dbRecord ); } - $prepared = self::$db->prepare( $sql ); - - foreach ( $dbRecord as $field => $value ) { - $prepared->bindValue( - ':' . $field, - $value, - PDO::PARAM_STR - ); - } - $prepared->execute(); + $this->prepareAndExecute( $sql, $dbRecord ); } /** @@ -174,27 +162,25 @@ } /** - * @param array $fields + * @param array $record * @return string SQL to insert a pending record, with parameters */ - protected function getInsertStatement( $fields ) { - $fieldList = implode( ',', $fields ); - - // Build a list of parameter names for safe db insert - // Same as the field list, but each parameter is prefixed with a colon - $paramList = ':' . implode( ', :', $fields ); + protected function getInsertStatement( $record ) { + list( $fieldList, $paramList ) = self::formatInsertParameters( + $record + ); $insert = "INSERT INTO pending ( $fieldList ) VALUES ( $paramList )"; return $insert; } /** - * @param array $fields + * @param array $record * @return string SQL to update a pending record, with parameters */ - protected function getUpdateStatement( $fields ) { + protected function getUpdateStatement( $record ) { $sets = array(); - foreach( $fields as $field ) { + foreach( array_keys( $record ) as $field ) { $sets[] = "$field = :$field"; } $update = 'UPDATE pending SET ' . diff --git a/Core/DataStores/SmashPigDatabase.php b/Core/DataStores/SmashPigDatabase.php index 727aa71..2b6978f 100644 --- a/Core/DataStores/SmashPigDatabase.php +++ b/Core/DataStores/SmashPigDatabase.php @@ -1,6 +1,7 @@ <?php namespace SmashPig\Core\DataStores; use PDO; +use PHPQueue\Exception\Exception; use SmashPig\Core\Context; abstract class SmashPigDatabase { @@ -46,4 +47,46 @@ * @return string Name of file (no directory) containing table creation SQL */ abstract protected function getTableScriptFile(); + + /** + * Build components of a parameterized insert statement + * + * @param array $record the associative array of values + * @return array with two string members, first a concatenated field list, + * then a concatenated list of parameters. + */ + protected static function formatInsertParameters( $record ) { + $fields = array_keys( $record ); + $fieldList = implode( ',', $fields ); + + // Build a list of parameter names for safe db insert + // Same as the field list, but each parameter is prefixed with a colon + $paramList = ':' . implode( ', :', $fields ); + return array( $fieldList, $paramList ); + } + + /** + * Prepares and executes a database command + * + * @param string $sql parameterized SQL command + * @param array $dbRecord associative array of values to bind + * @return bool true if execution succeeded + */ + protected function prepareAndExecute( $sql, $dbRecord ) { + $prepared = $this->getDatabase()->prepare( $sql ); + + foreach ( $dbRecord as $field => $value ) { + if( gettype( $value ) === 'integer' ) { + $paramType = PDO::PARAM_INT; + } else { + $paramType = PDO::PARAM_STR; + } + $prepared->bindValue( + ':' . $field, + $value, + $paramType + ); + } + return $prepared->execute(); + } } -- To view, visit https://gerrit.wikimedia.org/r/312614 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I22e2d3101383be42286e66b69dfd80c2a463e3c5 Gerrit-PatchSet: 3 Gerrit-Project: wikimedia/fundraising/SmashPig Gerrit-Branch: master Gerrit-Owner: Ejegg <eeggles...@wikimedia.org> Gerrit-Reviewer: Awight <awi...@wikimedia.org> Gerrit-Reviewer: Cdentinger <cdentin...@wikimedia.org> Gerrit-Reviewer: Ejegg <eeggles...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits