Ejegg has uploaded a new change for review. https://gerrit.wikimedia.org/r/312614
Change subject: WIP Refactor common database code ...................................................................... WIP Refactor common database code Ugh, why doesnt this work? 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, 49 insertions(+), 46 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/SmashPig refs/changes/14/312614/1 diff --git a/Core/DataStores/DamagedDatabase.php b/Core/DataStores/DamagedDatabase.php index fb85a16..bbbef86 100644 --- a/Core/DataStores/DamagedDatabase.php +++ b/Core/DataStores/DamagedDatabase.php @@ -58,25 +58,15 @@ $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() ) { + if ( self::prepareAndExecute( $insert, $dbRecord ) ) { return self::$db->lastInsertId(); } throw new SmashPigException( 'Unable to insert into damaged db' ); diff --git a/Core/DataStores/PaymentsInitialDatabase.php b/Core/DataStores/PaymentsInitialDatabase.php index d18701f..8bf3357 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(); + self::prepareAndExecute( $sql, $message ); } protected function getConfigKey() { diff --git a/Core/DataStores/PendingDatabase.php b/Core/DataStores/PendingDatabase.php index c006595..b07ea07 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. @@ -60,16 +58,7 @@ } else { $sql = $this->getInsertStatement( $fields ); } - $prepared = self::$db->prepare( $sql ); - - foreach ( $dbRecord as $field => $value ) { - $prepared->bindValue( - ':' . $field, - $value, - PDO::PARAM_STR - ); - } - $prepared->execute(); + self::prepareAndExecute( $sql, $dbRecord ); } /** @@ -178,11 +167,9 @@ * @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 ); + list( $fieldList, $paramList ) = self::formatInsertParameters( + $fields + ); $insert = "INSERT INTO pending ( $fieldList ) VALUES ( $paramList )"; return $insert; diff --git a/Core/DataStores/SmashPigDatabase.php b/Core/DataStores/SmashPigDatabase.php index 727aa71..ab3a685 100644 --- a/Core/DataStores/SmashPigDatabase.php +++ b/Core/DataStores/SmashPigDatabase.php @@ -46,4 +46,41 @@ * @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 static function prepareAndExecute( $sql, $dbRecord ) { + $prepared = static::$db->prepare( $sql ); + + foreach ( $dbRecord as $field => $value ) { + $prepared->bindValue( + ':' . $field, + $value, + PDO::PARAM_STR + ); + } + return $prepared->execute(); + } } -- To view, visit https://gerrit.wikimedia.org/r/312614 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I22e2d3101383be42286e66b69dfd80c2a463e3c5 Gerrit-PatchSet: 1 Gerrit-Project: wikimedia/fundraising/SmashPig Gerrit-Branch: master Gerrit-Owner: Ejegg <eeggles...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits