Exemplo n.º 1
0
 public static function queryFromStatement(Zotero_DB_Statement $stmt, $params = false)
 {
     try {
         // Execute statement if not coming from self::query()
         if ($params) {
             self::logQuery($stmt->sql, $params, $stmt->shardID);
             // If this is a write query, make sure shard is writeable
             if ($stmt->isWriteQuery && $stmt->shardID && !Zotero_Shards::shardIsWriteable($stmt->shardID)) {
                 throw new Exception("Cannot write to read-only shard {$stmt->shardID}", Z_ERROR_SHARD_READ_ONLY);
             }
             $instance = self::getInstance();
             $instance->checkShardTransaction($stmt->shardID);
             if (is_scalar($params)) {
                 $params = array($params);
             }
             $stmt->execute($params);
         }
         $stmt->setFetchMode(Zend_Db::FETCH_ASSOC);
         $mystmt = $stmt->getDriverStatement();
         // Not a read statement
         if (!$mystmt->field_count) {
             // Determine the type of query using first word
             preg_match('/^[^\\s\\(]*/', $stmt->sql, $matches);
             $queryMethod = strtolower($matches[0]);
             if ($queryMethod == "update" || $queryMethod == "delete") {
                 return $stmt->rowCount();
             } else {
                 if ($queryMethod == "insert") {
                     $insertID = (int) $stmt->link->lastInsertID();
                     if ($insertID) {
                         return $insertID;
                     }
                     $affectedRows = $stmt->rowCount();
                     if (!$affectedRows) {
                         return false;
                     }
                     return $affectedRows;
                 }
             }
             return true;
         }
         // Cast integers
         $intFieldNames = self::getIntegerColumns($mystmt);
         $results = array();
         while ($row = $stmt->fetch()) {
             if ($intFieldNames) {
                 foreach ($intFieldNames as $name) {
                     if (is_null($row[$name])) {
                         $row[$name] = null;
                     } else {
                         if (strlen($row[$name]) < 10) {
                             $row[$name] = (int) $row[$name];
                         }
                     }
                 }
             }
             $results[] = $row;
         }
     } catch (Exception $e) {
         self::error($e, $stmt->sql, $params, $stmt->shardID);
     }
     return $results;
 }