/** * Quick insert record * @param $table * @param array $columnData * @return \Doctrine\DBAL\Driver\Statement */ public function insert($table, array $columnData) { $columns = array_keys($columnData); $sql = sprintf("INSERT INTO %s (%s) VALUES (%s);", $table, implode(', ', $columns), implode(', ', array_fill(0, count($columnData), '?'))); $this->db->executeQuery($sql, array_values($columnData)); return $this->db->lastInsertId($table); }
private function createShare($type, $sourceId, $recipient, $targetName, $permissions, $parentId = null) { $qb = $this->connection->getQueryBuilder(); $values = ['share_type' => $qb->expr()->literal($type), 'share_with' => $qb->expr()->literal($recipient), 'uid_owner' => $qb->expr()->literal('user1'), 'item_type' => $qb->expr()->literal('folder'), 'item_source' => $qb->expr()->literal($sourceId), 'item_target' => $qb->expr()->literal('/' . $sourceId), 'file_source' => $qb->expr()->literal($sourceId), 'file_target' => $qb->expr()->literal($targetName), 'permissions' => $qb->expr()->literal($permissions), 'stime' => $qb->expr()->literal(time())]; if ($parentId !== null) { $values['parent'] = $qb->expr()->literal($parentId); } $qb->insert('share')->values($values)->execute(); return $this->connection->lastInsertId('*PREFIX*share'); }
/** * @param string $shareType * @param string $shareWith * @param null|int $parent * @return int */ protected function createShare($shareType, $shareWith, $parent) { $qb = $this->connection->getQueryBuilder(); $shareValues = ['share_type' => $qb->expr()->literal($shareType), 'share_with' => $qb->expr()->literal($shareWith), 'uid_owner' => $qb->expr()->literal('user1'), 'item_type' => $qb->expr()->literal('folder'), 'item_source' => $qb->expr()->literal(123), 'item_target' => $qb->expr()->literal('/123'), 'file_source' => $qb->expr()->literal(123), 'file_target' => $qb->expr()->literal('/test'), 'permissions' => $qb->expr()->literal(1), 'stime' => $qb->expr()->literal(time()), 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00')]; if ($parent) { $shareValues['parent'] = $qb->expr()->literal($parent); } $qb = $this->connection->getQueryBuilder(); $qb->insert('share')->values($shareValues)->execute(); return $this->connection->lastInsertId('*PREFIX*share'); }
/** * add server to the list of trusted ownCloud servers * * @param string $url * @return int * @throws HintException */ public function addServer($url) { $hash = $this->hash($url); $query = $this->connection->getQueryBuilder(); $query->insert($this->dbTable)->values(['url' => $query->createParameter('url'), 'url_hash' => $query->createParameter('url_hash')])->setParameter('url', $url)->setParameter('url_hash', $hash); $result = $query->execute(); if ($result) { return (int) $this->connection->lastInsertId('*PREFIX*' . $this->dbTable); } else { $message = 'Internal failure, Could not add ownCloud as trusted server: ' . $url; $message_t = $this->l->t('Could not add server'); throw new HintException($message, $message_t); } }
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; }
/** * Used to get the id of the last inserted element * @return int * @throws \BadMethodCallException When being called before an insert query has been run. */ public function getLastInsertId() { $from = $this->getQueryPart('from'); if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && !empty($from)) { return (int) $this->connection->lastInsertId($from['table']); } throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); }
/** * Used to get the id of the last inserted element * @return int * @throws \BadMethodCallException When being called before an insert query has been run. */ public function getLastInsertId() { if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { // lastInsertId() needs the prefix but no quotes $table = $this->prefixTableName($this->lastInsertedTable); return (int) $this->connection->lastInsertId($table); } throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); }
/** * Add a mount to the database * * @param string $mountPoint * @param string $storageBackend * @param string $authBackend * @param int $priority * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL * @return int the id of the new mount */ public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { if (!$priority) { $priority = 100; } $builder = $this->connection->getQueryBuilder(); $query = $builder->insert('external_mounts')->values(['mount_point' => $builder->createNamedParameter($mountPoint, \PDO::PARAM_STR), 'storage_backend' => $builder->createNamedParameter($storageBackend, \PDO::PARAM_STR), 'auth_backend' => $builder->createNamedParameter($authBackend, \PDO::PARAM_STR), 'priority' => $builder->createNamedParameter($priority, \PDO::PARAM_INT), 'type' => $builder->createNamedParameter($type, \PDO::PARAM_INT)]); $query->execute(); return (int) $this->connection->lastInsertId('external_mounts'); }
/** * @param $tableName * @param $idName * @return int */ protected function getLastInsertID($tableName, $idName) { $id = $this->connection->lastInsertId(); if ($id) { return $id; } // FIXME !!!! ORACLE WORKAROUND DO NOT COPY // FIXME INSTEAD HELP FIXING DOCTRINE // FIXME https://github.com/owncloud/core/issues/13303 // FIXME ALSO FIX https://github.com/owncloud/core/commit/2dd85ec984c12d3be401518f22c90d2327bec07a $qb = $this->connection->getQueryBuilder(); $result = $qb->select($qb->createFunction('MAX(`' . $idName . '`)'))->from($tableName)->execute(); return (int) $result->fetchColumn(); }
/** * {@inheritdoc} */ public function createTag($tagName, $userVisible, $userAssignable) { $userVisible = (int) $userVisible; $userAssignable = (int) $userAssignable; $query = $this->connection->getQueryBuilder(); $query->insert(self::TAG_TABLE)->values(['name' => $query->createNamedParameter($tagName), 'visibility' => $query->createNamedParameter($userVisible), 'editable' => $query->createNamedParameter($userAssignable)]); try { $query->execute(); } catch (UniqueConstraintViolationException $e) { throw new TagAlreadyExistsException('Tag ("' . $tagName . '", ' . $userVisible . ', ' . $userAssignable . ') already exists', 0, $e); } $tagId = $this->connection->lastInsertId('*PREFIX*' . self::TAG_TABLE); return new SystemTag((int) $tagId, $tagName, (bool) $userVisible, (bool) $userAssignable); }
public function testGetLastInsertId() { $qB = $this->connection->getQueryBuilder(); try { $qB->getLastInsertId(); $this->fail('getLastInsertId() should throw an exception, when being called before insert()'); } catch (\BadMethodCallException $e) { $this->assertTrue(true); } $qB->insert('appconfig')->values(['appid' => $qB->expr()->literal('testFirstResult'), 'configkey' => $qB->expr()->literal('testing' . 50), 'configvalue' => $qB->expr()->literal(100 - 50)])->execute(); $actual = $qB->getLastInsertId(); $this->assertNotNull($actual); $this->assertInternalType('int', $actual); $this->assertEquals($this->connection->lastInsertId('*PREFIX*appconfig'), $actual); }
/** * Creates a new subscription for a principal. * * If the creation was a success, an id must be returned that can be used to reference * this subscription in other methods, such as updateSubscription. * * @param string $principalUri * @param string $uri * @param array $properties * @return mixed */ function createSubscription($principalUri, $uri, array $properties) { if (!isset($properties['{http://calendarserver.org/ns/}source'])) { throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); } $values = ['principaluri' => $principalUri, 'uri' => $uri, 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), 'lastmodified' => time()]; foreach ($this->subscriptionPropertyMap as $xmlName => $dbName) { if (isset($properties[$xmlName])) { $values[$dbName] = $properties[$xmlName]; $fieldNames[] = $dbName; } } $query = $this->db->getQueryBuilder(); $query->insert('calendarsubscriptions')->values(['principaluri' => $query->createNamedParameter($values['principaluri']), 'uri' => $query->createNamedParameter($values['uri']), 'source' => $query->createNamedParameter($values['source']), 'lastmodified' => $query->createNamedParameter($values['lastmodified'])])->execute(); return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); }
/** * store meta data for a file or folder * * @param string $file * @param array $data * * @return int file id * @throws \RuntimeException */ public function put($file, array $data) { if (($id = $this->getId($file)) > -1) { $this->update($id, $data); return $id; } else { // normalize file $file = $this->normalize($file); if (isset($this->partial[$file])) { //add any saved partial data $data = array_merge($this->partial[$file], $data); unset($this->partial[$file]); } $requiredFields = array('size', 'mtime', 'mimetype'); foreach ($requiredFields as $field) { if (!isset($data[$field])) { //data not complete save as partial and return $this->partial[$file] = $data; return -1; } } $data['path'] = $file; $data['parent'] = $this->getParentId($file); $data['name'] = \OC_Util::basename($file); list($queryParts, $params) = $this->buildParts($data); $queryParts[] = '`storage`'; $params[] = $this->getNumericStorageId(); $queryParts = array_map(function ($item) { return trim($item, "`"); }, $queryParts); $values = array_combine($queryParts, $params); if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, ['storage', 'path_hash'])) { return (int) $this->connection->lastInsertId('*PREFIX*filecache'); } // The file was created in the mean time if (($id = $this->getId($file)) > -1) { $this->update($id, $data); return $id; } else { throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.'); } } }
public function testGetLastInsertId() { $qB = $this->connection->getQueryBuilder(); try { $qB->getLastInsertId(); $this->fail('getLastInsertId() should throw an exception, when being called before insert()'); } catch (\BadMethodCallException $e) { $this->assertTrue(true); } $qB->insert('properties')->values(['userid' => $qB->expr()->literal('testFirstResult'), 'propertypath' => $qB->expr()->literal('testing'), 'propertyname' => $qB->expr()->literal('testing'), 'propertyvalue' => $qB->expr()->literal('testing')])->execute(); $actual = $qB->getLastInsertId(); $this->assertNotNull($actual); $this->assertInternalType('int', $actual); $this->assertEquals($this->connection->lastInsertId('*PREFIX*properties'), $actual); $qB->delete('properties')->where($qB->expr()->eq('userid', $qB->expr()->literal('testFirstResult')))->execute(); try { $qB->getLastInsertId(); $this->fail('getLastInsertId() should throw an exception, when being called after delete()'); } catch (\BadMethodCallException $e) { $this->assertTrue(true); } }
/** * Creates a new subscription for a principal. * * If the creation was a success, an id must be returned that can be used to reference * this subscription in other methods, such as updateSubscription. * * @param string $principalUri * @param string $uri * @param array $properties * @return mixed */ function createSubscription($principalUri, $uri, array $properties) { if (!isset($properties['{http://calendarserver.org/ns/}source'])) { throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); } $values = ['principaluri' => $principalUri, 'uri' => $uri, 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), 'lastmodified' => time()]; $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; foreach ($this->subscriptionPropertyMap as $xmlName => $dbName) { if (array_key_exists($xmlName, $properties)) { $values[$dbName] = $properties[$xmlName]; if (in_array($dbName, $propertiesBoolean)) { $values[$dbName] = true; } } } $valuesToInsert = array(); $query = $this->db->getQueryBuilder(); foreach (array_keys($values) as $name) { $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); } $query->insert('calendarsubscriptions')->values($valuesToInsert)->execute(); return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); }
/** * Share a path * * @param \OCP\Share\IShare $share * @return \OCP\Share\IShare The share object * @throws ShareNotFound * @throws \Exception */ public function create(\OCP\Share\IShare $share) { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share'); $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType())); if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { //Set the UID of the user we share with $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { //Set the GID of the group we share with $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { //Set the token of the share $qb->setValue('token', $qb->createNamedParameter($share->getToken())); //If a password is set store it if ($share->getPassword() !== null) { $qb->setValue('share_with', $qb->createNamedParameter($share->getPassword())); } //If an expiration date is set store it if ($share->getExpirationDate() !== null) { $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); } if (method_exists($share, 'getParent')) { $qb->setValue('parent', $qb->createNamedParameter($share->getParent())); } } else { throw new \Exception('invalid share type!'); } } } // Set what is shares $qb->setValue('item_type', $qb->createParameter('itemType')); if ($share->getNode() instanceof \OCP\Files\File) { $qb->setParameter('itemType', 'file'); } else { $qb->setParameter('itemType', 'folder'); } // Set the file id $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId())); $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId())); // set the permissions $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions())); // Set who created this share $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy())); // Set who is the owner of this file/folder (and this the owner of the share) $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner())); // Set the file target $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget())); // Set the time this share was created $qb->setValue('stime', $qb->createNamedParameter(time())); // insert the data and fetch the id of the share $this->dbConn->beginTransaction(); $qb->execute(); $id = $this->dbConn->lastInsertId('*PREFIX*share'); $this->dbConn->commit(); // Now fetch the inserted share and create a complete share object $qb = $this->dbConn->getQueryBuilder(); $qb->select('*')->from('share')->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); $cursor = $qb->execute(); $data = $cursor->fetch(); $cursor->closeCursor(); if ($data === false) { throw new ShareNotFound(); } $share = $this->createShare($data); return $share; }
/** * @param $tableName * @param $idName * @return int */ protected function getLastInsertID($tableName, $idName) { return $this->connection->lastInsertId("*PREFIX*{$tableName}"); }
/** * @return int */ protected function getLastShareId() { return $this->connection->lastInsertId('*PREFIX*share'); }
/** * Used to get the id of the just inserted element * * @param string $tableName the name of the table where we inserted the item * @return int the id of the inserted element */ public function getInsertId($tableName) { return $this->connection->lastInsertId($tableName); }
/** * Used to get the id of the just inserted element * @param string $table the name of the table where we inserted the item * @return int the id of the inserted element */ public function lastInsertId($table = null) { return $this->connection->lastInsertId($table); }