Skip to content

Commit fb74df8

Browse files
committed
Improve image type support
1 parent 8a02d33 commit fb74df8

File tree

6 files changed

+44
-29
lines changed

6 files changed

+44
-29
lines changed

api.php

+18-13
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,30 @@ public function clear(): bool;
2020

2121
class CacheFactory
2222
{
23-
const PREFIX = 'phpcrudapi-%s-';
23+
const PREFIX = 'phpcrudapi-%s-%s-%s-';
2424

25-
private static function getPrefix(): String
25+
private static function getPrefix(Config $config): String
2626
{
27-
return sprintf(self::PREFIX, substr(md5(__FILE__), 0, 8));
27+
$driver = $config->getDriver();
28+
$database = $config->getDatabase();
29+
$filehash = substr(md5(__FILE__), 0, 8);
30+
return sprintf(self::PREFIX, $driver, $database, $filehash);
2831
}
2932

3033
public static function create(Config $config): Cache
3134
{
3235
switch ($config->getCacheType()) {
3336
case 'TempFile':
34-
$cache = new TempFileCache(self::getPrefix(), $config->getCachePath());
37+
$cache = new TempFileCache(self::getPrefix($config), $config->getCachePath());
3538
break;
3639
case 'Redis':
37-
$cache = new RedisCache(self::getPrefix(), $config->getCachePath());
40+
$cache = new RedisCache(self::getPrefix($config), $config->getCachePath());
3841
break;
3942
case 'Memcache':
40-
$cache = new MemcacheCache(self::getPrefix(), $config->getCachePath());
43+
$cache = new MemcacheCache(self::getPrefix($config), $config->getCachePath());
4144
break;
4245
case 'Memcached':
43-
$cache = new MemcachedCache(self::getPrefix(), $config->getCachePath());
46+
$cache = new MemcachedCache(self::getPrefix($config), $config->getCachePath());
4447
break;
4548
default:
4649
$cache = new NoCache();
@@ -1368,7 +1371,7 @@ public function convertColumnName(ReflectedColumn $column, $value): String
13681371
case 'pgsql':
13691372
return "encode($value::bytea, 'base64') as $value";
13701373
case 'sqlsrv':
1371-
return "CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:column($value)))', 'VARCHAR(MAX)') as $value";
1374+
return "CASE WHEN $value IS NULL THEN NULL ELSE (SELECT CAST($value as varbinary(max)) FOR XML PATH(''), BINARY BASE64) END as $value";
13721375

13731376
}
13741377
}
@@ -2579,6 +2582,9 @@ public function __construct(String $driver)
25792582
],
25802583
'sqlsrv' => [
25812584
'boolean' => 'bit',
2585+
'varchar' => 'nvarchar',
2586+
'clob' => 'ntext',
2587+
'blob' => 'image',
25822588
],
25832589
];
25842590

@@ -2651,18 +2657,17 @@ public function __construct(String $driver)
26512657
'datetime' => 'timestamp',
26522658
'datetime2' => 'timestamp',
26532659
'float' => 'double',
2654-
'image' => 'varbinary',
2660+
'image' => 'blob',
26552661
'int' => 'integer',
26562662
'money' => 'decimal',
2657-
'ntext' => 'longnvarchar',
2663+
'ntext' => 'clob',
26582664
'smalldatetime' => 'timestamp',
26592665
'smallmoney' => 'decimal',
2660-
'text' => 'longvarchar',
2666+
'text' => 'clob',
26612667
'timestamp' => 'binary',
2662-
'tinyint' => 'tinyint',
26632668
'udt' => 'varbinary',
26642669
'uniqueidentifier' => 'char',
2665-
'xml' => 'longnvarchar',
2670+
'xml' => 'clob',
26662671
],
26672672
];
26682673

src/Tqdev/PhpCrudApi/Cache/CacheFactory.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,30 @@
55

66
class CacheFactory
77
{
8-
const PREFIX = 'phpcrudapi-%s-';
8+
const PREFIX = 'phpcrudapi-%s-%s-%s-';
99

10-
private static function getPrefix(): String
10+
private static function getPrefix(Config $config): String
1111
{
12-
return sprintf(self::PREFIX, substr(md5(__FILE__), 0, 8));
12+
$driver = $config->getDriver();
13+
$database = $config->getDatabase();
14+
$filehash = substr(md5(__FILE__), 0, 8);
15+
return sprintf(self::PREFIX, $driver, $database, $filehash);
1316
}
1417

1518
public static function create(Config $config): Cache
1619
{
1720
switch ($config->getCacheType()) {
1821
case 'TempFile':
19-
$cache = new TempFileCache(self::getPrefix(), $config->getCachePath());
22+
$cache = new TempFileCache(self::getPrefix($config), $config->getCachePath());
2023
break;
2124
case 'Redis':
22-
$cache = new RedisCache(self::getPrefix(), $config->getCachePath());
25+
$cache = new RedisCache(self::getPrefix($config), $config->getCachePath());
2326
break;
2427
case 'Memcache':
25-
$cache = new MemcacheCache(self::getPrefix(), $config->getCachePath());
28+
$cache = new MemcacheCache(self::getPrefix($config), $config->getCachePath());
2629
break;
2730
case 'Memcached':
28-
$cache = new MemcachedCache(self::getPrefix(), $config->getCachePath());
31+
$cache = new MemcachedCache(self::getPrefix($config), $config->getCachePath());
2932
break;
3033
default:
3134
$cache = new NoCache();

src/Tqdev/PhpCrudApi/Database/ColumnConverter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function convertColumnName(ReflectedColumn $column, $value): String
4545
case 'pgsql':
4646
return "encode($value::bytea, 'base64') as $value";
4747
case 'sqlsrv':
48-
return "CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:column($value)))', 'VARCHAR(MAX)') as $value";
48+
return "CASE WHEN $value IS NULL THEN NULL ELSE (SELECT CAST($value as varbinary(max)) FOR XML PATH(''), BINARY BASE64) END as $value";
4949

5050
}
5151
}

src/Tqdev/PhpCrudApi/Database/TypeConverter.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public function __construct(String $driver)
2323
],
2424
'sqlsrv' => [
2525
'boolean' => 'bit',
26+
'varchar' => 'nvarchar',
27+
'clob' => 'ntext',
28+
'blob' => 'image',
2629
],
2730
];
2831

@@ -100,18 +103,17 @@ public function __construct(String $driver)
100103
'datetime' => 'timestamp',
101104
'datetime2' => 'timestamp',
102105
'float' => 'double',
103-
'image' => 'varbinary',
106+
'image' => 'blob',
104107
'int' => 'integer',
105108
'money' => 'decimal',
106-
'ntext' => 'longnvarchar',
109+
'ntext' => 'clob',
107110
'smalldatetime' => 'timestamp',
108111
'smallmoney' => 'decimal',
109-
'text' => 'longvarchar',
112+
'text' => 'clob',
110113
'timestamp' => 'binary',
111-
'tinyint' => 'tinyint',
112114
'udt' => 'varbinary',
113115
'uniqueidentifier' => 'char',
114-
'xml' => 'longnvarchar',
116+
'xml' => 'clob',
115117
],
116118
];
117119

test.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ function loadFixture(String $dir, Config $config)
114114
function run(array $drivers, String $dir, array $matches)
115115
{
116116
foreach ($drivers as $driver) {
117+
if (isset($matches[0])) {
118+
if (!preg_match('/' . $matches[0] . '/', $driver)) {
119+
continue;
120+
}
121+
}
117122
if (!extension_loaded("pdo_$driver")) {
118123
echo sprintf("%s: skipped, driver not loaded\n", $driver);
119124
continue;
@@ -124,7 +129,7 @@ function run(array $drivers, String $dir, array $matches)
124129
$config = new Config($settings);
125130
loadFixture($dir, $config);
126131
$start = microtime(true);
127-
$stats = runDir($config, "$dir/functional", $matches, '');
132+
$stats = runDir($config, "$dir/functional", array_slice($matches, 1), '');
128133
$end = microtime(true);
129134
$time = ($end - $start) * 1000;
130135
$total = $stats['total'];

tests/fixtures/blog_sqlsrv.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ GO
133133
CREATE TABLE [categories](
134134
[id] [int] IDENTITY,
135135
[name] [nvarchar](255) NOT NULL,
136-
[icon] [varbinary](max) NULL,
136+
[icon] [image] NULL,
137137
PRIMARY KEY CLUSTERED([id] ASC)
138138
)
139139
GO
@@ -202,7 +202,7 @@ GO
202202

203203
CREATE VIEW [tag_usage]
204204
AS
205-
SELECT top 100 PERCENT name, COUNT_BIG(name) AS [count] FROM tags, post_tags WHERE tags.id = post_tags.tag_id GROUP BY name ORDER BY [count] DESC, name
205+
SELECT top 100 PERCENT name, COUNT(name) AS [count] FROM tags, post_tags WHERE tags.id = post_tags.tag_id GROUP BY name ORDER BY [count] DESC, name
206206
GO
207207

208208
CREATE TABLE [products](

0 commit comments

Comments
 (0)