Skip to content

Commit 43cc926

Browse files
committed
fixes exists - not working properly for checking directories
fixes writeStream - updating size not working for SqlServer fixes delete - file name was not prefixed fixes listContents - prefixed path was not fully working change exists - not working propery under SqlServer change writeStream - on exception file name in message was prefixed
1 parent 83900ab commit 43cc926

File tree

2 files changed

+57
-34
lines changed

2 files changed

+57
-34
lines changed

src/DoctrineDBALAdapter.php

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DateTimeImmutable;
66
use Doctrine\DBAL\Connection;
77
use Doctrine\DBAL\ParameterType;
8+
use Doctrine\DBAL\Platforms\SQLServerPlatform;
89
use Doctrine\DBAL\Types\Types;
910
use League\Flysystem\Config;
1011
use League\Flysystem\DirectoryAttributes;
@@ -84,17 +85,17 @@ public function directoryExists(string $path): bool
8485
private function exists(string $prefixedPath, string $type): bool
8586
{
8687
return (bool) $this->connection->executeQuery(<<<SQL
87-
SELECT EXISTS (
88+
SELECT CASE WHEN EXISTS (
8889
SELECT
8990
1
9091
FROM {$this->table}
9192
WHERE
9293
path = :path AND
9394
type = :type
94-
)
95+
) THEN 1 ELSE 0 END
9596
SQL,
9697
[
97-
'path' => $prefixedPath,
98+
'path' => rtrim($prefixedPath, '\\/'),
9899
'type' => $type,
99100
],
100101
[
@@ -116,9 +117,6 @@ public function write(string $path, string $contents, Config $config): void
116117
$this->writeStream($path, $resource, $config);
117118
}
118119

119-
/**
120-
* {@inheritDoc}
121-
*/
122120
public function writeStream(string $path, $contents, Config $config): void
123121
{
124122
try {
@@ -130,31 +128,31 @@ public function writeStream(string $path, $contents, Config $config): void
130128
* UPDATE if file exists
131129
* INSERT if not
132130
*/
131+
$pathPrefixed = $this->prefixer->prefixPath($path);
133132
if ($this->fileExists($path)) {
134-
$path = $this->prefixer->prefixPath($path);
135133
$this->connection->update($this->table,
136134
[
137135
'contents' => $contents,
138136
'timestamp' => $config->get('timestamp', time()),
139137
'visibility' => $config->get(Config::OPTION_VISIBILITY, Visibility::PUBLIC),
140138
],
141139
[
142-
'path' => $path,
140+
'path' => $pathPrefixed,
143141
],
144142
[
145143
'contents' => Types::BINARY,
146144
'timestamp' => Types::INTEGER,
147-
]);
145+
]
146+
);
148147
} else {
149-
$path = $this->prefixer->prefixPath($path);
150148
/* @var int|string $timestamp */
151149
$this->connection->insert($this->table, [
152-
'path' => $path,
150+
'path' => $pathPrefixed,
153151
'type' => self::TYPE_FILE,
154152
'timestamp' => $config->get('timestamp', time()),
155-
'level' => $this->directoryLevel($path),
153+
'level' => $this->directoryLevel($pathPrefixed),
156154
'contents' => $contents,
157-
'mimetype' => $this->mimeTypeDetector->detectMimeType($path, $contents),
155+
'mimetype' => $this->mimeTypeDetector->detectMimeType($pathPrefixed, $contents),
158156
'visibility' => $config->get(Config::OPTION_VISIBILITY, Visibility::PUBLIC),
159157
], [
160158
'path' => ParameterType::STRING,
@@ -167,16 +165,23 @@ public function writeStream(string $path, $contents, Config $config): void
167165
]);
168166
}
169167

170-
$this->connection->executeStatement(<<<SQL
168+
if ($this->connection->getDatabasePlatform() instanceof SQLServerPlatform) {
169+
$lengthFnName = 'LEN';
170+
} else {
171+
$lengthFnName = 'LENGTH';
172+
}
173+
174+
$this->connection->executeStatement(
175+
<<<SQL
171176
UPDATE
172177
{$this->table}
173178
SET
174-
size = LENGTH(contents)
179+
size = {$lengthFnName}(contents)
175180
WHERE
176181
path = :path
177182
SQL,
178183
[
179-
'path' => $path,
184+
'path' => $pathPrefixed,
180185
],
181186
[
182187
'path' => ParameterType::STRING,
@@ -210,9 +215,6 @@ public function read(string $path): string
210215
}
211216
}
212217

213-
/**
214-
* {@inheritDoc}
215-
*/
216218
public function readStream(string $path): mixed
217219
{
218220
try {
@@ -254,7 +256,7 @@ public function delete(string $path): void
254256
->delete(
255257
$this->table,
256258
[
257-
'path' => $path,
259+
'path' => $this->prefixer->prefixPath($path),
258260
'type' => self::TYPE_FILE,
259261
],
260262
);
@@ -402,7 +404,10 @@ public function fileSize(string $path): FileAttributes
402404

403405
public function listContents(string $path, bool $deep): iterable
404406
{
405-
$path = $this->prefixer->prefixPath($path);
407+
// saved path does not contain trailing slash
408+
// but method could be called with "path: '/'"
409+
// without trimming we would list nothing
410+
$path = rtrim($this->prefixer->prefixPath($path), '\\/');
406411

407412
try {
408413
$queryBuilder = $this->connection->createQueryBuilder()
@@ -411,7 +416,6 @@ public function listContents(string $path, bool $deep): iterable
411416

412417
if (!empty($path)) {
413418
$expressionBuilder = $this->connection->createExpressionBuilder();
414-
415419
$queryBuilder
416420
->andWhere(
417421
$expressionBuilder->or(
@@ -420,20 +424,12 @@ public function listContents(string $path, bool $deep): iterable
420424
)
421425
);
422426
if ($deep) {
423-
$queryBuilder->andWhere(
424-
'level >= '.$queryBuilder->createNamedParameter($this->directoryLevel($path) + 1,
425-
ParameterType::INTEGER),
426-
);
427+
$queryBuilder->andWhere('level >= '.$queryBuilder->createNamedParameter($this->directoryLevel($path) + 1, ParameterType::INTEGER));
427428
} else {
428-
$queryBuilder->andWhere(
429-
'level = '.$queryBuilder->createNamedParameter($this->directoryLevel($path) + 1,
430-
ParameterType::INTEGER),
431-
);
432-
}
433-
} else {
434-
if (!$deep) {
435-
$queryBuilder->andWhere('level = 0');
429+
$queryBuilder->andWhere('level = '.$queryBuilder->createNamedParameter($this->directoryLevel($path) + 1, ParameterType::INTEGER));
436430
}
431+
} elseif (!$deep) {
432+
$queryBuilder->andWhere('level = 0');
437433
}
438434
$queryBuilder->orderBy('path', 'ASC');
439435

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace WGG\Flysystem\Doctrine\Tests;
4+
5+
use Doctrine\DBAL\DriverManager;
6+
use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase;
7+
use League\Flysystem\FilesystemAdapter;
8+
use WGG\Flysystem\Doctrine\DoctrineDBALAdapter;
9+
10+
use function dirname;
11+
12+
/**
13+
* @covers \WGG\Flysystem\Doctrine\DoctrineDBALAdapter
14+
*/
15+
class DoctrineDBALAdapterWithPrefixTest extends FilesystemAdapterTestCase
16+
{
17+
protected static function createFilesystemAdapter(): FilesystemAdapter
18+
{
19+
$connection = DriverManager::getConnection([
20+
'url' => 'sqlite:///:memory:',
21+
]);
22+
23+
$connection->executeStatement((string) file_get_contents(dirname(__DIR__).'/schema/sqlite.sql'));
24+
25+
return new DoctrineDBALAdapter(connection: $connection, prefix: 'prefix_test');
26+
}
27+
}

0 commit comments

Comments
 (0)