/** */ public function write($id, $session_data) { if (!$this->_db->isActive()) { $this->_db->reconnect(); $this->_db->beginDbTransaction(); } /* Check if session exists. */ try { $exists = $this->_db->selectValue(sprintf('SELECT 1 FROM %s WHERE session_id = ?', $this->_params['table']), array($id)); } catch (Horde_Db_Exception $e) { return false; } /* Update or insert session data. */ $values = array('session_data' => new Horde_Db_Value_Binary($session_data), 'session_lastmodified' => time()); try { if ($exists) { $this->_db->updateBlob($this->_params['table'], $values, array('session_id = ?', array($id))); } else { $this->_db->insertBlob($this->_params['table'], array_merge(array('session_id' => $id), $values)); } $this->_db->commitDbTransaction(); } catch (Horde_Db_Exception $e) { try { $this->_db->rollbackDbTransaction(); } catch (Horde_Db_Exception $e) { } return false; } return true; }
/** */ public function setMetaData($mailbox, $data) { if (!($uid = $this->_getUid($mailbox))) { $uid = $this->_createUid($mailbox); } $query = sprintf('SELECT field FROM %s where messageid = ?', self::MD_TABLE); $values = array($uid); try { $fields = $this->_db->selectValues($query, $values); } catch (Horde_Db_Exception $e) { return; } foreach ($data as $key => $val) { $val = new Horde_Db_Value_Binary($key == 'uidvalid' ? $val : serialize($val)); if (in_array($key, $fields)) { /* Update */ try { $this->_db->updateBlob(self::MD_TABLE, array('data' => $val), array('field = ? AND messageid = ?', array($key, $uid))); } catch (Horde_Db_Exception $e) { } } else { /* Insert */ try { $this->_db->insertBlob(self::MD_TABLE, array('data' => $val, 'field' => $key, 'messageid' => $uid)); } catch (Horde_Db_Exception $e) { } } } }
/** * Updates rows including BLOBs into a table. * * @since Horde_Db 2.2.0 * * @param string $table The table name. * @param array $fields A hash of column names and values. BLOB columns * must be provided as Horde_Db_Value_Binary objects. * @param string $where A WHERE clause. * * @throws Horde_Db_Exception */ public function updateBlob($table, $fields, $where = '') { $result = $this->_write->updateBlob($table, $fields, $where); $this->_lastQuery = $this->_write->getLastQuery(); // Once doing writes, keep using the write backend even for reads // at least during the same request, to help against stale data. $this->_read = $this->_write; return $result; }
/** * Updates an alarm hash in the backend. * * @param array $alarm An alarm hash. * @param boolean $keepsnooze Whether to keep the snooze value unchanged. * * @throws Horde_Alarm_Exception */ protected function _update(array $alarm, $keepsnooze = false) { $where = array(sprintf('alarm_id = ? AND %s', isset($alarm['user']) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)'), array($alarm['id'], isset($alarm['user']) ? $alarm['user'] : '')); $values = array('alarm_start' => $alarm['start']->setTimezone('UTC')->format(Horde_Date::DATE_DEFAULT), 'alarm_end' => empty($alarm['end']) ? null : $alarm['end']->setTimezone('UTC')->format(Horde_Date::DATE_DEFAULT), 'alarm_methods' => serialize($alarm['methods']), 'alarm_params' => new Horde_Db_Value_Text(base64_encode(serialize($alarm['params']))), 'alarm_title' => $this->_toDriver($alarm['title']), 'alarm_instanceid' => empty($alarm['instanceid']) ? null : $alarm['instanceid'], 'alarm_text' => empty($alarm['text']) ? null : new Horde_Db_Value_Text($this->_toDriver($alarm['text']))); if (!$keepsnooze) { $values['alarm_snooze'] = null; $values['alarm_dismissed'] = 0; } try { $this->_db->updateBlob($this->_params['table'], $values, $where); } catch (Horde_Db_Exception $e) { throw new Horde_Alarm_Exception(Horde_Alarm_Translation::t("Server error when querying database.")); } }
/** * Saves the specified object in the SQL database. * * @param Turba_Object $object The object to save. * * @return string The object id, possibly updated. * @throws Turba_Exception */ protected function _save(Turba_Object $object) { list($object_key, $object_id) = each($this->toDriverKeys(array('__key' => $object->getValue('__key')))); $attributes = $this->toDriverKeys($object->getAttributes()); $blob_fields = $this->toDriverKeys($this->getBlobs()); $date_fields = $this->toDriverKeys($this->getDateFields()); unset($attributes[$object_key]); list($fields, $values) = $this->_prepareWrite($attributes, $blob_fields, $date_fields); try { $this->_db->updateBlob($this->_params['table'], array_combine($fields, $values), array($object_key . ' = ?', array($object_id))); } catch (Horde_Db_Exception $e) { throw new Turba_Exception(_("Server error when saving data.")); } return $object_id; }
/** */ public function store($scope_ob) { if (!$this->_db->isActive()) { $this->_db->reconnect(); } $charset = $this->_db->getOption('charset'); // For each preference, check for an existing table row and // update it if it's there, or create a new one if it's not. foreach ($scope_ob->getDirty() as $name) { $value = $scope_ob->get($name); if (is_null($value)) { $this->remove($scope_ob->scope, $name); } else { $values = array($this->_params['user'], $name, $scope_ob->scope); // Does a row already exist for this preference? $query = 'SELECT 1 FROM ' . $this->_params['table'] . ' WHERE pref_uid = ? AND pref_name = ?' . ' AND pref_scope = ?'; try { $check = $this->_db->selectValue($query, $values); } catch (Horde_Db_Exception $e) { throw new Horde_Prefs_Exception($e); } /* Driver has no support for storing locked status. */ $value = Horde_String::convertCharset($value, 'UTF-8', $charset); $value = new Horde_Db_Value_Binary($value); if (empty($check)) { // Insert a new row. $values = array('pref_uid' => $this->_params['user'], 'pref_scope' => $scope_ob->scope, 'pref_name' => $name, 'pref_value' => $value); try { $this->_db->insertBlob($this->_params['table'], $values); } catch (Horde_Db_Exception $e) { throw new Horde_Prefs_Exception($e); } } else { // Update the existing row. try { $this->_db->updateBlob($this->_params['table'], array('pref_value' => $value), array('pref_uid = ? AND pref_name = ? AND pref_scope = ?', array($this->_params['user'], $name, $scope_ob->scope))); } catch (Horde_Db_Exception $e) { throw new Horde_Prefs_Exception($e); } } } } }