没有不值得去解决的问题,也没有不值得去学习的技术!

Generated SQL Returns No Records in Linux Containers on Windows 10: A DIRECTORY_SEPARATOR Issue

决定打印生成的 SQL,需要删除 ->first() 。进行一下对比。Windows 10 与 Linux 的分别如下,然后在 MySQL 中执行 Linux 下的 SQL ,查询结果为 NULL。区别在于 pages/\ 与 pages//

1. Executing the generated SQL works locally on Windows 10, but in the Linux container, no records are found. The records printed under Windows 10 are shown in Figure 1.

Executing the generated SQL works locally on Windows 10, but in the Linux container, no records are found. The records printed under Windows 10
Figure 1
PHP

$rawSchema = ThemeAssetEntity::select('schema')
	->where('theme_id', '=', $this->getThemeId())
	->where('category', '=', ThemeAssetEntity::CATEGORY_PAGE)
	->where('asset_key', 'regexp', $this->themeLayout->getTemplateExtensionsPattern($basename))
	->first();
var_dump($rawSchema);
exit;

2. Printing the records under the Linux container returns NULL. As shown in Figure 2.

Printing the records under the Linux container. The result is NULL
Figure 2

3. I decided to print the generated SQL, which required removing ->first() for comparison. The outputs for Windows 10 and Linux are shown below. After executing the SQL from Linux in MySQL, the query result is NULL. The difference lies between pages/\ and pages//. As shown in Figure 3.

Decided to print the generated SQL, needed to remove ->first() for comparison. The outputs for Windows 10 and Linux are shown below, then executing the SQL from Linux in MySQL, the query result is NULL. The difference lies between pages/\ and pages//
Figure 3
PHP

$rawSchema = ThemeAssetEntity::select('schema')
	->where('theme_id', '=', $this->getThemeId())
	->where('category', '=', ThemeAssetEntity::CATEGORY_PAGE)
	->where('asset_key', 'regexp', $this->themeLayout->getTemplateExtensionsPattern($basename));
$query = str_replace(array('?'), array('\'%s\''), $rawSchema->toSql());
$query = vsprintf($query, $rawSchema->getBindings());
dump($query);
exit;

SQL

select `schema` from `theme_asset` where `theme_id` = '96b1c8b9-5b18-4760-9e26-50ab009ac011' and `category` = 'page' and `asset_key` regexp '^pages/\product_detail(\.blade\.php|\.liquid|\.json)'

SQL

select `schema` from `theme_asset` where `theme_id` = '96dcab33-3e1d-4279-ab96-39693f03515f' and `category` = 'page' and `asset_key` regexp '^pages//product_detail(\.blade\.php|\.liquid|\.json)'

4. Analyze the function that generates the regular expression. Printing DIRECTORY_SEPARATOR shows inconsistent results across the two environments: \ and /, respectively.

PHP

    public function getTemplateExtensionsPattern($basename)
    {
        $escapeDot = array_map(function($extension){
            return str_replace('.', '\\.' ,$extension);
        }, $this->getTemplateExtensions());
        print_r(DIRECTORY_SEPARATOR);
        exit;
       return sprintf('^%s%s%s%s', $this->getPageDir(), '/'.DIRECTORY_SEPARATOR, $basename, '('.join('|', $escapeDot) .')');
    }

Plaintext

\

Plaintext

/

5. Remove .DIRECTORY_SEPARATOR because it is inconsistent across the two operating systems. Since only / is used in the table, there is no need to escape it. The final generated SQL is now consistent across both operating systems and returns query results. As shown in Figure 4.

Remove .DIRECTORY_SEPARATOR because it is inconsistent across the two operating systems. And since only / is used in the table, there is no need to escape it. The final generated SQL is consistent across both operating systems and can return query results
Figure 4
PHP

public function getTemplateExtensionsPattern($basename)
{
	$escapeDot = array_map(function($extension){
		return str_replace('.', '\\.' ,$extension);
	}, $this->getTemplateExtensions());

   return sprintf('^%s%s%s%s', $this->getPageDir(), '/', $basename, '('.join('|', $escapeDot) .')');
}

SQL

select `schema` from `theme_asset` where `theme_id` = '96dcab33-3e1d-4279-ab96-39693f03515f' and `category` = 'page' and `asset_key` regexp '^pages/product_detail(\.blade\.php|\.liquid|\.json)'

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系