001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella.spi;
017:
018: /**
019: * A base class for connections.
020: * <p>Subclassing is more safe than directly implementing {@link Connection} interface.
021: *
022: * @author Fyodor Kupolov
023: * @version 1.0
024: */
025: public abstract class AbstractConnection implements Connection {
026: private DialectIdentifier dialectIdentifier;
027: private boolean readonly;
028: //Counter for statements use counter.statements++ to update statistics
029: protected final StatementCounter counter = new StatementCounter();
030:
031: /**
032: * May be used by sublasses to allow full customization
033: */
034: protected AbstractConnection() {
035: }
036:
037: /**
038: * Instantiates a connection with dialectIdentifier and connection parameters.
039: * @param dialectIdentifier dialect identifier.
040: * @param parameters connection parameters to use for general properties.
041: */
042: protected AbstractConnection(DialectIdentifier dialectIdentifier,
043: ConnectionParameters parameters) {
044: this (parameters);
045: if (dialectIdentifier == null) {
046: throw new IllegalArgumentException(
047: "Dialect identifier cannot be null");
048: }
049: this .dialectIdentifier = dialectIdentifier;
050: }
051:
052: /**
053: * Instantiates a connection with parameters.
054: * @param parameters connection parameters to use for general properties.
055: */
056: protected AbstractConnection(ConnectionParameters parameters) {
057: if (parameters == null) {
058: throw new IllegalArgumentException(
059: "Connection parameters cannot be null");
060: }
061: //General Scriptella property for debugging non transactional providers
062: readonly = parameters.getBooleanProperty("readonly");
063: }
064:
065: public DialectIdentifier getDialectIdentifier() {
066: return dialectIdentifier;
067: }
068:
069: protected void setDialectIdentifier(
070: DialectIdentifier dialectIdentifier) {
071: this .dialectIdentifier = dialectIdentifier;
072: }
073:
074: public long getExecutedStatementsCount() {
075: return counter.statements; //The default implementation
076: }
077:
078: /**
079: * Returns readonly mode.
080: * <p>readonly=true means updates must be skipped by the driver.
081: * This property is configurable by readonly property of connection declaration element.
082: * Drivers are not required to support this feauture.
083: * @return true if connection is in readonly mode.
084: */
085: public boolean isReadonly() {
086: return readonly;
087: }
088:
089: public void commit() throws ProviderException {
090: }
091:
092: public void rollback() throws ProviderException,
093: UnsupportedOperationException {
094: throw new UnsupportedOperationException(
095: "Transactions are not supported by " + toString());
096: }
097:
098: public String toString() {
099: String simpleName = getClass().getSimpleName();
100: return simpleName.length() == 0 ? "connection" : simpleName;
101: }
102:
103: /**
104: * Helper class to use for executed statements counting.
105: */
106: public static class StatementCounter {
107: /**
108: * Stores number of executed statements.
109: */
110: public volatile long statements;
111: }
112: }
|