chengjie месяцев назад: 5
Родитель
Сommit
fbd4918380
1 измененных файлов с 81 добавлено и 6 удалено
  1. 81 6
      public/mg/kylx365_db_admin.html

+ 81 - 6
public/mg/kylx365_db_admin.html

@@ -632,6 +632,7 @@
632 632
                     <div class="sql-editor-left">
633 633
                         <textarea class="sql-textarea" v-model="sqlQuery" placeholder="输入SQL查询语句..."></textarea>
634 634
                         <div class="btn-group" style="justify-content: flex-start;">
635
+                            <button type="button" class="btn btn-default" @click="insertSqlClause('WHERE')">WHERE</button>
635 636
                             <button type="button" class="btn btn-default" @click="insertSqlClause('GROUP BY')">GROUP BY</button>
636 637
                             <button type="button" class="btn btn-default" @click="insertSqlClause('HAVING')">HAVING</button>
637 638
                             <button type="button" class="btn btn-default" @click="insertSqlClause('ORDER BY')">ORDER BY</button>
@@ -1179,13 +1180,17 @@
1179 1180
                         this.sqlQuery = this.sqlQuery.replace(limitRegex, `LIMIT ${this.selectedLimit}${this.sqlQuery.endsWith(';') ? ';' : ''}`);
1180 1181
                     } else {
1181 1182
                         // 如果没有LIMIT子句,添加到末尾
1183
+                        // 确保前面有换行符
1184
+                        const hasNewlineBefore = this.sqlQuery.trimEnd().endsWith('\n');
1185
+                        const prefix = hasNewlineBefore ? '' : '\n';
1186
+                        
1182 1187
                         // 检查SQL是否以分号结尾
1183 1188
                         if (this.sqlQuery.trim().endsWith(';')) {
1184 1189
                             // 在分号前添加LIMIT
1185
-                            this.sqlQuery = this.sqlQuery.replace(/;\s*$/, `\nLIMIT ${this.selectedLimit};`);
1190
+                            this.sqlQuery = this.sqlQuery.replace(/;\s*$/, `${prefix}LIMIT ${this.selectedLimit};`);
1186 1191
                         } else {
1187 1192
                             // 直接在末尾添加LIMIT
1188
-                            this.sqlQuery = this.sqlQuery.trim() + `\nLIMIT ${this.selectedLimit};`;
1193
+                            this.sqlQuery = this.sqlQuery.trim() + `${prefix}LIMIT ${this.selectedLimit};`;
1189 1194
                         }
1190 1195
                     }
1191 1196
 
@@ -1499,6 +1504,51 @@
1499 1504
                     let insertBefore = '';
1500 1505
                     
1501 1506
                     switch (clauseUpper) {
1507
+                        case 'WHERE':
1508
+                            // WHERE应该在FROM之后,GROUP BY/HAVING/ORDER BY/LIMIT之前
1509
+                            if (sqlParts.from > -1) {
1510
+                                // 找到FROM子句后的位置
1511
+                                const fromEndPos = this.findClauseEndPosition(sqlUpper, sqlParts.from);
1512
+                                insertPosition = fromEndPos;
1513
+                                
1514
+                                // 如果有LIMIT子句,先处理换行
1515
+                                if (sqlParts.limit > -1) {
1516
+                                    // 检查LIMIT前是否有换行
1517
+                                    const limitLineStart = this.sqlQuery.lastIndexOf('\n', sqlParts.limit);
1518
+                                    if (limitLineStart === -1 || limitLineStart < fromEndPos) {
1519
+                                        // 在LIMIT前插入换行符
1520
+                                        const beforeLimit = this.sqlQuery.substring(0, sqlParts.limit).trimEnd();
1521
+                                        const afterLimit = this.sqlQuery.substring(sqlParts.limit);
1522
+                                        this.sqlQuery = beforeLimit + '\n' + afterLimit;
1523
+                                        // 更新LIMIT位置
1524
+                                        sqlParts.limit = beforeLimit.length + 1;
1525
+                                    }
1526
+                                }
1527
+                            } else if (sqlParts.groupBy > -1) {
1528
+                                // 如果没有FROM但有GROUP BY,插入在GROUP BY之前
1529
+                                insertPosition = sqlParts.groupBy;
1530
+                                insertBefore = 'GROUP BY';
1531
+                            } else if (sqlParts.having > -1) {
1532
+                                // 如果没有GROUP BY但有HAVING,插入在HAVING之前
1533
+                                insertPosition = sqlParts.having;
1534
+                                insertBefore = 'HAVING';
1535
+                            } else if (sqlParts.orderBy > -1) {
1536
+                                // 如果没有HAVING但有ORDER BY,插入在ORDER BY之前
1537
+                                insertPosition = sqlParts.orderBy;
1538
+                                insertBefore = 'ORDER BY';
1539
+                            } else if (sqlParts.limit > -1) {
1540
+                                // 如果只有LIMIT,插入在LIMIT之前
1541
+                                insertPosition = sqlParts.limit;
1542
+                                insertBefore = 'LIMIT';
1543
+                            } else {
1544
+                                // 如果没有以上子句,插入在查询末尾(可能有分号)
1545
+                                insertPosition = this.sqlQuery.length;
1546
+                                if (this.sqlQuery.trim().endsWith(';')) {
1547
+                                    insertPosition = this.sqlQuery.lastIndexOf(';');
1548
+                                }
1549
+                            }
1550
+                            break;
1551
+                            
1502 1552
                         case 'GROUP BY':
1503 1553
                             // GROUP BY应该在WHERE之后,HAVING/ORDER BY/LIMIT之前
1504 1554
                             if (sqlParts.where > -1) {
@@ -1578,21 +1628,46 @@
1578 1628
                         let newQuery = '';
1579 1629
                         if (insertBefore) {
1580 1630
                             // 在特定子句之前插入
1581
-                            const beforeInsert = this.sqlQuery.substring(0, insertPosition);
1631
+                            const beforeInsert = this.sqlQuery.substring(0, insertPosition).trimEnd();
1582 1632
                             const afterInsert = this.sqlQuery.substring(insertPosition);
1583 1633
                             
1634
+                            // 确保只有一个换行符
1635
+                            const hasNewline = beforeInsert.endsWith('\n');
1636
+                            const prefix = hasNewline ? '' : '\n';
1637
+                            
1584 1638
                             // 添加换行符和子句
1585
-                            newQuery = beforeInsert + '\n' + clause + ' ' + afterInsert;
1639
+                            newQuery = beforeInsert + prefix + clause + ' ' + afterInsert;
1586 1640
                         } else {
1587 1641
                             // 在子句末尾插入
1588
-                            const beforeInsert = this.sqlQuery.substring(0, insertPosition);
1642
+                            const beforeInsert = this.sqlQuery.substring(0, insertPosition).trimEnd();
1589 1643
                             const afterInsert = this.sqlQuery.substring(insertPosition);
1590 1644
                             
1645
+                            // 确保只有一个换行符
1646
+                            const hasNewline = beforeInsert.endsWith('\n');
1647
+                            const prefix = hasNewline ? '' : '\n';
1648
+                            
1591 1649
                             // 添加换行符和子句
1592
-                            newQuery = beforeInsert + '\n' + clause + ' ' + afterInsert;
1650
+                            newQuery = beforeInsert + prefix + clause + ' ' + afterInsert;
1593 1651
                         }
1594 1652
                         
1595 1653
                         this.sqlQuery = newQuery;
1654
+                        
1655
+                        // 对所有子句,确保LIMIT在新行
1656
+                        // 查找LIMIT位置(可能已改变)
1657
+                        const newSqlUpper = this.sqlQuery.toUpperCase();
1658
+                        const newLimitPos = newSqlUpper.indexOf('LIMIT');
1659
+                        
1660
+                        if (newLimitPos > -1) {
1661
+                            // 检查LIMIT前是否有换行
1662
+                            const beforeLimit = this.sqlQuery.substring(0, newLimitPos).trimEnd();
1663
+                            const afterLimit = this.sqlQuery.substring(newLimitPos);
1664
+                            
1665
+                            // 确保LIMIT前有换行
1666
+                            if (!beforeLimit.endsWith('\n')) {
1667
+                                this.sqlQuery = beforeLimit + '\n' + afterLimit;
1668
+                            }
1669
+                        }
1670
+                        
1596 1671
                         this.showToastMessage(`已插入 "${clause}" 子句到查询`, 'success');
1597 1672
                         
1598 1673
                         // 设置光标位置到插入的子句之后