java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java
branchv_0
changeset 22 37fe883f8410
child 24 65e3fffae091
equal deleted inserted replaced
21:d42ed0d10a10 22:37fe883f8410
       
     1 /**
       
     2  * SQL-DK
       
     3  * Copyright © 2013 František Kučera (frantovo.cz)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 package info.globalcode.sql.dk.formatting;
       
    19 
       
    20 import info.globalcode.sql.dk.Parameter;
       
    21 import java.io.OutputStream;
       
    22 import java.util.EmptyStackException;
       
    23 import java.util.EnumSet;
       
    24 import java.util.List;
       
    25 import java.util.Stack;
       
    26 
       
    27 /**
       
    28  *
       
    29  * @author Ing. František Kučera (frantovo.cz)
       
    30  */
       
    31 public abstract class AbstractFormatter implements Formatter {
       
    32 
       
    33 	private Stack<State> state = new Stack<>();
       
    34 	private OutputStream outputStream;
       
    35 	private ColumnsHeader currentColumnsHeader;
       
    36 	private String currentQuery;
       
    37 	private int currentColumnsCount;
       
    38 
       
    39 	public AbstractFormatter(OutputStream outputStream) {
       
    40 		this.outputStream = outputStream;
       
    41 		state.push(State.ROOT);
       
    42 	}
       
    43 
       
    44 	/*
       
    45 	 * root
       
    46 	 * .database
       
    47 	 * ..resultSet
       
    48 	 * ...@query
       
    49 	 * ...@parameters
       
    50 	 * ...@columnsHeader
       
    51 	 * ...row
       
    52 	 * ....@columnValue
       
    53 	 * ..updatesResult
       
    54 	 * ...@query
       
    55 	 * ...@parameters
       
    56 	 * ...@updatedRowsCount
       
    57 	 * ...generatedKeys
       
    58 	 * ....resultSet (see above)
       
    59 	 */
       
    60 	protected enum State {
       
    61 
       
    62 		ROOT,
       
    63 		DATABASE,
       
    64 		RESULT_SET,
       
    65 		ROW,
       
    66 		UPDATES_RESULT,
       
    67 		GENERATED_KEYS
       
    68 	}
       
    69 
       
    70 	/**
       
    71 	 * Go down in hierarchy.
       
    72 	 * Pushes new state and verifies the old one.
       
    73 	 *
       
    74 	 * @param current the new state – currently entering
       
    75 	 * @param expected expected previous states (any of them is valid)
       
    76 	 * @return previous state
       
    77 	 * @throws IllegalStateException if previous state was not one from expected
       
    78 	 */
       
    79 	private State pushState(State current, EnumSet expected) {
       
    80 		State previous = state.peek();
       
    81 
       
    82 		if (expected.contains(previous)) {
       
    83 			state.push(current);
       
    84 			return previous;
       
    85 		} else {
       
    86 			throw new IllegalStateException("Formatter was in wrong state: " + previous + " when it should be in one of: " + expected);
       
    87 		}
       
    88 	}
       
    89 
       
    90 	protected State peekState(EnumSet expected) {
       
    91 		State current = state.peek();
       
    92 
       
    93 		if (expected.contains(current)) {
       
    94 			return current;
       
    95 		} else {
       
    96 			throw new IllegalStateException("Formatter is in wrong state: " + current + " when it should be in one of: " + expected);
       
    97 		}
       
    98 
       
    99 	}
       
   100 
       
   101 	/**
       
   102 	 * Go up in hierarchy.
       
   103 	 * Pops the superior state/branch.
       
   104 	 *
       
   105 	 * @param expected expected superior state
       
   106 	 * @return the superior state
       
   107 	 * @throws IllegalStateException if superior state was not one from expected or if there is no
       
   108 	 * more superior state (we are at root level)
       
   109 	 */
       
   110 	private State popState(EnumSet expected) {
       
   111 		try {
       
   112 			State superior = state.pop();
       
   113 			if (expected.contains(superior)) {
       
   114 				return superior;
       
   115 			} else {
       
   116 				throw new IllegalStateException("Formatter had wrong superior state: " + superior + " when it should be in one of: " + expected);
       
   117 			}
       
   118 		} catch (EmptyStackException e) {
       
   119 			throw new IllegalStateException("Formatter was already at root level – there is nothing above that.", e);
       
   120 		}
       
   121 	}
       
   122 
       
   123 	@Override
       
   124 	public void writeStartDatabase() {
       
   125 		pushState(State.DATABASE, EnumSet.of(State.ROOT));
       
   126 	}
       
   127 
       
   128 	@Override
       
   129 	public void writeEndDatabase() {
       
   130 		popState(EnumSet.of(State.ROOT));
       
   131 	}
       
   132 
       
   133 	@Override
       
   134 	public void writeStartResultSet() {
       
   135 		pushState(State.RESULT_SET, EnumSet.of(State.DATABASE, State.GENERATED_KEYS));
       
   136 	}
       
   137 
       
   138 	@Override
       
   139 	public void writeEndResultSet() {
       
   140 		popState(EnumSet.of(State.DATABASE, State.GENERATED_KEYS));
       
   141 		currentColumnsHeader = null;
       
   142 	}
       
   143 
       
   144 	@Override
       
   145 	public void writeQuery(String sql) {
       
   146 		peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
       
   147 
       
   148 		if (currentColumnsHeader == null) {
       
   149 			currentQuery = sql;
       
   150 		} else {
       
   151 			throw new IllegalStateException("Query string '" + sql + "' must be set before columns header – was already set: " + currentColumnsHeader);
       
   152 		}
       
   153 	}
       
   154 
       
   155 	@Override
       
   156 	public void writeParameters(List<Parameter> parameters) {
       
   157 		peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
       
   158 
       
   159 		if (currentColumnsHeader != null) {
       
   160 			throw new IllegalStateException("Parameters '" + parameters + "' must be set before columns header – was already set: " + currentColumnsHeader);
       
   161 		}
       
   162 
       
   163 		if (currentQuery == null) {
       
   164 			throw new IllegalStateException("Parameters '" + parameters + "' must be set after query – was not yet set.");
       
   165 		}
       
   166 	}
       
   167 
       
   168 	@Override
       
   169 	public void writeColumnsHeader(ColumnsHeader header) {
       
   170 		peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
       
   171 
       
   172 		if (currentColumnsHeader == null) {
       
   173 			currentColumnsHeader = header;
       
   174 		} else {
       
   175 			throw new IllegalStateException("Columns header can be set only once per result set – was already set: " + currentColumnsHeader);
       
   176 		}
       
   177 	}
       
   178 
       
   179 	@Override
       
   180 	public void writeStartRow() {
       
   181 		pushState(State.ROW, EnumSet.of(State.RESULT_SET));
       
   182 		currentColumnsCount = 0;
       
   183 	}
       
   184 
       
   185 	@Override
       
   186 	public void writeEndRow() {
       
   187 		popState(EnumSet.of(State.RESULT_SET));
       
   188 	}
       
   189 
       
   190 	@Override
       
   191 	public void writeColumnValue(Object value) {
       
   192 		peekState(EnumSet.of(State.ROW));
       
   193 		currentColumnsCount++;
       
   194 
       
   195 		int declaredCount = currentColumnsHeader.getColumnCount();
       
   196 		if (currentColumnsCount > declaredCount) {
       
   197 			throw new IllegalStateException("Current columns count is " + currentColumnsCount + " which is more than declared " + declaredCount + " in header.");
       
   198 		}
       
   199 	}
       
   200 
       
   201 	@Override
       
   202 	public void writeStartUpdatesResult() {
       
   203 		pushState(State.RESULT_SET, EnumSet.of(State.DATABASE));
       
   204 	}
       
   205 
       
   206 	@Override
       
   207 	public void writeEndUpdatesResult() {
       
   208 		popState(EnumSet.of(State.DATABASE));
       
   209 		currentColumnsHeader = null;
       
   210 	}
       
   211 
       
   212 	@Override
       
   213 	public void writeUpdatedRowsCount(int updatedRowsCount) {
       
   214 		peekState(EnumSet.of(State.UPDATES_RESULT));
       
   215 	}
       
   216 
       
   217 	@Override
       
   218 	public void writeStartGeneratedKeys() {
       
   219 		pushState(State.GENERATED_KEYS, EnumSet.of(State.UPDATES_RESULT));
       
   220 	}
       
   221 
       
   222 	@Override
       
   223 	public void writeEndGeneratedKeys() {
       
   224 		popState(EnumSet.of(State.UPDATES_RESULT));
       
   225 	}
       
   226 
       
   227 	protected OutputStream getOutputStream() {
       
   228 		return outputStream;
       
   229 	}
       
   230 
       
   231 	protected ColumnsHeader getCurrentColumnsHeader() {
       
   232 		return currentColumnsHeader;
       
   233 	}
       
   234 
       
   235 	protected int getCurrentColumnsCount() {
       
   236 		return currentColumnsCount;
       
   237 	}
       
   238 	/**
       
   239 	 * TODO: write SQLWarning
       
   240 	 */
       
   241 }