001: /*
002: * sqlc 1
003: * SQL Compiler
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/products/sqlc/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sqlc;
024:
025: import java.util.Collection;
026:
027: import javax.xml.parsers.FactoryConfigurationError;
028: import javax.xml.transform.TransformerException;
029:
030: import org.apache.tools.ant.BuildException;
031: import org.apache.tools.ant.Project;
032: import org.apache.xpath.CachedXPathAPI;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.traversal.NodeIterator;
035:
036: import biz.hammurapi.ant.XmlSourceEntry;
037: import biz.hammurapi.xml.dom.AbstractDomObject;
038:
039: /**
040: * @ant.element name="statements"
041: * @author Pavel Vlasov
042: * @version $Revision: 1.6 $
043: */
044: public class StatementsEntry extends XmlSourceEntry {
045:
046: public void getStatements(Collection statementsReceiver,
047: Collection tableEntriesReceiver) {
048: try {
049: CachedXPathAPI cxpa = new CachedXPathAPI();
050: Element documentElement = getDocumentElement();
051: if (documentElement != null) {
052: NodeIterator nit = cxpa
053: .selectNodeIterator(documentElement,
054: "/statements/query|/statements/update|/statements/table");
055: Element se;
056: while ((se = (Element) nit.nextNode()) != null) {
057: if ("query".equals(se.getNodeName())) {
058: log("Loading query " + se.getAttribute("name"),
059: Project.MSG_VERBOSE);
060: QueryEntry entry = new QueryEntry();
061: if (se.hasAttribute("generateMutators")) {
062: entry
063: .setGenerateMutators("yes"
064: .equalsIgnoreCase(se
065: .getAttribute("generateMutators")));
066: }
067: if (se.hasAttribute("singleRow")) {
068: entry
069: .setSingleRow("yes"
070: .equalsIgnoreCase(se
071: .getAttribute("singleRow")));
072: }
073: setStatementAttributes(se, entry);
074: loadColumns(cxpa, se, entry);
075: loadParameters(cxpa, se, entry);
076: statementsReceiver.add(entry);
077: entry.setSmart("yes".equalsIgnoreCase(se
078: .getAttribute("smart")));
079: entry.setTask(task);
080: } else if ("update".equals(se.getNodeName())) {
081: log(
082: "Loading update "
083: + se.getAttribute("name"),
084: Project.MSG_VERBOSE);
085: UpdateEntry entry = new UpdateEntry();
086: setStatementAttributes(se, entry);
087: loadParameters(cxpa, se, entry);
088: statementsReceiver.add(entry);
089: entry.setTask(task);
090: } else if ("table".equals(se.getNodeName())) {
091: log("Loading table "
092: + se.getAttribute("catalog") + ":"
093: + se.getAttribute("schema") + ":"
094: + se.getAttribute("table"),
095: Project.MSG_VERBOSE);
096: TableEntry entry = new TableEntry();
097: if (se.hasAttribute("catalog")) {
098: entry
099: .setCatalog(se
100: .getAttribute("catalog"));
101: }
102: if (se.hasAttribute("schema")) {
103: entry.setSchema(se.getAttribute("schema"));
104: }
105: if (se.hasAttribute("table")) {
106: entry.setTable(se.getAttribute("table"));
107: }
108: if (se.hasAttribute("generateMutators")) {
109: entry
110: .setGenerateMutators("yes"
111: .equalsIgnoreCase(se
112: .getAttribute("generateMutators")));
113: }
114: if (se.hasAttribute("paramsPerColumnInsert")) {
115: entry
116: .setParamPerColumnInsert("yes"
117: .equalsIgnoreCase(se
118: .getAttribute("paramsPerColumnInsert")));
119: }
120: if (se.hasAttribute("qualifiedName")) {
121: entry.setQualifiedName(se
122: .getAttribute("qualifiedName"));
123: }
124: loadColumns(cxpa, se, entry);
125: tableEntriesReceiver.add(entry);
126: entry.setTask(task);
127: } else {
128: throw new BuildException(
129: "Unsupported entry type: "
130: + se.getNodeName());
131: }
132: }
133: }
134: } catch (TransformerException e) {
135: throw new BuildException("Cannot load queries", e);
136: } catch (FactoryConfigurationError e) {
137: throw new BuildException("Cannot load queries", e);
138: }
139: }
140:
141: /**
142: * @param cxpa
143: * @param qe
144: * @param entry
145: * @throws TransformerException
146: */
147: private void loadColumns(CachedXPathAPI cxpa, Element qe,
148: ColumnHolder entry) throws TransformerException {
149: NodeIterator cit = cxpa.selectNodeIterator(qe, "column");
150: Element ce;
151: while ((ce = (Element) cit.nextNode()) != null) {
152: ColumnType ct = new ColumnType();
153: if (ce.hasAttribute("type")) {
154: ct.setType(ce.getAttribute("type"));
155: }
156: if ("yes".equals(ce.getAttribute("skip"))) {
157: ct.setSkip(true);
158: }
159: ct.setName(ce.getAttribute("name"));
160: entry.addConfiguredColumn(ct);
161: }
162: }
163:
164: /**
165: * @param cxpa
166: * @param qe
167: * @param entry
168: * @throws TransformerException
169: */
170: private void loadParameters(CachedXPathAPI cxpa, Element qe,
171: StatementEntry entry) throws TransformerException {
172: NodeIterator pit = cxpa.selectNodeIterator(qe, "parameter");
173: Element pe;
174: while ((pe = (Element) pit.nextNode()) != null) {
175: ParamType pt = new ParamType();
176: if (pe.hasAttribute("type")) {
177: pt.setType(pe.getAttribute("type"));
178: }
179: pt.setPosition(Integer.parseInt(pe.getAttribute("name")));
180: entry.addConfiguredParameter(pt);
181: }
182: }
183:
184: /**
185: * @param cxpa
186: * @param qe
187: * @param entry
188: */
189: private void setStatementAttributes(Element qe, StatementEntry entry) {
190: entry.setStatementText(AbstractDomObject.getElementText(qe));
191: if (qe.hasAttribute("name")) {
192: entry.setName(qe.getAttribute("name"));
193: }
194:
195: if (qe.hasAttribute("hasNullableParameters")) {
196: entry.setHasNullableParameters("yes".equalsIgnoreCase(qe
197: .getAttribute("hasNullableParameters")));
198: }
199:
200: if (qe.hasAttribute("description")) {
201: entry.setDescription(qe.getAttribute("description"));
202: }
203:
204: if (qe.hasAttribute("parameters")) {
205: entry.setParameters(qe.getAttribute("parameters"));
206: }
207: }
208:
209: protected StatementCompilerTask task;
210:
211: void setTask(StatementCompilerTask task) {
212: this.task = task;
213: }
214: }
|