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: Delete.java 3648 2007-01-29 12:16:16Z 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.TableNameRequiredException;
013: import com.uwyn.rife.database.exceptions.UnsupportedSqlFeatureException;
014: import com.uwyn.rife.template.Template;
015: import com.uwyn.rife.template.TemplateFactory;
016: import com.uwyn.rife.tools.StringUtils;
017:
018: /**
019: * Object representation of a SQL "DELETE" query.
020: *
021: * <p>This object may be used to dynamically construct a SQL statement in a
022: * database-independent fashion. After it is finished, it may be executed using
023: * {@link com.uwyn.rife.database.DbQueryManager#executeUpdate(Query)
024: * DbQueryManager.executeUpdate()}.
025: *
026: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
027: * @author Steven Grimm (koreth[remove] at midwinter dot com)
028: * @version $Revision: 3648 $
029: * @since 1.0
030: */
031: public class Delete extends AbstractWhereQuery<Delete> implements
032: Cloneable {
033: private String mHint = null;
034: private String mFrom = null;
035:
036: public Delete(Datasource datasource) {
037: super (datasource);
038:
039: if (null == datasource)
040: throw new IllegalArgumentException(
041: "datasource can't be null.");
042:
043: clear();
044: }
045:
046: public String getHint() {
047: return mHint;
048: }
049:
050: public void clear() {
051: super .clear();
052:
053: mHint = null;
054: mFrom = null;
055: }
056:
057: public String getFrom() {
058: return mFrom;
059: }
060:
061: public Capabilities getCapabilities() {
062: return null;
063: }
064:
065: public String getSql() {
066: if (null == mSql) {
067: if (null == mFrom) {
068: throw new TableNameRequiredException("Delete");
069: } else {
070: Template template = TemplateFactory.SQL.get("sql."
071: + StringUtils.encodeClassname(mDatasource
072: .getAliasedDriver()) + ".delete");
073:
074: if (mHint != null) {
075: if (!template.hasValueId("HINT")) {
076: throw new UnsupportedSqlFeatureException(
077: "HINT", mDatasource.getAliasedDriver());
078: }
079: template.setValue("EXPRESSION", mHint);
080: template.setBlock("HINT", "HINT");
081: }
082:
083: template.setValue("TABLE", mFrom);
084:
085: if (mWhere != null && mWhere.length() > 0) {
086: template.setValue("CONDITION", mWhere);
087: template.setValue("WHERE", template
088: .getBlock("WHERE"));
089: }
090:
091: mSql = template.getBlock("QUERY");
092:
093: assert mSql != null;
094: assert mSql.length() > 0;
095: }
096: }
097:
098: return mSql;
099: }
100:
101: public Delete hint(String hint) {
102: clearGenerated();
103: mHint = hint;
104:
105: return this ;
106: }
107:
108: public Delete from(String from) {
109: if (null == from)
110: throw new IllegalArgumentException("from can't be null.");
111: if (0 == from.length())
112: throw new IllegalArgumentException("from can't be empty.");
113:
114: clearGenerated();
115: mFrom = from;
116:
117: return this ;
118: }
119:
120: public Delete clone() {
121: return super.clone();
122: }
123: }
|