sqlarg.groovy 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. void sqlto(String sql, Object... value)
  2. {
  3. def builder = new StringBuilder();
  4. def ecpeidx = []
  5. def charArray = sql.toCharArray()
  6. charArray.eachWithIndex { char ch, int i ->
  7. if (ch == '?')
  8. ecpeidx << i
  9. }
  10. if (ecpeidx.size() != value.length)
  11. throw new IndexOutOfBoundsException("SQL转义字符和参数个数不匹配。SQL转义字符共有: ${ecpeidx.size()}, 参数个数共有: ${value.length}")
  12. int argidx = 0;
  13. int lidx = 0;
  14. for (int idx in ecpeidx) {
  15. builder.append(sql.substring(lidx, idx + 1))
  16. def argv = value[argidx];
  17. builder.append(argv instanceof String ? "'$argv'" : argv)
  18. ++argidx
  19. lidx = idx
  20. }
  21. builder.append(sql.substring(lidx))
  22. println(builder.toString().replace('?', ''))
  23. }
  24. sqlto("""
  25. WITH A AS (
  26. \tSELECT
  27. \t\tfi.*
  28. \tFROM
  29. \t\tflightinfo fi
  30. \tWHERE
  31. \t\tfi.landingAirport = ?
  32. \t\tAND fi.flightDate BETWEEN ?
  33. \t\tAND ?
  34. \t\tAND landingAirport = ?
  35. \t\tAND flightDate BETWEEN ?
  36. \tAND ?) SELECT
  37. \tli.FlightNO AS flightNO,
  38. \tli.FlightDate AS flightDate,
  39. \tli.departureAirport,
  40. \t(
  41. \tSELECT
  42. \t\ta.flightState
  43. \tFROM
  44. \t\tA AS a
  45. \tWHERE
  46. \t\ta.flightNO = li.flightNO
  47. \t\tAND a.flightDate = li.flightDate
  48. \t\tAND a.departureAirport = li.departureAirport
  49. \t\tAND a.landingAirport = li.arriveAirport
  50. \t) AS flightStatus,
  51. \t(
  52. \tSELECT
  53. \t\tifnull(
  54. \t\t\ta.actualLandingTime,
  55. \t\tifnull( a.estimatedLandingTime, a.planLandingTime ))
  56. \tFROM
  57. \t\tA AS a
  58. \tWHERE
  59. \t\ta.flightNO = li.flightNO
  60. \t\tAND a.flightDate = li.flightDate
  61. \t\tAND a.departureAirport = li.departureAirport
  62. \t\tAND a.landingAirport = li.arriveAirport
  63. \t) AS arrivalTime,
  64. \t(
  65. \tSELECT
  66. \t\ta.departureTerminal
  67. \tFROM
  68. \t\tA AS a
  69. \tWHERE
  70. \t\ta.flightNO = li.flightNO
  71. \t\tAND a.flightDate = li.flightDate
  72. \t\tAND a.departureAirport = li.departureAirport
  73. \t\tAND a.landingAirport = li.arriveAirport
  74. \t) AS departureTerminal,
  75. \t(
  76. \tSELECT
  77. \t\ta.landingTerminal
  78. \tFROM
  79. \t\tA AS a
  80. \tWHERE
  81. \t\ta.flightNO = li.flightNO
  82. \t\tAND a.flightDate = li.flightDate
  83. \t\tAND a.departureAirport = li.departureAirport
  84. \t\tAND a.landingAirport = li.arriveAirport
  85. \t) AS arrivalTerminal,
  86. \t(
  87. \tSELECT
  88. \t\ta.extractionPlace
  89. \tFROM
  90. \t\tA AS a
  91. \tWHERE
  92. \t\ta.flightNO = li.flightNO
  93. \t\tAND a.flightDate = li.flightDate
  94. \t\tAND a.departureAirport = li.departureAirport
  95. \t\tAND a.landingAirport = li.arriveAirport
  96. \t\tLIMIT 1
  97. \t) AS luggageCarousel,
  98. \t(
  99. \tSELECT
  100. \t\ta.landingStand
  101. \tFROM
  102. \t\tA AS a
  103. \tWHERE
  104. \t\ta.flightNO = li.flightNO
  105. \t\tAND a.flightDate = li.flightDate
  106. \t\tAND a.departureAirport = li.departureAirport
  107. \t\tAND a.landingAirport = li.arriveAirport
  108. \t\tLIMIT 1
  109. \t) AS parkingSpace,
  110. \tsum(
  111. \tNOT isnull( li.checkInNO )) AS checkIns,
  112. \tsum(
  113. \tIF
  114. \t( li.canLoad = "Y", 1, 0 )) AS projectedLoad,
  115. \tsum( li.loadID && li.abnormalState <> "OFF" ) AS loadedQuantity,
  116. \tsum( li.arrivedID ) AS numberOfDestinationArrivals,
  117. \tsum(
  118. \tIF
  119. \t( li.loadID <> 1, li.loadID - li.arrivedID, 0 )) AS endPointNotReached,
  120. \tsum(
  121. \tNOT isnull( li.specialType )) AS specialQuantity,
  122. \tsum( li.compensatID ) AS numberOfClaims,
  123. \tsum( li.unloadID ) AS uninstalled,
  124. \tsum(
  125. \tIF
  126. \t( li.unloadID <> 1 AND li.boardID = 1, 1, 0 )) AS numberToBeUninstalled,
  127. \tsum(
  128. \tIF
  129. \t( li.arrivedID = 1 AND isnull( li.transferFlightNO ), 1, 0 )) AS terminateArrivalQuantity,
  130. \tsum(
  131. \tIF
  132. \t( li.arrivedID <> 1 AND isnull( li.transferFlightNO ), 1, 0 )) AS terminateUnreachedQuantity,
  133. \tsum((
  134. \t\tSELECT
  135. \t\t\t1
  136. \t\tFROM
  137. \t\t\tluggageinfo li2
  138. \t\tWHERE
  139. \t\t\tli.luggageSN = li2.luggageSN
  140. \t\t\tAND li.flightDate = li2.inFlightDate
  141. \t\t\tAND li.flightNO = li2.inFlightNO
  142. \t\t\tAND ( li2.loadID = 1 OR li2.sorteID = 1 OR li2.boardID = 1 OR li2.arrivedID = 1 )
  143. \t\t\tLIMIT 1
  144. \t\t)) AS quantityShipped,
  145. \tsum(
  146. \t\tisnull((
  147. \t\t\tSELECT
  148. \t\t\t\t1
  149. \t\t\tFROM
  150. \t\t\t\tluggageinfo li2
  151. \t\t\tWHERE
  152. \t\t\t\tli.luggageSN = li2.luggageSN
  153. \t\t\t\tAND li.flightDate = li2.inFlightDate
  154. \t\t\t\tAND li.flightNO = li2.inFlightNO
  155. \t\t\t\tAND ( li2.loadID = 1 OR li2.sorteID = 1 OR li2.boardID = 1 OR li2.arrivedID = 1 )
  156. \t\t\t\tLIMIT 1
  157. \t\t\t))) AS undeliveredQuantity,
  158. \tconcat( group_concat( DISTINCT li.containerID ), "(", sum( NOT isnull( li.containerID )), ")" ) AS numberOfContainers,
  159. \tsum(
  160. \tisnull( li.containerID )) AS numberOfBulk
  161. FROM
  162. \tluggageinfo AS li
  163. WHERE
  164. \tarriveAirport = ?
  165. \tAND flightDate BETWEEN ?
  166. \tAND ?
  167. GROUP BY
  168. \tli.flightNO,
  169. \tli.flightDate,
  170. \tli.departureAirport,
  171. \tli.arriveAirport
  172. """, "2022-07-13","2022-07-13","CA1659","CA1659",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,1,1,null,null,null,null,null,null,null,null)