Ejegg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/312938

Change subject: Error-check the rest of the db calls
......................................................................

Error-check the rest of the db calls

Make prepareAndExecute return the executed statement for retrieval

Change-Id: I818d42b43e9b782bf1b74c0a673a13aea75df453
---
M Core/DataStores/DamagedDatabase.php
M Core/DataStores/PaymentsInitialDatabase.php
M Core/DataStores/PendingDatabase.php
M Core/DataStores/SmashPigDatabase.php
4 files changed, 56 insertions(+), 54 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/SmashPig 
refs/changes/38/312938/1

diff --git a/Core/DataStores/DamagedDatabase.php 
b/Core/DataStores/DamagedDatabase.php
index 6b806c0..09edde3 100644
--- a/Core/DataStores/DamagedDatabase.php
+++ b/Core/DataStores/DamagedDatabase.php
@@ -74,20 +74,18 @@
         * @return array|null Records with retry_date prior to now
         */
        public function fetchRetryMessages( $limit ) {
-               $prepared = self::$db->prepare(
-                       '
-                       SELECT * FROM damaged
+               $sql = 'SELECT * FROM damaged
                        WHERE retry_date < :now
                        ORDER BY retry_date ASC
-                       LIMIT ' . $limit
+                       LIMIT ' . $limit;
+
+               $params = array(
+                       'now' => UtcDate::getUtcDatabaseString()
                );
-               $prepared->bindValue(
-                       ':now',
-                       UtcDate::getUtcDatabaseString(),
-                       PDO::PARAM_STR
-               );
-               $prepared->execute();
-               $rows = $prepared->fetchAll( PDO::FETCH_ASSOC );
+
+               $executed = $this->prepareAndExecute( $sql, $params);
+
+               $rows = $executed->fetchAll( PDO::FETCH_ASSOC );
                return array_map(
                        array( $this, 'messageFromDbRow' ),
                        $rows
@@ -100,13 +98,12 @@
         * @param array $message
         */
        public function deleteMessage( $message ) {
-               $prepared = self::$db->prepare(
-                       '
-                       DELETE FROM damaged
-                       WHERE id = :id'
+               $sql = 'DELETE FROM damaged
+                       WHERE id = :id';
+               $params = array(
+                       'id' => $message['damaged_id']
                );
-               $prepared->bindValue( ':id', $message['damaged_id'], 
PDO::PARAM_STR );
-               $prepared->execute();
+               $this->prepareAndExecute( $sql, $params );
        }
 
        /**
@@ -120,16 +117,13 @@
                if ( $queue ) {
                        $sql .= ' AND original_queue = :queue';
                }
-               $prepared = self::$db->prepare( $sql );
-               $prepared->bindValue(
-                       ':date',
-                       UtcDate::getUtcDatabaseString( $originalDate ),
-                       PDO::PARAM_STR
+               $params = array(
+                       'date' => UtcDate::getUtcDatabaseString( $originalDate 
),
                );
                if ( $queue ) {
-                       $prepared->bindValue( ':queue', $queue, PDO::PARAM_STR 
);
+                       $params['queue'] = $queue;
                }
-               $prepared->execute();
+               $this->prepareAndExecute( $sql, $params );
        }
 
        /**
diff --git a/Core/DataStores/PaymentsInitialDatabase.php 
b/Core/DataStores/PaymentsInitialDatabase.php
index 490b6f5..5411c58 100644
--- a/Core/DataStores/PaymentsInitialDatabase.php
+++ b/Core/DataStores/PaymentsInitialDatabase.php
@@ -38,15 +38,16 @@
         * @return array|null Record related to a transaction, or null if 
nothing matches
         */
        public function fetchMessageByGatewayOrderId( $gatewayName, $orderId ) {
-               $prepared = self::$db->prepare( '
-                       select * from payments_initial
+               $sql = 'select * from payments_initial
                        where gateway = :gateway
                                and order_id = :order_id
-                       limit 1' );
-               $prepared->bindValue( ':gateway', $gatewayName, PDO::PARAM_STR 
);
-               $prepared->bindValue( ':order_id', $orderId, PDO::PARAM_STR );
-               $prepared->execute();
-               $row = $prepared->fetch( PDO::FETCH_ASSOC );
+                       limit 1';
+               $params = array(
+                       'gateway' => $gatewayName,
+                       'order_id' => $orderId,
+               );
+               $executed = $this->prepareAndExecute( $sql, $params );
+               $row = $executed->fetch( PDO::FETCH_ASSOC );
                if ( !$row ) {
                        return null;
                }
diff --git a/Core/DataStores/PendingDatabase.php 
b/Core/DataStores/PendingDatabase.php
index 6abb227..5dedce3 100644
--- a/Core/DataStores/PendingDatabase.php
+++ b/Core/DataStores/PendingDatabase.php
@@ -67,16 +67,17 @@
         * @return array|null Record related to a transaction, or null if 
nothing matches
         */
        public function fetchMessageByGatewayOrderId( $gatewayName, $orderId ) {
-               $prepared = self::$db->prepare( '
-                       select * from pending
+               $sql = 'select * from pending
                        where gateway = :gateway
                                and order_id = :order_id
-                       limit 1' );
+                       limit 1';
 
-               $prepared->bindValue( ':gateway', $gatewayName, PDO::PARAM_STR 
);
-               $prepared->bindValue( ':order_id', $orderId, PDO::PARAM_STR );
-               $prepared->execute();
-               $row = $prepared->fetch( PDO::FETCH_ASSOC );
+               $params = array(
+                       'gateway' => $gatewayName,
+                       'order_id' => $orderId,
+               );
+               $executed = $this->prepareAndExecute( $sql, $params );
+               $row = $executed->fetch( PDO::FETCH_ASSOC );
                if ( !$row ) {
                        return null;
                }
@@ -90,14 +91,14 @@
         * @return array|null Message or null if nothing is found.
         */
        public function fetchMessageByGatewayOldest( $gatewayName ) {
-               $prepared = self::$db->prepare( '
-                       select * from pending
+               $sql = 'select * from pending
                        where gateway = :gateway
                        order by date asc
-                       limit 1' );
-               $prepared->bindValue( ':gateway', $gatewayName, PDO::PARAM_STR 
);
-               $prepared->execute();
-               $row = $prepared->fetch( PDO::FETCH_ASSOC );
+                       limit 1';
+
+               $params = array( 'gateway' => $gatewayName );
+               $executed = $this->prepareAndExecute( $sql, $params );
+               $row = $executed->fetch( PDO::FETCH_ASSOC );
                if ( !$row ) {
                        return null;
                }
@@ -112,14 +113,14 @@
         * @return array|null Messages or null if nothing is found.
         */
        public function fetchMessagesByGatewayNewest( $gatewayName, $limit = 1 
) {
-               $prepared = self::$db->prepare( "
+               $sql = "
                        select * from pending
                        where gateway = :gateway
                        order by date desc
-                       limit $limit" );
-               $prepared->bindValue( ':gateway', $gatewayName, PDO::PARAM_STR 
);
-               $prepared->execute();
-               $rows = $prepared->fetchAll( PDO::FETCH_ASSOC );
+                       limit $limit";
+               $params = array( 'gateway' => $gatewayName );
+               $executed = $this->prepareAndExecute( $sql, $params );
+               $rows = $executed->fetchAll( PDO::FETCH_ASSOC );
                if ( !$rows ) {
                        return null;
                }
@@ -142,13 +143,16 @@
                        throw new RuntimeException( 'Message doesn\'t have an 
order_id!' );
                }
 
-               $prepared = self::$db->prepare( '
+               $sql = '
                        delete from pending
                        where gateway = :gateway
-                               and order_id = :order_id' );
-               $prepared->bindValue( ':gateway', $message['gateway'], 
PDO::PARAM_STR );
-               $prepared->bindValue( ':order_id', $message['order_id'], 
PDO::PARAM_STR );
-               $prepared->execute();
+                               and order_id = :order_id';
+               $params = array(
+                       'gateway' => $message['gateway'],
+                       'order_id' => $message['order_id'],
+               );
+
+               $this->prepareAndExecute( $sql, $params );
        }
 
        /**
diff --git a/Core/DataStores/SmashPigDatabase.php 
b/Core/DataStores/SmashPigDatabase.php
index 505de95..0ce040f 100644
--- a/Core/DataStores/SmashPigDatabase.php
+++ b/Core/DataStores/SmashPigDatabase.php
@@ -1,6 +1,7 @@
 <?php namespace SmashPig\Core\DataStores;
 
 use PDO;
+use PDOStatement;
 use SmashPig\Core\Context;
 
 abstract class SmashPigDatabase {
@@ -69,6 +70,7 @@
         *
         * @param string $sql parameterized SQL command
         * @param array $dbRecord associative array of values to bind
+        * @return PDOStatement the executed statement for any fetching
         * @throws DataStoreException
         */
        protected function prepareAndExecute( $sql, $dbRecord ) {
@@ -91,5 +93,6 @@
                        $info = print_r( $this->getDatabase()->errorInfo() );
                        throw new DataStoreException( "Failed to execute $sql: 
$info" );
                }
+               return $prepared;
        }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/312938
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I818d42b43e9b782bf1b74c0a673a13aea75df453
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

Reply via email to