Skip to content

Commit

Permalink
Improve image type support
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee committed Oct 7, 2018
1 parent 8a02d33 commit fb74df8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 29 deletions.
31 changes: 18 additions & 13 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@ public function clear(): bool;

class CacheFactory
{
const PREFIX = 'phpcrudapi-%s-';
const PREFIX = 'phpcrudapi-%s-%s-%s-';

private static function getPrefix(): String
private static function getPrefix(Config $config): String
{
return sprintf(self::PREFIX, substr(md5(__FILE__), 0, 8));
$driver = $config->getDriver();
$database = $config->getDatabase();
$filehash = substr(md5(__FILE__), 0, 8);
return sprintf(self::PREFIX, $driver, $database, $filehash);
}

public static function create(Config $config): Cache
{
switch ($config->getCacheType()) {
case 'TempFile':
$cache = new TempFileCache(self::getPrefix(), $config->getCachePath());
$cache = new TempFileCache(self::getPrefix($config), $config->getCachePath());
break;
case 'Redis':
$cache = new RedisCache(self::getPrefix(), $config->getCachePath());
$cache = new RedisCache(self::getPrefix($config), $config->getCachePath());
break;
case 'Memcache':
$cache = new MemcacheCache(self::getPrefix(), $config->getCachePath());
$cache = new MemcacheCache(self::getPrefix($config), $config->getCachePath());
break;
case 'Memcached':
$cache = new MemcachedCache(self::getPrefix(), $config->getCachePath());
$cache = new MemcachedCache(self::getPrefix($config), $config->getCachePath());
break;
default:
$cache = new NoCache();
Expand Down Expand Up @@ -1368,7 +1371,7 @@ public function convertColumnName(ReflectedColumn $column, $value): String
case 'pgsql':
return "encode($value::bytea, 'base64') as $value";
case 'sqlsrv':
return "CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:column($value)))', 'VARCHAR(MAX)') as $value";
return "CASE WHEN $value IS NULL THEN NULL ELSE (SELECT CAST($value as varbinary(max)) FOR XML PATH(''), BINARY BASE64) END as $value";

}
}
Expand Down Expand Up @@ -2579,6 +2582,9 @@ public function __construct(String $driver)
],
'sqlsrv' => [
'boolean' => 'bit',
'varchar' => 'nvarchar',
'clob' => 'ntext',
'blob' => 'image',
],
];

Expand Down Expand Up @@ -2651,18 +2657,17 @@ public function __construct(String $driver)
'datetime' => 'timestamp',
'datetime2' => 'timestamp',
'float' => 'double',
'image' => 'varbinary',
'image' => 'blob',
'int' => 'integer',
'money' => 'decimal',
'ntext' => 'longnvarchar',
'ntext' => 'clob',
'smalldatetime' => 'timestamp',
'smallmoney' => 'decimal',
'text' => 'longvarchar',
'text' => 'clob',
'timestamp' => 'binary',
'tinyint' => 'tinyint',
'udt' => 'varbinary',
'uniqueidentifier' => 'char',
'xml' => 'longnvarchar',
'xml' => 'clob',
],
];

Expand Down
17 changes: 10 additions & 7 deletions src/Tqdev/PhpCrudApi/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@

class CacheFactory
{
const PREFIX = 'phpcrudapi-%s-';
const PREFIX = 'phpcrudapi-%s-%s-%s-';

private static function getPrefix(): String
private static function getPrefix(Config $config): String
{
return sprintf(self::PREFIX, substr(md5(__FILE__), 0, 8));
$driver = $config->getDriver();
$database = $config->getDatabase();
$filehash = substr(md5(__FILE__), 0, 8);
return sprintf(self::PREFIX, $driver, $database, $filehash);
}

public static function create(Config $config): Cache
{
switch ($config->getCacheType()) {
case 'TempFile':
$cache = new TempFileCache(self::getPrefix(), $config->getCachePath());
$cache = new TempFileCache(self::getPrefix($config), $config->getCachePath());
break;
case 'Redis':
$cache = new RedisCache(self::getPrefix(), $config->getCachePath());
$cache = new RedisCache(self::getPrefix($config), $config->getCachePath());
break;
case 'Memcache':
$cache = new MemcacheCache(self::getPrefix(), $config->getCachePath());
$cache = new MemcacheCache(self::getPrefix($config), $config->getCachePath());
break;
case 'Memcached':
$cache = new MemcachedCache(self::getPrefix(), $config->getCachePath());
$cache = new MemcachedCache(self::getPrefix($config), $config->getCachePath());
break;
default:
$cache = new NoCache();
Expand Down
2 changes: 1 addition & 1 deletion src/Tqdev/PhpCrudApi/Database/ColumnConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function convertColumnName(ReflectedColumn $column, $value): String
case 'pgsql':
return "encode($value::bytea, 'base64') as $value";
case 'sqlsrv':
return "CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:column($value)))', 'VARCHAR(MAX)') as $value";
return "CASE WHEN $value IS NULL THEN NULL ELSE (SELECT CAST($value as varbinary(max)) FOR XML PATH(''), BINARY BASE64) END as $value";

}
}
Expand Down
12 changes: 7 additions & 5 deletions src/Tqdev/PhpCrudApi/Database/TypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function __construct(String $driver)
],
'sqlsrv' => [
'boolean' => 'bit',
'varchar' => 'nvarchar',
'clob' => 'ntext',
'blob' => 'image',
],
];

Expand Down Expand Up @@ -100,18 +103,17 @@ public function __construct(String $driver)
'datetime' => 'timestamp',
'datetime2' => 'timestamp',
'float' => 'double',
'image' => 'varbinary',
'image' => 'blob',
'int' => 'integer',
'money' => 'decimal',
'ntext' => 'longnvarchar',
'ntext' => 'clob',
'smalldatetime' => 'timestamp',
'smallmoney' => 'decimal',
'text' => 'longvarchar',
'text' => 'clob',
'timestamp' => 'binary',
'tinyint' => 'tinyint',
'udt' => 'varbinary',
'uniqueidentifier' => 'char',
'xml' => 'longnvarchar',
'xml' => 'clob',
],
];

Expand Down
7 changes: 6 additions & 1 deletion test.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ function loadFixture(String $dir, Config $config)
function run(array $drivers, String $dir, array $matches)
{
foreach ($drivers as $driver) {
if (isset($matches[0])) {
if (!preg_match('/' . $matches[0] . '/', $driver)) {
continue;
}
}
if (!extension_loaded("pdo_$driver")) {
echo sprintf("%s: skipped, driver not loaded\n", $driver);
continue;
Expand All @@ -124,7 +129,7 @@ function run(array $drivers, String $dir, array $matches)
$config = new Config($settings);
loadFixture($dir, $config);
$start = microtime(true);
$stats = runDir($config, "$dir/functional", $matches, '');
$stats = runDir($config, "$dir/functional", array_slice($matches, 1), '');
$end = microtime(true);
$time = ($end - $start) * 1000;
$total = $stats['total'];
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/blog_sqlsrv.sql
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ GO
CREATE TABLE [categories](
[id] [int] IDENTITY,
[name] [nvarchar](255) NOT NULL,
[icon] [varbinary](max) NULL,
[icon] [image] NULL,
PRIMARY KEY CLUSTERED([id] ASC)
)
GO
Expand Down Expand Up @@ -202,7 +202,7 @@ GO

CREATE VIEW [tag_usage]
AS
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
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
GO

CREATE TABLE [products](
Expand Down

0 comments on commit fb74df8

Please sign in to comment.