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: DropTable.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.TableNameRequiredException;
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: import java.util.ArrayList;
019: import java.util.List;
020:
021: /**
022: * Object representation of a SQL "DROP TABLE" query.
023: *
024: * <p>This object may be used to dynamically construct a SQL statement in a
025: * database-independent fashion. After it is finished, it may be executed using
026: * {@link com.uwyn.rife.database.DbQueryManager#executeUpdate(Query)
027: * DbQueryManager.executeUpdate()}.
028: *
029: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
030: * @author Steven Grimm (koreth[remove] at midwinter dot com)
031: * @version $Revision: 3634 $
032: * @since 1.0
033: */
034: public class DropTable extends AbstractQuery implements Cloneable {
035: private List<String> mTables = null;
036:
037: public DropTable(Datasource datasource) {
038: super (datasource);
039:
040: if (null == datasource)
041: throw new IllegalArgumentException(
042: "datasource can't be null.");
043:
044: clear();
045: }
046:
047: public List<String> getTables() {
048: return mTables;
049: }
050:
051: public void clear() {
052: super .clear();
053:
054: mTables = new ArrayList<String>();
055:
056: assert 0 == mTables.size();
057: }
058:
059: public Capabilities getCapabilities() {
060: return null;
061: }
062:
063: public String getSql() throws DbQueryException {
064: if (null == mSql) {
065: if (0 == mTables.size()) {
066: throw new TableNameRequiredException("DropTable");
067: } else {
068: Template template = TemplateFactory.SQL.get("sql."
069: + StringUtils.encodeClassname(mDatasource
070: .getAliasedDriver()) + ".drop_table");
071:
072: if (1 == mTables.size()) {
073: template.setValue("EXPRESSION", mTables.get(0));
074: } else {
075: if (template.hasValueId("TABLES")) {
076: template.setValue("TABLES", StringUtils
077: .join(mTables, template
078: .getBlock("SEPERATOR")));
079: }
080:
081: String block = template.getBlock("TABLES");
082: if (0 == block.length()) {
083: throw new UnsupportedSqlFeatureException(
084: "MULTIPLE TABLE DROP", mDatasource
085: .getAliasedDriver());
086: }
087:
088: template.setValue("EXPRESSION", block);
089: }
090:
091: mSql = template.getBlock("QUERY");
092:
093: if (template.hasValueId("TABLES")) {
094: template.removeValue("TABLES");
095: }
096: template.removeValue("EXPRESSION");
097:
098: assert mSql != null;
099: assert mSql.length() > 0;
100: }
101: }
102:
103: return mSql;
104: }
105:
106: public DropTable table(String table) {
107: if (null == table)
108: throw new IllegalArgumentException("table can't be null.");
109: if (0 == table.length())
110: throw new IllegalArgumentException("table can't be empty.");
111:
112: mTables.add(table);
113: clearGenerated();
114:
115: return this ;
116: }
117:
118: public DropTable clone() {
119: DropTable new_instance = (DropTable) super .clone();
120: if (new_instance != null) {
121: if (mTables != null) {
122: new_instance.mTables = new ArrayList<String>();
123: new_instance.mTables.addAll(mTables);
124: }
125: }
126:
127: return new_instance;
128: }
129: }
|