/ojc-core/iepse/iepcore/src/com/sun/jbi/engine/iep/core/derby/Union.java

https://bitbucket.org/ldassonville/openesb-components · Java · 129 lines · 88 code · 7 blank · 34 comment · 9 complexity · eafea3195cf3eb91cab21e17771e6769 MD5 · raw file

  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)Union.java
  24. *
  25. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  26. *
  27. * END_HEADER - DO NOT EDIT
  28. */
  29. package com.sun.jbi.engine.iep.core.derby;
  30. import com.sun.jbi.engine.iep.core.runtime.operator.OperatorConstants;
  31. import java.sql.Connection;
  32. import java.sql.DriverManager;
  33. import java.sql.PreparedStatement;
  34. import java.sql.ResultSet;
  35. import java.sql.Timestamp;
  36. /**
  37. *
  38. * @author Bing Lu
  39. */
  40. public class Union implements OperatorConstants {
  41. public static void operate(String outRName, String inRName, String colNames, Timestamp ts0, Timestamp ts1) throws Exception {
  42. Connection con = null;
  43. PreparedStatement s = null;
  44. PreparedStatement ps = null;
  45. ResultSet rs = null;
  46. try {
  47. con = DriverManager.getConnection("jdbc:default:connection");
  48. String[] cols = Util.getTokens(colNames, DELIM);
  49. String outputTableName = outRName;
  50. String inputTableName = inRName;
  51. StringBuffer sb = new StringBuffer();
  52. sb.append("SELECT DISTINCT ");
  53. for (String col : cols) {
  54. sb.append(col + ",");
  55. }
  56. sb.append(COL_TIMESTAMP + " FROM " + inputTableName + " r WHERE ");
  57. sb.append("r." + COL_TAG + " = '+' AND ");
  58. sb.append("? < r." + COL_TIMESTAMP + " AND r." + COL_TIMESTAMP + " <= ? AND ");
  59. sb.append("(SELECT COUNT(*) FROM " + inputTableName + " s WHERE ");
  60. for (String col : cols) {
  61. sb.append("s." + col + " = r." + col + " AND ");
  62. }
  63. sb.append("s." + COL_TIMESTAMP + " < r." + COL_TIMESTAMP + " AND ");
  64. sb.append("s." + COL_TAG + " = '+')");
  65. sb.append(" = "); // existing: + = -
  66. sb.append("(SELECT COUNT(*) FROM " + inputTableName + " s WHERE ");
  67. for (String col : cols) {
  68. sb.append("s." + col + " = r." + col + " AND ");
  69. }
  70. sb.append("s." + COL_TIMESTAMP + " < r." + COL_TIMESTAMP + " AND ");
  71. sb.append("s." + COL_TAG + " = '-') AND ");
  72. sb.append("(SELECT COUNT(*) FROM " + inputTableName + " s WHERE ");
  73. for (String col : cols) {
  74. sb.append("s." + col + " = r." + col + " AND ");
  75. }
  76. sb.append("s." + COL_TIMESTAMP + " = r." + COL_TIMESTAMP + " AND ");
  77. sb.append("s." + COL_TAG + " = '-')");
  78. sb.append(" < "); // new: - < +
  79. sb.append("(SELECT COUNT(*) FROM " + inputTableName + " s WHERE ");
  80. for (String col : cols) {
  81. sb.append("s." + col + " = r." + col + " AND ");
  82. }
  83. sb.append("s." + COL_TIMESTAMP + " = r." + COL_TIMESTAMP + " AND ");
  84. sb.append("s." + COL_TAG + " = '+')");
  85. sb.append(" ORDER BY " + COL_TIMESTAMP);
  86. String sqlStr = sb.toString();
  87. s = con.prepareStatement(sqlStr);
  88. s.setTimestamp(1, ts0);
  89. s.setTimestamp(2, ts1);
  90. rs = s.executeQuery();
  91. // For each (Name, Value, Timestamp) = (n, v, t)
  92. // INSERT INTO R (Name, Value, Timestamp, Tag) VALUES (n, v, t, '+')
  93. sb = new StringBuffer();
  94. sb.append("INSERT INTO " + outputTableName + " (");
  95. for (String col : cols) {
  96. sb.append(col + ", ");
  97. }
  98. sb.append(COL_TIMESTAMP + ", " + COL_TAG + ") VALUES (");
  99. for (int i = 0, I = cols.length; i < I; i++) {
  100. sb.append("?, ");
  101. }
  102. sb.append("?, '+')");
  103. sqlStr = sb.toString();
  104. ps = con.prepareStatement(sqlStr);
  105. int colTotal = cols.length + 1; // 1 for Timestamp
  106. while (rs.next()) {
  107. for (int i = 1; i <= colTotal; i++) {
  108. ps.setObject(i, rs.getObject(i));
  109. }
  110. ps.addBatch();
  111. }
  112. ps.executeBatch();
  113. } catch (Exception e) {
  114. throw e;
  115. } finally {
  116. Util.close(rs);
  117. Util.close(s);
  118. Util.close(ps);
  119. Util.close(con);
  120. }
  121. }
  122. }