kylx365_db_admin.html 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  6. <title>数据库管理</title>
  7. <script src="https://kylx365-1253256735.file.myqcloud.com/js/jquery-1.10.2.min.js"></script>
  8. <script src="https://kylx365-1253256735.file.myqcloud.com/js/vue.min.js"></script>
  9. <style>
  10. /* 表格项样式 */
  11. .table-item {
  12. padding: 10px;
  13. margin: 5px 0;
  14. background: white;
  15. border-radius: 4px;
  16. cursor: pointer;
  17. transition: background-color 0.2s;
  18. }
  19. .table-comment {
  20. font-size: 12px;
  21. color: #999;
  22. margin-top: 2px;
  23. }
  24. .main00 {
  25. width: 100%;
  26. height: 100vh;
  27. min-height: 600px;
  28. background: white;
  29. display: flex;
  30. flex-direction: column;
  31. overflow: hidden;
  32. }
  33. .ListTop {
  34. width: 100%;
  35. height: 60px;
  36. background: white;
  37. border-bottom: 1px solid #EEEEEE;
  38. justify-content: flex-start;
  39. flex-shrink: 0;
  40. }
  41. .ListTop3 {
  42. margin-left: 40px;
  43. height: 50px;
  44. align-items: center;
  45. }
  46. .title {
  47. font-size: 24px;
  48. color: #333333;
  49. font-weight: bold;
  50. }
  51. .main0 {
  52. width: 100%;
  53. background: white;
  54. flex: 1;
  55. min-height: 0;
  56. overflow: hidden;
  57. display: flex;
  58. position: relative;
  59. align-items: flex-start;
  60. }
  61. .tables-panel {
  62. width: 300px;
  63. border-right: 1px solid #EEEEEE;
  64. background: #F9F9F9;
  65. display: flex;
  66. flex-direction: column;
  67. height: 100%;
  68. position: relative;
  69. flex-shrink: 0;
  70. }
  71. .search-box {
  72. padding: 15px 0;
  73. border-bottom: 1px solid #EEEEEE;
  74. background: white;
  75. width: 100%;
  76. justify-content: center;
  77. width: 100%;
  78. }
  79. .search-input {
  80. width: 200px;
  81. height: 32px;
  82. padding: 0 32px 0 12px;
  83. border: 1px solid #DDDDDD;
  84. border-radius: 4px;
  85. font-size: 14px;
  86. color: #333333;
  87. }
  88. .search-input:focus {
  89. border-color: #4A90E2;
  90. outline: none;
  91. }
  92. .btn33 {
  93. width: 32px;
  94. height: 32px;
  95. margin-left: 8px;
  96. border-radius: 4px;
  97. background: #F5F5F5;
  98. cursor: pointer;
  99. justify-content: center;
  100. align-items: center;
  101. }
  102. .btn33:hover {
  103. background: #EEEEEE;
  104. }
  105. .content-panel {
  106. width: calc(100% - 300px);
  107. height: 100%;
  108. display: flex;
  109. flex-direction: column;
  110. overflow: hidden;
  111. }
  112. .sql-editor {
  113. padding: 20px;
  114. border-bottom: 1px solid #EEEEEE;
  115. background: white;
  116. height: 200px;
  117. display: flex;
  118. flex-direction: row;
  119. }
  120. .sql-editor-left {
  121. flex: 1;
  122. display: flex;
  123. flex-direction: column;
  124. margin-right: 20px;
  125. }
  126. .sql-editor-right {
  127. width: 400px;
  128. display: flex;
  129. flex-direction: column;
  130. border-left: 1px solid #EEEEEE;
  131. padding-left: 20px;
  132. position: relative;
  133. }
  134. .columns-list {
  135. flex: 1;
  136. overflow-y: auto;
  137. border: 1px solid #DDDDDD;
  138. border-radius: 4px;
  139. background: #FFFFFF;
  140. }
  141. .column-item {
  142. padding: 8px 10px;
  143. border-bottom: 1px solid #EEEEEE;
  144. cursor: pointer;
  145. display: flex;
  146. flex-direction: row;
  147. justify-content: space-between;
  148. }
  149. .column-item:hover {
  150. background-color: #F0F7FF;
  151. }
  152. .column-left {
  153. display: flex;
  154. flex-direction: column;
  155. flex: 1;
  156. }
  157. .column-right {
  158. display: flex;
  159. align-items: center;
  160. min-width: 100px;
  161. padding-left: 10px;
  162. }
  163. .column-name {
  164. font-weight: bold;
  165. }
  166. .column-type {
  167. font-size: 13px;
  168. color: #666;
  169. background-color: #f5f5f5;
  170. padding: 2px 6px;
  171. border-radius: 3px;
  172. white-space: nowrap;
  173. }
  174. .column-comment {
  175. font-size: 12px;
  176. color: #999;
  177. margin-top: 2px;
  178. }
  179. .sql-textarea {
  180. width: 100%;
  181. height: 120px;
  182. padding: 10px;
  183. border: 1px solid #DDDDDD;
  184. border-radius: 4px;
  185. font-family: monospace;
  186. font-size: 14px;
  187. resize: none;
  188. margin-bottom: 10px;
  189. }
  190. .sql-textarea:focus {
  191. border-color: #4A90E2;
  192. outline: none;
  193. }
  194. .results-panel {
  195. flex: 1;
  196. overflow: auto;
  197. padding: 20px;
  198. background: white;
  199. position: relative;
  200. }
  201. .table-container {
  202. width: 100%;
  203. overflow-x: auto;
  204. max-height: calc(100vh - 380px); /* 减去其他元素的高度 */
  205. overflow-y: auto;
  206. position: relative; /* 为内部元素提供定位上下文 */
  207. }
  208. .data-table {
  209. width: 100%;
  210. border-collapse: separate; /* 改为separate以支持边框样式 */
  211. border-spacing: 0; /* 消除单元格间距 */
  212. font-size: 14px;
  213. }
  214. .data-table th {
  215. background: #F5F5F5;
  216. padding: 10px;
  217. text-align: left;
  218. border: 1px solid #DDDDDD;
  219. position: sticky;
  220. top: 0;
  221. z-index: 10;
  222. box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
  223. }
  224. /* 确保表头单元格背景色完整覆盖 */
  225. .data-table thead th {
  226. background: #F5F5F5;
  227. }
  228. /* 优化表头在滚动时的视觉效果 */
  229. .table-container:not(:hover) .data-table th {
  230. transition: box-shadow 0.3s ease;
  231. }
  232. .table-container:hover .data-table th {
  233. box-shadow: 0 3px 5px -2px rgba(0, 0, 0, 0.2);
  234. }
  235. .data-table td {
  236. padding: 8px 10px;
  237. border: 1px solid #DDDDDD;
  238. max-width: 300px;
  239. overflow: hidden;
  240. text-overflow: ellipsis;
  241. white-space: nowrap;
  242. }
  243. .data-table tr:nth-child(even) {
  244. background-color: #F9F9F9;
  245. }
  246. .data-table tr:hover {
  247. background-color: #F0F7FF;
  248. }
  249. .btn {
  250. min-width: 30px;
  251. height: 36px;
  252. padding: 0 16px;
  253. margin-left: 12px;
  254. border: none;
  255. border-radius: 4px;
  256. cursor: pointer;
  257. font-size: 14px;
  258. transition: all 0.3s;
  259. }
  260. .btn-primary {
  261. background: #4A90E2;
  262. color: white;
  263. }
  264. .btn-primary:hover {
  265. background: #357ABD;
  266. }
  267. .btn-default {
  268. background: white;
  269. border: 1px solid #DDDDDD;
  270. color: #666666;
  271. }
  272. .btn-default:hover {
  273. background: #F5F5F5;
  274. border-color: #CCCCCC;
  275. }
  276. .toast {
  277. position: fixed;
  278. top: 20px;
  279. left: 50%;
  280. transform: translateX(-50%);
  281. padding: 12px 24px;
  282. background: rgba(0, 0, 0, 0.7);
  283. color: white;
  284. border-radius: 4px;
  285. z-index: 9999;
  286. opacity: 0;
  287. transition: opacity 0.3s;
  288. }
  289. .toast.show {
  290. opacity: 1;
  291. }
  292. .toast.success {
  293. background: #4CAF50;
  294. }
  295. .toast.error {
  296. background: #F44336;
  297. }
  298. .toast.info {
  299. background: #2196F3;
  300. }
  301. /* Loading样式 */
  302. .loading-overlay {
  303. position: absolute;
  304. top: 0;
  305. left: 0;
  306. width: 100%;
  307. height: 100%;
  308. background-color: rgba(255, 255, 255, 0.7);
  309. display: flex;
  310. justify-content: center;
  311. align-items: center;
  312. z-index: 100;
  313. }
  314. .loading-spinner {
  315. width: 50px;
  316. height: 50px;
  317. border: 5px solid #f3f3f3;
  318. border-top: 5px solid #4A90E2;
  319. border-radius: 50%;
  320. animation: spin 1s linear infinite;
  321. }
  322. @keyframes spin {
  323. 0% {
  324. transform: rotate(0deg);
  325. }
  326. 100% {
  327. transform: rotate(360deg);
  328. }
  329. }
  330. .clear-btn {
  331. position: absolute;
  332. right: 12px;
  333. top: 50%;
  334. transform: translateY(-50%);
  335. width: 16px;
  336. height: 16px;
  337. background-color: #999;
  338. border-radius: 50%;
  339. display: flex;
  340. align-items: center;
  341. justify-content: center;
  342. cursor: pointer;
  343. transition: background-color 0.2s;
  344. }
  345. .clear-btn:hover {
  346. background-color: #666;
  347. }
  348. .clear-x {
  349. position: relative;
  350. width: 8px;
  351. height: 8px;
  352. }
  353. .clear-x:before,
  354. .clear-x:after {
  355. content: '';
  356. position: absolute;
  357. width: 8px;
  358. height: 2px;
  359. background-color: white;
  360. top: 3px;
  361. left: 0;
  362. }
  363. .clear-x:before {
  364. transform: rotate(45deg);
  365. }
  366. .clear-x:after {
  367. transform: rotate(-45deg);
  368. }
  369. .FlexRow {
  370. display: flex;
  371. flex-direction: row;
  372. }
  373. .FlexColumn {
  374. display: flex;
  375. flex-direction: column;
  376. }
  377. .table-list {
  378. flex: 1;
  379. overflow-y: auto;
  380. padding: 10px;
  381. }
  382. .table-item {
  383. padding: 10px;
  384. margin: 5px 0;
  385. background: white;
  386. border-radius: 4px;
  387. cursor: pointer;
  388. overflow-x: hidden;
  389. transition: background-color 0.2s;
  390. }
  391. .table-item:hover {
  392. background-color: #f0f0f0;
  393. }
  394. .table-item.active {
  395. background-color: #e6f3ff;
  396. border-left: 3px solid #4A90E2;
  397. }
  398. .btn-group {
  399. display: flex;
  400. justify-content: flex-end;
  401. margin-top: 10px;
  402. }
  403. .no-data {
  404. text-align: center;
  405. padding: 40px;
  406. color: #999;
  407. font-size: 16px;
  408. }
  409. .pagination {
  410. display: flex;
  411. justify-content: center;
  412. margin-top: 20px;
  413. padding: 10px;
  414. }
  415. .pagination-btn {
  416. padding: 5px 10px;
  417. margin: 0 5px;
  418. border: 1px solid #DDDDDD;
  419. border-radius: 4px;
  420. background: white;
  421. cursor: pointer;
  422. }
  423. .pagination-btn:hover {
  424. background: #F5F5F5;
  425. }
  426. .pagination-btn.active {
  427. background: #4A90E2;
  428. color: white;
  429. border-color: #4A90E2;
  430. }
  431. .pagination-btn.disabled {
  432. color: #CCCCCC;
  433. cursor: not-allowed;
  434. }
  435. /* 完整内容弹出层样式 - 改进版 */
  436. .complete-content-overlay {
  437. position: fixed;
  438. top: 0;
  439. left: 0;
  440. right: 0;
  441. bottom: 0;
  442. display: flex;
  443. justify-content: center;
  444. align-items: center;
  445. z-index: 1000;
  446. background-color: rgba(0, 0, 0, 0.5);
  447. pointer-events: auto;
  448. }
  449. .complete-content-container {
  450. width: 80%;
  451. max-width: 800px;
  452. max-height: 80vh;
  453. background-color: white;
  454. border-radius: 8px;
  455. padding: 20px;
  456. padding-top: 50px; /* 为固定的关闭按钮留出空间 */
  457. position: relative;
  458. box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  459. overflow: auto;
  460. animation: fadeIn 0.3s ease-out;
  461. }
  462. @keyframes fadeIn {
  463. from {
  464. opacity: 0;
  465. transform: translateY(20px);
  466. }
  467. to {
  468. opacity: 1;
  469. transform: translateY(0);
  470. }
  471. }
  472. .close-btn {
  473. position: absolute;
  474. right: 15px; /* 根据弹窗宽度计算位置 */
  475. top: 15px; /* 根据弹窗位置计算 */
  476. z-index: 1010; /* 确保按钮在最上层 */
  477. width: 40px;
  478. height: 40px;
  479. display: flex;
  480. align-items: center;
  481. justify-content: center;
  482. background: rgba(255, 255, 255, 0.9);
  483. border: none;
  484. border-radius: 50%;
  485. font-size: 24px;
  486. cursor: pointer;
  487. color: #999;
  488. box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  489. }
  490. .close-btn:hover {
  491. color: #333;
  492. background: #fff;
  493. }
  494. .complete-content {
  495. white-space: pre-wrap;
  496. word-break: break-word;
  497. font-family: monospace;
  498. line-height: 1.5;
  499. }
  500. </style>
  501. </head>
  502. <body class="container FlexRow">
  503. <div id="app" class="main00 FlexColumn">
  504. <div class="ListTop FlexRow">
  505. <div class="ListTop3 FlexRow" style="width: 100%; justify-content: space-between;">
  506. <div class="title">数据库管理</div>
  507. </div>
  508. </div>
  509. <div class="main0 FlexRow">
  510. <!-- 左侧表格列表 -->
  511. <div class="tables-panel" style="position: relative;">
  512. <div v-if="isTablesLoading" class="loading-overlay">
  513. <div class="loading-spinner"></div>
  514. </div>
  515. <div class="search-box FlexRow">
  516. <div style="position: relative;">
  517. <input type="text" class="search-input" v-model="searchText" @input="searchTables" @keyup.enter="searchTables"
  518. placeholder="搜索表格...">
  519. <div class="clear-btn" v-show="searchText" @click="clearSearch">
  520. <span class="clear-x"></span>
  521. </div>
  522. </div>
  523. <div class="btn33 FlexRow" @click="searchTables">
  524. <img title="搜索" alt="搜索"
  525. src="https://kylx365-1253256735.file.myqcloud.com/web/universalpic_search_gray_30x30.png"
  526. style="width: 20px; height: 20px;" />
  527. </div>
  528. </div>
  529. <!-- 表格列表 -->
  530. <div class="table-list">
  531. <div v-if="tables && tables.length > 0">
  532. <div v-for="(table, index) in filteredTables" :key="index" @click="selectTable(table)"
  533. :class="['table-item', { active: selectedTable === table }]">
  534. <div class="table-name">{{ table }}</div>
  535. <div class="table-comment" v-if="tableComments[table]">{{ tableComments[table] }}</div>
  536. </div>
  537. <div v-if="filteredTables.length === 0" class="no-data">
  538. 没有匹配的表格
  539. </div>
  540. </div>
  541. <div v-else-if="tables && tables.length === 0" class="no-data">
  542. 未找到表格
  543. </div>
  544. <div v-else class="no-data">
  545. 加载中...
  546. </div>
  547. </div>
  548. </div>
  549. <!-- 右侧内容区域 -->
  550. <div class="content-panel">
  551. <!-- SQL编辑器 -->
  552. <div class="sql-editor">
  553. <!-- 左侧SQL编辑区域 -->
  554. <div class="sql-editor-left">
  555. <textarea class="sql-textarea" v-model="sqlQuery" placeholder="输入SQL查询语句..."></textarea>
  556. <div class="btn-group" style="justify-content: flex-start;">
  557. <button type="button" class="btn btn-default" @click="insertSqlClause('GROUP BY')">GROUP BY</button>
  558. <button type="button" class="btn btn-default" @click="insertSqlClause('HAVING')">HAVING</button>
  559. <button type="button" class="btn btn-default" @click="insertSqlClause('ORDER BY')">ORDER BY</button>
  560. <select class="btn btn-default" v-model="selectedLimit" @change="updateQueryLimit"
  561. style="margin-left: 8px;">
  562. <option v-for="limit in limitOptions" :key="limit" :value="limit">
  563. LIMIT {{ limit }}
  564. </option>
  565. </select>
  566. <div style="margin-left: auto;">
  567. <button type="button" class="btn btn-default" @click="clearQuery">清空</button>
  568. <button type="button" class="btn btn-primary" @click="executeQuery">执行</button>
  569. </div>
  570. </div>
  571. </div>
  572. <!-- 右侧字段列表 -->
  573. <div class="sql-editor-right">
  574. <div v-if="isColumnsLoading" class="loading-overlay">
  575. <div class="loading-spinner"></div>
  576. </div>
  577. <div class="columns-list">
  578. <div v-if="tableColumnsList && tableColumnsList.length > 0">
  579. <div v-for="(column, index) in tableColumnsList" :key="index" class="column-item"
  580. @click="copyColumnName(column.name)" @dblclick="insertColumnName(column.name)">
  581. <div class="column-left">
  582. <div class="column-name">{{ column.name }}</div>
  583. <div class="column-comment" v-if="column.comment">{{ column.comment }}</div>
  584. </div>
  585. <div class="column-right">
  586. <div class="column-type">{{ column.type }}</div>
  587. </div>
  588. </div>
  589. </div>
  590. <div v-else class="no-data">
  591. 请选择表格查看字段列表
  592. </div>
  593. </div>
  594. </div>
  595. </div>
  596. <!-- 查询结果 -->
  597. <div class="results-panel" style="position: relative;">
  598. <div v-if="isQueryLoading" class="loading-overlay">
  599. <div class="loading-spinner"></div>
  600. </div>
  601. <div v-if="queryExecuted" class="table-container">
  602. <div v-if="allResults && allResults.length > 0">
  603. <div style="margin-bottom: 10px; color: #666;">
  604. 显示 {{ queryResults.length }} 条记录,共 {{ allResults.length }} 条
  605. </div>
  606. <div v-if="isQueryLoading" class="loading-indicator">
  607. <div class="spinner"></div>
  608. <div>数据加载中...</div>
  609. </div>
  610. <div v-else-if="!tableColumns || tableColumns.length === 0" class="no-data">
  611. 未检测到有效的表格列
  612. </div>
  613. <template v-else>
  614. <table class="data-table">
  615. <thead>
  616. <tr>
  617. <th v-for="(column, index) in tableColumns"
  618. :key="'th-'+index"
  619. v-show="!column.startsWith('__')">
  620. {{ column }}
  621. </th>
  622. </tr>
  623. </thead>
  624. <tbody>
  625. <tr v-for="(row, rowIndex) in queryResults"
  626. :key="'tr-'+rowIndex">
  627. <td v-for="(column, colIndex) in tableColumns"
  628. :key="'td-'+rowIndex+'-'+colIndex"
  629. v-show="!column.startsWith('__')"
  630. @dblclick="formatTableCell(row[column]).endsWith('...') ? showCompleteField(row, column) : copyColumnName(formatTableCell(row[column]))"
  631. >
  632. {{ formatTableCell(row[column]) }}
  633. </td>
  634. </tr>
  635. </tbody>
  636. </table>
  637. <div v-if="queryResults.length === 0" class="no-data">
  638. 没有可显示的数据
  639. <div v-if="allResults.length > 0" style="margin-top: 10px; font-size: 12px; color: #999;">
  640. 共 {{allResults.length}} 条数据
  641. </div>
  642. </div>
  643. </template>
  644. </div>
  645. <div v-else class="no-data">
  646. 查询未返回数据
  647. </div>
  648. </div>
  649. <div v-else-if="queryExecuted" class="no-data">
  650. 查询未返回数据
  651. </div>
  652. <div v-else class="no-data">
  653. 请选择表格并执行查询
  654. </div>
  655. </div>
  656. </div>
  657. </div>
  658. <!-- Toast提示 -->
  659. <div class="toast" :class="{ show: showToast, [toastType]: showToast }">{{ toastMessage }}</div>
  660. <!-- 完整内容弹出层 -->
  661. <div v-if="showCompleteContent" class="complete-content-overlay" @click.self="closeCompleteContent">
  662. <div class="complete-content-container">
  663. <button class="close-btn" @click="closeCompleteContent">×</button>
  664. <div class="complete-content" @dblclick="copyToClipboard(completeContent)">
  665. {{ completeContent }}
  666. </div>
  667. </div>
  668. </div>
  669. </div>
  670. <script>
  671. new Vue({
  672. el: '#app',
  673. data: {
  674. searchText: '',
  675. tables: [], // 存储表格名称列表
  676. tableComments: {}, // 存储表格注释,格式为 {表名: 注释}
  677. filteredTables: [],
  678. selectedTable: null,
  679. sqlQuery: '',
  680. queryResults: [],
  681. tableColumns: [],
  682. showToast: false,
  683. toastMessage: '',
  684. toastType: 'info',
  685. isTablesLoading: false,
  686. isQueryLoading: false,
  687. isColumnsLoading: false,
  688. queryExecuted: false,
  689. allResults: [],
  690. tableColumnsList: [], // 存储表格字段列表
  691. limitOptions: [10, 100, 500, 1000, 2000, 3000], // LIMIT选项
  692. selectedLimit: 100, // 默认选中的LIMIT值
  693. showCompleteContent: false,
  694. completeContent: '',
  695. currentRow: null,
  696. currentColumn: null
  697. },
  698. computed: {
  699. },
  700. mounted() {
  701. this.loadTables();
  702. },
  703. methods: {
  704. // 加载数据库表格列表
  705. loadTables() {
  706. this.isTablesLoading = true;
  707. this.tableComments = {};
  708. // 调用后端API获取表格列表
  709. fetch('/api/GetKylx365Tables')
  710. .then(response => {
  711. // console.log('API响应状态:', response.status);
  712. if (!response.ok) {
  713. throw new Error(`HTTP error! status: ${response.status}`);
  714. }
  715. return response.json();
  716. })
  717. .then(data => {
  718. //console.log('API返回数据:', data);
  719. // 检查数据格式,处理可能的不同响应结构
  720. //console.log('检查数据格式:', data);
  721. // 尝试确定数据的实际结构
  722. let resultData = null;
  723. if (data && data.result && Array.isArray(data.result)) {
  724. //console.log('标准格式: data.result 是数组');
  725. resultData = data.result;
  726. } else if (data && Array.isArray(data)) {
  727. //console.log('替代格式: data 本身是数组');
  728. resultData = data;
  729. } else if (data && typeof data === 'object') {
  730. //console.log('检查对象中的数组属性');
  731. // 尝试在对象中找到数组属性
  732. for (const key in data) {
  733. if (Array.isArray(data[key])) {
  734. //console.log(`找到数组属性: ${key}`);
  735. resultData = data[key];
  736. break;
  737. }
  738. }
  739. }
  740. if (resultData && Array.isArray(resultData)) {
  741. //console.log('数据验证通过,开始处理数据');
  742. //console.log('数据结果长度:', resultData.length);
  743. if (resultData.length > 0 && typeof resultData[0] === 'object' && !Array.isArray(resultData[0])) {
  744. //console.log('检测到对象数组格式,第一个对象:', resultData[0]);
  745. //console.log('第一个对象的属性:', Object.keys(resultData[0]));
  746. // 检查并适应不同的属性名称
  747. const firstItem = resultData[0];
  748. const tableNameKey = 'table_name' in firstItem ? 'table_name' :
  749. 'tableName' in firstItem ? 'tableName' :
  750. 'name' in firstItem ? 'name' : null;
  751. const tableCommentKey = 'table_comment' in firstItem ? 'table_comment' :
  752. 'tableComment' in firstItem ? 'tableComment' :
  753. 'comment' in firstItem ? 'comment' : null;
  754. //console.log('使用的属性名:', { tableNameKey, tableCommentKey });
  755. if (tableNameKey) {
  756. // 如果返回的是对象数组(包含name和comment)
  757. this.tables = resultData.map(item => {
  758. const tableName = item[tableNameKey] || '';
  759. //console.log('处理表名:', tableName);
  760. return tableName;
  761. }).filter(name => name); // 过滤掉空表名
  762. //console.log('处理后的表格列表:', this.tables);
  763. // 存储表格注释
  764. if (tableCommentKey) {
  765. resultData.forEach(item => {
  766. const tableName = item[tableNameKey] || '';
  767. const tableComment = item[tableCommentKey] || '';
  768. if (tableName && tableComment) {
  769. this.tableComments[tableName] = tableComment;
  770. }
  771. });
  772. //console.log('处理后的表格注释:', this.tableComments);
  773. }
  774. } else {
  775. //console.error('无法找到表名属性');
  776. this.showToastMessage('数据格式错误:无法找到表名属性', 'error');
  777. }
  778. } else {
  779. //console.log('检测到简单数组格式');
  780. // 如果返回的只是表名数组
  781. this.tables = resultData.filter(name => {
  782. //console.log('处理表名:', name);
  783. return name && typeof name === 'string';
  784. });
  785. //console.log('处理后的表格列表:', this.tables);
  786. }
  787. this.filteredTables = [...this.tables];
  788. // console.log('更新后的过滤表格列表:', this.filteredTables);
  789. } else {
  790. this.tables = [];
  791. this.filteredTables = [];
  792. this.showToastMessage('获取表格列表失败', 'error');
  793. }
  794. this.isTablesLoading = false;
  795. })
  796. .catch(error => {
  797. //console.error('获取表格列表失败详细错误:', error);
  798. this.showToastMessage(`获取表格列表失败: ${error.message}`, 'error');
  799. this.isTablesLoading = false;
  800. });
  801. },
  802. // 搜索表格
  803. searchTables() {
  804. //console.log('开始搜索表格,搜索文本:', this.searchText);
  805. if (!this.searchText.trim()) {
  806. //console.log('搜索文本为空,显示所有表格');
  807. this.filteredTables = [...this.tables];
  808. return;
  809. }
  810. const searchTerm = this.searchText.toLowerCase();
  811. //console.log('过滤表格列表,搜索条件:', searchTerm);
  812. this.filteredTables = this.tables.filter(table => {
  813. const tableName = table.toLowerCase();
  814. const tableComment = (this.tableComments[table] || '').toLowerCase();
  815. const matches = tableName.includes(searchTerm) || tableComment.includes(searchTerm);
  816. // if (matches) {
  817. // console.log(`表格匹配: ${table} (${this.tableComments[table] || '无注释'})`);
  818. // }
  819. return matches;
  820. });
  821. //console.log('过滤后的表格数量:', this.filteredTables.length);
  822. },
  823. // 清空搜索
  824. clearSearch() {
  825. this.searchText = '';
  826. this.filteredTables = [...this.tables];
  827. },
  828. // 选择表格
  829. selectTable(table) {
  830. this.selectedTable = table;
  831. // 重置LIMIT值为默认的100
  832. this.selectedLimit = 100;
  833. // 获取表格字段列表
  834. this.loadTableColumns(table);
  835. // 监听字段列表变化,当加载完成后构建查询语句
  836. this.$watch('tableColumnsList', (newVal) => {
  837. if (newVal && newVal.length > 0) {
  838. const columns = newVal.map(col => `\`${col.name}\``).join(',');
  839. this.sqlQuery = `SELECT ${columns}\nFROM \`${table}\`\nLIMIT ${this.selectedLimit};`;
  840. } else {
  841. this.sqlQuery = `SELECT *\nFROM \`${table}\`\nLIMIT ${this.selectedLimit};`;
  842. }
  843. }, {immediate: true});
  844. },
  845. // 加载表格字段列表
  846. loadTableColumns(tableName) {
  847. this.isColumnsLoading = true;
  848. this.tableColumnsList = [];
  849. // 调用API获取表格字段列表
  850. fetch(`/api/GetKylx365TableColumnByTable?table=${encodeURIComponent(tableName)}`)
  851. .then(response => response.json())
  852. .then(data => {
  853. //console.log('收到的原始数据:', data);
  854. // 验证数据格式
  855. if (!data) {
  856. //console.error('接收到空数据');
  857. throw new Error('接收到空数据');
  858. }
  859. // 尝试确定数据的实际结构
  860. let columnsData = null;
  861. if (data && data.result && Array.isArray(data.result)) {
  862. //console.log('标准格式: data.result 是数组');
  863. columnsData = data.result;
  864. } else if (data && Array.isArray(data)) {
  865. //console.log('替代格式: data 本身是数组');
  866. columnsData = data;
  867. } else if (data && typeof data === 'object') {
  868. //console.log('检查对象中的数组属性');
  869. // 尝试在对象中找到数组属性
  870. for (const key in data) {
  871. if (Array.isArray(data[key])) {
  872. //console.log(`找到数组属性: ${key}`);
  873. columnsData = data[key];
  874. break;
  875. }
  876. }
  877. }
  878. if (columnsData && Array.isArray(columnsData)) {
  879. //console.log('数据验证通过,开始处理数据');
  880. //console.log('字段数据长度:', columnsData.length);
  881. if (columnsData.length > 0) {
  882. // 检查第一个元素的格式
  883. const firstItem = columnsData[0];
  884. //console.log('第一个字段项:', firstItem);
  885. if (typeof firstItem === 'object' && !Array.isArray(firstItem)) {
  886. // 检查并适应不同的属性名称
  887. const nameKey = 'Field' in firstItem ? 'Field' :
  888. 'name' in firstItem ? 'name' :
  889. 'column_name' in firstItem ? 'column_name' :
  890. 'columnName' in firstItem ? 'columnName' :
  891. 'field' in firstItem ? 'field' : null;
  892. const typeKey = 'Type' in firstItem ? 'Type' :
  893. 'type' in firstItem ? 'type' :
  894. 'column_type' in firstItem ? 'column_type' :
  895. 'columnType' in firstItem ? 'columnType' :
  896. 'data_type' in firstItem ? 'data_type' : null;
  897. const commentKey = 'Comment' in firstItem ? 'Comment' :
  898. 'comment' in firstItem ? 'comment' :
  899. 'column_comment' in firstItem ? 'column_comment' :
  900. 'columnComment' in firstItem ? 'columnComment' : null;
  901. // 组合Default和Extra作为注释
  902. const getComment = (item) => {
  903. let comment = [];
  904. if (item['Default']) {
  905. comment.push(`默认值: ${item['Default']}`);
  906. }
  907. if (item['Extra'] && item['Extra'] !== '') {
  908. comment.push(item['Extra']);
  909. }
  910. if (item['Null'] === 'NO') {
  911. comment.push('不可为空');
  912. }
  913. return comment.join(', ');
  914. };
  915. //console.log('使用的属性名:', { nameKey, typeKey, commentKey });
  916. if (nameKey) {
  917. // 转换为标准格式
  918. this.tableColumnsList = columnsData.map(item => {
  919. return {
  920. name: item[nameKey] || '',
  921. type: typeKey ? (item[typeKey] || '') : '',
  922. comment: [
  923. commentKey ? (item[commentKey] || '') : '',
  924. getComment(item)
  925. ].filter(Boolean).join(' | ')
  926. };
  927. }).filter(col => col.name); // 过滤掉没有名称的列
  928. //console.log('处理后的字段列表:', this.tableColumnsList);
  929. } else {
  930. console.error('无法找到字段名属性');
  931. this.showToastMessage('数据格式错误:无法找到字段名属性', 'error');
  932. this.tableColumnsList = [];
  933. }
  934. } else if (typeof firstItem === 'string') {
  935. // 如果只是字段名数组
  936. this.tableColumnsList = columnsData.map(name => {
  937. return {
  938. name: name,
  939. type: '',
  940. comment: ''
  941. };
  942. });
  943. //console.log('处理后的字段列表(仅名称):', this.tableColumnsList);
  944. } else {
  945. console.error('未知的字段数据格式');
  946. this.showToastMessage('未知的字段数据格式', 'error');
  947. this.tableColumnsList = [];
  948. }
  949. } else {
  950. //console.log('字段列表为空');
  951. this.tableColumnsList = [];
  952. }
  953. } else {
  954. //console.error('无法找到有效的字段数据');
  955. this.tableColumnsList = [];
  956. this.showToastMessage('获取字段列表失败', 'error');
  957. }
  958. this.isColumnsLoading = false;
  959. })
  960. .catch(error => {
  961. this.showToastMessage('获取字段列表失败,请稍后重试', 'error');
  962. this.isColumnsLoading = false;
  963. });
  964. },
  965. // 清空查询
  966. clearQuery() {
  967. this.sqlQuery = '';
  968. },
  969. // 复制字段名称到剪贴板
  970. copyColumnName(columnName) {
  971. // 创建一个临时文本区域元素
  972. const textarea = document.createElement('textarea');
  973. textarea.value = columnName;
  974. textarea.setAttribute('readonly', '');
  975. textarea.style.position = 'absolute';
  976. textarea.style.left = '-9999px';
  977. document.body.appendChild(textarea);
  978. // 选择文本并复制
  979. textarea.select();
  980. let success = false;
  981. try {
  982. success = document.execCommand('copy');
  983. this.showToastMessage(`已复制字段 "${columnName}" 到剪贴板`, 'success');
  984. } catch (err) {
  985. success = false;
  986. this.showToastMessage('复制失败,请手动复制', 'error');
  987. console.error('复制到剪贴板失败:', err);
  988. }
  989. // 移除临时元素
  990. document.body.removeChild(textarea);
  991. // 如果浏览器支持现代剪贴板API,也尝试使用它
  992. if (navigator.clipboard && window.isSecureContext) {
  993. navigator.clipboard.writeText(columnName).catch(err => {
  994. console.log('剪贴板API失败,但已使用备用方法', err);
  995. });
  996. }
  997. return success;
  998. },
  999. // 插入字段名称到SQL查询(双击时触发)
  1000. insertColumnName(columnName) {
  1001. // 获取文本框元素
  1002. const textarea = document.querySelector('.sql-textarea');
  1003. // 添加反引号的字段名
  1004. const quotedColumnName = `\`${columnName}\``;
  1005. // 如果文本框存在
  1006. if (textarea) {
  1007. // 获取当前光标位置
  1008. const startPos = textarea.selectionStart;
  1009. const endPos = textarea.selectionEnd;
  1010. // 在光标位置插入字段名称
  1011. const textBefore = this.sqlQuery.substring(0, startPos);
  1012. const textAfter = this.sqlQuery.substring(endPos);
  1013. // 更新SQL查询
  1014. this.sqlQuery = textBefore + quotedColumnName + textAfter;
  1015. // 设置新的光标位置
  1016. this.$nextTick(() => {
  1017. textarea.focus();
  1018. textarea.selectionStart = startPos + quotedColumnName.length;
  1019. textarea.selectionEnd = startPos + quotedColumnName.length;
  1020. });
  1021. this.showToastMessage(`已插入字段 "${columnName}" 到查询`, 'info');
  1022. } else {
  1023. // 如果无法获取文本框元素,则直接在末尾添加
  1024. this.sqlQuery += ' ' + quotedColumnName;
  1025. }
  1026. },
  1027. // 更新SQL查询中的LIMIT值
  1028. updateQueryLimit() {
  1029. // 检查SQL查询是否为空
  1030. if (!this.sqlQuery.trim()) {
  1031. return;
  1032. }
  1033. // 正则表达式匹配LIMIT子句
  1034. const limitRegex = /\bLIMIT\s+\d+\s*;?\s*$/i;
  1035. if (limitRegex.test(this.sqlQuery)) {
  1036. // 如果已经有LIMIT子句,替换数字
  1037. this.sqlQuery = this.sqlQuery.replace(limitRegex, `LIMIT ${this.selectedLimit}${this.sqlQuery.endsWith(';') ? ';' : ''}`);
  1038. } else {
  1039. // 如果没有LIMIT子句,添加到末尾
  1040. // 检查SQL是否以分号结尾
  1041. if (this.sqlQuery.trim().endsWith(';')) {
  1042. // 在分号前添加LIMIT
  1043. this.sqlQuery = this.sqlQuery.replace(/;\s*$/, `\nLIMIT ${this.selectedLimit};`);
  1044. } else {
  1045. // 直接在末尾添加LIMIT
  1046. this.sqlQuery = this.sqlQuery.trim() + `\nLIMIT ${this.selectedLimit};`;
  1047. }
  1048. }
  1049. this.showToastMessage(`已设置查询限制为 ${this.selectedLimit} 条记录`, 'info');
  1050. },
  1051. // 执行查询
  1052. executeQuery() {
  1053. if (!this.sqlQuery.trim()) {
  1054. this.showToastMessage('请输入SQL查询语句', 'info');
  1055. return;
  1056. }
  1057. this.isQueryLoading = true;
  1058. this.queryExecuted = true;
  1059. // 重置查询结果
  1060. this.queryResults = [];
  1061. this.tableColumns = [];
  1062. this.allResults = [];
  1063. // 调用后端API执行SQL查询,使用RunKylx365DBSql接口
  1064. const apiUrl = '/api/RunKylx365DBSql';
  1065. fetch(apiUrl, {
  1066. method: 'POST',
  1067. headers: {
  1068. 'Content-Type': 'application/json',
  1069. 'Accept': 'application/json'
  1070. },
  1071. body: JSON.stringify({
  1072. sql: this.sqlQuery.trim()
  1073. })
  1074. })
  1075. .then(response => {
  1076. if (!response.ok) {
  1077. throw new Error(`HTTP error! status: ${response.status}`);
  1078. }
  1079. return response.json();
  1080. })
  1081. .then(data => {
  1082. // 检查数据格式
  1083. let resultData = null;
  1084. let errorMessage = null;
  1085. // 检查标准格式 {"errcode": 10000, result: [...]}
  1086. if (data && typeof data === 'object' && 'errcode' in data) {
  1087. if (data.errcode !== 10000) {
  1088. errorMessage = data.errmsg || '查询返回错误';
  1089. console.error('查询返回错误:', errorMessage);
  1090. } else if (data.result && Array.isArray(data.result)) {
  1091. resultData = data.result;
  1092. }
  1093. }
  1094. // 检查其他可能的格式
  1095. else if (data && Array.isArray(data)) {
  1096. resultData = data;
  1097. } else if (data && typeof data === 'object') {
  1098. // 尝试在对象中找到数组属性
  1099. for (const key in data) {
  1100. if (Array.isArray(data[key])) {
  1101. resultData = data[key];
  1102. break;
  1103. }
  1104. }
  1105. }
  1106. if (errorMessage) {
  1107. console.error('查询返回错误:', errorMessage);
  1108. this.showToastMessage(errorMessage, 'error');
  1109. this.queryResults = [];
  1110. this.tableColumns = [];
  1111. } else if (resultData && Array.isArray(resultData)) {
  1112. this.processQueryResults(resultData);
  1113. } else {
  1114. this.queryResults = [];
  1115. this.tableColumns = [];
  1116. this.showToastMessage('查询未返回数据', 'info');
  1117. }
  1118. this.isQueryLoading = false;
  1119. })
  1120. .catch(error => {
  1121. this.showToastMessage('执行查询失败,请稍后重试', 'error');
  1122. this.isQueryLoading = false;
  1123. });
  1124. },
  1125. // 处理查询结果
  1126. processQueryResults(results) {
  1127. // 确保结果是一个数组
  1128. if (!results || !Array.isArray(results) || results.length === 0) {
  1129. console.warn('结果为空或非数组');
  1130. this.$set(this, 'queryResults', []);
  1131. this.$set(this, 'tableColumns', []);
  1132. this.$set(this, 'allResults', []);
  1133. this.showToastMessage('查询未返回数据', 'info');
  1134. console.groupEnd();
  1135. return;
  1136. }
  1137. // 创建深拷贝避免引用问题
  1138. const safeResults = JSON.parse(JSON.stringify(results));
  1139. try {
  1140. // 直接使用原始结果数据
  1141. const formattedResults = results;
  1142. // 使用Vue.set确保响应式更新
  1143. this.$set(this, 'allResults', formattedResults);
  1144. // 提取表格列,排除Vue内部属性
  1145. if (formattedResults.length > 0) {
  1146. // 确保所有行有相同的列结构
  1147. const allColumns = new Set();
  1148. formattedResults.forEach(row => {
  1149. Object.keys(row).forEach(key => {
  1150. if (!key.startsWith('__')) {
  1151. allColumns.add(key);
  1152. }
  1153. });
  1154. });
  1155. const columns = Array.from(allColumns);
  1156. // 验证数据完整性
  1157. const missingData = formattedResults.filter(row => {
  1158. return columns.some(col => !(col in row));
  1159. });
  1160. if (missingData.length > 0) {
  1161. console.warn('发现数据不完整:', missingData);
  1162. }
  1163. this.$set(this, 'tableColumns', columns);
  1164. } else {
  1165. this.$set(this, 'tableColumns', []);
  1166. }
  1167. this.$set(this, 'queryResults', formattedResults);
  1168. this.showToastMessage(`查询成功,返回 ${formattedResults.length} 条记录`, 'success');
  1169. } catch (error) {
  1170. console.error('处理查询结果时出错:', error);
  1171. this.showToastMessage('处理查询结果时出错: ' + error.message, 'error');
  1172. this.queryResults = [];
  1173. this.tableColumns = [];
  1174. this.allResults = [];
  1175. }
  1176. },
  1177. // 格式化表格单元格内容
  1178. formatTableCell(value) {
  1179. if (value === null || value === undefined) {
  1180. return 'NULL';
  1181. }
  1182. if (typeof value === 'object') {
  1183. try {
  1184. return JSON.stringify(value);
  1185. } catch (e) {
  1186. return '[Object]';
  1187. }
  1188. }
  1189. if (typeof value === 'boolean') {
  1190. return value ? '是' : '否';
  1191. }
  1192. return value;
  1193. },
  1194. // 显示完整字段内容
  1195. showCompleteField(row, column) {
  1196. // 检查当前单元格内容是否有省略
  1197. const cellContent = this.formatTableCell(row[column]);
  1198. if (!cellContent.includes('...')) {
  1199. return;
  1200. }
  1201. this.currentRow = row;
  1202. this.currentColumn = column;
  1203. this.showCompleteContent = true;
  1204. this.completeContent = '加载中...';
  1205. // 查找主键字段 - 优先使用实际存在的列名
  1206. let primaryKey = null;
  1207. // 检查当前行是否有id字段
  1208. if ('id' in row) {
  1209. primaryKey = 'id';
  1210. }
  1211. // 检查是否有ID字段(大写)
  1212. else if ('ID' in row) {
  1213. primaryKey = 'ID';
  1214. }
  1215. // 检查表字段列表中是否有id字段
  1216. else if (this.tableColumnsList.some(col => col.name.toLowerCase() === 'id')) {
  1217. primaryKey = 'id';
  1218. }
  1219. // 检查表字段列表中是否有ID字段(大写)
  1220. else if (this.tableColumnsList.some(col => col.name === 'ID')) {
  1221. primaryKey = 'ID';
  1222. }
  1223. // 使用第一个存在的字段作为主键
  1224. else if (this.tableColumnsList.length > 0) {
  1225. const firstCol = this.tableColumnsList[0].name;
  1226. if (firstCol in row) {
  1227. primaryKey = firstCol;
  1228. }
  1229. }
  1230. if (!primaryKey || !row[primaryKey]) {
  1231. this.completeContent = '无法确定主键字段或主键值为空';
  1232. return;
  1233. }
  1234. // 构建查询SQL
  1235. const sql = `SELECT \`${column}\` FROM \`${this.selectedTable}\` WHERE \`${primaryKey}\`='${row[primaryKey]}';`;
  1236. // 调用API获取完整数据
  1237. fetch('/api/RunKylx365DBSql', {
  1238. method: 'POST',
  1239. headers: {
  1240. 'Content-Type': 'application/json',
  1241. },
  1242. body: JSON.stringify({
  1243. sql: sql,
  1244. IsCompleteField: true
  1245. })
  1246. })
  1247. .then(response => response.json())
  1248. .then(data => {
  1249. if (data && data.result && data.result.length > 0) {
  1250. this.completeContent = this.formatTableCell(data.result[0][column]);
  1251. } else {
  1252. this.completeContent = '未获取到完整数据';
  1253. }
  1254. })
  1255. .catch(error => {
  1256. this.completeContent = '获取完整数据失败: ' + error.message;
  1257. });
  1258. },
  1259. // 关闭完整内容弹出层
  1260. closeCompleteContent() {
  1261. this.showCompleteContent = false;
  1262. this.completeContent = '';
  1263. this.currentRow = null;
  1264. this.currentColumn = null;
  1265. },
  1266. // 复制到剪贴板
  1267. copyToClipboard(text) {
  1268. navigator.clipboard.writeText(text).then(() => {
  1269. this.showToastMessage('已复制到剪贴板', 'success');
  1270. }).catch(err => {
  1271. this.showToastMessage('复制失败: ' + err, 'error');
  1272. });
  1273. },
  1274. // 插入SQL子句(ORDER BY, GROUP BY, HAVING等)
  1275. insertSqlClause(clause) {
  1276. // 检查SQL查询是否为空
  1277. if (!this.sqlQuery.trim()) {
  1278. this.sqlQuery = clause + ' ';
  1279. return;
  1280. }
  1281. // 检查子句是否已存在(不区分大小写)
  1282. const sqlUpper = this.sqlQuery.toUpperCase();
  1283. const clauseUpper = clause.toUpperCase();
  1284. if (sqlUpper.includes(clauseUpper)) {
  1285. this.showToastMessage(`查询中已存在 "${clause}" 子句`, 'info');
  1286. return;
  1287. }
  1288. // 解析SQL查询,识别各个子句的位置
  1289. const sqlParts = {
  1290. select: -1,
  1291. from: -1,
  1292. where: -1,
  1293. groupBy: -1,
  1294. having: -1,
  1295. orderBy: -1,
  1296. limit: -1
  1297. };
  1298. // 查找各个子句的位置
  1299. sqlParts.select = sqlUpper.indexOf('SELECT');
  1300. sqlParts.from = sqlUpper.indexOf('FROM', sqlParts.select);
  1301. sqlParts.where = sqlUpper.indexOf('WHERE', sqlParts.from);
  1302. sqlParts.groupBy = sqlUpper.indexOf('GROUP BY', sqlParts.from);
  1303. sqlParts.having = sqlUpper.indexOf('HAVING', sqlParts.from);
  1304. sqlParts.orderBy = sqlUpper.indexOf('ORDER BY', sqlParts.from);
  1305. // 查找LIMIT子句(可能在末尾或分号前)
  1306. const limitMatch = sqlUpper.match(/LIMIT\s+\d+/i);
  1307. if (limitMatch) {
  1308. sqlParts.limit = limitMatch.index;
  1309. }
  1310. // 确定插入位置
  1311. let insertPosition = -1;
  1312. let insertBefore = '';
  1313. switch (clauseUpper) {
  1314. case 'GROUP BY':
  1315. // GROUP BY应该在WHERE之后,HAVING/ORDER BY/LIMIT之前
  1316. if (sqlParts.where > -1) {
  1317. // 找到WHERE子句后的位置
  1318. const whereEndPos = this.findClauseEndPosition(sqlUpper, sqlParts.where);
  1319. insertPosition = whereEndPos;
  1320. } else if (sqlParts.having > -1) {
  1321. // 如果没有WHERE但有HAVING,插入在HAVING之前
  1322. insertPosition = sqlParts.having;
  1323. insertBefore = 'HAVING';
  1324. } else if (sqlParts.orderBy > -1) {
  1325. // 如果没有WHERE和HAVING但有ORDER BY,插入在ORDER BY之前
  1326. insertPosition = sqlParts.orderBy;
  1327. insertBefore = 'ORDER BY';
  1328. } else if (sqlParts.limit > -1) {
  1329. // 如果只有LIMIT,插入在LIMIT之前
  1330. insertPosition = sqlParts.limit;
  1331. insertBefore = 'LIMIT';
  1332. } else {
  1333. // 如果没有以上子句,插入在查询末尾(可能有分号)
  1334. insertPosition = this.sqlQuery.length;
  1335. if (this.sqlQuery.trim().endsWith(';')) {
  1336. insertPosition = this.sqlQuery.lastIndexOf(';');
  1337. }
  1338. }
  1339. break;
  1340. case 'HAVING':
  1341. // HAVING应该在GROUP BY之后,ORDER BY/LIMIT之前
  1342. if (sqlParts.groupBy > -1) {
  1343. // 找到GROUP BY子句后的位置
  1344. const groupByEndPos = this.findClauseEndPosition(sqlUpper, sqlParts.groupBy);
  1345. insertPosition = groupByEndPos;
  1346. } else if (sqlParts.orderBy > -1) {
  1347. // 如果没有GROUP BY但有ORDER BY,插入在ORDER BY之前
  1348. insertPosition = sqlParts.orderBy;
  1349. insertBefore = 'ORDER BY';
  1350. } else if (sqlParts.limit > -1) {
  1351. // 如果只有LIMIT,插入在LIMIT之前
  1352. insertPosition = sqlParts.limit;
  1353. insertBefore = 'LIMIT';
  1354. } else {
  1355. // 如果没有以上子句,插入在查询末尾(可能有分号)
  1356. insertPosition = this.sqlQuery.length;
  1357. if (this.sqlQuery.trim().endsWith(';')) {
  1358. insertPosition = this.sqlQuery.lastIndexOf(';');
  1359. }
  1360. }
  1361. break;
  1362. case 'ORDER BY':
  1363. // ORDER BY应该在GROUP BY和HAVING之后,LIMIT之前
  1364. if (sqlParts.having > -1) {
  1365. // 找到HAVING子句后的位置
  1366. const havingEndPos = this.findClauseEndPosition(sqlUpper, sqlParts.having);
  1367. insertPosition = havingEndPos;
  1368. } else if (sqlParts.groupBy > -1) {
  1369. // 如果没有HAVING但有GROUP BY,找到GROUP BY子句后的位置
  1370. const groupByEndPos = this.findClauseEndPosition(sqlUpper, sqlParts.groupBy);
  1371. insertPosition = groupByEndPos;
  1372. } else if (sqlParts.limit > -1) {
  1373. // 如果只有LIMIT,插入在LIMIT之前
  1374. insertPosition = sqlParts.limit;
  1375. insertBefore = 'LIMIT';
  1376. } else {
  1377. // 如果没有以上子句,插入在查询末尾(可能有分号)
  1378. insertPosition = this.sqlQuery.length;
  1379. if (this.sqlQuery.trim().endsWith(';')) {
  1380. insertPosition = this.sqlQuery.lastIndexOf(';');
  1381. }
  1382. }
  1383. break;
  1384. }
  1385. // 执行插入
  1386. if (insertPosition > -1) {
  1387. let newQuery = '';
  1388. if (insertBefore) {
  1389. // 在特定子句之前插入
  1390. const beforeInsert = this.sqlQuery.substring(0, insertPosition);
  1391. const afterInsert = this.sqlQuery.substring(insertPosition);
  1392. // 添加换行符和子句
  1393. newQuery = beforeInsert + '\n' + clause + ' ' + afterInsert;
  1394. } else {
  1395. // 在子句末尾插入
  1396. const beforeInsert = this.sqlQuery.substring(0, insertPosition);
  1397. const afterInsert = this.sqlQuery.substring(insertPosition);
  1398. // 添加换行符和子句
  1399. newQuery = beforeInsert + '\n' + clause + ' ' + afterInsert;
  1400. }
  1401. this.sqlQuery = newQuery;
  1402. this.showToastMessage(`已插入 "${clause}" 子句到查询`, 'success');
  1403. // 设置光标位置到插入的子句之后
  1404. this.$nextTick(() => {
  1405. const textarea = document.querySelector('.sql-textarea');
  1406. if (textarea) {
  1407. textarea.focus();
  1408. const newPosition = insertPosition + clause.length + 1;
  1409. textarea.selectionStart = newPosition;
  1410. textarea.selectionEnd = newPosition;
  1411. }
  1412. });
  1413. } else {
  1414. // 如果无法确定位置,则在末尾添加
  1415. if (this.sqlQuery.trim().endsWith(';')) {
  1416. this.sqlQuery = this.sqlQuery.replace(/;\s*$/, `\n${clause} ;`);
  1417. } else {
  1418. this.sqlQuery += `\n${clause} `;
  1419. }
  1420. this.showToastMessage(`已插入 "${clause}" 子句到查询末尾`, 'info');
  1421. }
  1422. },
  1423. // 查找子句结束位置的辅助方法
  1424. findClauseEndPosition(sqlUpper, clauseStartPos) {
  1425. // 子句可能的结束标记
  1426. const endMarkers = ['GROUP BY', 'HAVING', 'ORDER BY', 'LIMIT', ';'];
  1427. let endPos = sqlUpper.length;
  1428. // 查找最近的下一个子句
  1429. for (const marker of endMarkers) {
  1430. const pos = sqlUpper.indexOf(marker, clauseStartPos + 1);
  1431. if (pos > -1 && pos < endPos) {
  1432. endPos = pos;
  1433. }
  1434. }
  1435. return endPos;
  1436. },
  1437. // 显示提示消息
  1438. showToastMessage(message, type = 'info') {
  1439. this.toastMessage = message;
  1440. this.toastType = type;
  1441. this.showToast = true;
  1442. // 3秒后自动隐藏
  1443. setTimeout(() => {
  1444. this.showToast = false;
  1445. }, 3000);
  1446. },
  1447. }
  1448. });
  1449. </script>
  1450. </body>
  1451. </html>