Beispiel #1
0
 /**
  * Fixes column definition in already existing table, so outgoing foreign key will be successfully set
  *
  * @param string $sql
  * @param string $column
  * @param array $refColumnDefinition
  * @return Mage_Core_Model_Resource_Setup_Query_Modifier
  */
 protected function _fixColumnDefinitionInTable($table, $column, $refColumnDefinition)
 {
     $description = $this->_adapter->fetchAll('DESCRIBE ' . $table);
     foreach ($description as $columnData) {
         $columnName = $this->_prepareIdentifier($columnData['Field']);
         if ($columnName != $column) {
             continue;
         }
         $definition = $refColumnDefinition['type'];
         if ($refColumnDefinition['unsigned']) {
             $definition .= ' UNSIGNED';
         }
         if ($columnData['Null'] == 'YES') {
             $definition .= ' NULL';
         } else {
             $definition .= ' NOT NULL';
         }
         if ($columnData['Default']) {
             $definition .= ' DEFAULT ' . $columnData['Default'];
         }
         if ($columnData['Extra']) {
             $definition .= ' ' . $columnData['Extra'];
         }
         $query = 'ALTER TABLE ' . $table . ' MODIFY COLUMN ' . $column . ' ' . $definition;
         $this->_adapter->query($query);
     }
     return $this;
 }
Beispiel #2
0
 public function updateTable($table, $conditionExpr, $valueExpr)
 {
     if (strpos($table, '/') !== false) {
         $table = $this->getTable($table);
     }
     $sql = 'update ' . $table . ' set ' . $valueExpr . ' where ' . $conditionExpr;
     $this->_conn->query($sql);
     return $this;
 }
 /**
  * Run the jobs that are in the queue
  *
  * @param mixed  $limit Limit of jobs to run
  * @return  int  Number of jobs run
  */
 public function run($limit = NULL)
 {
     // Cleanup crashed jobs
     $pids = $this->_db->fetchCol("SELECT pid FROM {$this->_table} WHERE pid IS NOT NULL GROUP BY pid");
     foreach ($pids as $pid) {
         // Old pid is no longer running, release it's reserved tasks
         if (is_numeric($pid) && !file_exists("/proc/{$pid}/status")) {
             Mage::log("A crashed job queue process was detected for pid {$pid}", Zend_Log::NOTICE, self::ERROR_LOG);
             $this->_db->update($this->_table, array('pid' => new Zend_Db_Expr('NULL')), array('pid = ?' => $pid));
         }
     }
     // Reserve all new jobs since last run
     $pid = getmypid();
     $limit = $limit ? "LIMIT {$limit}" : '';
     $batchSize = $this->_db->query("UPDATE {$this->_db->quoteIdentifier($this->_table, true)} SET pid = {$pid} WHERE pid IS NULL ORDER BY job_id {$limit}")->rowCount();
     // Run all reserved jobs
     $result = $this->_db->query($this->_db->select()->from($this->_table, '*')->where('pid = ?', $pid)->order(array('job_id')));
     while ($row = $result->fetch()) {
         $where = $this->_db->quoteInto('job_id = ?', $row['job_id']);
         $data = substr($row['data'], 0, 1) == '{' ? json_decode($row['data'], TRUE) : ($data = unserialize($row['data']));
         // Check retries
         if ($row['retries'] >= $row['max_retries']) {
             $this->_db->delete($this->_table, $where);
             Mage::log("{$row['pid']}: Mage::getSingleton({$row['class']})->{$row['method']}(" . json_encode($data) . ")\n" . $row['error_log'], Zend_Log::ERR, self::ERROR_LOG);
             continue;
         }
         // Run job!
         try {
             $model = Mage::getSingleton($row['class']);
             $method = $row['method'];
             $model->{$method}(new Varien_Object($data));
             $this->_db->delete($this->_table, $where);
             Mage::log("{$row['pid']}: Mage::getSingleton({$row['class']})->{$row['method']}(" . json_encode($data) . ")", Zend_Log::INFO, self::SUCCESS_LOG);
         } catch (Exception $e) {
             // Increment retries and log error information
             $error = date('c') . " ERROR: " . get_class($e) . ": '{$e->getMessage()}' in {$e->getFile()}:{$e->getLine()}\n" . "Stack trace:\n" . $e->getTraceAsString();
             $bind = array('pid' => new Zend_Db_Expr('NULL'), 'retries' => new Zend_Db_Expr('retries + 1'), 'error_log' => new Zend_Db_Expr('SUBSTR(CONCAT(error_log,' . $this->_db->quote($error) . ',"\\n\\n"),1,20000)'));
             $this->_db->update($this->_table, $bind, $where);
         }
     }
     return $batchSize;
 }
Beispiel #4
0
 /**
  * Retrive table partical data SQL insert
  *
  * @param string $tableName
  * @param int $count
  * @param int $offset
  * @return string
  */
 public function getTableDataSql($tableName, $count, $offset = 0)
 {
     $sql = null;
     $quotedTableName = $this->_read->quoteIdentifier($tableName);
     $select = $this->_read->select()->from($tableName)->limit($count, $offset);
     $query = $this->_read->query($select);
     while ($row = $query->fetch()) {
         if (is_null($sql)) {
             $sql = 'INSERT INTO ' . $quotedTableName . ' VALUES ';
         } else {
             $sql .= ',';
         }
         $sql .= $this->_quoteRow($tableName, $row);
     }
     if (!is_null($sql)) {
         $sql .= ';' . "\n";
     }
     return $sql;
 }
Beispiel #5
0
 /**
  * Run sql code
  *
  * @param $command
  * @return Mage_Backup_Model_Resource_Db
  */
 public function runCommand($command)
 {
     $this->_write->query($command);
     return $this;
 }
 /**
  * Special handling for PDO query().
  * All bind parameter names must begin with ':'.
  *
  * @param string|Zend_Db_Select $sql  The SQL statement with placeholders.
  * @param mixed                 $bind An array of data or data itself to bind to the placeholders.
  *
  * @return Varien_Db_Statement_Pdo_Mysql
  * @throws Zend_Db_Adapter_Exception To re-throw PDOException.
  */
 public function query($sql, $bind = array())
 {
     if (false === $this->enableDbQuoteOptimization()) {
         return parent::query($sql, $bind);
     }
     if (is_array($bind) && count($bind) > 0) {
         foreach ($bind as $k => $v) {
             $bind[$k] = $this->castToNumeric($v);
         }
     }
     return parent::query($sql, $bind);
 }
Beispiel #7
0
 private function _saveSettings($settings)
 {
     foreach ($settings as $key => $value) {
         $this->_resource->query("insert into {$this->_table} (`scope`, `scope_id`, `key`, `{$value['type']}_value`, `type`)\n                    values (?,?,?,?,?)", array($this->_scope, $this->_scopeId, $key, $value['value'], $value['type']));
     }
 }
 private function _deleteSettings($websiteId, $storeId)
 {
     $this->_connection->query("DELETE FROM {$this->_table} WHERE `website_id`={$websiteId} and `store_id`={$storeId}");
     return $this;
 }