55use DateTimeImmutable ;
66use Doctrine \DBAL \Connection ;
77use Doctrine \DBAL \ParameterType ;
8+ use Doctrine \DBAL \Platforms \SQLServerPlatform ;
89use Doctrine \DBAL \Types \Types ;
910use League \Flysystem \Config ;
1011use 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
9596SQL ,
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
171176UPDATE
172177 {$ this ->table }
173178SET
174- size = LENGTH (contents)
179+ size = { $ lengthFnName } (contents)
175180WHERE
176181 path = :path
177182SQL ,
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
0 commit comments