Advertisement
pveselov

postgres jdbc diff for notifications callbacks.

Jan 27th, 2015
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 15.50 KB | None | 0 0
  1. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/jdbc2/AbstractJdbc2Connection.java
  2. ===================================================================
  3. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/jdbc2/AbstractJdbc2Connection.java   (revision 29586)
  4. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/jdbc2/AbstractJdbc2Connection.java   (working copy)
  5. @@ -16,6 +16,7 @@
  6.  import org.postgresql.core.Encoding;
  7.  import org.postgresql.core.Field;
  8.  import org.postgresql.core.Logger;
  9. +import org.postgresql.core.NotificationReceiver;
  10.  import org.postgresql.core.ProtocolConnection;
  11.  import org.postgresql.core.Query;
  12.  import org.postgresql.core.QueryExecutor;
  13. @@ -39,9 +40,12 @@
  14.  import java.sql.SQLException;
  15.  import java.sql.SQLWarning;
  16.  import java.util.Enumeration;
  17. +import java.util.HashSet;
  18. +import java.util.Iterator;
  19.  import java.util.Locale;
  20.  import java.util.NoSuchElementException;
  21.  import java.util.Properties;
  22. +import java.util.Set;
  23.  import java.util.StringTokenizer;
  24.  import java.util.Vector;
  25.  
  26. @@ -49,7 +53,7 @@
  27.   * This class defines methods of the jdbc2 specification.
  28.   * The real Connection class (for jdbc2) is org.postgresql.jdbc2.Jdbc2Connection
  29.   */
  30. -public abstract class AbstractJdbc2Connection implements BaseConnection
  31. +public abstract class AbstractJdbc2Connection implements BaseConnection, NotificationReceiver
  32.  {
  33.      //
  34.      // Driver-wide connection ID counter, used for logging
  35. @@ -97,6 +101,8 @@
  36.  
  37.      public abstract DatabaseMetaData getMetaData() throws SQLException;
  38.  
  39. +    public Set notificationListeners = new HashSet();
  40. +
  41.      //
  42.      // Ctor.
  43.      //
  44. @@ -143,7 +149,7 @@
  45.              logger.info(Driver.getVersion());
  46.  
  47.          // Now make the initial connection and set up local state
  48. -        this.protoConnection = ConnectionFactory.openConnection(host, port, user, database, info, logger);
  49. +        this.protoConnection = ConnectionFactory.openConnection(host, port, user, database, info, logger, this);
  50.          this.dbVersionNumber = protoConnection.getServerVersion();
  51.          this.compatible = info.getProperty("compatible", Driver.MAJORVERSION + "." + Driver.MINORVERSION);
  52.  
  53. @@ -250,7 +256,7 @@
  54.              firstWarning.setNextWarning(warn);
  55.          else
  56.              firstWarning = warn;
  57. -
  58. +        warningReady();
  59.      }
  60.  
  61.      public ResultSet execSQLQuery(String s) throws SQLException {
  62. @@ -1133,4 +1139,31 @@
  63.              copyManager = new CopyManager(this);
  64.          return copyManager;
  65.      }
  66. +
  67. +    @Override
  68. +    public void removeNotificationListener(NotificationReceiver n) {
  69. +        notificationListeners.remove(n);
  70. +    }
  71. +
  72. +    @Override
  73. +    public void addNotificationListener(NotificationReceiver n) {
  74. +        if (n == null) { throw new NullPointerException(); }
  75. +        notificationListeners.add(n);
  76. +    }
  77. +
  78. +    @Override
  79. +    public void notificationReady() {
  80. +        Iterator i = notificationListeners.iterator();
  81. +        while (i.hasNext()) {
  82. +            ((NotificationReceiver)i.next()).notificationReady();
  83. +        }
  84. +    }
  85. +
  86. +    @Override
  87. +    public void warningReady() {
  88. +        Iterator i = notificationListeners.iterator();
  89. +        while (i.hasNext()) {
  90. +            ((NotificationReceiver)i.next()).warningReady();
  91. +        }
  92. +    }
  93.  }
  94. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v2/ProtocolConnectionImpl.java
  95. ===================================================================
  96. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v2/ProtocolConnectionImpl.java  (revision 29586)
  97. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v2/ProtocolConnectionImpl.java  (working copy)
  98. @@ -21,12 +21,13 @@
  99.   * @author Oliver Jowett (oliver@opencloud.com)
  100.   */
  101.  class ProtocolConnectionImpl implements ProtocolConnection {
  102. -    ProtocolConnectionImpl(PGStream pgStream, String user, String database, Logger logger) {
  103. +    ProtocolConnectionImpl(PGStream pgStream, String user, String database, Logger logger, NotificationReceiver nr) {
  104.          this.pgStream = pgStream;
  105.          this.user = user;
  106.          this.database = database;
  107.          this.logger = logger;
  108.          this.executor = new QueryExecutorImpl(this, pgStream, logger);
  109. +        notificationReceiver = nr;
  110.      }
  111.  
  112.      public String getHost() {
  113. @@ -182,11 +183,17 @@
  114.              warnings = newWarning;
  115.          else
  116.              warnings.setNextWarning(newWarning);
  117. +        if (notificationReceiver != null) {
  118. +            notificationReceiver.warningReady();
  119. +        }
  120.      }
  121.  
  122.      synchronized void addNotification(PGNotification notification)
  123.      {
  124.          notifications.add(notification);
  125. +        if (notificationReceiver != null) {
  126. +            notificationReceiver.notificationReady();
  127. +        }
  128.      }
  129.  
  130.      synchronized void setTransactionState(int state)
  131. @@ -216,4 +223,5 @@
  132.      private final String database;
  133.      private final QueryExecutorImpl executor;
  134.      private final Logger logger;
  135. +    private final NotificationReceiver notificationReceiver;
  136.  }
  137. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v2/QueryExecutorImpl.java
  138. ===================================================================
  139. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v2/QueryExecutorImpl.java   (revision 29586)
  140. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v2/QueryExecutorImpl.java   (working copy)
  141. @@ -18,6 +18,7 @@
  142.  import org.postgresql.util.PSQLState;
  143.  import org.postgresql.util.GT;
  144.  import org.postgresql.copy.CopyOperation;
  145. +import org.postgresql.util.PSQLWarning;
  146.  
  147.  /**
  148.   * QueryExecutor implementation for the V2 protocol.
  149. @@ -493,7 +494,9 @@
  150.                  break;
  151.  
  152.              case 'N':  // Error Notification
  153. -                handler.handleWarning(receiveNotification());
  154. +                SQLWarning w = receiveNotification();
  155. +                handler.handleWarning(w);
  156. +                protoConnection.addWarning(new SQLWarning(w.getMessage()));
  157.                  break;
  158.  
  159.              case 'P':  // Portal Name
  160. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v2/ConnectionFactoryImpl.java
  161. ===================================================================
  162. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v2/ConnectionFactoryImpl.java   (revision 29586)
  163. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v2/ConnectionFactoryImpl.java   (working copy)
  164. @@ -37,7 +37,7 @@
  165.      private static final int AUTH_REQ_MD5 = 5;
  166.      private static final int AUTH_REQ_SCM = 6;
  167.  
  168. -    public ProtocolConnection openConnectionImpl(String host, int port, String user, String database, Properties info, Logger logger) throws SQLException {
  169. +    public ProtocolConnection openConnectionImpl(String host, int port, String user, String database, Properties info, Logger logger, NotificationReceiver nr) throws SQLException {
  170.          // Extract interesting values from the info properties:
  171.          //  - the SSL setting
  172.          boolean requireSSL = (info.getProperty("ssl") != null);
  173. @@ -85,7 +85,7 @@
  174.              doAuthentication(newStream, user, info.getProperty("password"), logger);
  175.  
  176.              // Do final startup.
  177. -            ProtocolConnectionImpl protoConnection = new ProtocolConnectionImpl(newStream, user, database, logger);
  178. +            ProtocolConnectionImpl protoConnection = new ProtocolConnectionImpl(newStream, user, database, logger, nr);
  179.              readStartupMessages(newStream, protoConnection, logger);
  180.  
  181.              // Run some initial queries
  182. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v3/ProtocolConnectionImpl.java
  183. ===================================================================
  184. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v3/ProtocolConnectionImpl.java  (revision 29586)
  185. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v3/ProtocolConnectionImpl.java  (working copy)
  186. @@ -22,7 +22,7 @@
  187.   * @author Oliver Jowett (oliver@opencloud.com)
  188.   */
  189.  class ProtocolConnectionImpl implements ProtocolConnection {
  190. -    ProtocolConnectionImpl(PGStream pgStream, String user, String database, Properties info, Logger logger) {
  191. +    ProtocolConnectionImpl(PGStream pgStream, String user, String database, Properties info, Logger logger, NotificationReceiver nr) {
  192.          this.pgStream = pgStream;
  193.          this.user = user;
  194.          this.database = database;
  195. @@ -30,6 +30,7 @@
  196.          this.executor = new QueryExecutorImpl(this, pgStream, info, logger);
  197.          // default value for server versions that don't report standard_conforming_strings
  198.          this.standardConformingStrings = false;
  199. +        notificationReceiver = nr;
  200.      }
  201.  
  202.      public String getHost() {
  203. @@ -176,11 +177,18 @@
  204.              warnings = newWarning;
  205.          else
  206.              warnings.setNextWarning(newWarning);
  207. +
  208. +        if (notificationReceiver != null) {
  209. +            notificationReceiver.warningReady();
  210. +        }
  211.      }
  212.  
  213.      synchronized void addNotification(PGNotification notification)
  214.      {
  215.          notifications.add(notification);
  216. +        if (notificationReceiver != null) {
  217. +            notificationReceiver.notificationReady();
  218. +        }
  219.      }
  220.  
  221.      synchronized void setTransactionState(int state)
  222. @@ -215,4 +223,6 @@
  223.      private final String database;
  224.      private final QueryExecutorImpl executor;
  225.      private final Logger logger;
  226. +    private final NotificationReceiver notificationReceiver;
  227. +
  228.  }
  229. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v3/QueryExecutorImpl.java
  230. ===================================================================
  231. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v3/QueryExecutorImpl.java   (revision 29586)
  232. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v3/QueryExecutorImpl.java   (working copy)
  233. @@ -1855,8 +1855,9 @@
  234.                  break;
  235.  
  236.              case 'N':  // Notice Response
  237. -                SQLWarning warning = receiveNoticeResponse();
  238. +                PSQLWarning warning = receiveNoticeResponse();
  239.                  handler.handleWarning(warning);
  240. +                protoConnection.addWarning(warning.copy());
  241.                  break;
  242.  
  243.              case 'S':    // Parameter Status
  244. @@ -2101,7 +2102,7 @@
  245.          return new PSQLException(errorMsg);
  246.      }
  247.  
  248. -    private SQLWarning receiveNoticeResponse() throws IOException {
  249. +    private PSQLWarning receiveNoticeResponse() throws IOException {
  250.          int nlen = pgStream.ReceiveInteger4();
  251.          ServerErrorMessage warnMsg = new ServerErrorMessage(pgStream.ReceiveString(nlen - 4), logger.getLogLevel());
  252.  
  253. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v3/ConnectionFactoryImpl.java
  254. ===================================================================
  255. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v3/ConnectionFactoryImpl.java   (revision 29586)
  256. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/v3/ConnectionFactoryImpl.java   (working copy)
  257. @@ -45,7 +45,7 @@
  258.      private static class UnsupportedProtocolException extends IOException {
  259.      }
  260.  
  261. -    public ProtocolConnection openConnectionImpl(String host, int port, String user, String database, Properties info, Logger logger) throws SQLException {
  262. +    public ProtocolConnection openConnectionImpl(String host, int port, String user, String database, Properties info, Logger logger, NotificationReceiver nr) throws SQLException {
  263.          // Extract interesting values from the info properties:
  264.          //  - the SSL setting
  265.          boolean requireSSL = (info.getProperty("ssl") != null);
  266. @@ -106,7 +106,7 @@
  267.              doAuthentication(newStream, host, user, info, logger);
  268.  
  269.              // Do final startup.
  270. -            ProtocolConnectionImpl protoConnection = new ProtocolConnectionImpl(newStream, user, database, info, logger);
  271. +            ProtocolConnectionImpl protoConnection = new ProtocolConnectionImpl(newStream, user, database, info, logger, nr);
  272.              readStartupMessages(newStream, protoConnection, logger);
  273.  
  274.              runInitialQueries(protoConnection, info, logger);
  275. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/NotificationReceiver.java
  276. ===================================================================
  277. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/NotificationReceiver.java   (revision 0)
  278. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/NotificationReceiver.java   (revision 0)
  279. @@ -0,0 +1,8 @@
  280. +package org.postgresql.core;
  281. +
  282. +public interface NotificationReceiver {
  283. +
  284. +    void notificationReady();
  285. +    void warningReady();
  286. +
  287. +}
  288. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/ConnectionFactory.java
  289. ===================================================================
  290. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/ConnectionFactory.java  (revision 29586)
  291. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/core/ConnectionFactory.java  (working copy)
  292. @@ -51,7 +51,7 @@
  293.       * @return the new, initialized, connection
  294.       * @throws SQLException if the connection could not be established.
  295.       */
  296. -    public static ProtocolConnection openConnection(String host, int port, String user, String database, Properties info, Logger logger) throws SQLException {
  297. +    public static ProtocolConnection openConnection(String host, int port, String user, String database, Properties info, Logger logger, NotificationReceiver nr) throws SQLException {
  298.          String protoName = info.getProperty("protocolVersion");
  299.  
  300.          for (int i = 0; i < versions.length; ++i)
  301. @@ -61,7 +61,7 @@
  302.                  continue;
  303.  
  304.              ConnectionFactory factory = (ConnectionFactory) versions[i][1];
  305. -            ProtocolConnection connection = factory.openConnectionImpl(host, port, user, database, info, logger);
  306. +            ProtocolConnection connection = factory.openConnectionImpl(host, port, user, database, info, logger, nr);
  307.              if (connection != null)
  308.                  return connection;
  309.          }
  310. @@ -86,5 +86,5 @@
  311.       * @throws SQLException if the connection could not be established for a reason other
  312.       *    than protocol version incompatibility.
  313.       */
  314. -    public abstract ProtocolConnection openConnectionImpl(String host, int port, String user, String database, Properties info, Logger logger) throws SQLException;
  315. +    public abstract ProtocolConnection openConnectionImpl(String host, int port, String user, String database, Properties info, Logger logger, NotificationReceiver nr) throws SQLException;
  316.  }
  317. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/PGConnection.java
  318. ===================================================================
  319. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/PGConnection.java    (revision 29586)
  320. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/PGConnection.java    (working copy)
  321. @@ -9,6 +9,7 @@
  322.  
  323.  import java.sql.*;
  324.  import org.postgresql.copy.CopyManager;
  325. +import org.postgresql.core.NotificationReceiver;
  326.  import org.postgresql.fastpath.Fastpath;
  327.  import org.postgresql.largeobject.LargeObjectManager;
  328.  
  329. @@ -110,5 +111,8 @@
  330.       */
  331.      public int getPrepareThreshold();
  332.  
  333. +    void addNotificationListener(NotificationReceiver n);
  334. +    void removeNotificationListener(NotificationReceiver n);
  335. +
  336.  }
  337.  
  338. Index: lib/postgresql-jdbc-9.1-902.src/org/postgresql/util/PSQLWarning.java
  339. ===================================================================
  340. --- lib/postgresql-jdbc-9.1-902.src/org/postgresql/util/PSQLWarning.java    (revision 29586)
  341. +++ lib/postgresql-jdbc-9.1-902.src/org/postgresql/util/PSQLWarning.java    (working copy)
  342. @@ -38,4 +38,9 @@
  343.      {
  344.          return serverError;
  345.      }
  346. +
  347. +    public PSQLWarning copy() {
  348. +        return new PSQLWarning(serverError);
  349. +    }
  350. +
  351.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement