Awight has uploaded a new change for review.

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

Change subject: [WIP] Playing with the FIFO API
......................................................................

[WIP] Playing with the FIFO API

Change-Id: I3a704669a65fcc7d55b8d6a354d4f16aee586dc7
---
M src/PHPQueue/Backend/PDO.php
1 file changed, 38 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/php-queue 
refs/changes/77/284977/1

diff --git a/src/PHPQueue/Backend/PDO.php b/src/PHPQueue/Backend/PDO.php
index 6544ca6..a3890c4 100644
--- a/src/PHPQueue/Backend/PDO.php
+++ b/src/PHPQueue/Backend/PDO.php
@@ -9,6 +9,7 @@
     extends Base
     implements IndexedFifoQueueStore, KeyValueStore
 {
+    private $auto_commit = true;
     private $connection_string;
     private $db_user;
     private $db_password;
@@ -27,8 +28,14 @@
         if (!empty($options['db_password'])) {
             $this->db_password = $options['db_password'];
         }
+        if (!empty($options['queue'])) {
+            $this->db_table = $options['queue'];
+        }
         if (!empty($options['db_table'])) {
             $this->db_table = $options['db_table'];
+        }
+        if (isset($options['auto_commit'])) {
+            $this->auto_commit = $options['auto_commit'];
         }
         if (!empty($options['pdo_options']) && 
is_array($options['pdo_options'])) {
             $this->pdo_options = array_merge($this->pdo_options, 
$options['pdo_options']);
@@ -48,6 +55,14 @@
     public function connect()
     {
         $this->connection = new \PDO($this->connection_string, $this->db_user, 
$this->db_password, $this->pdo_options);
+
+        // FIXME: I feel terrible about this.  Should we switch on a feature
+        // flag?  Can we replace with a generic createTopic?
+        $this->createTable($this->db_table);
+
+        if (!$this->auto_commit) {
+            $this->connection->beginTransaction();
+        }
     }
 
     /**
@@ -118,6 +133,9 @@
         $result = $sth->fetch(\PDO::FETCH_ASSOC);
         if ($result) {
             $this->last_job_id = $result['id'];
+            // TODO: Note that we could alternatively increment a client offset
+            // here.
+            $this->clear($result['id']);
             return json_decode($result['data'], true);
         }
         return null;
@@ -151,20 +169,35 @@
         return true;
     }
 
+    public function commit()
+    {
+        $this->getConnection()->commit();
+    }
+
     public function createTable($table_name)
     {
         if (empty($table_name)) {
             throw new BackendException('Invalid table name.');
         }
-        $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (
-                    `id` mediumint(20) NOT NULL AUTO_INCREMENT,
-                    `data` mediumtext NULL DEFAULT '',
-                    PRIMARY KEY (`id`)
-                ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;", 
$table_name);
+        switch ($this->getDriverName()) {
+        case 'mysql':
+            $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (
+                        `id` mediumint(20) NOT NULL AUTO_INCREMENT,
+                        `data` mediumtext NULL DEFAULT '',
+                        PRIMARY KEY (`id`)
+                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 
;", $table_name);
+            break;
+        default:
+            throw new BackendException('Unknown database driver: ' . $driver);
+        }
         $this->getConnection()->exec($sql);
 
         return true;
     }
+    
+    protected function getDriverName() {
+        return $this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME);
+    }
 
     public function deleteTable($table_name)
     {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3a704669a65fcc7d55b8d6a354d4f16aee586dc7
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/php-queue
Gerrit-Branch: master
Gerrit-Owner: Awight <awi...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to