kylx365_db_admin.html 76 KB

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