Table Name Fixer

介绍了一款用于自动转换SQL文件中表名的工具,该工具能够帮助开发人员在发布SQL文件时自动添加指定的所有者前缀,解决了因权限限制带来的不便。文章详细描述了在实现过程中遇到的技术挑战及解决方案,包括正则表达式的选择与应用,以及如何正确处理不同编码格式的文件。

最近碰到个问题,客户那边发布 SQL 文件到生产库,处于稽核的考虑,不能直接使用 JDBC 的用户,须使用第三方用户名。那么这样就会给开发人员带来麻烦,每次发布的时候,表名必须带上生产环境的  JDBC 用户名。

为了解决这个问题,特地开发个辅助软件,用于将开发人员提交的 SQL 文件中的表名自动加上指定的表所有者。

 

截图:

 

这里面还有两个插曲:

1、为了替换里面的表名,肯定不能简单的 Replace,那能借助什么呢?自然是强大的正则表达式了。幸好 Microsoft 在 ATL 中内置了对正则表达式的支持库。不过,居然没有 Perl 样式的 /b,无法匹配单词分隔符,VC 现在的正则表达式库 ATLRX.h 里面的 /b 只相当于 Perl 的 /s,代表空白字符。那没办法,只好另外选择第三方的正则表达式库了。提到 C++ 里面的正则表达式,首先想到的就是 GRETA 和 Boost 库(参考我的另一篇文章:Visual Studio 2008 中的 ATL),不过可惜,这几个库都巨大无比,虽然功能强大,不过我可不想带个 DLL 一起发布,能减小体积的事情,干嘛搞那么大?于是,在网上搜索了一下,一个小巧又功能强大、和 Perl 样式兼容的正则表达式库跃入眼帘,DEELX 正则表达式引擎

2、即使工程支持 Unicode  编码,用 CStdioFile 读取关键词文件,如果不是 ASCII 编码,Unicode 或者 UTF-8 格式,都会变成乱码。于是,从  CStdioFile 派生了一个新类 CXStdioFile,依据文件头来区分文件编码。

附文件头编码:

UNI16_BE:    0xFE, 0xFF

UNI16_LE:    0xFF, 0xFE

UTF8_SIGN:   0xEF, 0xBB, 0xBF    //UTF-8 带签名格式

当然,要避免乱码,还是得使用 WideCharToMultiByte 和 MultiByteToWideChar。

 

 

 

 

(rtx5070_env) PS E:\PyTorch_Build\pytorch> # 1. 加载模块加载器 (rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-ExecutionPolicy Bypass -Scope Process -Force (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\module_loader.ps1 Import-Module: E:\PyTorch_Build\pytorch\module_loader.ps1:72 Line | 72 | Import-Module PyEnvManager -Force | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The specified module 'PyEnvManager' was not loaded because no valid module file was found in any module | directory. Set-PythonEnv: C:\Users\Administrator\Documents\PowerShell\Microsoft.PowerShell_profile.ps1:5 Line | 5 | Set-PythonEnv -EnvName global | ~~~~~~~~~~~~~ | The term 'Set-PythonEnv' is not recognized as a name of a cmdlet, function, script file, or executable program. | Check the spelling of the name, or if a path was included, verify that the path is correct and try again. ✅ 环境管理模块已加载 Get-Command: E:\PyTorch_Build\pytorch\module_loader.ps1:98 Line | 98 | Get-Command Set-PythonEnv, New-EnvCheckpoint, Get-EnvGuardStatus | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The term 'Set-PythonEnv' is not recognized as a name of a cmdlet, function, script file, or executable program. | Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Get-Command: E:\PyTorch_Build\pytorch\module_loader.ps1:98 Line | 98 | Get-Command Set-PythonEnv, New-EnvCheckpoint, Get-EnvGuardStatus | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The term 'New-EnvCheckpoint' is not recognized as a name of a cmdlet, function, script file, or executable | program. Check the spelling of the name, or if a path was included, verify that the path is correct and try | again. Get-Command: E:\PyTorch_Build\pytorch\module_loader.ps1:98 Line | 98 | Get-Command Set-PythonEnv, New-EnvCheckpoint, Get-EnvGuardStatus | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The term 'Get-EnvGuardStatus' is not recognized as a name of a cmdlet, function, script file, or executable | program. Check the spelling of the name, or if a path was included, verify that the path is correct and try | again. (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 2. 修复环境 (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\env_repair_toolkit.ps1 -ForceReinstall Import-Module: E:\PyTorch_Build\pytorch\module_loader.ps1:72 Line | 72 | Import-Module PyEnvManager -Force | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The specified module 'PyEnvManager' was not loaded because no valid module file was found in any module | directory. Set-PythonEnv: C:\Users\Administrator\Documents\PowerShell\Microsoft.PowerShell_profile.ps1:5 Line | 5 | Set-PythonEnv -EnvName global | ~~~~~~~~~~~~~ | The term 'Set-PythonEnv' is not recognized as a name of a cmdlet, function, script file, or executable program. | Check the spelling of the name, or if a path was included, verify that the path is correct and try again. ✅ 环境管理模块已加载 Get-Command: E:\PyTorch_Build\pytorch\module_loader.ps1:98 Line | 98 | Get-Command Set-PythonEnv, New-EnvCheckpoint, Get-EnvGuardStatus | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The term 'Set-PythonEnv' is not recognized as a name of a cmdlet, function, script file, or executable program. | Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Get-Command: E:\PyTorch_Build\pytorch\module_loader.ps1:98 Line | 98 | Get-Command Set-PythonEnv, New-EnvCheckpoint, Get-EnvGuardStatus | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The term 'New-EnvCheckpoint' is not recognized as a name of a cmdlet, function, script file, or executable | program. Check the spelling of the name, or if a path was included, verify that the path is correct and try | again. Get-Command: E:\PyTorch_Build\pytorch\module_loader.ps1:98 Line | 98 | Get-Command Set-PythonEnv, New-EnvCheckpoint, Get-EnvGuardStatus | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The term 'Get-EnvGuardStatus' is not recognized as a name of a cmdlet, function, script file, or executable | program. Check the spelling of the name, or if a path was included, verify that the path is correct and try | again. WARNING: 创建检查点失败: The term 'New-EnvCheckpoint' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. ⚠️ 环境未正确激活 Set-PythonEnv: E:\PyTorch_Build\pytorch\env_repair_toolkit.ps1:44 Line | 44 | Set-PythonEnv -EnvName pytorch | ~~~~~~~~~~~~~ | The term 'Set-PythonEnv' is not recognized as a name of a cmdlet, function, script file, or executable program. | Check the spelling of the name, or if a path was included, verify that the path is correct and try again. ❌ PyTorch未安装: Traceback (most recent call last): File "<string>", line 1, in <module> File "E:\PyTorch_Build\pytorch\torch\__init__.py", line 35, in <module> from typing_extensions import ParamSpec as _ParamSpec, TypeIs as _TypeIs ModuleNotFoundError: No module named 'typing_extensions' 请运行编译安装脚本 Id Name PSJobTypeName State HasMoreData Location Command -- ---- ------------- ----- ----------- -------- ------- 5 EnvGuard BackgroundJob Running True localhost … ✅ 环境守护进程已启动 (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 3. 验证环境状态 (rtx5070_env) PS E:\PyTorch_Build\pytorch> Get-EnvGuardStatus Get-EnvGuardStatus: The term 'Get-EnvGuardStatus' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 4. 测试环境切换 (rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-PythonEnv -EnvName global Set-PythonEnv: The term 'Set-PythonEnv' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. (rtx5070_env) PS E:\PyTorch_Build\pytorch> Set-PythonEnv -EnvName pytorch Set-PythonEnv: The term 'Set-PythonEnv' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 5. 测试检查点创建 (rtx5070_env) PS E:\PyTorch_Build\pytorch> New-EnvCheckpoint New-EnvCheckpoint: The term 'New-EnvCheckpoint' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 6. 测试PyTorch安装 (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import torch; print(torch.cuda.is_available())" Traceback (most recent call last): File "<string>", line 1, in <module> File "E:\PyTorch_Build\pytorch\torch\__init__.py", line 35, in <module> from typing_extensions import ParamSpec as _ParamSpec, TypeIs as _TypeIs ModuleNotFoundError: No module named 'typing_extensions' (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 验证模块加载 (rtx5070_env) PS E:\PyTorch_Build\pytorch> Get-Module -Name PyEnvManager (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 查看可用命令 (rtx5070_env) PS E:\PyTorch_Build\pytorch> Get-Command -Module PyEnvManager (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 检查守护进程状态 (rtx5070_env) PS E:\PyTorch_Build\pytorch> Get-EnvGuardStatus Get-EnvGuardStatus: The term 'Get-EnvGuardStatus' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 检查环境激活状态 (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import sys; print(f'当前前缀: {sys.prefix}')" 当前前缀: E:\PyTorch_Build\pytorch\rtx5070_env (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 验证文件权限 (rtx5070_env) PS E:\PyTorch_Build\pytorch> Get-Acl "E:\PyTorchBuild_Secure\rtx5070_env\Scripts\python.exe" | >> Format-Table Owner, AccessToString -Wrap Owner AccessToString ----- -------------- BF-202503252000\Administrator BF-202503252000\Administrator Allow FullControl BF-202503252000\Administrator Allow FullControl BUILTIN\Administrators Allow FullControl NT AUTHORITY\SYSTEM Allow FullControl NT AUTHORITY\Authenticated Users Allow Modify, Synchronize BUILTIN\Users Allow ReadAndExecute, Synchronize (rtx5070_env) PS E:\PyTorch_Build\pytorch>
09-04
PS C:\Users\Administrator> pip install pywin32 >> WARNING: Ignoring invalid distribution -odelscope (e:\python310\lib\site-packages) Looking in links: e:\ai_temp\pip_packages Requirement already satisfied: pywin32 in e:\python310\lib\site-packages (306) WARNING: Ignoring invalid distribution -odelscope (e:\python310\lib\site-packages) WARNING: Ignoring invalid distribution -odelscope (e:\python310\lib\site-packages) PS C:\Users\Administrator> python find_recent_toolbox.py >> E:\Python310\python.exe: can't open file 'C:\\Users\\Administrator\\find_recent_toolbox.py': [Errno 2] No such file or directory PS C:\Users\Administrator> # 查找最近24小时创建的目录 >> $startTime = (Get-Date).AddHours(-24) >> $toolboxPaths = @() >> >> foreach ($drive in @('C:', 'E:')) { >> Get-ChildItem -Path $drive -Recurse -Directory -ErrorAction SilentlyContinue | >> Where-Object { $_.CreationTime -gt $startTime } | >> ForEach-Object { >> $dir = $_.FullName >> >> # 检查关键文件 >> $criticalFiles = @('*toolbox*.py', '*permission*.dll', '*admin*.exe') >> $found = $false >> foreach ($pattern in $criticalFiles) { >> if (Get-ChildItem -Path $dir -Filter $pattern -ErrorAction SilentlyContinue) { >> $found = $true >> break >> } >> } >> >> if ($found) { >> # 检查权限特征 >> $permKeywords = @('SeDebugPrivilege', 'AdjustTokenPrivileges', 'CreateProcessAsUser') >> $codeFiles = Get-ChildItem -Path $dir -Include *.py, *.c, *.cpp, *.dll -Recurse -ErrorAction SilentlyContinue | >> Select-Object -First 5 >> >> $keywordHits = @() >> foreach ($file in $codeFiles) { >> $content = Get-Content $file.FullName -Raw -ErrorAction SilentlyContinue >> if ($content) { >> foreach ($keyword in $permKeywords) { >> if ($content -match $keyword) { >> $keywordHits += "$keyword in $($file.Name)" >> } >> } >> } >> } >> >> $toolboxPaths += [PSCustomObject]@{ >> Path = $dir >> Created = $_.CreationTime >> PermissionFeatures = $keywordHits >> } >> } >> } >> } >> >> # 输出结果 >> if ($toolboxPaths) { >> $toolboxPaths | Format-Table Path, Created, PermissionFeatures -AutoSize >> } else { >> Write-Host "未找到符合条件的工具箱" -ForegroundColor Red >> } >> Path Created PermissionFeatures ---- ------- ------------------ E:\ProjectEcosystem 2025/8/12 23:48:38 {} PS C:\Users\Administrator> // C/C++ 示例 - 提升进程权限 >> BOOL EnableDebugPrivilege() { >> HANDLE hToken; >> TOKEN_PRIVILEGES tkp; >> >> if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) >> return FALSE; >> >> LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid); >> >> tkp.PrivilegeCount = 1; >> tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; >> >> AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, NULL); >> CloseHandle(hToken); >> return TRUE; >> } >> 所在位置 行:2 字符: 27 + BOOL EnableDebugPrivilege() { + ~ “(”后面应为表达式。 所在位置 行:6 字符: 45 + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGE ... + ~ “(”后面应为表达式。 所在位置 行:6 字符: 85 + ... n(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ... + ~ 参数列表中缺少参量。 所在位置 行:6 字符: 87 + ... GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) + ~ 不允许使用与号(&)。& 运算符是为将来使用而保留的;请用双引号将与号引起来("&"),以将其作为字符串的一部分传递。 所在位置 行:6 字符: 95 + ... GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) + ~ if(条件)后面缺少语句块。 所在位置 行:9 字符: 30 + LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid ... + ~ 参数列表中缺少参量。 所在位置 行:9 字符: 46 + LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid ... + ~ 管道元素中的“,”后面缺少表达式。 所在位置 行:9 字符: 47 + LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid ... + ~ 不允许使用与号(&)。& 运算符是为将来使用而保留的;请用双引号将与号引起来("&"),以将其作为字符串的一部分传递。 所在位置 行:14 字符: 33 + AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, NULL); + ~ 参数列表中缺少参量。 所在位置 行:14 字符: 41 + AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, NULL); + ~ 管道元素中的“,”后面缺少表达式。 并未报告所有分析错误。请更正报告的错误并重试。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedExpression PS C:\Users\Administrator> # 使用ctypes调用Windows API提升权限 >> import ctypes >> >> def enable_debug_privilege(): >> ADVAPI32 = ctypes.windll.advapi32 >> token = ctypes.c_void_p() >> >> # 获取当前进程令牌 >> ADVAPI32.OpenProcessToken( >> ctypes.windll.kernel32.GetCurrentProcess(), >> 40, # TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY >> ctypes.byref(token) >> ) >> >> # 查找调试权限值 >> luid = ctypes.c_ulonglong() >> ADVAPI32.LookupPrivilegeValueW(None, "SeDebugPrivilege", ctypes.byref(luid)) >> >> # 调整权限 >> new_state = (1, luid, 2) # SE_PRIVILEGE_ENABLED=2 >> ADVAPI32.AdjustTokenPrivileges(token, False, new_state, 0, None, None) >> 所在位置 行:4 字符: 28 + def enable_debug_privilege(): + ~ “(”后面应为表达式。 所在位置 行:6 字符: 29 + token = ctypes.c_void_p() + ~ “(”后面应为表达式。 所在位置 行:10 字符: 50 + ctypes.windll.kernel32.GetCurrentProcess(), + ~ “(”后面应为表达式。 所在位置 行:16 字符: 31 + luid = ctypes.c_ulonglong() + ~ “(”后面应为表达式。 所在位置 行:17 字符: 40 + ADVAPI32.LookupPrivilegeValueW(None, "SeDebugPrivilege", ctypes.b ... + ~ 参数列表中缺少参量。 所在位置 行:20 字符: 20 + new_state = (1, luid, 2) # SE_PRIVILEGE_ENABLED=2 + ~ “,”后面缺少表达式。 所在位置 行:20 字符: 21 + new_state = (1, luid, 2) # SE_PRIVILEGE_ENABLED=2 + ~~~~ 表达式或语句中包含意外的标记“luid”。 所在位置 行:20 字符: 20 + new_state = (1, luid, 2) # SE_PRIVILEGE_ENABLED=2 + ~ 表达式中缺少右“)”。 所在位置 行:20 字符: 28 + new_state = (1, luid, 2) # SE_PRIVILEGE_ENABLED=2 + ~ 表达式或语句中包含意外的标记“)”。 所在位置 行:21 字符: 41 + ADVAPI32.AdjustTokenPrivileges(token, False, new_state, 0, None, ... + ~ 参数列表中缺少参量。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedExpression PS C:\Users\Administrator> 不重建 都做了那么多了 为什么你老想着要重来?每次都从头开始 回到原点 那不就是在原地踏步 浪费时间吗?那有什么意义?
08-15
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值