001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: SequenceValue.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.database.queries;
009:
010: import com.uwyn.rife.database.Datasource;
011: import com.uwyn.rife.database.capabilities.Capabilities;
012: import com.uwyn.rife.database.exceptions.DbQueryException;
013: import com.uwyn.rife.database.exceptions.SequenceNameRequiredException;
014: import com.uwyn.rife.database.exceptions.SequenceOperationRequiredException;
015: import com.uwyn.rife.database.exceptions.UnsupportedSqlFeatureException;
016: import com.uwyn.rife.datastructures.EnumClass;
017: import com.uwyn.rife.template.Template;
018: import com.uwyn.rife.template.TemplateFactory;
019: import com.uwyn.rife.tools.StringUtils;
020:
021: public class SequenceValue extends AbstractQuery implements Cloneable,
022: ReadQuery {
023: private String mName = null;
024: private Operation mOperation = null;
025:
026: public static final Operation NEXT = new Operation("NEXT");
027: public static final Operation CURRENT = new Operation("CURRENT");
028:
029: public SequenceValue(Datasource datasource) {
030: super (datasource);
031:
032: if (null == datasource)
033: throw new IllegalArgumentException(
034: "datasource can't be null.");
035:
036: clear();
037: }
038:
039: public void clear() {
040: super .clear();
041:
042: mName = null;
043: mOperation = null;
044: }
045:
046: public String getName() {
047: return mName;
048: }
049:
050: public Operation getOperation() {
051: return mOperation;
052: }
053:
054: public Capabilities getCapabilities() {
055: return null;
056: }
057:
058: public String getSql() throws DbQueryException {
059: if (null == mSql) {
060: if (null == mName) {
061: throw new SequenceNameRequiredException("SequenceValue");
062: } else if (null == mOperation) {
063: throw new SequenceOperationRequiredException(
064: "SequenceValue");
065: } else {
066: Template template = TemplateFactory.SQL.get("sql."
067: + StringUtils.encodeClassname(mDatasource
068: .getAliasedDriver())
069: + ".sequence_value");
070:
071: if (template.hasValueId("NAME")) {
072: template.setValue("NAME", mName);
073: }
074:
075: mSql = template.getBlock("OPERATION_" + mOperation);
076: if (0 == mSql.length()) {
077: throw new UnsupportedSqlFeatureException(
078: "SEQUENCE VALUE " + mOperation, mDatasource
079: .getAliasedDriver());
080: }
081:
082: assert mSql != null;
083: assert mSql.length() > 0;
084: }
085: }
086:
087: return mSql;
088: }
089:
090: public SequenceValue name(String name) {
091: if (null == name)
092: throw new IllegalArgumentException("name can't be null.");
093: if (0 == name.length())
094: throw new IllegalArgumentException("name can't be empty.");
095:
096: clearGenerated();
097: mName = name;
098:
099: return this ;
100: }
101:
102: public SequenceValue operation(Operation operation) {
103: clearGenerated();
104: mOperation = operation;
105:
106: return this ;
107: }
108:
109: public SequenceValue next() {
110: return operation(NEXT);
111: }
112:
113: public SequenceValue current() {
114: return operation(CURRENT);
115: }
116:
117: public SequenceValue clone() {
118: return (SequenceValue) super .clone();
119: }
120:
121: public static class Operation extends EnumClass<String> {
122: Operation(String identifier) {
123: super(identifier);
124: }
125: }
126: }
|