/** * add new server-to-server share * * @param string $remote * @param string $token * @param string $password * @param string $name * @param string $owner * @param boolean $accepted * @param string $user * @param int $remoteId * @return Mount|null */ public function addShare($remote, $token, $password, $name, $owner, $accepted = false, $user = null, $remoteId = -1) { $user = $user ? $user : $this->uid; $accepted = $accepted ? 1 : 0; $name = Filesystem::normalizePath('/' . $name); if (!$accepted) { // To avoid conflicts with the mount point generation later, // we only use a temporary mount point name here. The real // mount point name will be generated when accepting the share, // using the original share item name. $tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}'; $mountPoint = $tmpMountPointName; $hash = md5($tmpMountPointName); $data = ['remote' => $remote, 'share_token' => $token, 'password' => $password, 'name' => $name, 'owner' => $owner, 'user' => $user, 'mountpoint' => $mountPoint, 'mountpoint_hash' => $hash, 'accepted' => $accepted, 'remote_id' => $remoteId]; $i = 1; while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) { // The external share already exists for the user $data['mountpoint'] = $tmpMountPointName . '-' . $i; $data['mountpoint_hash'] = md5($data['mountpoint']); $i++; } return null; } $mountPoint = Files::buildNotExistingFileName('/', $name); $mountPoint = Filesystem::normalizePath('/' . $mountPoint); $hash = md5($mountPoint); $query = $this->connection->prepare(' INSERT INTO `*PREFIX*share_external` (`remote`, `share_token`, `password`, `name`, `owner`, `user`, `mountpoint`, `mountpoint_hash`, `accepted`, `remote_id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) '); $query->execute(array($remote, $token, $password, $name, $owner, $user, $mountPoint, $hash, $accepted, $remoteId)); $options = array('remote' => $remote, 'token' => $token, 'password' => $password, 'mountpoint' => $mountPoint, 'owner' => $owner); return $this->mountShare($options); }
/** * Try to create a new group * @param string $gid The name of the group to create * @return bool * * Tries to create a new group. If the group name already exists, false will * be returned. */ public function createGroup($gid) { $this->fixDI(); // Add group $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', ['gid' => $gid]); // Add to cache $this->groupCache[$gid] = $gid; return $result === 1; }
private function createCacheEntry($internalPath, $storageId) { $this->connection->insertIfNotExist('*PREFIX*filecache', ['storage' => $storageId, 'path' => $internalPath, 'path_hash' => md5($internalPath), 'parent' => -1, 'name' => basename($internalPath), 'mimetype' => 0, 'mimepart' => 0, 'size' => 0, 'storage_mtime' => 0, 'encrypted' => 0, 'unencrypted_size' => 0, 'etag' => '', 'permissions' => 31], ['storage', 'path_hash']); $id = (int) $this->connection->lastInsertId('*PREFIX*filecache'); $this->fileIds[] = $id; return $id; }
/** * Sets a value. If the key did not exist before it will be created. * * @param string $app app * @param string $key key * @param string|float|int $value value * @return bool True if the value was inserted or updated, false if the value was the same */ public function setValue($app, $key, $value) { if (!$this->hasKey($app, $key)) { $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', ['appid' => $app, 'configkey' => $key, 'configvalue' => $value], ['appid', 'configkey']); if ($inserted) { if (!isset($this->cache[$app])) { $this->cache[$app] = []; } $this->cache[$app][$key] = $value; return true; } } $sql = $this->conn->getQueryBuilder(); $sql->update('appconfig')->set('configvalue', $sql->createParameter('configvalue'))->where($sql->expr()->eq('appid', $sql->createParameter('app')))->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))->setParameter('configvalue', $value)->setParameter('app', $app)->setParameter('configkey', $key); /* * Only limit to the existing value for non-Oracle DBs: * http://docs.oracle.com/cd/E11882_01/server.112/e26088/conditions002.htm#i1033286 * > Large objects (LOBs) are not supported in comparison conditions. */ if (!$this->conn instanceof \OC\DB\OracleConnection) { // Only update the value when it is not the same $sql->andWhere($sql->expr()->neq('configvalue', $sql->createParameter('configvalue')))->setParameter('configvalue', $value); } $changedRow = (bool) $sql->execute(); $this->cache[$app][$key] = $value; return $changedRow; }
private function addToCache(ICachedMountInfo $mount) { if ($mount->getStorageId() !== -1) { $this->connection->insertIfNotExist('*PREFIX*mounts', ['storage_id' => $mount->getStorageId(), 'root_id' => $mount->getRootId(), 'user_id' => $mount->getUser()->getUID(), 'mount_point' => $mount->getMountPoint()], ['root_id', 'user_id']); } else { $this->logger->error('Error getting storage info for mount at ' . $mount->getMountPoint()); } }
/** * attempts to map the given entry * @param string $fdn fully distinguished name (from LDAP) * @param string $name * @param string $uuid a unique identifier as used in LDAP * @return bool */ public function map($fdn, $name, $uuid) { $row = array('ldap_dn' => $fdn, 'owncloud_name' => $name, 'directory_uuid' => $uuid); try { $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); // insertIfNotExist returns values as int return (bool) $result; } catch (\Exception $e) { return false; } }
/** * Sets a value. If the key did not exist before it will be created. * * @param string $app app * @param string $key key * @param string $value value * @return bool True if the value was inserted or updated, false if the value was the same */ public function setValue($app, $key, $value) { if (!$this->hasKey($app, $key)) { $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', ['appid' => $app, 'configkey' => $key, 'configvalue' => $value], ['appid', 'configkey']); if ($inserted) { if (!isset($this->cache[$app])) { $this->cache[$app] = []; } $this->cache[$app][$key] = $value; return true; } } $sql = $this->conn->getQueryBuilder(); $sql->update('appconfig')->set('configvalue', $sql->createParameter('configvalue'))->where($sql->expr()->eq('appid', $sql->createParameter('app')))->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))->andWhere($sql->expr()->neq('configvalue', $sql->createParameter('configvalue')))->setParameter('configvalue', $value)->setParameter('app', $app)->setParameter('configkey', $key)->setParameter('configvalue', $value); $changedRow = (bool) $sql->execute(); $this->cache[$app][$key] = $value; return $changedRow; }
/** * Set a user defined value * * @param string $userId the userId of the user that we want to store the value under * @param string $appName the appName that we want to store the value under * @param string $key the key under which the value is being stored * @param string $value the value that you want to store * @param string $preCondition only update if the config value was previously the value passed as $preCondition * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met */ public function setUserValue($userId, $appName, $key, $value, $preCondition = null) { // TODO - FIXME $this->fixDIInit(); // Check if the key does exist $sql = 'SELECT `configvalue` FROM `*PREFIX*preferences` ' . 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'; $result = $this->connection->executeQuery($sql, array($userId, $appName, $key)); $oldValue = $result->fetchColumn(); $result->closeCursor(); $exists = $oldValue !== false; if ($oldValue === strval($value)) { // no changes return; } $affectedRows = 0; if (!$exists && $preCondition === null) { $this->connection->insertIfNotExist('*PREFIX*preferences', ['configvalue' => $value, 'userid' => $userId, 'appid' => $appName, 'configkey' => $key], ['configkey', 'userid', 'appid']); $affectedRows = 1; } elseif ($exists) { $data = array($value, $userId, $appName, $key); $sql = 'UPDATE `*PREFIX*preferences` SET `configvalue` = ? ' . 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ? '; if ($preCondition !== null) { if ($this->getSystemValue('dbtype', 'sqlite') === 'oci') { //oracle hack: need to explicitly cast CLOB to CHAR for comparison $sql .= 'AND to_char(`configvalue`) = ?'; } else { $sql .= 'AND `configvalue` = ?'; } $data[] = $preCondition; } $affectedRows = $this->connection->executeUpdate($sql, $data); } // only add to the cache if we already loaded data for the user if ($affectedRows > 0 && isset($this->userCache[$userId])) { if (!isset($this->userCache[$userId][$appName])) { $this->userCache[$userId][$appName] = array(); } $this->userCache[$userId][$appName][$key] = $value; } if ($preCondition !== null && $affectedRows === 0) { throw new PreConditionNotMetException(); } }
/** * Insert a file locking row if it does not exists. * * @param string $path * @param int $lock * @return int number of inserted rows */ protected function initLockField($path, $lock = 0) { $expire = $this->getExpireTime(); return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']); }
protected function initLockField($path) { $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => 0, 'ttl' => 0], ['key']); }
public function addApplicable($mountId, $type, $value) { $this->connection->insertIfNotExist('*PREFIX*external_applicable', ['mount_id' => $mountId, 'type' => $type, 'value' => $value], ['mount_id', 'type', 'value']); }
private function addToCache(ICachedMountInfo $mount) { $this->connection->insertIfNotExist('*PREFIX*mounts', ['storage_id' => $mount->getStorageId(), 'root_id' => $mount->getRootId(), 'user_id' => $mount->getUser()->getUID(), 'mount_point' => $mount->getMountPoint()], ['root_id', 'user_id']); }
/** * Insert a row if the matching row does not exists. * * @param string $table The table name (will replace *PREFIX* with the actual prefix) * @param array $input data that should be inserted into the table (column name => value) * @param array|null $compare List of values that should be checked for "if not exists" * If this is null or an empty array, all keys of $input will be compared * Please note: text fields (clob) must not be used in the compare array * @return int number of inserted rows * @throws \Doctrine\DBAL\DBALException */ public function insertIfNotExist($table, $input, array $compare = null) { return $this->connection->insertIfNotExist($table, $input, $compare); }