|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License, version 2.0, as published by the |
| 6 | + * Free Software Foundation. |
| 7 | + * |
| 8 | + * This program is also distributed with certain software (including but not |
| 9 | + * limited to OpenSSL) that is licensed under separate terms, as designated in a |
| 10 | + * particular file or component or in included license documentation. The |
| 11 | + * authors of MySQL hereby grant you an additional permission to link the |
| 12 | + * program and your derivative works with the separately licensed software that |
| 13 | + * they have included with MySQL. |
| 14 | + * |
| 15 | + * Without limiting anything contained in the foregoing, this file, which is |
| 16 | + * part of MySQL Connector/J, is also subject to the Universal FOSS Exception, |
| 17 | + * version 1.0, a copy of which can be found at |
| 18 | + * http://oss.oracle.com/licenses/universal-foss-exception. |
| 19 | + * |
| 20 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 21 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 22 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 23 | + * for more details. |
| 24 | + * |
| 25 | + * You should have received a copy of the GNU General Public License along with |
| 26 | + * this program; if not, write to the Free Software Foundation, Inc., |
| 27 | + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 28 | + */ |
| 29 | + |
| 30 | +package com.mysql.cj.protocol; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import com.mysql.cj.exceptions.CJOperationNotSupportedException; |
| 36 | +import com.mysql.cj.exceptions.ExceptionFactory; |
| 37 | + |
| 38 | +public interface ServerSessionStateController { |
| 39 | + |
| 40 | + public static int SESSION_TRACK_SYSTEM_VARIABLES = 0x00; |
| 41 | + public static int SESSION_TRACK_SCHEMA = 0x01; |
| 42 | + public static int SESSION_TRACK_STATE_CHANGE = 0x02; |
| 43 | + public static int SESSION_TRACK_GTIDS = 0x03; |
| 44 | + public static int SESSION_TRACK_TRANSACTION_CHARACTERISTICS = 0x04; |
| 45 | + public static int SESSION_TRACK_TRANSACTION_STATE = 0x05; |
| 46 | + |
| 47 | + /** |
| 48 | + * Set the object containing server session changes collected from the latest query execution. Used internally. |
| 49 | + * |
| 50 | + * @param changes |
| 51 | + * {@link ServerSessionStateChanges} object. |
| 52 | + * |
| 53 | + */ |
| 54 | + default void setSessionStateChanges(ServerSessionStateChanges changes) { |
| 55 | + throw ExceptionFactory.createException(CJOperationNotSupportedException.class, "Not supported"); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get the object containing server session changes collected from the latest query execution. |
| 60 | + * <p> |
| 61 | + * Please note that the driver could issue some queries internally. With that there is no guarantee that all session changes are reflected in the |
| 62 | + * {@link ServerSessionStateChanges} object after the recent user's query. If this is an issue, a {@link SessionStateChangesListener} can be added via |
| 63 | + * {@link #addSessionStateChangesListener(SessionStateChangesListener)} to catch all session changes. |
| 64 | + * </p> |
| 65 | + * |
| 66 | + * @return {@link ServerSessionStateChanges} object. |
| 67 | + */ |
| 68 | + default ServerSessionStateChanges getSessionStateChanges() { |
| 69 | + throw ExceptionFactory.createException(CJOperationNotSupportedException.class, "Not supported"); |
| 70 | + } |
| 71 | + |
| 72 | + @FunctionalInterface |
| 73 | + public static interface SessionStateChangesListener { |
| 74 | + void handleSessionStateChanges(ServerSessionStateChanges changes); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Add the {@link SessionStateChangesListener} that will process {@link ServerSessionStateChanges} on it's arrival. |
| 79 | + * |
| 80 | + * @param l |
| 81 | + * {@link SessionStateChangesListener} object. |
| 82 | + */ |
| 83 | + default void addSessionStateChangesListener(SessionStateChangesListener l) { |
| 84 | + throw ExceptionFactory.createException(CJOperationNotSupportedException.class, "Not supported"); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Remove {@link SessionStateChangesListener}. |
| 89 | + * |
| 90 | + * @param l |
| 91 | + * {@link SessionStateChangesListener} object. |
| 92 | + */ |
| 93 | + default void removeSessionStateChangesListener(SessionStateChangesListener l) { |
| 94 | + throw ExceptionFactory.createException(CJOperationNotSupportedException.class, "Not supported"); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * The object containing server session changes collected from the latest query execution. |
| 99 | + * <p> |
| 100 | + * Driver is getting these changes when connection property trackSessionState=true and server supports session tracking. |
| 101 | + * </p> |
| 102 | + * |
| 103 | + */ |
| 104 | + public static interface ServerSessionStateChanges { |
| 105 | + List<SessionStateChange> getSessionStateChangesList(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * A single server session change record. |
| 110 | + * <p> |
| 111 | + * The server messages about session changes are parsed according to their known types: |
| 112 | + * <ul> |
| 113 | + * <li>{@link #SESSION_TRACK_SYSTEM_VARIABLES} - two values, the system variable name and it's new value;</li> |
| 114 | + * <li>{@link #SESSION_TRACK_SCHEMA} - single value, the new schema name;</li> |
| 115 | + * <li>{@link #SESSION_TRACK_STATE_CHANGE} - single value, "1" or "0";</li> |
| 116 | + * <li>{@link #SESSION_TRACK_GTIDS} - single value, list of GTIDs as reported by server;</li> |
| 117 | + * <li>{@link #SESSION_TRACK_TRANSACTION_CHARACTERISTICS} - single value, transaction characteristics statement;</li> |
| 118 | + * <li>{@link #SESSION_TRACK_TRANSACTION_STATE} - single value, transaction state record.</li> |
| 119 | + * </ul> |
| 120 | + * For the unknown change type the raw payload is written into the single value. |
| 121 | + * </p> |
| 122 | + * <p> |
| 123 | + * See more details in the <a href="https://dev.mysql.com/doc/refman/8.0/en/session-state-tracking.html">server documentation</a>. |
| 124 | + * </p> |
| 125 | + */ |
| 126 | + public static class SessionStateChange { |
| 127 | + private int type; |
| 128 | + private List<String> values = new ArrayList<>(); |
| 129 | + |
| 130 | + public SessionStateChange(int type) { |
| 131 | + this.type = type; |
| 132 | + } |
| 133 | + |
| 134 | + public int getType() { |
| 135 | + return this.type; |
| 136 | + } |
| 137 | + |
| 138 | + public List<String> getValues() { |
| 139 | + return this.values; |
| 140 | + } |
| 141 | + |
| 142 | + public SessionStateChange addValue(String value) { |
| 143 | + this.values.add(value); |
| 144 | + return this; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments