当前位置: 首页 > news >正文

网页版预编译SQL转换工具

前言

需要一个预编译sql 转 可执行sql的工具,需要将预编译的sql和参数一起组合成一条可执行sql脚本。

本文基于 sql-formatter.min.js 插件

代码实现

实现效果和示例
在这里插入图片描述

作用: 通过本工具可以有效的将预编译的sql 和参数 组合构建成可执行的sql语句,方便调试和查看。

代码全文:


<!DOCTYPE html>
<html>
<head><title>SQL预编译转换工具</title><style>body { font-family: Arial, sans-serif; margin: 20px; }.container { max-width: 800px; }textarea { width: 100%; height: 100px; margin-bottom: 10px; }.button-group {display: flex;gap: 10px;margin: 15px 0;}button { padding: 8px 16px; background: #4CAF50; color: white; border: none; cursor: pointer; }button:hover { background: #45a049; }button:disabled { background: #cccccc; cursor: not-allowed; }.result { margin-top: 20px; padding: 15px; border: 1px solid #ccc; white-space: pre-wrap; background: #f9f9f9;position: relative;}.error { color: red; }.hint { font-size: 0.9em; color: #666; margin: 10px 0;padding: 10px;background: #f0f0f0;border-radius: 4px;}.radio-group {margin: 15px 0;padding: 10px;background: #e9f7fe;border-radius: 4px;}.copy-btn { background: #2196F3; }.copy-btn:hover { background: #0b7dda; }.format-option { margin-right: 15px; }</style>
</head>
<body><div class="container"><h2>预编译SQL转换工具</h2><div class="hint"><strong>参数输入说明:</strong><br>1. 带类型格式:<code>值(类型)</code>(如:<code>xiaowang(String)</code><br>2. 不带类型格式:直接输入值(如:<code>xiaowang</code><br>3. 混合输入示例:<code>xiaowang(String),123,2025-01-01(Date),true</code></div><label for="sqlTemplate">预编译SQL模板(使用?作为占位符):</label><textarea id="sqlTemplate" placeholder="例如:SELECT * FROM users WHERE id = ? AND name = ?">SELECT * FROM users WHERE id = ? AND name = ?</textarea><label for="params">参数列表(逗号分隔):</label><textarea id="params" placeholder="格式:参数1,参数2,...&#10;示例:&#10;带类型:xiaowang(String),123(Integer)&#10;不带类型:xiaowang,123&#10;混合:xiaowang(String),123,2025-01-01(Date)">xiaowang(String),123</textarea><div class="radio-group"><strong>格式化选项:</strong><br><label class="format-option"><input type="radio" name="format" value="true" checked>是(美化SQL格式)</label><label class="format-option"><input type="radio" name="format" value="false">否(原始输出)</label></div><div class="button-group"><button onclick="convertSQL()">转换为可执行SQL</button><button id="copyBtn" class="copy-btn" onclick="copyResult()" disabled>📋 复制结果</button></div><div class="result" id="result"></div></div><script src="https://cdnjs.cloudflare.com/ajax/libs/sql-formatter/2.3.3/sql-formatter.min.js"></script><script>function convertSQL() {const template = document.getElementById('sqlTemplate').value.trim();const paramsInput = document.getElementById('params').value.trim();const shouldFormat = document.querySelector('input[name="format"]:checked').value === 'true';const resultDiv = document.getElementById('result');const copyBtn = document.getElementById('copyBtn');// 清除之前的结果resultDiv.innerHTML = '';copyBtn.disabled = true;// 验证输入if (!template) {resultDiv.innerHTML = '<span class="error">错误:请输入SQL模板</span>';return;}// 解析参数(支持带类型和不带类型)const paramArray = paramsInput.split(',').map(param => param.trim().replace(/\s+/g, ''));const params = [];for (const param of paramArray) {// 尝试解析带类型的参数const typeMatch = param.match(/^(.+?)\((String|Integer|Date|Boolean)\)$/);if (typeMatch) {const value = typeMatch[1];const type = typeMatch[2];// 处理带类型的值let processedValue;switch (type) {case 'String':processedValue = `'${value.replace(/'/g, "''")}'`;break;case 'Integer':if (!/^-?\d+$/.test(value)) {resultDiv.innerHTML = `<span class="error">错误:非整数参数 "${param}"</span>`;return;}processedValue = value;break;case 'Date':// 简单验证日期格式(实际项目中可能需要更严格的验证)if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) {resultDiv.innerHTML = `<span class="error">错误:日期格式应为YYYY-MM-DD "${value}"</span>`;return;}processedValue = `'${value}'`;break;case 'Boolean':processedValue = value.toLowerCase() === 'true' ? 'TRUE' : 'FALSE';break;default:resultDiv.innerHTML = `<span class="error">错误:未知类型 "${type}"</span>`;return;}params.push({ original: param, processed: processedValue });} else {// 处理不带类型的值(默认作为String处理)let processedValue;// 尝试自动检测类型if (/^-?\d+$/.test(param)) {// 数字processedValue = param;} else if (/^true|false$/i.test(param)) {// 布尔值processedValue = param.toLowerCase() === 'true' ? 'TRUE' : 'FALSE';} else if (/^\d{4}-\d{2}-\d{2}$/.test(param)) {// 日期processedValue = `'${param}'`;} else {// 默认作为字符串processedValue = `'${param.replace(/'/g, "''")}'`;}params.push({ original: param, processed: processedValue });}}// 计算占位符数量const placeholderCount = (template.match(/\?/g) || []).length;// 验证参数数量if (params.length !== placeholderCount) {resultDiv.innerHTML = `<span class="error">错误:参数数量不匹配。需要 ${placeholderCount} 个参数,但提供了 ${params.length} 个</span>`;return;}// 转换SQLlet sql = template;params.forEach(param => {sql = sql.replace('?', param.processed, 1);});// 格式化处理if (shouldFormat && sqlFormatter) {try {sql = sqlFormatter.format(sql);} catch (e) {console.error("格式化错误:", e);}}// 显示结果resultDiv.textContent = sql;copyBtn.disabled = false;}function copyResult() {const resultDiv = document.getElementById('result');const range = document.createRange();range.selectNode(resultDiv);window.getSelection().removeAllRanges();window.getSelection().addRange(range);try {const successful = document.execCommand('copy');const msg = successful ? '复制成功!' : '复制失败,请手动选择';alert(msg);} catch (err) {alert('复制失败,请手动选择');}window.getSelection().removeAllRanges();}</script>
</body>
</html>
http://www.dtcms.com/a/610143.html

相关文章:

  • 基于Springboot+vue的心理健康测评预约心理咨询师论坛系统
  • MySQL数据库入门指南
  • 品牌营销型网站建设策划工程在哪个网站做推广比较合适
  • 安卓 4.4.2 电视盒子 ADB 设置应用开机自启动
  • 绝对值伺服“编码器计数值溢出“保护报警
  • 小程序下载图片问题处理
  • 网站首页被k网站信息同步
  • 线性代数 - 叉积的分量形式与矩阵形式
  • 做网站业务的 怎么跑客户元氏网站制作
  • 2025-11-14 学习记录--Python-特征归一化方法(Min-Max或StandardScaler)
  • 一款基于鲁班猫和STM32的自主导航实践
  • Cognex VisionPro 相机工具集成代码分析笔记
  • 五大3D软件深度横评:Maya、3DMax、Cinema 4D、Houdini与Blender
  • 网站可以不备案有口碑的武进网站建设
  • 重庆建网站 私单建设银行人力资源系统网站怎么进
  • 基于ADP自适应动态规划算法的控制系统matlab性能仿真,采用RNN进行控制对象参数辨识
  • 宝塔面板建站教程中国建设银行北京天竺支行网站
  • 本地建站教程企业邮箱邮箱
  • 开源的力量:如何用开源技术构建高效IT架构?
  • 如何降低程序的时间复杂度,提高运行时效?
  • openEuler系统下sudo权限配置与使用指南
  • 一个网站好不好做网站赔钱了
  • 受欢迎的丹阳网站建设有哪些调查网站可以做兼职
  • 品牌型网站成功案例图片网站建设的流程分析
  • 手机网站用什么域名潍坊网站建设 世纪环球16楼
  • 17、grafana安装
  • 2025年蚌埠市“三首产品”、市级服务型制造示范、市级企业技术中心等5个项目认定申报指南大全
  • 做一个个人主页的网站怎么做房地产销售段子
  • 个人网站定制国内免费的短视频素材网站
  • 1999-2023年 微观企业劳动生产率数据