Skip to content

Commit 3012211

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 change exists - not working propery under SqlServer change writeStream - on exception file name in message was prefixed
1 parent 83900ab commit 3012211

File tree

3 files changed

+64
-25
lines changed

3 files changed

+64
-25
lines changed

schema/mssql.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
create table flysystem_files
2+
(
3+
id bigint identity primary key,
4+
path nvarchar(255) not null,
5+
type nvarchar(4) not null,
6+
contents varbinary(max),
7+
size int default 0 not null,
8+
level int not null,
9+
mimetype nvarchar(127),
10+
visibility nvarchar(7) default 'public' not null,
11+
timestamp int default 0 not null
12+
);

src/DoctrineDBALAdapter.php

Lines changed: 25 additions & 25 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,14 +85,14 @@ 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
[
9798
'path' => $prefixedPath,
@@ -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
);
@@ -406,8 +408,8 @@ public function listContents(string $path, bool $deep): iterable
406408

407409
try {
408410
$queryBuilder = $this->connection->createQueryBuilder()
409-
->from($this->table)
410-
->select('path, size, mimetype, timestamp, type, visibility');
411+
->from($this->table)
412+
->select('path, size, mimetype, timestamp, type, visibility');
411413

412414
if (!empty($path)) {
413415
$expressionBuilder = $this->connection->createExpressionBuilder();
@@ -430,10 +432,8 @@ public function listContents(string $path, bool $deep): iterable
430432
ParameterType::INTEGER),
431433
);
432434
}
433-
} else {
434-
if (!$deep) {
435-
$queryBuilder->andWhere('level = 0');
436-
}
435+
} elseif (!$deep) {
436+
$queryBuilder->andWhere('level = 0');
437437
}
438438
$queryBuilder->orderBy('path', 'ASC');
439439

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)