5
5
use DateTimeImmutable ;
6
6
use Doctrine \DBAL \Connection ;
7
7
use Doctrine \DBAL \ParameterType ;
8
+ use Doctrine \DBAL \Platforms \SQLServerPlatform ;
8
9
use Doctrine \DBAL \Types \Types ;
9
10
use League \Flysystem \Config ;
10
11
use League \Flysystem \DirectoryAttributes ;
@@ -84,14 +85,14 @@ public function directoryExists(string $path): bool
84
85
private function exists (string $ prefixedPath , string $ type ): bool
85
86
{
86
87
return (bool ) $ this ->connection ->executeQuery (<<<SQL
87
- SELECT EXISTS (
88
+ SELECT CASE WHEN EXISTS (
88
89
SELECT
89
90
1
90
91
FROM {$ this ->table }
91
92
WHERE
92
93
path = :path AND
93
94
type = :type
94
- )
95
+ ) THEN 1 ELSE 0 END
95
96
SQL ,
96
97
[
97
98
'path ' => $ prefixedPath ,
@@ -116,9 +117,6 @@ public function write(string $path, string $contents, Config $config): void
116
117
$ this ->writeStream ($ path , $ resource , $ config );
117
118
}
118
119
119
- /**
120
- * {@inheritDoc}
121
- */
122
120
public function writeStream (string $ path , $ contents , Config $ config ): void
123
121
{
124
122
try {
@@ -130,31 +128,31 @@ public function writeStream(string $path, $contents, Config $config): void
130
128
* UPDATE if file exists
131
129
* INSERT if not
132
130
*/
131
+ $ pathPrefixed = $ this ->prefixer ->prefixPath ($ path );
133
132
if ($ this ->fileExists ($ path )) {
134
- $ path = $ this ->prefixer ->prefixPath ($ path );
135
133
$ this ->connection ->update ($ this ->table ,
136
134
[
137
135
'contents ' => $ contents ,
138
136
'timestamp ' => $ config ->get ('timestamp ' , time ()),
139
137
'visibility ' => $ config ->get (Config::OPTION_VISIBILITY , Visibility::PUBLIC ),
140
138
],
141
139
[
142
- 'path ' => $ path ,
140
+ 'path ' => $ pathPrefixed ,
143
141
],
144
142
[
145
143
'contents ' => Types::BINARY ,
146
144
'timestamp ' => Types::INTEGER ,
147
- ]);
145
+ ]
146
+ );
148
147
} else {
149
- $ path = $ this ->prefixer ->prefixPath ($ path );
150
148
/* @var int|string $timestamp */
151
149
$ this ->connection ->insert ($ this ->table , [
152
- 'path ' => $ path ,
150
+ 'path ' => $ pathPrefixed ,
153
151
'type ' => self ::TYPE_FILE ,
154
152
'timestamp ' => $ config ->get ('timestamp ' , time ()),
155
- 'level ' => $ this ->directoryLevel ($ path ),
153
+ 'level ' => $ this ->directoryLevel ($ pathPrefixed ),
156
154
'contents ' => $ contents ,
157
- 'mimetype ' => $ this ->mimeTypeDetector ->detectMimeType ($ path , $ contents ),
155
+ 'mimetype ' => $ this ->mimeTypeDetector ->detectMimeType ($ pathPrefixed , $ contents ),
158
156
'visibility ' => $ config ->get (Config::OPTION_VISIBILITY , Visibility::PUBLIC ),
159
157
], [
160
158
'path ' => ParameterType::STRING ,
@@ -167,16 +165,23 @@ public function writeStream(string $path, $contents, Config $config): void
167
165
]);
168
166
}
169
167
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
171
176
UPDATE
172
177
{$ this ->table }
173
178
SET
174
- size = LENGTH (contents)
179
+ size = { $ lengthFnName } (contents)
175
180
WHERE
176
181
path = :path
177
182
SQL ,
178
183
[
179
- 'path ' => $ path ,
184
+ 'path ' => $ pathPrefixed ,
180
185
],
181
186
[
182
187
'path ' => ParameterType::STRING ,
@@ -210,9 +215,6 @@ public function read(string $path): string
210
215
}
211
216
}
212
217
213
- /**
214
- * {@inheritDoc}
215
- */
216
218
public function readStream (string $ path ): mixed
217
219
{
218
220
try {
@@ -254,7 +256,7 @@ public function delete(string $path): void
254
256
->delete (
255
257
$ this ->table ,
256
258
[
257
- 'path ' => $ path ,
259
+ 'path ' => $ this -> prefixer -> prefixPath ( $ path) ,
258
260
'type ' => self ::TYPE_FILE ,
259
261
],
260
262
);
@@ -406,8 +408,8 @@ public function listContents(string $path, bool $deep): iterable
406
408
407
409
try {
408
410
$ 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 ' );
411
413
412
414
if (!empty ($ path )) {
413
415
$ expressionBuilder = $ this ->connection ->createExpressionBuilder ();
@@ -430,10 +432,8 @@ public function listContents(string $path, bool $deep): iterable
430
432
ParameterType::INTEGER ),
431
433
);
432
434
}
433
- } else {
434
- if (!$ deep ) {
435
- $ queryBuilder ->andWhere ('level = 0 ' );
436
- }
435
+ } elseif (!$ deep ) {
436
+ $ queryBuilder ->andWhere ('level = 0 ' );
437
437
}
438
438
$ queryBuilder ->orderBy ('path ' , 'ASC ' );
439
439
0 commit comments