001    /*
002    @license.text@
003     */
004    package biz.hammurapi.sqlc;
005    
006    import java.util.Collection;
007    
008    import javax.xml.parsers.FactoryConfigurationError;
009    import javax.xml.transform.TransformerException;
010    
011    import org.apache.tools.ant.BuildException;
012    import org.apache.tools.ant.Project;
013    import org.apache.xpath.CachedXPathAPI;
014    import org.w3c.dom.Element;
015    import org.w3c.dom.traversal.NodeIterator;
016    
017    import biz.hammurapi.ant.XmlSourceEntry;
018    import biz.hammurapi.xml.dom.AbstractDomObject;
019    
020    
021    /**
022     * @ant.element name="statements"
023     * @author Pavel Vlasov
024     * @version $Revision: 1.6 $
025     */
026    public class StatementsEntry extends XmlSourceEntry {
027            
028            public void getStatements(Collection statementsReceiver, Collection tableEntriesReceiver) {
029                    try {
030                            CachedXPathAPI cxpa = new CachedXPathAPI();
031                            Element documentElement = getDocumentElement();
032                            if (documentElement!=null) {
033                                    NodeIterator nit = cxpa.selectNodeIterator(documentElement, "/statements/query|/statements/update|/statements/table");
034                                    Element se;
035                                    while ((se=(Element) nit.nextNode())!=null) {
036                                            if ("query".equals(se.getNodeName())) {
037                                                    log("Loading query "+se.getAttribute("name"), Project.MSG_VERBOSE);
038                                                    QueryEntry entry=new QueryEntry();
039                                                    if (se.hasAttribute("generateMutators")) {
040                                                            entry.setGenerateMutators("yes".equalsIgnoreCase(se.getAttribute("generateMutators")));
041                                                    }
042                                                    if (se.hasAttribute("singleRow")) {
043                                                            entry.setSingleRow("yes".equalsIgnoreCase(se.getAttribute("singleRow")));
044                                                    }
045                                                    setStatementAttributes(se, entry);
046                                                    loadColumns(cxpa, se, entry);
047                                                    loadParameters(cxpa, se, entry);                                        
048                                                    statementsReceiver.add(entry);
049                                                    entry.setSmart("yes".equalsIgnoreCase(se.getAttribute("smart")));
050                                                    entry.setTask(task);
051                                            } else if ("update".equals(se.getNodeName())) {
052                                                    log("Loading update "+se.getAttribute("name"), Project.MSG_VERBOSE);
053                                                    UpdateEntry entry=new UpdateEntry();
054                                                    setStatementAttributes(se, entry);
055                                                    loadParameters(cxpa, se, entry);                                        
056                                                    statementsReceiver.add(entry);                                  
057                                                    entry.setTask(task);
058                                            } else if ("table".equals(se.getNodeName())) {
059                                                    log("Loading table "+se.getAttribute("catalog")+":"+se.getAttribute("schema")+":"+se.getAttribute("table"), Project.MSG_VERBOSE);
060                                                    TableEntry entry=new TableEntry();
061                                                    if (se.hasAttribute("catalog")) {
062                                                            entry.setCatalog(se.getAttribute("catalog"));
063                                                    }
064                                                    if (se.hasAttribute("schema")) {
065                                                            entry.setSchema(se.getAttribute("schema"));
066                                                    }
067                                                    if (se.hasAttribute("table")) {
068                                                            entry.setTable(se.getAttribute("table"));
069                                                    }
070                                                    if (se.hasAttribute("generateMutators")) {
071                                                            entry.setGenerateMutators("yes".equalsIgnoreCase(se.getAttribute("generateMutators")));
072                                                    }
073                                                    if (se.hasAttribute("paramsPerColumnInsert")) {
074                                                            entry.setParamPerColumnInsert("yes".equalsIgnoreCase(se.getAttribute("paramsPerColumnInsert")));
075                                                    }
076                                                    if (se.hasAttribute("qualifiedName")) {
077                                                            entry.setQualifiedName(se.getAttribute("qualifiedName"));
078                                                    }
079                                                    loadColumns(cxpa, se, entry);   
080                                                    tableEntriesReceiver.add(entry);
081                                                    entry.setTask(task);
082                                            } else {
083                                                    throw new BuildException("Unsupported entry type: "+se.getNodeName());
084                                            }
085                                    }
086                            }
087                    } catch (TransformerException e) {
088                            throw new BuildException("Cannot load queries", e);
089                    } catch (FactoryConfigurationError e) {
090                            throw new BuildException("Cannot load queries", e);
091                    }
092            }
093    
094            /**
095             * @param cxpa
096             * @param qe
097             * @param entry
098             * @throws TransformerException
099             */
100            private void loadColumns(CachedXPathAPI cxpa, Element qe, ColumnHolder entry) throws TransformerException {
101                    NodeIterator cit=cxpa.selectNodeIterator(qe,"column");
102                    Element ce;
103                    while ((ce=(Element) cit.nextNode())!=null) {
104                            ColumnType ct=new ColumnType();
105                            if (ce.hasAttribute("type")) {
106                                    ct.setType(ce.getAttribute("type"));
107                            }
108                            if ("yes".equals(ce.getAttribute("skip"))) {
109                                    ct.setSkip(true);
110                            }
111                            ct.setName(ce.getAttribute("name"));
112                            entry.addConfiguredColumn(ct);
113                    }
114            }
115    
116            /**
117             * @param cxpa
118             * @param qe
119             * @param entry
120             * @throws TransformerException
121             */
122            private void loadParameters(CachedXPathAPI cxpa, Element qe, StatementEntry entry) throws TransformerException {
123                    NodeIterator pit=cxpa.selectNodeIterator(qe,"parameter");
124                    Element pe;
125                    while ((pe=(Element) pit.nextNode())!=null) {
126                            ParamType pt=new ParamType();
127                            if (pe.hasAttribute("type")) {
128                                    pt.setType(pe.getAttribute("type"));
129                            }
130                            pt.setPosition(Integer.parseInt(pe.getAttribute("name")));
131                            entry.addConfiguredParameter(pt);
132                    }
133            }
134    
135            /**
136             * @param cxpa
137             * @param qe
138             * @param entry
139             */
140            private void setStatementAttributes(Element qe, StatementEntry entry) {
141                    entry.setStatementText(AbstractDomObject.getElementText(qe));
142                    if (qe.hasAttribute("name")) {
143                            entry.setName(qe.getAttribute("name"));
144                    }
145                    
146                    if (qe.hasAttribute("hasNullableParameters")) {
147                            entry.setHasNullableParameters("yes".equalsIgnoreCase(qe.getAttribute("hasNullableParameters")));
148                    }
149                    
150                    if (qe.hasAttribute("description")) {
151                            entry.setDescription(qe.getAttribute("description"));
152                    }
153                    
154                    if (qe.hasAttribute("parameters")) {
155                        entry.setParameters(qe.getAttribute("parameters"));
156                    }
157            }
158            
159            protected StatementCompilerTask task;
160            
161            void setTask(StatementCompilerTask task) {
162                this.task=task;
163            }       
164    }