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: CreateSequence.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.UnsupportedSqlFeatureException;
015: import com.uwyn.rife.template.Template;
016: import com.uwyn.rife.template.TemplateFactory;
017: import com.uwyn.rife.tools.StringUtils;
018:
019: /**
020: * Object representation of a SQL "CREATE SEQUENCE" query.
021: *
022: * <p>This object may be used to dynamically construct a SQL statement in a
023: * database-independent fashion. After it is finished, it may be executed using
024: * {@link com.uwyn.rife.database.DbQueryManager#executeUpdate(Query)
025: * DbQueryManager.executeUpdate()}.
026: *
027: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
028: * @author Steven Grimm (koreth[remove] at midwinter dot com)
029: * @version $Revision: 3634 $
030: * @since 1.0
031: */
032: public class CreateSequence extends AbstractQuery implements Cloneable {
033: private String mName = null;
034:
035: public CreateSequence(Datasource datasource) {
036: super (datasource);
037:
038: if (null == datasource)
039: throw new IllegalArgumentException(
040: "datasource can't be null.");
041:
042: clear();
043: }
044:
045: public void clear() {
046: super .clear();
047:
048: mName = null;
049: }
050:
051: public String getName() {
052: return mName;
053: }
054:
055: public Capabilities getCapabilities() {
056: return null;
057: }
058:
059: public String getSql() throws DbQueryException {
060: if (null == mSql) {
061: if (null == mName) {
062: throw new SequenceNameRequiredException(
063: "CreateSequence");
064: } else {
065: Template template = TemplateFactory.SQL.get("sql."
066: + StringUtils.encodeClassname(mDatasource
067: .getAliasedDriver())
068: + ".create_sequence");
069:
070: if (template.hasValueId("NAME")) {
071: template.setValue("NAME", mName);
072: }
073:
074: mSql = template.getBlock("QUERY");
075: if (0 == mSql.length()) {
076: throw new UnsupportedSqlFeatureException(
077: "CREATE SEQUENCE", mDatasource
078: .getAliasedDriver());
079: }
080:
081: assert mSql != null;
082: assert mSql.length() > 0;
083: }
084: }
085:
086: return mSql;
087: }
088:
089: public CreateSequence name(String name) {
090: if (null == name)
091: throw new IllegalArgumentException("name can't be null.");
092: if (0 == name.length())
093: throw new IllegalArgumentException("name can't be empty.");
094:
095: clearGenerated();
096: mName = name;
097:
098: return this ;
099: }
100:
101: public CreateSequence clone() {
102: return (CreateSequence) super.clone();
103: }
104: }
|