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: ReadQueryTemplate.java 3654 2007-02-06 06:50:16Z gbevin $
007: */
008: package com.uwyn.rife.database.queries;
009:
010: import com.uwyn.rife.database.capabilities.Capabilities;
011: import com.uwyn.rife.template.Template;
012:
013: /**
014: * An instance of <code>ReadQueryTemplate</code> will obtain a SQL from a
015: * {@link Template} block. If the template is provided but no block name,
016: * the entire content of the template will be used as the SQL query.
017: *
018: * <p>This allows you to write your custom SQL queries in dedicated templates,
019: * to name them, and to use them together with the functionalities that are
020: * provided by {@link com.uwyn.rife.database.DbQueryManager}
021: *
022: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
023: * @version $Revision: 3654 $
024: * @since 1.6
025: */
026: public class ReadQueryTemplate implements ReadQuery {
027: private Template mTemplate = null;
028: private String mBlock = null;
029:
030: /**
031: * Creates a new empty instance of <code>ReadQueryTemplate</code>.
032: * @since 1.6
033: */
034: public ReadQueryTemplate() {
035: }
036:
037: /**
038: * Creates a new instance of <code>ReadQueryTemplate</code> with the
039: * template instance whose content provides the SQL query that will be
040: * executed.
041: *
042: * @param template the template instance
043: * @since 1.6
044: */
045: public ReadQueryTemplate(Template template) {
046: setTemplate(template);
047: }
048:
049: /**
050: * Creates a new instance of <code>ReadQueryTemplate</code> with the
051: * template instance and block name that provide the SQL that will
052: * be executed.
053: *
054: * @param template the template instance
055: * @param block the name of the template block
056: * @since 1.6
057: */
058: public ReadQueryTemplate(Template template, String block) {
059: setTemplate(template);
060: setBlock(block);
061: }
062:
063: /**
064: * Sets the template instance.
065: *
066: * @param template the template instance
067: * @return this <code>ReadQueryTemplate</code> instance.
068: * @see #setTemplate
069: * @see #getTemplate
070: * @since 1.6
071: */
072: public ReadQueryTemplate template(Template template) {
073: setTemplate(template);
074: return this ;
075: }
076:
077: /**
078: * Sets the template instance.
079: *
080: * @param template the template instance
081: * @see #template
082: * @see #getTemplate
083: * @since 1.6
084: */
085: public void setTemplate(Template template) {
086: mTemplate = template;
087: }
088:
089: /**
090: * Retrieves the template instance.
091: *
092: * @return the template instance; or
093: * <p><code>null</code> if no template instance was provided
094: * @see #template
095: * @see #setTemplate
096: * @since 1.6
097: */
098: public Template getTemplate() {
099: return mTemplate;
100: }
101:
102: /**
103: * Sets the name of the template block.
104: *
105: * @param block the name of the template block
106: * @return this <code>ReadQueryTemplate</code> instance.
107: * @see #setBlock
108: * @see #getBlock
109: * @since 1.6
110: */
111: public ReadQueryTemplate block(String block) {
112: setBlock(block);
113: return this ;
114: }
115:
116: /**
117: * Sets the name of the template block.
118: *
119: * @param block the name of the template block
120: * @see #block
121: * @see #getBlock
122: * @since 1.6
123: */
124: public void setBlock(String block) {
125: mBlock = block;
126: }
127:
128: /**
129: * Retrieves the name of the template block.
130: *
131: * @return the name of the template block; or
132: * <p><code>null</code> if no block name was provided
133: * @see #block
134: * @see #setBlock
135: * @since 1.6
136: */
137: public String getBlock() {
138: return mBlock;
139: }
140:
141: public void clear() {
142: mTemplate = null;
143: mBlock = null;
144: }
145:
146: public String getSql() {
147: if (null == mTemplate) {
148: return null;
149: }
150:
151: if (null == mBlock) {
152: return mTemplate.getContent();
153: }
154:
155: return mTemplate.getBlock(mBlock);
156: }
157:
158: public QueryParameters getParameters() {
159: return null;
160: }
161:
162: public Capabilities getCapabilities() {
163: return null;
164: }
165:
166: public void setExcludeUnsupportedCapabilities(boolean flag) {
167: }
168: }
|